diff --git a/cppwg/generators.py b/cppwg/generators.py index 1b3305b..7ce416a 100644 --- a/cppwg/generators.py +++ b/cppwg/generators.py @@ -11,8 +11,6 @@ import pygccxml -from cppwg.input.class_info import CppClassInfo -from cppwg.input.free_function_info import CppFreeFunctionInfo from cppwg.input.info_helper import CppInfoHelper from cppwg.input.package_info import PackageInfo from cppwg.parsers.package_info_parser import PackageInfoParser @@ -289,67 +287,11 @@ def parse_package_info(self) -> None: # If no package info file exists, create a PackageInfo object with default settings self.package_info = PackageInfo("cppwg_package", self.source_root) - def add_discovered_classes(self) -> None: - """ - Add discovered classes. - - Add class info objects for classes discovered by pygccxml from - parsing the C++ source code. This is run for modules which set - `use_all_classes` to True. No class info objects were created for - those modules while parsing the package info yaml file. - """ - for module_info in self.package_info.module_info_collection: - if module_info.use_all_classes: - class_decls = self.source_ns.classes(allow_empty=True) - - for class_decl in class_decls: - if module_info.is_decl_in_source_path(class_decl): - class_info = CppClassInfo(class_decl.name) - class_info.update_names() - class_info.module_info = module_info - module_info.class_info_collection.append(class_info) - - def add_class_decls(self) -> None: - """ - Add declarations to class info objects. - - Update all class info objects with their corresponding - declarations found by pygccxml in the C++ source code. - """ + def update_from_ns(self) -> None: + """Update modules with information from the source namespace.""" for module_info in self.package_info.module_info_collection: module_info.update_from_ns(self.source_ns) - def add_discovered_free_functions(self) -> None: - """ - Add discovered free function. - - Add free function info objects discovered by pygccxml from - parsing the C++ source code. This is run for modules which set - `use_all_free_functions` to True. No free function info objects were - created for those modules while parsing the package info yaml file. - """ - for module_info in self.package_info.module_info_collection: - if module_info.use_all_free_functions: - free_functions = self.source_ns.free_functions(allow_empty=True) - - for free_function in free_functions: - if module_info.is_decl_in_source_path(free_function): - ff_info = CppFreeFunctionInfo(free_function.name) - ff_info.module_info = module_info - module_info.free_function_info_collection.append(ff_info) - - def add_free_function_decls(self) -> None: - """ - Add declarations to free function info objects. - - Update all free function info objects with their corresponding - declarations found by pygccxml in the C++ source code. - """ - for module_info in self.package_info.module_info_collection: - for ff_info in module_info.free_function_info_collection: - decls = self.source_ns.free_functions(ff_info.name, allow_empty=True) - ff_info.decls = [decls[0]] - def write_header_collection(self) -> None: """Write the header collection to file.""" header_collection_writer = CppHeaderCollectionWriter( @@ -389,17 +331,8 @@ def generate_wrapper(self) -> None: # Parse the headers with pygccxml and castxml self.parse_header_collection() - # Add discovered classes from the parsed code - self.add_discovered_classes() - - # Add declarations to class info objects - self.add_class_decls() - - # Add discovered free functions from the parsed code - self.add_discovered_free_functions() - - # Add declarations to free function info objects - self.add_free_function_decls() + # Update modules with information from the source namespace + self.update_from_ns() # Log list of unknown classes in the source root self.log_unknown_classes() diff --git a/cppwg/input/class_info.py b/cppwg/input/class_info.py index deed989..358a1f8 100644 --- a/cppwg/input/class_info.py +++ b/cppwg/input/class_info.py @@ -26,7 +26,7 @@ class CppClassInfo(CppTypeInfo): def __init__(self, name: str, class_config: Optional[Dict[str, Any]] = None): - super(CppClassInfo, self).__init__(name, class_config) + super().__init__(name, class_config) self.cpp_names: List[str] = None self.py_names: List[str] = None @@ -86,7 +86,7 @@ def requires(self, other: "ClassInfo") -> bool: # noqa: F821 return True return False - def update_from_ns(self, ns: "namespace_t") -> None: # noqa: F821 + def update_from_ns(self, source_ns: "namespace_t") -> None: # noqa: F821 """ Update class with information from the source namespace. @@ -94,7 +94,7 @@ def update_from_ns(self, ns: "namespace_t") -> None: # noqa: F821 Parameters ---------- - ns : pygccxml.declarations.namespace_t + source_ns : pygccxml.declarations.namespace_t The source namespace """ logger = logging.getLogger() @@ -109,7 +109,7 @@ def update_from_ns(self, ns: "namespace_t") -> None: # noqa: F821 class_name = class_cpp_name.replace(" ", "") # e.g. Foo<2,2> try: - class_decl = ns.class_(class_name) + class_decl = source_ns.class_(class_name) except declaration_not_found_t as e1: if ( @@ -136,7 +136,7 @@ def update_from_ns(self, ns: "namespace_t") -> None: # noqa: F821 class_name = ",".join(class_name.split(",")[0:pos]) + " >" try: - class_decl = ns.class_(class_name) + class_decl = source_ns.class_(class_name) except declaration_not_found_t as e2: logger.error(f"Could not find declaration for class {class_name}") diff --git a/cppwg/input/cpp_type_info.py b/cppwg/input/cpp_type_info.py index 3260d85..1c89538 100644 --- a/cppwg/input/cpp_type_info.py +++ b/cppwg/input/cpp_type_info.py @@ -31,7 +31,7 @@ class CppTypeInfo(BaseInfo): def __init__(self, name: str, type_config: Optional[Dict[str, Any]] = None): - super(CppTypeInfo, self).__init__(name) + super().__init__(name) self.module_info: Optional["ModuleInfo"] = None # noqa: F821 self.source_file_full_path: Optional[str] = None diff --git a/cppwg/input/free_function_info.py b/cppwg/input/free_function_info.py index 9879011..89e3e72 100644 --- a/cppwg/input/free_function_info.py +++ b/cppwg/input/free_function_info.py @@ -12,9 +12,22 @@ def __init__( self, name: str, free_function_config: Optional[Dict[str, Any]] = None ): - super(CppFreeFunctionInfo, self).__init__(name, free_function_config) + super().__init__(name, free_function_config) @property def parent(self) -> "ModuleInfo": # noqa: F821 """Returns the parent module info object.""" return self.module_info + + def update_from_ns(self, source_ns: "namespace_t") -> None: # noqa: F821 + """ + Update with information from the source namespace. + + Adds the free function declaration. + + Parameters + ---------- + source_ns : pygccxml.declarations.namespace_t + The source namespace + """ + self.decls = source_ns.free_functions(self.name, allow_empty=True)[0] diff --git a/cppwg/input/method_info.py b/cppwg/input/method_info.py index 49e6109..2077d3b 100644 --- a/cppwg/input/method_info.py +++ b/cppwg/input/method_info.py @@ -17,7 +17,7 @@ class CppMethodInfo(CppTypeInfo): def __init__(self, name: str, _): - super(CppMethodInfo, self).__init__(name) + super().__init__(name) self.class_info: Optional["CppClassInfo"] = None # noqa: F821 diff --git a/cppwg/input/module_info.py b/cppwg/input/module_info.py index b5509cd..f1f0195 100644 --- a/cppwg/input/module_info.py +++ b/cppwg/input/module_info.py @@ -4,6 +4,8 @@ from typing import Any, Dict, List, Optional from cppwg.input.base_info import BaseInfo +from cppwg.input.class_info import CppClassInfo +from cppwg.input.free_function_info import CppFreeFunctionInfo class ModuleInfo(BaseInfo): @@ -32,7 +34,7 @@ class ModuleInfo(BaseInfo): def __init__(self, name: str, module_config: Optional[Dict[str, Any]] = None): - super(ModuleInfo, self).__init__(name) + super().__init__(name) self.package_info: Optional["PackageInfo"] = None # noqa: F821 self.source_locations: List[str] = None @@ -117,17 +119,45 @@ def sort_classes(self) -> None: order_changed = True - def update_from_ns(self, ns: "namespace_t") -> None: # noqa: F821 + def update_from_ns(self, source_ns: "namespace_t") -> None: # noqa: F821 """ Update module with information from the source namespace. Parameters ---------- - ns : pygccxml.declarations.namespace_t + source_ns : pygccxml.declarations.namespace_t The source namespace """ + # Add discovered classes: if `use_all_classes` is True, this module + # has no class info objects. Use class declarations from the + # source namespace to create class info objects. + if self.use_all_classes: + class_decls = source_ns.classes(allow_empty=True) + for class_decl in class_decls: + if self.is_decl_in_source_path(class_decl): + class_info = CppClassInfo(class_decl.name) + class_info.update_names() + class_info.module_info = self + self.class_info_collection.append(class_info) + + # Update classes with information from source namespace. for class_info in self.class_info_collection: - class_info.update_from_ns(ns) + class_info.update_from_ns(source_ns) - # Sort the class info collection in inheritance order + # Sort classes by dependence self.sort_classes() + + # Add discovered free functions: if `use_all_free_functions` is True, + # this module has no free function info objects. Use free function + # decls from the source namespace to create free function info objects. + if self.use_all_free_functions: + free_functions = source_ns.free_functions(allow_empty=True) + for free_function in free_functions: + if self.is_decl_in_source_path(free_function): + ff_info = CppFreeFunctionInfo(free_function.name) + ff_info.module_info = self + self.free_function_info_collection.append(ff_info) + + # Update free functions with information from source namespace. + for ff_info in self.free_function_info_collection: + ff_info.update_from_ns(source_ns) diff --git a/cppwg/input/package_info.py b/cppwg/input/package_info.py index fdbe78c..82ad62d 100644 --- a/cppwg/input/package_info.py +++ b/cppwg/input/package_info.py @@ -50,7 +50,7 @@ def __init__( package_config : Dict[str, Any] A dictionary of package configuration settings """ - super(PackageInfo, self).__init__(name) + super().__init__(name) self.name: str = name self.source_locations: List[str] = None diff --git a/cppwg/input/variable_info.py b/cppwg/input/variable_info.py index 3cfd792..541c246 100644 --- a/cppwg/input/variable_info.py +++ b/cppwg/input/variable_info.py @@ -10,4 +10,4 @@ class CppVariableInfo(CppTypeInfo): def __init__(self, name: str, variable_config: Optional[Dict[str, Any]] = None): - super(CppVariableInfo, self).__init__(name, variable_config) + super().__init__(name, variable_config) diff --git a/cppwg/writers/class_writer.py b/cppwg/writers/class_writer.py index d40d561..db69ea1 100644 --- a/cppwg/writers/class_writer.py +++ b/cppwg/writers/class_writer.py @@ -45,7 +45,7 @@ def __init__( ) -> None: logger = logging.getLogger() - super(CppClassWrapperWriter, self).__init__(wrapper_templates) + super().__init__(wrapper_templates) self.class_info: "CppClassInfo" = class_info # noqa: F821 diff --git a/cppwg/writers/constructor_writer.py b/cppwg/writers/constructor_writer.py index f57601a..5994420 100644 --- a/cppwg/writers/constructor_writer.py +++ b/cppwg/writers/constructor_writer.py @@ -40,7 +40,7 @@ def __init__( wrapper_templates: Dict[str, str], ) -> None: - super(CppConstructorWrapperWriter, self).__init__(wrapper_templates) + super().__init__(wrapper_templates) self.class_info: "CppClassInfo" = class_info # noqa: F821 self.ctor_decl: "constructor_t" = ctor_decl # noqa: F821 diff --git a/cppwg/writers/free_function_writer.py b/cppwg/writers/free_function_writer.py index c3f29ac..2c87ef4 100644 --- a/cppwg/writers/free_function_writer.py +++ b/cppwg/writers/free_function_writer.py @@ -22,7 +22,7 @@ class CppFreeFunctionWrapperWriter(CppBaseWrapperWriter): def __init__(self, free_function_info, wrapper_templates) -> None: - super(CppFreeFunctionWrapperWriter, self).__init__(wrapper_templates) + super().__init__(wrapper_templates) self.free_function_info: CppFreeFunctionInfo = free_function_info self.wrapper_templates: Dict[str, str] = wrapper_templates diff --git a/cppwg/writers/method_writer.py b/cppwg/writers/method_writer.py index 2f7e87f..29ce8a7 100644 --- a/cppwg/writers/method_writer.py +++ b/cppwg/writers/method_writer.py @@ -40,7 +40,7 @@ def __init__( wrapper_templates: Dict[str, str], ) -> None: - super(CppMethodWrapperWriter, self).__init__(wrapper_templates) + super().__init__(wrapper_templates) self.class_info: "CppClassInfo" = class_info # noqa: F821 self.method_decl: "member_function_t" = method_decl # noqa: F821