Skip to content

Commit

Permalink
gccrs: feat: Made changes to ensure no wrong assignments are done.
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* backend/rust-compile-base.cc (HIRCompileBase::lvalue_p): Created a function that
	checks the lvalue.
	* backend/rust-compile-base.h: Created the Signature for above function
	* backend/rust-compile-expr.cc (lvalue_p): Moved the function from here to
	rust-compile-base.
	(CompileExpr::visit): Made changes to ensure proper readability and checking for
	wrong assignments.

Signed-off-by: Sri Ganesh Thota <[email protected]>
  • Loading branch information
sriganeshres committed Dec 14, 2024
1 parent 167b909 commit 2a551f5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 52 deletions.
46 changes: 46 additions & 0 deletions gcc/rust/backend/rust-compile-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1015,5 +1015,51 @@ HIRCompileBase::unit_expression (location_t locus)
return Backend::constructor_expression (unit_type, false, {}, -1, locus);
}

bool
HIRCompileBase::lvalue_p (const_tree ref)
{
const enum tree_code code = TREE_CODE (ref);

switch (code)
{
case REALPART_EXPR:
case IMAGPART_EXPR:
case COMPONENT_REF:
return lvalue_p (TREE_OPERAND (ref, 0));

case COMPOUND_LITERAL_EXPR:
case STRING_CST:
case CONST_DECL:
case INTEGER_CST:
return true;

case MEM_REF:
case TARGET_MEM_REF:
/* MEM_REFs can appear from -fgimple parsing or folding, so allow them
here as well. */
case INDIRECT_REF:
case ARRAY_REF:
case VAR_DECL:
case PARM_DECL:
case RESULT_DECL:
case ERROR_MARK:
return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
&& TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);

case BIND_EXPR:
return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
case PLUS_EXPR:
case MINUS_EXPR:
case MULT_EXPR:
case POINTER_PLUS_EXPR:
case POINTER_DIFF_EXPR:
case MULT_HIGHPART_EXPR:
case TRUNC_DIV_EXPR:
return false;
default:
return false;
}
}

} // namespace Compile
} // namespace Rust
1 change: 1 addition & 0 deletions gcc/rust/backend/rust-compile-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class HIRCompileBase
virtual ~HIRCompileBase () {}

static tree address_expression (tree expr, location_t locus);
bool lvalue_p (const_tree ref);

protected:
HIRCompileBase (Context *ctx) : ctx (ctx) {}
Expand Down
56 changes: 4 additions & 52 deletions gcc/rust/backend/rust-compile-expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ CompileExpr::visit (HIR::ContinueExpr &expr)

if (!ctx->lookup_label_decl (ref, &label))
{
rust_error_at (expr.get_label ().get_locus (),
rust_error_at (expr.get_label ().get_locus (), ErrorCode::E0770,
"failed to lookup compiled label");
return;
}
Expand Down Expand Up @@ -958,63 +958,15 @@ CompileExpr::visit (HIR::LiteralExpr &expr)
}
}

bool
lvalue_p (const_tree ref)
{
const enum tree_code code = TREE_CODE (ref);

switch (code)
{
case REALPART_EXPR:
case IMAGPART_EXPR:
case COMPONENT_REF:
return lvalue_p (TREE_OPERAND (ref, 0));

case C_MAYBE_CONST_EXPR:
return lvalue_p (TREE_OPERAND (ref, 1));

case COMPOUND_LITERAL_EXPR:
case STRING_CST:
case CONST_DECL:
case INTEGER_CST:
return true;

case MEM_REF:
case TARGET_MEM_REF:
/* MEM_REFs can appear from -fgimple parsing or folding, so allow them
here as well. */
case INDIRECT_REF:
case ARRAY_REF:
case VAR_DECL:
case PARM_DECL:
case RESULT_DECL:
case ERROR_MARK:
return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
&& TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);

case BIND_EXPR:
return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
case PLUS_EXPR:
case MINUS_EXPR:
case MULT_EXPR:
case POINTER_PLUS_EXPR:
case POINTER_DIFF_EXPR:
case MULT_HIGHPART_EXPR:
case TRUNC_DIV_EXPR:
return false;
default:
rust_debug ("unknown");
return false;
}
}

void
CompileExpr::visit (HIR::AssignmentExpr &expr)
{
auto lvalue = CompileExpr::Compile (expr.get_lhs (), ctx);
auto rvalue = CompileExpr::Compile (expr.get_rhs (), ctx);

if (!lvalue_p (lvalue)
bool validl_value = lvalue_p (lvalue);

if (!validl_value
|| expr.get_lhs ().get_expression_type ()
== HIR::Expr::ExprType::Operator)
{
Expand Down

0 comments on commit 2a551f5

Please sign in to comment.