Added objects/converters for other member function types

This commit is contained in:
kevstone 2024-01-19 19:48:44 +00:00
parent 3f459cf299
commit 21f62d27f3
11 changed files with 124 additions and 27 deletions

View File

@ -1,11 +1,11 @@
import cpp_code_object.member_function as member_function
import code_object.member_function as member_function
import utils.debug as debug
import re
# Representation of a C++ class member function
class ConstructorFunction(member_function.MemberFunction):
def __init__(self, funcsig, body, class_name) -> None:
def __init__(self, funcsig, body, class_name="") -> None:
super().__init__(funcsig, body, class_name)
# Overridden to set name to class_name::
@ -14,7 +14,7 @@ class ConstructorFunction(member_function.MemberFunction):
funcsig_string = re.sub(r"^[^\(]*\(", rf"{self._class_name}(", self._funcsig.as_string())
return funcsig_string
# Overridden to set name to class_name::class_name
# Overridden to set name to class_name::class_name
@property
def funcsig_as_definition(self):
funcsig_string = re.sub(r"^[^\(]*\(", rf"{self._class_name}::{self._class_name}(", self._funcsig.as_string())

View File

@ -1,11 +1,11 @@
import cpp_code_object.member_function as member_function
import code_object.member_function as member_function
import utils.debug as debug
import re
# Representation of a C++ class member function
class DestructorFunction(member_function.MemberFunction):
def __init__(self, funcsig, body, class_name) -> None:
def __init__(self, funcsig, body, class_name="") -> None:
super().__init__(funcsig, body, class_name)
# Overridden to set name to ~class_name::

View File

@ -1,17 +1,25 @@
import cpp_code_object.cppfunction as cppfunction
import code_object.function as function
import utils.debug as debug
import re
# Representation of a C++ class member function
class MemberFunction(cppfunction.CppFunction):
def __init__(self, funcsig, body, class_name) -> None:
class MemberFunction(function.Function):
def __init__(self, funcsig, body, class_name="") -> None:
super().__init__(funcsig, body)
self._class_name = class_name
self._const = False
self.using = []
@property
def class_name(self):
return self._class_name
@class_name.setter
def class_name(self, name):
self._class_name = name
# Overridden to add class_name::
@property
def funcsig_as_definition(self):

View File

@ -1,10 +1,10 @@
import cpp_code_object.member_function as member_function
import code_object.member_function as member_function
import utils.debug as debug
import re
class VirtualMemberFunction(member_function.MemberFunction):
def __init__(self, funcsig, body, class_name) -> None:
def __init__(self, funcsig, body, class_name="") -> None:
super().__init__(funcsig, body, class_name)
# Overridden to add "override"

View File

@ -0,0 +1,16 @@
import utils.debug as debug
import code_object.constructor_function as constructor_function
import code_object_converter.member_function_converter as member_function_converter
import code_object_converter.conversion_funcs as conversion_funcs
class ConstructorFunctionConverter(member_function_converter.MemberFunctionConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, constructor_function.ConstructorFunction), f"Expected ConstructorFunction, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_funcsig = conversion_funcs.convert_ccode_object(self._ccode_object.funcsig, conversion_data)
cpp_body = conversion_funcs.convert_ccode_object(self._ccode_object.body, conversion_data)
return constructor_function.ConstructorFunction(cpp_funcsig, cpp_body)

View File

@ -4,47 +4,59 @@ import code_object.arg as arg
import code_object.binary_operation as binary_operation
import code_object.code_objects as code_objects
import code_object.compound_statement as compound_statement
import code_object.constructor_function as constructor_function
import code_object.declaration_specifier as declaration_specifier
import code_object.destructor_function as destructor_function
import code_object.for_statement as for_statement
import code_object.funcsig_pointer as funcsig_pointer
import code_object.funcsig as funcsig
import code_object.function as function
import code_object.function_call as function_call
import code_object.global_function as global_function
import code_object.member_function as member_function
import code_object.variable_declaration as variable_declaration
import code_object.virtual_member_function as virtual_member_function
import code_object_converter.array_access_converter as array_access_converter
import code_object_converter.arg_converter as arg_converter
import code_object_converter.binary_operation_converter as binary_operation_converter
import code_object_converter.code_objects_converter as code_objects_converter
import code_object_converter.compound_statement_converter as compound_statement_converter
import code_object_converter.constructor_function_converter as constructor_function_converter
import code_object_converter.declaration_specifier_converter as declaration_specifier_converter
import code_object_converter.destructor_function_converter as destructor_function_converter
import code_object_converter.for_statement_converter as for_statement_converter
import code_object_converter.funcsig_converter as funcsig_converter
import code_object_converter.funcsig_pointer_converter as funcsig_pointer_converter
import code_object_converter.function_call_converter as function_call_converter
import code_object_converter.function_converter as function_converter
import code_object_converter.global_function_converter as global_function_converter
import code_object_converter.member_function_converter as member_function_converter
import code_object_converter.variable_declaration_converter as variable_declaration_converter
import code_object_converter.virtual_member_function_converter as virtual_member_function_converter
import code_object_converter.code_interface_converter as code_interface_converter
import utils.debug as debug
# Mapping from C code objects to their converters
CodeInterfaceConverterClasses = {
arr_access.ArrayAccess : array_access_converter.ArrayAccessConverter,
arg.Arg : arg_converter.ArgConverter,
binary_operation.BinaryOperation : binary_operation_converter.BinaryOperationConverter,
code_objects.CodeObjects : code_objects_converter.CodeObjectsConverter,
compound_statement.CompoundStatement : compound_statement_converter.CompoundStatementConverter,
declaration_specifier.DeclSpec : declaration_specifier_converter.DeclSpecConverter,
for_statement.ForStatement : for_statement_converter.ForStatementConverter,
funcsig.FuncSig : funcsig_converter.FuncSigConverter,
funcsig_pointer.FuncSigPointer : funcsig_pointer_converter.FuncSigPointerConverter,
function_call.FunctionCall : function_call_converter.FunctionCallConverter,
function.Function : function_converter.FunctionConverter,
global_function.GlobalFunction : global_function_converter.GlobalFunctionConverter,
variable_declaration.VariableDeclaration : variable_declaration_converter.VariableDeclarationConverter,
arr_access.ArrayAccess : array_access_converter.ArrayAccessConverter,
arg.Arg : arg_converter.ArgConverter,
binary_operation.BinaryOperation : binary_operation_converter.BinaryOperationConverter,
code_objects.CodeObjects : code_objects_converter.CodeObjectsConverter,
compound_statement.CompoundStatement : compound_statement_converter.CompoundStatementConverter,
constructor_function.ConstructorFunction : constructor_function_converter.ConstructorFunctionConverter,
declaration_specifier.DeclSpec : declaration_specifier_converter.DeclSpecConverter,
destructor_function.DestructorFunction : destructor_function_converter.DestructorFunctionConverter,
for_statement.ForStatement : for_statement_converter.ForStatementConverter,
funcsig.FuncSig : funcsig_converter.FuncSigConverter,
funcsig_pointer.FuncSigPointer : funcsig_pointer_converter.FuncSigPointerConverter,
function_call.FunctionCall : function_call_converter.FunctionCallConverter,
function.Function : function_converter.FunctionConverter,
global_function.GlobalFunction : global_function_converter.GlobalFunctionConverter,
member_function.MemberFunction : member_function_converter.MemberFunctionConverter,
variable_declaration.VariableDeclaration : variable_declaration_converter.VariableDeclarationConverter,
virtual_member_function.VirtualMemberFunction : virtual_member_function_converter.VirtualMemberFunctionConverter,
}
# Convert a code_object into a C++ code_object

View File

@ -0,0 +1,17 @@
import utils.debug as debug
import code_object.destructor_function as destructor_function
import code_object_converter.member_function_converter as member_function_converter
import code_object_converter.conversion_funcs as conversion_funcs
class DestructorFunctionConverter(member_function_converter.MemberFunctionConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, destructor_function.DestructorFunction), f"Expected DestructorFunction, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_funcsig = conversion_funcs.convert_ccode_object(self._ccode_object.funcsig, conversion_data)
cpp_body = conversion_funcs.convert_ccode_object(self._ccode_object.body, conversion_data)
return destructor_function.DestructorFunction(cpp_funcsig, cpp_body)

View File

@ -0,0 +1,17 @@
import utils.debug as debug
import code_object.member_function as member_function
import code_object_converter.function_converter as function_converter
import code_object_converter.conversion_funcs as conversion_funcs
class MemberFunctionConverter(function_converter.FunctionConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, member_function.MemberFunction), f"Expected MemberFunction, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_funcsig = conversion_funcs.convert_ccode_object(self._ccode_object.funcsig, conversion_data)
cpp_body = conversion_funcs.convert_ccode_object(self._ccode_object.body, conversion_data)
return member_function.MemberFunction(cpp_funcsig, cpp_body)

View File

@ -0,0 +1,17 @@
import utils.debug as debug
import code_object.virtual_member_function as virtual_member_function
import code_object_converter.member_function_converter as member_function_converter
import code_object_converter.conversion_funcs as conversion_funcs
class VirtualMemberFunctionConverter(member_function_converter.MemberFunctionConverter):
def __init__(self, ccode_object) -> None:
super().__init__(ccode_object)
assert isinstance(ccode_object, virtual_member_function.VirtualMemberFunction), f"Expected MemberFunction, got type=[{type(ccode_object)}]"
def create_cpp_code_object(self, conversion_data):
cpp_funcsig = conversion_funcs.convert_ccode_object(self._ccode_object.funcsig, conversion_data)
cpp_body = conversion_funcs.convert_ccode_object(self._ccode_object.body, conversion_data)
return virtual_member_function.VirtualMemberFunction(cpp_funcsig, cpp_body)

View File

@ -1,8 +1,12 @@
import utils.debug as debug
import code_object.code_objects as code_objects
import code_object.function as function
import code_object.global_function as global_function
import code_object.function as function
import code_object.constructor_function as constructor_function
import code_object.destructor_function as destructor_function
import code_object.member_function as member_function
import code_object.virtual_member_function as virtual_member_function
# Represents a coherent unit of C code that needs to be parsed together: usually a single C file
#
@ -113,18 +117,20 @@ class DefaultCCode:
# Add a function object from the funcsig and body (AST)
# Override is_xxx functions to identify class member functions
def add_function(self, cfuncsig, body):
cfunc = function.Function(cfuncsig, body)
if self.is_constructor(cfuncsig):
self._constructor = cfunc
self._constructor = constructor_function.ConstructorFunction(cfuncsig, body, self._class_name)
elif self.is_destructor(cfuncsig):
self._destructor = cfunc
self._destructor = destructor_function.DestructorFunction(cfuncsig, body, self._class_name)
elif self.is_virtual_member_function(cfuncsig):
cfunc = virtual_member_function.VirtualMemberFunction(cfuncsig, body, self._class_name)
self._virtual_member_functions.append(cfunc)
elif self.is_member_function(cfuncsig):
cfunc = member_function.MemberFunction(cfuncsig, body, self._class_name)
self._member_functions.append(cfunc)
else:
# Must be a "free"" function
cfunc = function.Function(cfuncsig, body)
self._functions.append(cfunc)
# Debug Support

View File

@ -65,6 +65,7 @@ class DefaultCppCode:
return self._constructor
def add_constructor(self, constructor):
constructor.class_name = self._class_name
self._constructor = constructor
@property
@ -72,6 +73,7 @@ class DefaultCppCode:
return self._destructor
def add_destructor(self, destructor):
destructor.class_name = self._class_name
self._destructor = destructor
@property
@ -79,6 +81,7 @@ class DefaultCppCode:
return self._member_functions
def add_member_function(self, member_function):
member_function.class_name = self._class_name
self._member_functions.append(member_function)
@property
@ -90,6 +93,7 @@ class DefaultCppCode:
return self._virtual_member_functions
def add_virtual_member_function(self, virtual_member_function):
virtual_member_function.class_name = self._class_name
self._virtual_member_functions.append(virtual_member_function)
@property