mirror of https://github.com/ecmwf/eccodes.git
Added compound statement and variable declaration converters
This commit is contained in:
parent
75dac9fab0
commit
42ad179867
|
@ -1,6 +1,7 @@
|
|||
import utils.debug as debug
|
||||
import code_object.code_interface as code_interface
|
||||
import code_object.code_lines as code_lines
|
||||
import code_object.arg as arg
|
||||
|
||||
# Represent a variable declaration of the form VAR = VALUE
|
||||
# Models the VAR_DECL CursorKind in libclang
|
||||
|
@ -8,16 +9,12 @@ import code_object.code_lines as code_lines
|
|||
# VAR is the variable, stored as an Arg
|
||||
# OP is the operation (see class operation.Operation)
|
||||
# VALUE is the value used by OP (stored as a code_interface subclass)
|
||||
#
|
||||
# value can be a string or a code_interface subclass
|
||||
class VariableDeclaration(code_interface.CodeInterface):
|
||||
def __init__(self, variable, value=None) -> None:
|
||||
self._variable = variable
|
||||
if isinstance(value, str):
|
||||
self._value = code_lines.CodeLines(value)
|
||||
else:
|
||||
self._value = value
|
||||
self._value = value
|
||||
|
||||
assert isinstance(self._variable, arg.Arg), f"Variable must be an Arg class"
|
||||
assert isinstance(self._value, code_interface.CodeInterface), f"Value must be a CodeInterface class (or a string)"
|
||||
|
||||
@property
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
import code_object_converter.code_interface_converter as code_interface_converter
|
||||
import code_object_converter.conversion_funcs as conversion_funcs
|
||||
import code_object.code_objects as code_objects
|
||||
import utils.debug as debug
|
||||
|
||||
# Base class for converting a C code object to C++
|
||||
#
|
||||
# Call convert_ccode_object() for generic convert behaviour
|
||||
class CodeObjectsConverter(code_interface_converter.CodeInterfaceConverter):
|
||||
|
||||
# Override to set the correct class (e.g. Compound Statement)
|
||||
@property
|
||||
def code_objects_class(self):
|
||||
return code_objects.CodeObjects
|
||||
|
||||
def create_cpp_code_object(self, conversion_data):
|
||||
cpp_code_objects = self.code_objects_class()
|
||||
|
||||
for entry in self._ccode_object.code_objects:
|
||||
converted_object = conversion_funcs.convert_ccode_object(entry, conversion_data)
|
||||
cpp_code_objects.add_code_object(converted_object)
|
||||
|
||||
return cpp_code_objects
|
|
@ -1,16 +1,10 @@
|
|||
import code_object_converter.code_interface_converter as code_interface_converter
|
||||
import code_object_converter.code_objects_converter as code_objects_converter
|
||||
import code_object_converter.conversion_funcs as conversion_funcs
|
||||
import code_object.compound_statement as compound_statement
|
||||
import utils.debug as debug
|
||||
|
||||
# Base class for converting a C code object to C++
|
||||
#
|
||||
# Call convert_ccode_object() for generic convert behaviour
|
||||
class CompoundStatementConverter(code_interface_converter.CodeInterfaceConverter):
|
||||
class CompoundStatementConverter(code_objects_converter.CodeObjectsConverter):
|
||||
|
||||
def create_cpp_code_object(self, conversion_data):
|
||||
debug.line("create_cpp_code_object", f"Creating C++ compound statement")
|
||||
cpp_compound_statement = compound_statement.CompoundStatement()
|
||||
converted_objects = conversion_funcs.convert_ccode_object_collection(self._ccode_object, conversion_data)
|
||||
cpp_compound_statement.add_code_object_collection(converted_objects)
|
||||
return cpp_compound_statement
|
||||
@property
|
||||
def code_objects_class(self):
|
||||
return compound_statement.CompoundStatement
|
||||
|
|
|
@ -1,38 +1,37 @@
|
|||
|
||||
import code_object.arg as arg
|
||||
import code_object.compound_statement as compound_statement
|
||||
import code_object.code_objects as code_objects
|
||||
import code_object.compound_statement as compound_statement
|
||||
import code_object.funcsig as funcsig
|
||||
import code_object.variable_declaration as variable_declaration
|
||||
|
||||
import code_object_converter.arg_converter as arg_converter
|
||||
import code_object_converter.code_interface_converter as code_interface_converter
|
||||
import code_object_converter.funcsig_converter as funcsig_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.funcsig_converter as funcsig_converter
|
||||
import code_object_converter.variable_declaration_converter as variable_declaration_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 = {
|
||||
arg.Arg : arg_converter.ArgConverter,
|
||||
compound_statement.CompoundStatement : compound_statement_converter.CompoundStatementConverter,
|
||||
funcsig.FuncSig : funcsig_converter.FuncSigConverter,
|
||||
arg.Arg : arg_converter.ArgConverter,
|
||||
code_objects.CodeObjects : code_objects_converter.CodeObjectsConverter,
|
||||
compound_statement.CompoundStatement : compound_statement_converter.CompoundStatementConverter,
|
||||
funcsig.FuncSig : funcsig_converter.FuncSigConverter,
|
||||
variable_declaration.VariableDeclaration : variable_declaration_converter.VariableDeclarationConverter,
|
||||
}
|
||||
|
||||
# Convert a single C code_object into a C++ code_object
|
||||
# Convert a code_object into a C++ code_object
|
||||
def convert_ccode_object(ccode_object, conversion_data):
|
||||
debug.line("convert_ccode_object", f"Converting ccode_object instance=[{ccode_object}]")
|
||||
|
||||
converter_class = CodeInterfaceConverterClasses.get(type(ccode_object), code_interface_converter.CodeInterfaceConverter)
|
||||
debug.line("convert_ccode_object", f"ccode_object type=[{type(ccode_object)}] converter_class=[{type(converter_class)}]")
|
||||
debug.line("convert_ccode_object", f"converter_class=[{converter_class}]")
|
||||
converter = converter_class(ccode_object)
|
||||
cpp_obj = converter.to_cpp_code_object(conversion_data)
|
||||
|
||||
debug.line("convert_ccode_object", f"Converted cpp_obj instance=[{cpp_obj}]")
|
||||
|
||||
return cpp_obj
|
||||
|
||||
# Convert a collection of C code_objects into C++ code_objects
|
||||
def convert_ccode_object_collection(ccode_objects, conversion_data):
|
||||
cpp_code_objects = code_objects.CodeObjects()
|
||||
for cobj in ccode_objects.code_objects:
|
||||
debug.line("convert_ccode_object_collection",f"cobj as string=[{cobj.as_string()}]")
|
||||
cpp_obj = convert_ccode_object(cobj, conversion_data)
|
||||
cpp_code_objects.add_code_object(cpp_obj)
|
||||
|
||||
return cpp_code_objects
|
||||
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
|
||||
import utils.debug as debug
|
||||
import code_object.variable_declaration as variable_declaration
|
||||
import code_object_converter.code_interface_converter as code_interface_converter
|
||||
import code_object_converter.conversion_funcs as conversion_funcs
|
||||
|
||||
class VariableDeclarationConverter(code_interface_converter.CodeInterfaceConverter):
|
||||
def __init__(self, ccode_object) -> None:
|
||||
super().__init__(ccode_object)
|
||||
assert isinstance(ccode_object, variable_declaration.VariableDeclaration), f"Expected VariableDeclaration, got type=[{type(ccode_object)}]"
|
||||
|
||||
def create_cpp_code_object(self, conversion_data):
|
||||
self._conversion_data = conversion_data
|
||||
|
||||
cpp_variable = conversion_funcs.convert_ccode_object(self._ccode_object.variable, conversion_data)
|
||||
cpp_value = conversion_funcs.convert_ccode_object(self._ccode_object.value, conversion_data)
|
||||
|
||||
cpp_variable_declaration = variable_declaration.VariableDeclaration(cpp_variable, cpp_value)
|
||||
return cpp_variable_declaration
|
||||
|
||||
|
||||
|
|
@ -57,7 +57,7 @@ class DefaultCCodeConverter:
|
|||
global_decl_ast_parser = self.cast_parser_class()
|
||||
global_decl_ccode_objects = global_decl_ast_parser.to_ccode_objects(self._ccode.global_declarations, self._conversion_data, self._ccode.macro_details)
|
||||
debug.line("convert_global_declarations", global_decl_ccode_objects.as_lines())
|
||||
global_decl_cpp_code_objects = conversion_funcs.convert_ccode_object_collection(global_decl_ccode_objects, self._conversion_data)
|
||||
global_decl_cpp_code_objects = conversion_funcs.convert_ccode_object(global_decl_ccode_objects, self._conversion_data)
|
||||
self._cppcode.add_global_declaration(global_decl_cpp_code_objects)
|
||||
debug.line("convert_global_declarations", f"Converted C++ code [as_lines]...")
|
||||
debug.line("convert_global_declarations", global_decl_cpp_code_objects.as_lines())
|
||||
|
@ -75,7 +75,7 @@ class DefaultCCodeConverter:
|
|||
def convert_cfunction_body(self, cfunc):
|
||||
function_body_ast_parser = self.cast_parser_class()
|
||||
function_body_ccode_objects = function_body_ast_parser.to_ccode_objects(cfunc.body, self._conversion_data, self._ccode.macro_details)
|
||||
function_body_cppcode_objects = conversion_funcs.convert_ccode_object_collection(function_body_ccode_objects, self._conversion_data)
|
||||
function_body_cppcode_objects = conversion_funcs.convert_ccode_object(function_body_ccode_objects, self._conversion_data)
|
||||
return function_body_cppcode_objects.as_lines()
|
||||
|
||||
# Standard function conversion: returns: funcsig, body
|
||||
|
|
Loading…
Reference in New Issue