Skip to content

Commit d8351d9

Browse files
committed
Decouple the HIR::OperatorExpr from resolving operator overloads
This means we can reuse the same code for operations that are not HIR::OperatorExpr's such as ArrayIndexExpr which can resolve to core::ops::index lang items.
1 parent 5d5396d commit d8351d9

File tree

5 files changed

+34
-4
lines changed

5 files changed

+34
-4
lines changed

gcc/rust/backend/rust-compile-expr.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref,
790790

791791
tree
792792
CompileExpr::resolve_operator_overload (
793-
Analysis::RustLangItem::ItemType lang_item_type, HIR::OperatorExpr &expr,
793+
Analysis::RustLangItem::ItemType lang_item_type, HIR::OperatorExprMeta expr,
794794
tree lhs, tree rhs, HIR::Expr *lhs_expr, HIR::Expr *rhs_expr)
795795
{
796796
TyTy::FnType *fntype;

gcc/rust/backend/rust-compile-expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ class CompileExpr : public HIRCompileBase
799799

800800
tree
801801
resolve_operator_overload (Analysis::RustLangItem::ItemType lang_item_type,
802-
HIR::OperatorExpr &expr, tree lhs, tree rhs,
802+
HIR::OperatorExprMeta expr, tree lhs, tree rhs,
803803
HIR::Expr *lhs_expr, HIR::Expr *rhs_expr);
804804

805805
tree compile_bool_literal (const HIR::LiteralExpr &expr,

gcc/rust/hir/tree/rust-hir-expr.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4029,6 +4029,36 @@ class AsyncBlockExpr : public ExprWithBlock
40294029
return new AsyncBlockExpr (*this);
40304030
}
40314031
};
4032+
4033+
// this is a utility helper class for type-checking and code-generation
4034+
class OperatorExprMeta
4035+
{
4036+
public:
4037+
OperatorExprMeta (HIR::CompoundAssignmentExpr &expr)
4038+
: node_mappings (expr.get_mappings ()), locus (expr.get_locus ())
4039+
{}
4040+
4041+
OperatorExprMeta (HIR::ArithmeticOrLogicalExpr &expr)
4042+
: node_mappings (expr.get_mappings ()), locus (expr.get_locus ())
4043+
{}
4044+
4045+
OperatorExprMeta (HIR::NegationExpr &expr)
4046+
: node_mappings (expr.get_mappings ()), locus (expr.get_locus ())
4047+
{}
4048+
4049+
OperatorExprMeta (HIR::DereferenceExpr &expr)
4050+
: node_mappings (expr.get_mappings ()), locus (expr.get_locus ())
4051+
{}
4052+
4053+
const Analysis::NodeMapping &get_mappings () const { return node_mappings; }
4054+
4055+
Location get_locus () const { return locus; }
4056+
4057+
private:
4058+
const Analysis::NodeMapping node_mappings;
4059+
Location locus;
4060+
};
4061+
40324062
} // namespace HIR
40334063
} // namespace Rust
40344064

gcc/rust/typecheck/rust-hir-type-check-expr.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ TypeCheckExpr::visit (HIR::ArrayIndexExpr &expr)
288288

289289
bool
290290
TypeCheckExpr::resolve_operator_overload (
291-
Analysis::RustLangItem::ItemType lang_item_type, HIR::OperatorExpr &expr,
291+
Analysis::RustLangItem::ItemType lang_item_type, HIR::OperatorExprMeta expr,
292292
TyTy::BaseType *lhs, TyTy::BaseType *rhs)
293293
{
294294
// look up lang item for arithmetic type

gcc/rust/typecheck/rust-hir-type-check-expr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ class TypeCheckExpr : public TypeCheckBase
12391239
protected:
12401240
bool
12411241
resolve_operator_overload (Analysis::RustLangItem::ItemType lang_item_type,
1242-
HIR::OperatorExpr &expr, TyTy::BaseType *lhs,
1242+
HIR::OperatorExprMeta expr, TyTy::BaseType *lhs,
12431243
TyTy::BaseType *rhs);
12441244

12451245
private:

0 commit comments

Comments
 (0)