Fixed return statement bug

This commit is contained in:
kevstone 2024-02-06 15:13:46 +00:00
parent 57a02f2831
commit 97b01e1374
3 changed files with 31 additions and 4 deletions

View File

@ -16,11 +16,13 @@ class ReturnStatement(code_interface.CodeInterface):
return self._expression
def as_lines(self):
lines = ["return"]
return_string = "return"
if self._expression:
lines[-1] += " "
lines.extend(self._expression.as_lines())
return_string += " " + self._expression.as_string()
lines = [f"{return_string}"]
if not lines[-1].endswith(";"):
lines[-1] += ";"

View File

@ -10,6 +10,7 @@ import code_object.binary_operation as binary_operation
import code_object.struct_member_access as struct_member_access
import code_object_converter.conversion_pack.arg_utils as arg_utils
import code_object.variable_declaration as variable_declaration
import code_object.paren_expression as paren_expression
from code_object_converter.conversion_funcs import as_commented_out_code
from utils.string_funcs import is_number
@ -60,6 +61,19 @@ class DefaultConversionValidation(conversion_validation.ConversionValidation):
# Just return the passed in value!
return calling_arg_value
def validate_unary_expression(self, cunary_expression, cppunary_expression):
debug.line("validate_unary_operation", f"cppunary_expression.keyword=[{debug.as_debug_string(cppunary_expression.keyword)}] cppunary_expression.expression=[{debug.as_debug_string(cppunary_expression.expression)}]")
if cppunary_expression.keyword == "sizeof":
assert isinstance(cppunary_expression.expression, paren_expression.ParenExpression)
cpparg = arg_utils.to_cpparg(cppunary_expression.expression.expression, self._conversion_data)
if cpparg and self._conversion_data.is_container_type(cpparg.decl_spec.type):
cpp_size_call = literal.Literal(f"{cpparg.name}.size()")
debug.line("validate_unary_operation", f"Updating size evaluation: [{debug.as_debug_string(cppunary_expression)}]->[{debug.as_debug_string(cpp_size_call)}]")
return cpp_size_call
return cppunary_expression
def validate_unary_operation(self, cunary_operation, cppunary_operation):
debug.line("validate_unary_operation", f"cppunary_operation.operand string=[{debug.as_debug_string(cppunary_operation.operand)}] type=[{type(cppunary_operation.operand).__name__}]")

View File

@ -12,6 +12,7 @@ import code_object.if_statement as if_statement
import code_object.virtual_member_function as virtual_member_function
import code_object.constructor_function as constructor_function
import code_object.function_call as function_call
import code_object_converter.conversion_pack.arg_utils as arg_utils
from grib_accessor.grib_accessor_conversion_pack.grib_accessor_special_function_call_conversion import apply_special_function_call_conversions
from code_object.code_interface import NONE_VALUE
@ -108,15 +109,25 @@ class GribAccessorConversionValidation(default_conversion_validation.DefaultConv
def validate_return_statement(self, creturn_statement, cppreturn_statement):
debug.line("validate_return_statement", f"creturn_statement=[{debug.as_debug_string(creturn_statement)}] cppreturn_statement=[{debug.as_debug_string(cppreturn_statement)}]")
mapping = self._conversion_data.funcsig_mapping_for_current_cfuncname()
if mapping:
if mapping.cppfuncsig.return_type.type == "GribStatus":
cppfunc_return_type = mapping.cppfuncsig.return_type.type
cpparg = arg_utils.to_cpparg(cppreturn_statement.expression, self._conversion_data)
updated_cpp_expression = None
if cpparg:
if cpparg.decl_spec.type != cppfunc_return_type:
updated_cpp_expression = literal.Literal(f"static_cast<GribStatus>({cpparg.name})")
elif cppfunc_return_type == "GribStatus":
cpp_expression = cppreturn_statement.expression.as_string()
if cpp_expression == "0":
updated_cpp_expression = literal.Literal("GribStatus::SUCCESS")
else:
updated_cpp_expression = literal.Literal(f"static_cast<GribStatus>({cpp_expression})")
if updated_cpp_expression:
return return_statement.ReturnStatement(updated_cpp_expression)
return super().validate_return_statement(creturn_statement, cppreturn_statement)