mirror of https://github.com/ecmwf/eccodes.git
Fixed some issues converting the updates StructMemberAccess data
This commit is contained in:
parent
81db7afeec
commit
0c9770c061
|
@ -932,6 +932,9 @@ class AstParser:
|
|||
name = self.parse_ast_node(children[0])
|
||||
index = self.parse_ast_node(children[1])
|
||||
|
||||
if isinstance(name, struct_member_access.StructMemberAccess):
|
||||
debug.line("parse_ARRAY_SUBSCRIPT_EXPR", f"[FOR INFO] Array name is StructMemberAccess [{debug.as_debug_string(name)}]")
|
||||
|
||||
arr_access = array_access.ArrayAccess(name, index)
|
||||
|
||||
return arr_access
|
||||
|
|
|
@ -5,6 +5,7 @@ import code_object_converter.code_interface_converter as code_interface_converte
|
|||
import code_object_converter.conversion_funcs as conversion_funcs
|
||||
import code_object.struct_member_access as struct_member_access
|
||||
from code_object.code_interface import NONE_VALUE
|
||||
import code_object_converter.conversion_pack.arg_utils as arg_utils
|
||||
|
||||
class ArrayAccessConverter(code_interface_converter.CodeInterfaceConverter):
|
||||
def __init__(self, ccode_object) -> None:
|
||||
|
@ -22,14 +23,18 @@ class ArrayAccessConverter(code_interface_converter.CodeInterfaceConverter):
|
|||
# If the name has been converted to a struct_member_access, representing a container size, e.g. foo.size(),
|
||||
# then we need to deal with the array access part!
|
||||
if isinstance(cpp_name, struct_member_access.StructMemberAccess):
|
||||
if cpp_name.member and cpp_name.member.name == "size()":
|
||||
# If we're accessing index 0, then we can just remove the [0] part,
|
||||
# otherwise something unexpected has happened!
|
||||
if cpp_index.as_string() == "0":
|
||||
debug.line("create_cpp_code_object", f"Stripping array access, from=[{debug.as_debug_string(self._ccode_object)}] to=[{debug.as_debug_string(cpp_name)}]")
|
||||
return cpp_name
|
||||
else:
|
||||
assert False, f"Unexpected array access=[{debug.as_debug_string(self._ccode_object)}]"
|
||||
|
||||
if cpp_name.member:
|
||||
cpp_member_name = arg_utils.extract_name(cpp_name.member)
|
||||
if cpp_member_name and cpp_member_name == "size()":
|
||||
# If we're accessing index 0, then we can just remove the [0] part,
|
||||
# otherwise something unexpected has happened!
|
||||
if cpp_index.as_string() == "0":
|
||||
debug.line("create_cpp_code_object", f"Stripping array access, from=[{debug.as_debug_string(self._ccode_object)}] to=[{debug.as_debug_string(cpp_name)}]")
|
||||
return cpp_name
|
||||
else:
|
||||
assert False, f"Unexpected array access=[{debug.as_debug_string(self._ccode_object)}]"
|
||||
|
||||
cpparray_access = array_access.ArrayAccess(cpp_name, cpp_index)
|
||||
|
||||
return conversion_pack.conversion_validation.validate_array_access(self._ccode_object, cpparray_access)
|
||||
|
|
|
@ -150,7 +150,8 @@ class DefaultConversionValidation(conversion_validation.ConversionValidation):
|
|||
if isinstance(cppleft, struct_member_access.StructMemberAccess):
|
||||
cpparg = self._conversion_data.cpparg_for_cppname(cppleft.name)
|
||||
if cppleft.member:
|
||||
if cppleft.member.name == "size()":
|
||||
cppleft_member_name = arg_utils.extract_name(cppleft.member)
|
||||
if cppleft_member_name and cppleft_member_name == "size()":
|
||||
if cppleft.as_string() == cppright.as_string():
|
||||
# We've converted something like *len = strlen(v); where len and v are both mapped to (e.g.) stringValue,
|
||||
# resulting in the redundant call stringValue.size() = stringValue.size()!
|
||||
|
@ -158,9 +159,9 @@ class DefaultConversionValidation(conversion_validation.ConversionValidation):
|
|||
return as_commented_out_code(cppbinary_operation, "Removing: C++ code is redundant")
|
||||
|
||||
elif cppright.as_string() == "0":
|
||||
cppleft.member.name = "clear()"
|
||||
cppleft.member.name = literal.Literal("clear()")
|
||||
else:
|
||||
cppleft.member.name = f"resize({cppright.as_string()})"
|
||||
cppleft.member.name = literal.Literal(f"resize({cppright.as_string()})")
|
||||
|
||||
# Double-check it's not const!
|
||||
debug.line("validate_binary_operation", f"Performing const check cppleft.name=[{debug.as_debug_string(cppleft.name)}] cpparg=[{debug.as_debug_string(cpparg)}]")
|
||||
|
@ -174,12 +175,12 @@ class DefaultConversionValidation(conversion_validation.ConversionValidation):
|
|||
cppright_value = cppright.as_string()
|
||||
|
||||
if is_number(cppright_value) or not self.is_cppfunction_returning_container(cppright):
|
||||
cppleft.index = "[0]"
|
||||
cppleft = array_access.ArrayAccess(cppleft.name, literal.Literal("0"))
|
||||
debug.line("validate_binary_operation", f"Assigning number to container, so updating it to access first element: cppleft=[{debug.as_debug_string(cppleft)}] cppright_value=[{cppright_value}]")
|
||||
return binary_operation.BinaryOperation(cppleft, cppbinary_op, cppright)
|
||||
|
||||
# Check if we're assigning to a data member and need to ensure the function is non-const
|
||||
data_member_name = cppleft.name
|
||||
data_member_name = arg_utils.extract_name(cppleft.name)
|
||||
debug.line("validate_binary_operation", f"CHECK is_cppdata_member({data_member_name})")
|
||||
|
||||
if self._conversion_data.is_cppdata_member(data_member_name):
|
||||
|
|
|
@ -2,17 +2,19 @@
|
|||
import utils.debug as debug
|
||||
from code_object.struct_member_access import StructMemberAccess
|
||||
import default.default_conversion_pack.default_container_utils as default_container_utils
|
||||
import code_object_converter.conversion_pack.arg_utils as arg_utils
|
||||
import code_object.literal as literal
|
||||
|
||||
|
||||
class GribAccessorContainerUtils(default_container_utils.DefaultContainerUtils):
|
||||
|
||||
def create_cpp_container_length_arg(self, name):
|
||||
cpp_member = super().create_cpp_container_length_arg(name)
|
||||
|
||||
if cpp_member.name == "initData":
|
||||
# Need to change from initData.size() to initData.args.size()
|
||||
args_member = StructMemberAccess(".", "args", None)
|
||||
size_member = StructMemberAccess(".", "size()", None)
|
||||
args_member.member = size_member
|
||||
cpp_name = arg_utils.extract_name(cpp_member)
|
||||
if cpp_name and cpp_name == "initData":
|
||||
# Need to change from initData.size() to initData.length
|
||||
args_member = StructMemberAccess(".", literal.Literal("length"), None)
|
||||
cpp_member.member = args_member
|
||||
|
||||
debug.line("create_cpp_container_length_arg", f"Updated initData length access: cpp_member=[{debug.as_debug_string(cpp_member)}]")
|
||||
|
|
|
@ -16,6 +16,7 @@ import code_object_converter.conversion_pack.arg_utils as arg_utils
|
|||
import code_object.cast_expression as cast_expression
|
||||
import code_object.macro_instantation as macro_instantation
|
||||
import code_object.struct_member_access as struct_member_access
|
||||
import code_object.array_access as array_access
|
||||
from utils.string_funcs import strip_semicolon
|
||||
|
||||
from grib_accessor.grib_accessor_conversion_pack.grib_accessor_special_function_call_conversion import special_function_name_mapping
|
||||
|
@ -215,7 +216,7 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv
|
|||
|
||||
if cpparg:
|
||||
if cpparg.decl_spec.type != cppfunc_return_type:
|
||||
updated_cpp_expression = literal.Literal(f"static_cast<GribStatus>({cpparg.name})")
|
||||
updated_cpp_expression = literal.Literal(f"static_cast<{cppfunc_return_type}>({cpparg.name})")
|
||||
elif cppfunc_return_type == "GribStatus":
|
||||
cpp_expression = cppreturn_statement.expression.as_string()
|
||||
if cpp_expression == "0":
|
||||
|
@ -229,25 +230,29 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv
|
|||
return super().validate_return_statement(creturn_statement, cppreturn_statement)
|
||||
|
||||
def validate_struct_member_access(self, cstruct_member_access, cppstruct_member_access):
|
||||
if cppstruct_member_access.name == "buffer_" and cppstruct_member_access.member.name == "data":
|
||||
# Just need to change ->data to .data()
|
||||
cppstruct_member_access.member.access = "."
|
||||
cppstruct_member_access.member.name += "()"
|
||||
return cppstruct_member_access
|
||||
|
||||
cppstruct_member_access_name = arg_utils.extract_name(cppstruct_member_access.name)
|
||||
if cppstruct_member_access_name == "buffer_":
|
||||
if cstruct_member_access.as_string().endswith("->data"):
|
||||
# Just need to change ->data to .data()
|
||||
cppstruct_member_access_member = struct_member_access.StructMemberAccess(".", literal.Literal("data()"))
|
||||
return struct_member_access.StructMemberAccess(cppstruct_member_access.access,
|
||||
cppstruct_member_access.variable,
|
||||
cppstruct_member_access_member)
|
||||
|
||||
# Handle AccessorPtr types
|
||||
cpparg = self._conversion_data.cpparg_for_cppname(cppstruct_member_access.name)
|
||||
if cpparg and cpparg.decl_spec.type == "AccessorPtr":
|
||||
data_member = cppstruct_member_access.member
|
||||
if data_member:
|
||||
if data_member.name == "name":
|
||||
data_member.name += "().get().c_str()" # It's read-only so this is ok!
|
||||
if data_member.name == "parent":
|
||||
data_member_name = arg_utils.extract_name(data_member.name)
|
||||
if data_member_name == "name":
|
||||
data_member.name = literal.Literal(f"{data_member_name}().get().c_str()") # It's read-only so this is ok!
|
||||
if data_member_name == "parent":
|
||||
# For now we'll strip off the parent bit as that is accessing the grib_section, which we'll probably get
|
||||
# another way!
|
||||
updated_cppstruct_member_access = struct_member_access.StructMemberAccess(cppstruct_member_access.access,
|
||||
cppstruct_member_access.name,
|
||||
cppstruct_member_access.index)
|
||||
cppstruct_member_access.name)
|
||||
debug.line("validate_struct_member_access", f"Updating AccessorPtr grib_section access: [{debug.as_debug_string(cppstruct_member_access)}]->[{updated_cppstruct_member_access}]")
|
||||
return updated_cppstruct_member_access
|
||||
|
||||
|
|
Loading…
Reference in New Issue