Skip to content

Commit

Permalink
ast: Add new Expr::Kinds
Browse files Browse the repository at this point in the history
Collapses all of the OperatorExprs into Expr instead of first having to check for OperatorExpr and
then check for each OperatorExpr::Kind.

gcc/rust/ChangeLog:

	* ast/rust-ast.h: Add new Expr::Kinds.
	* ast/rust-expr.h: Implement missing get_expr_kind(), Add get_function_expr_ptr()
  • Loading branch information
CohenArthur committed Jan 30, 2025
1 parent ad46f08 commit 51a2da3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,16 @@ class Expr : public Visitable
Identifier,
FormatArgs,
MacroInvocation,
Borrow,
Dereference,
ErrorPropagation,
Negation,
ArithmeticOrLogical,
Comparison,
LazyBoolean,
TypeCast,
Assignment,
CompoundAssignment,
};

virtual Kind get_expr_kind () const = 0;
Expand Down
31 changes: 31 additions & 0 deletions gcc/rust/ast/rust-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ class BorrowExpr : public OperatorExpr
bool get_is_double_borrow () const { return double_borrow; }
bool is_raw_borrow () const { return raw_borrow; }

Expr::Kind get_expr_kind () const override { return Expr::Kind::Borrow; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -437,6 +439,8 @@ class DereferenceExpr : public OperatorExpr
return *main_or_left_expr;
}

Expr::Kind get_expr_kind () const override { return Expr::Kind::Dereference; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -468,6 +472,11 @@ class ErrorPropagationExpr : public OperatorExpr
return *main_or_left_expr;
}

Expr::Kind get_expr_kind () const override
{
return Expr::Kind::ErrorPropagation;
}

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -511,6 +520,8 @@ class NegationExpr : public OperatorExpr
return *main_or_left_expr;
}

Expr::Kind get_expr_kind () const override { return Expr::Kind::Negation; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -599,6 +610,11 @@ class ArithmeticOrLogicalExpr : public OperatorExpr
void visit_lhs (ASTVisitor &vis) { main_or_left_expr->accept_vis (vis); }
void visit_rhs (ASTVisitor &vis) { right_expr->accept_vis (vis); }

Expr::Kind get_expr_kind () const override
{
return Expr::Kind::ArithmeticOrLogical;
}

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -686,6 +702,8 @@ class ComparisonExpr : public OperatorExpr

ExprType get_kind () { return expr_type; }

Expr::Kind get_expr_kind () const override { return Expr::Kind::Comparison; }

/* TODO: implement via a function call to std::cmp::PartialEq::eq(&op1, &op2)
* maybe? */
protected:
Expand Down Expand Up @@ -774,6 +792,8 @@ class LazyBooleanExpr : public OperatorExpr

ExprType get_kind () { return expr_type; }

Expr::Kind get_expr_kind () const override { return Expr::Kind::LazyBoolean; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -836,6 +856,8 @@ class TypeCastExpr : public OperatorExpr
return *type_to_convert_to;
}

Expr::Kind get_expr_kind () const override { return Expr::Kind::TypeCast; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -914,6 +936,8 @@ class AssignmentExpr : public OperatorExpr
return *right_expr;
}

Expr::Kind get_expr_kind () const override { return Expr::Kind::Assignment; }

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -1000,6 +1024,11 @@ class CompoundAssignmentExpr : public OperatorExpr
return right_expr;
}

Expr::Kind get_expr_kind () const override
{
return Expr::Kind::CompoundAssignment;
}

protected:
/* Use covariance to implement clone function as returning this object rather
* than base */
Expand Down Expand Up @@ -2139,6 +2168,8 @@ class CallExpr : public ExprWithoutBlock
return *function;
}

std::unique_ptr<Expr> &get_function_expr_ptr () { return function; }

const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; }

Expand Down

0 comments on commit 51a2da3

Please sign in to comment.