Skip to content

Commit

Permalink
#12 Simplify update_from_ns
Browse files Browse the repository at this point in the history
  • Loading branch information
kwabenantim committed Sep 22, 2024
1 parent bbb7184 commit 24d3b50
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 90 deletions.
75 changes: 4 additions & 71 deletions cppwg/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions cppwg/input/class_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -86,15 +86,15 @@ 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.
Adds the class declarations and base class declarations.
Parameters
----------
ns : pygccxml.declarations.namespace_t
source_ns : pygccxml.declarations.namespace_t
The source namespace
"""
logger = logging.getLogger()
Expand All @@ -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 (
Expand All @@ -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}")
Expand Down
2 changes: 1 addition & 1 deletion cppwg/input/cpp_type_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion cppwg/input/free_function_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 1 addition & 1 deletion cppwg/input/method_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 35 additions & 5 deletions cppwg/input/module_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion cppwg/input/package_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cppwg/input/variable_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion cppwg/writers/class_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion cppwg/writers/constructor_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cppwg/writers/free_function_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cppwg/writers/method_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 24d3b50

Please sign in to comment.