From 6813425c648e0d41ccc3d78f47d61307d3672891 Mon Sep 17 00:00:00 2001 From: kevstone Date: Sun, 11 Feb 2024 11:21:40 +0000 Subject: [PATCH] Fixed bug with function_call conversion --- .../conversion_pack/container_utils.py | 3 ++- .../function_call_converter.py | 26 ++++++++++++------- .../value_declaration_reference_converter.py | 19 +++++++------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/clang_convert/code_object_converter/conversion_pack/container_utils.py b/src/clang_convert/code_object_converter/conversion_pack/container_utils.py index c1dcf427c..ca8e8e9c4 100755 --- a/src/clang_convert/code_object_converter/conversion_pack/container_utils.py +++ b/src/clang_convert/code_object_converter/conversion_pack/container_utils.py @@ -5,6 +5,7 @@ from code_object.struct_member_access import StructMemberAccess from code_object.literal import Literal import code_object_converter.conversion_pack.buffer_mapping as buffer_mapping import code_object_converter.conversion_pack.arg_utils as arg_utils +from code_object.code_interface import NONE_VALUE class ContainerUtils: def create_cpp_container_buffer_arg(self, name): @@ -45,7 +46,7 @@ class ContainerUtils: # 2. Try standard arg conversions cpparg = conversion_data.funcbody_cpparg_for_carg_name(cname) - if cpparg and conversion_data.is_container_type(cpparg.decl_spec.type): + if cpparg and cpparg != NONE_VALUE and conversion_data.is_container_type(cpparg.decl_spec.type): return self.create_cpp_container_buffer_arg(cpparg.name) return None 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 62fe5c6b7..938a4de94 100755 --- a/src/clang_convert/code_object_converter/function_call_converter.py +++ b/src/clang_convert/code_object_converter/function_call_converter.py @@ -27,17 +27,23 @@ class FunctionCallConverter(code_interface_converter.CodeInterfaceConverter): debug.line("create_cpp_code_object", f"FunctionCallConverter [1] mapping.cfuncsig=[{debug.as_debug_string(mapping.cfuncsig)}] -> mapping.cppfuncsig=[{debug.as_debug_string(mapping.cppfuncsig)}]") cpp_args = [] for i, arg_entry in enumerate(mapping.cppfuncsig.args): - debug.line("create_cpp_code_object", f"FunctionCallConverter [1] --> i=[{i}] arg_entry=[{debug.as_debug_string(arg_entry)}]") + debug.line("create_cpp_code_object", f"FunctionCallConverter [2] --> i=[{i}] arg_entry=[{debug.as_debug_string(arg_entry)}]") if arg_entry != NONE_VALUE: + # We expect this argument to be converted to the C++ version, however, some calls don't always convert as expected. + # For example: [int grib_value_count(grib_accessor* a, long* count)] -> [GribStatus gribValueCount(AccessorPtr ptr, long& count)] + # This can be called with an accessor pointer if we're calling a different class instance, however if we're calling ourself it will be NONE_VALUE + # Therefore, if a value doesn't convert, we'll just use the C value (which we handle later) cpp_arg_entry = conversion_funcs.convert_ccode_object(cfunction_call.args[i], conversion_pack) - assert cpp_arg_entry != NONE_VALUE, f"Expected cpp_arg_entry for carg=[{debug.as_debug_string(cfunction_call.args[i])}], got NoneValue!" - - # Check if we have a container arg - cpp_arg_name = arg_utils.extract_name(cpp_arg_entry) - if cpp_arg_name: - cpp_container_arg = conversion_pack.container_utils.cname_to_cpp_container(cpp_arg_name, conversion_pack.conversion_data) - if cpp_container_arg: - cpp_arg_entry = cpp_container_arg + if cpp_arg_entry == NONE_VALUE: + cpp_arg_entry = arg_entry + debug.line("create_cpp_code_object", f"FunctionCallConverter [3] carg=[{debug.as_debug_string(cfunction_call.args[i])}] has no C++ conversion (may be a arg), setting cpp_arg_entry=[{debug.as_debug_string(cpp_arg_entry)}]") + else: + # Check if we have a container arg + cpp_arg_name = arg_utils.extract_name(cpp_arg_entry) + if cpp_arg_name: + cpp_container_arg = conversion_pack.container_utils.cname_to_cpp_container(cpp_arg_name, conversion_pack.conversion_data) + if cpp_container_arg: + cpp_arg_entry = cpp_container_arg cpp_args.append(cpp_arg_entry) @@ -46,7 +52,7 @@ class FunctionCallConverter(code_interface_converter.CodeInterfaceConverter): debug.line("create_cpp_code_object", f"cppfunction_call NOW EQUALS [{debug.as_debug_string(cppfunction_call)}]") else: - debug.line("create_cpp_code_object", f"FunctionCallConverter [2]") + debug.line("create_cpp_code_object", f"FunctionCallConverter [4]") # 2. Perform a manual conversion cpp_name = conversion_funcs.convert_ccode_object(cfunction_call.name, conversion_pack) cpp_args = [] diff --git a/src/clang_convert/code_object_converter/value_declaration_reference_converter.py b/src/clang_convert/code_object_converter/value_declaration_reference_converter.py index b0b3b14c8..e56104e3e 100755 --- a/src/clang_convert/code_object_converter/value_declaration_reference_converter.py +++ b/src/clang_convert/code_object_converter/value_declaration_reference_converter.py @@ -21,19 +21,20 @@ class ValueDeclarationReferenceConverter(code_interface_converter.CodeInterfaceC if cppfuncsig: return value_declaration_reference.ValueDeclarationReference(cppfuncsig.name) - # 2. Check if it is an arg - cpparg = conversion_pack.conversion_data.funcbody_cpparg_for_carg_name(cdecl_ref_expr_value) - debug.line("create_cpp_code_object", f"ValueDeclarationReferenceConverter [2] cdecl_ref_expr_value=[{debug.as_debug_string(cdecl_ref_expr_value)}] cpparg=[{debug.as_debug_string(cpparg)}]") + # 2. Check if it is a mapped buffer (e.g. char *c, int* len => std::string& str) + cpp_container_arg = conversion_pack.container_utils.cname_to_cpp_container(cdecl_ref_expr_value, conversion_pack.conversion_data) + debug.line("create_cpp_code_object", f"ValueDeclarationReferenceConverter [2] cdecl_ref_expr_value=[{debug.as_debug_string(cdecl_ref_expr_value)}] cpp_container_arg=[{debug.as_debug_string(cpp_container_arg)}]") + if cpp_container_arg: + return cpp_container_arg #value_declaration_reference.ValueDeclarationReference(cpp_container_arg.as_string()) + + # 3. Check if it is an arg + #cpparg = conversion_pack.conversion_data.funcbody_cpparg_for_carg_name(cdecl_ref_expr_value) + cpparg = conversion_pack.conversion_data.cpparg_for_cname(cdecl_ref_expr_value) + debug.line("create_cpp_code_object", f"ValueDeclarationReferenceConverter [3] cdecl_ref_expr_value=[{debug.as_debug_string(cdecl_ref_expr_value)}] cpparg=[{debug.as_debug_string(cpparg)}]") if cpparg == NONE_VALUE: return NONE_VALUE elif cpparg: return value_declaration_reference.ValueDeclarationReference(cpparg.name) - - # 3. Check if it is a mapped buffer (e.g. char *c, int* len => std::string& str) - cpp_container_arg = conversion_pack.container_utils.cname_to_cpp_container(cdecl_ref_expr_value, conversion_pack.conversion_data) - debug.line("create_cpp_code_object", f"ValueDeclarationReferenceConverter [3] cdecl_ref_expr_value=[{debug.as_debug_string(cdecl_ref_expr_value)}] cpp_container_arg=[{debug.as_debug_string(cpp_container_arg)}]") - if cpp_container_arg: - return cpp_container_arg #value_declaration_reference.ValueDeclarationReference(cpp_container_arg.as_string()) # 4. Perform a default conversion cppdecl_ref_expr_value = conversion_funcs.convert_ccode_object(cdecl_ref_expr_value, conversion_pack)