diff --git a/src/clang_convert/code_object_converter/function_call_converter.py b/src/clang_convert/code_object_converter/function_call_converter.py index 938a4de94..10bcef789 100755 --- a/src/clang_convert/code_object_converter/function_call_converter.py +++ b/src/clang_convert/code_object_converter/function_call_converter.py @@ -6,6 +6,7 @@ import code_object_converter.conversion_funcs as conversion_funcs import code_object.arg as arg from code_object.code_interface import NONE_VALUE import code_object_converter.conversion_pack.arg_utils as arg_utils +import re class FunctionCallConverter(code_interface_converter.CodeInterfaceConverter): def __init__(self, ccode_object) -> None: @@ -18,6 +19,16 @@ class FunctionCallConverter(code_interface_converter.CodeInterfaceConverter): debug.line("create_cpp_code_object", f"FunctionCallConverter [IN] cfunction_call=[{debug.as_debug_string(cfunction_call)}]") + m = re.match(r".*->", cfunction_call.name) + if m: + # The function call is through a pointer. We'll remove the pointer part and convert the function + # The pointer will be converted elsewhere... + function_name_pointer = m.group(0) + cfunction_call = function_call.FunctionCall(cfunction_call.name[m.end():], cfunction_call.args) + debug.line("create_cpp_code_object", f"Converting [{debug.as_debug_string(self._ccode_object)}] without function name pointer [{function_name_pointer}] => [{debug.as_debug_string(cfunction_call)}]") + else: + function_name_pointer = "" + # 1. Check if there is a function mapping defined mapping = conversion_pack.conversion_data.funcsig_mapping_for_cfuncname(cfunction_call.name) @@ -64,12 +75,19 @@ class FunctionCallConverter(code_interface_converter.CodeInterfaceConverter): cppfunction_call = function_call.FunctionCall(cpp_name, cpp_args) - # 3. Apply validation (and special handling) + # 3. Restore function name pointer before performing validation... + if function_name_pointer: + cppfunction_call = function_call.FunctionCall(function_name_pointer+cppfunction_call.name, cppfunction_call.args) + debug.line("create_cpp_code_object", f"Restoring function_name_pointer=[{function_name_pointer}] --> [{debug.as_debug_string(cppfunction_call)}]") + + # 4. Apply validation (and special handling) updated_cppfunction_call = conversion_pack.conversion_validation.validate_function_call(cfunction_call, cppfunction_call) - # 4. Add the function call to the conversion data in case we need to process it later (e.g. for using C::x declarations) + # 5. Add the function call to the conversion data in case we need to process it later (e.g. for using C::x declarations) # Only if function_call type! if isinstance(updated_cppfunction_call, function_call.FunctionCall): conversion_pack.conversion_data.add_cppfunction_call(updated_cppfunction_call) + + return updated_cppfunction_call diff --git a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/0TEMPLATE_conversion_pack_updates.py b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/0TEMPLATE_conversion_pack_updates.py new file mode 100755 index 000000000..0f3aa3737 --- /dev/null +++ b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/0TEMPLATE_conversion_pack_updates.py @@ -0,0 +1,30 @@ + +import utils.debug as debug +import grib_accessor.grib_accessor_conversion_pack.conversion_pack_updates.base_conversion_pack_updates as base_conversion_pack_updates +from code_object.arg import Arg +from code_object_converter.conversion_pack.funcsig_mapping import FuncSigMapping +from code_object_converter.conversion_pack.arg_indexes import ArgIndexes +from code_object.funcsig import FuncSig +from code_object.code_interface import NONE_VALUE + +class TemplateConversionPackUpdates(base_conversion_pack_updates.BaseConversionPackUpdates): + def __init__(self) -> None: + super().__init__() + + self._update_funcs.update({ + "pack_long": self.apply_updates_for_pack_long + }) + + self._funcsig_mappings.extend([ + # template static int unpack_helper(grib_accessor* a, T* val, size_t* len) + FuncSigMapping(FuncSig("int", "unpack_helper", [Arg("grib_accessor*", "a"), Arg("T*", "val"), Arg("size_t*", "len")]), + FuncSig("GribStatus", "unpackHelper", [NONE_VALUE, Arg("std::vector&", "vecTValues"), NONE_VALUE]), + ArgIndexes(cbuffer=1, clength=2, cpp_container=1)), + ]) + + self._all_function_arg_mappings.update({ + Arg("unsigned char*","p") : Arg("AccessorDataPointer","p"), + }) + + def apply_updates_for_pack_long(self, conversion_pack): + conversion_pack.conversion_data.add_funcbody_arg_mapping(Arg("unsigned char*","mdata"), Arg("AccessorDataPointer","mdata")) diff --git a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/base_conversion_pack_updates.py b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/base_conversion_pack_updates.py index 0fbf6b67b..9c1779e0a 100755 --- a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/base_conversion_pack_updates.py +++ b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/base_conversion_pack_updates.py @@ -8,6 +8,7 @@ class BaseConversionPackUpdates: def __init__(self) -> None: self._update_funcs = {} self._funcsig_mappings = [] + self._all_function_arg_mappings = {} def add_funcsig_mappings_to_conversion_data(self, conversion_data): for mapping in self._funcsig_mappings: @@ -32,3 +33,7 @@ class BaseConversionPackUpdates: DataMember("grib_accessor_class**", "cclass->super"): DataMember("AccessorData", conversion_pack.conversion_data.info.super_class_name), }.items(): conversion_pack.conversion_data.add_data_member_mapping(cmember, cppmember) + + # All function arg mappings + for carg, cpparg in self._all_function_arg_mappings.items(): + conversion_pack.conversion_data.add_funcbody_arg_mapping(carg, cpparg) diff --git a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/bytes_conversion_pack_updates.py b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/bytes_conversion_pack_updates.py new file mode 100755 index 000000000..4df7c14a2 --- /dev/null +++ b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/conversion_pack_updates/bytes_conversion_pack_updates.py @@ -0,0 +1,17 @@ + +import utils.debug as debug +import grib_accessor.grib_accessor_conversion_pack.conversion_pack_updates.base_conversion_pack_updates as base_conversion_pack_updates +from code_object.arg import Arg +from code_object_converter.conversion_pack.funcsig_mapping import FuncSigMapping +from code_object_converter.conversion_pack.arg_indexes import ArgIndexes +from code_object.funcsig import FuncSig +from code_object.code_interface import NONE_VALUE + +class BytesConversionPackUpdates(base_conversion_pack_updates.BaseConversionPackUpdates): + def __init__(self) -> None: + super().__init__() + + self._all_function_arg_mappings.update({ + Arg("unsigned char*","p") : Arg("AccessorDataPointer","p"), + }) + diff --git a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_conversion_validation.py b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_conversion_validation.py index 64234082f..ba0af1042 100755 --- a/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_conversion_validation.py +++ b/src/clang_convert/grib_accessor/grib_accessor_conversion_pack/grib_accessor_conversion_validation.py @@ -69,6 +69,13 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv if special_function_call: return special_function_call + if cppfunction_call.name.startswith("super->"): + updated_func_name = cppfunction_call.name.replace("super->", f"{self._conversion_data.info.super_class_name}::") + updated_cppfunction_call = function_call.FunctionCall(updated_func_name, cppfunction_call.args) + + debug.line("validate_function_call", f"updated func name: [{debug.as_debug_string(cppfunction_call)}]->[{debug.as_debug_string(updated_cppfunction_call)}]") + return updated_cppfunction_call + if cfunction_call.name == "sscanf": # Need to convert to using conversion helper function: # template diff --git a/src/clang_convert/grib_accessor/supporting/type_mappings.py b/src/clang_convert/grib_accessor/supporting/type_mappings.py index d8f2416b5..f06ad6408 100755 --- a/src/clang_convert/grib_accessor/supporting/type_mappings.py +++ b/src/clang_convert/grib_accessor/supporting/type_mappings.py @@ -2,6 +2,7 @@ from code_object.arg import Arg from code_object.declaration_specifier import DeclSpec import code_object_converter.conversion_pack.conversion_data as conversion_data +from code_object.code_interface import NONE_VALUE # ==================== FUNCSIG TYPE MAPPINGS: Begin ==================== @@ -22,11 +23,12 @@ def add_funcsig_type_mappings_to_conversion_data(conversion_data): # ==================== FUNCBODY TYPE MAPPINGS: Begin ==================== common_grib_funcbody_type_mappings = { - "grib_accessor*" : "AccessorPtr", - "grib_handle*" : DeclSpec.NONE, - "grib_context*" : DeclSpec.NONE, - "grib_expression*" : "GribExpressionPtr", - "grib_codetable*" : "GribCodeTablePtr", + "grib_accessor*" : "AccessorPtr", + "grib_accessor_class*" : NONE_VALUE, + "grib_handle*" : NONE_VALUE, + "grib_context*" : NONE_VALUE, + "grib_expression*" : "GribExpressionPtr", + "grib_codetable*" : "GribCodeTablePtr", } grib_array_funcbody_type_mappings = {