From 34bdc97ba790bc876ff7735e234003013b28c800 Mon Sep 17 00:00:00 2001 From: kevstone Date: Sat, 13 Jan 2024 21:34:11 +0000 Subject: [PATCH] Added code_interface_converter --- .../code_interface_converter.py | 36 +++++++++++++++++++ .../code_object_converter/conversion_data.py | 8 +++++ .../default/default_ccode_converter.py | 9 +++++ 3 files changed, 53 insertions(+) create mode 100755 src/clang_convert/code_object_converter/code_interface_converter.py create mode 100755 src/clang_convert/code_object_converter/conversion_data.py diff --git a/src/clang_convert/code_object_converter/code_interface_converter.py b/src/clang_convert/code_object_converter/code_interface_converter.py new file mode 100755 index 000000000..2b5d66a4a --- /dev/null +++ b/src/clang_convert/code_object_converter/code_interface_converter.py @@ -0,0 +1,36 @@ +import code_object.code_interface as code_interface +import code_object_converter.conversion_data as conversion_data +import code_object.code_objects as code_objects +import copy +import debug + +# Base class for converting a C code object to C++ +# +# Call convert_ccode_object() for generic convert behaviour +class CodeInterfaceConverter: + def __init__(self, ccode_object) -> None: + assert isinstance(ccode_object, code_interface.CodeInterface), f"Cannot convert object of type [{type(ccode_object)}]" + self._ccode_object = ccode_object + + # Convert the ccode_object to a cpp_code_object + # It must be overridden - this version just returns a copy of the ccode_object + def to_cpp_code_object(self, conversion_data): + cpp_code_object = copy.deepcopy(self._ccode_object) + debug.line("to_cpp_code", f"Base version - just returning a copy of the C code object") + return cpp_code_object + +# Mapping from C code objects to their converters +CodeInterfaceConverterClasses = { +# code_interface.CodeInterface: CodeInterfaceConverter, +} + +# Convert a collection of C code_objects into C++ code_objects +def convert_ccode_objects(ccode_objects, conversion_data): + cpp_code_objects = code_objects.CodeObjects() + for obj in ccode_objects.code_objects: + converter_class = CodeInterfaceConverterClasses.get(type(obj), CodeInterfaceConverter) + debug.line("convert_ccode_object", f"ccode_object type=[{type(obj)}] converter_class=[{type(converter_class)}]") + converter = converter_class(obj) + cpp_code_objects.add_code_object(obj) + + return cpp_code_objects diff --git a/src/clang_convert/code_object_converter/conversion_data.py b/src/clang_convert/code_object_converter/conversion_data.py new file mode 100755 index 000000000..66f2be0e5 --- /dev/null +++ b/src/clang_convert/code_object_converter/conversion_data.py @@ -0,0 +1,8 @@ + +# Contains data to be used by the converters. +# +# The converters will add/update the entries here as required +class ConversionData: + def __init__(self) -> None: + pass + diff --git a/src/clang_convert/default/default_ccode_converter.py b/src/clang_convert/default/default_ccode_converter.py index 6e7c197d5..362b53fab 100755 --- a/src/clang_convert/default/default_ccode_converter.py +++ b/src/clang_convert/default/default_ccode_converter.py @@ -5,6 +5,8 @@ import default.default_cast_parser as default_cast_parser import code_object.cppfunction as cppfunction import code_object_converter.cfuncsig_converter as cfuncsig_converter import default.default_cppcode as default_cppcode +import code_object_converter.conversion_data as conversion_data +import code_object_converter.code_interface_converter as code_interface_converter # Convert a CCode object into a CppCode object, using the cconverter and derived classes as helpers @@ -25,6 +27,7 @@ class DefaultCCodeConverter: def convert(self): self.create_cpp_code() self.create_transforms() + self.create_conversion_data() self.convert_global_declarations() self.convert_functions() @@ -36,10 +39,16 @@ class DefaultCCodeConverter: def create_transforms(self): self._transforms = transforms.Transforms() + def create_conversion_data(self): + self._conversion_data = conversion_data.ConversionData() + def convert_global_declarations(self): global_decl_ast_parser = self.cast_parser_class() global_decl_ccode_objects = global_decl_ast_parser.to_ccode_objects(self._ccode.global_declarations, self._transforms, self._ccode.macro_details) debug.line("convert_global_declarations", global_decl_ccode_objects.as_lines()) + global_decl_cpp_code_objects = code_interface_converter.convert_ccode_objects(global_decl_ccode_objects, self._conversion_data) + debug.line("convert_global_declarations", f"Converted C++ code...") + debug.line("convert_global_declarations", global_decl_cpp_code_objects.as_lines()) # Helper to create the funcsig mapping and update the transforms def convert_cfunction_funcsig(self, cfunc):