diff options
author | Philip Herron <philip.herron@embecosm.com> | 2022-01-13 15:58:34 +0000 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2022-01-14 12:32:49 +0000 |
commit | e9ffd4308d70da55b4c698b07b484063bcda0b01 (patch) | |
tree | 44e707326b0cd47aa0b8a2a7b57f4edcdc4d1863 /gcc | |
parent | 52780af6602763fac6297f7983878b38e0188bb9 (diff) | |
download | gcc-e9ffd4308d70da55b4c698b07b484063bcda0b01.zip gcc-e9ffd4308d70da55b4c698b07b484063bcda0b01.tar.gz gcc-e9ffd4308d70da55b4c698b07b484063bcda0b01.tar.bz2 |
Add HIR::ExprType get_expression_type() const
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-expr.h | 152 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir.h | 69 |
2 files changed, 176 insertions, 45 deletions
diff --git a/gcc/rust/hir/tree/rust-hir-expr.h b/gcc/rust/hir/tree/rust-hir-expr.h index 3a5b8974..1d7d528 100644 --- a/gcc/rust/hir/tree/rust-hir-expr.h +++ b/gcc/rust/hir/tree/rust-hir-expr.h @@ -47,14 +47,17 @@ protected: return clone_expr_with_block_impl (); } - bool is_expr_without_block () const final override { return false; }; - public: // Unique pointer custom clone function std::unique_ptr<ExprWithBlock> clone_expr_with_block () const { return std::unique_ptr<ExprWithBlock> (clone_expr_with_block_impl ()); } + + BlockType get_block_expr_type () const final override + { + return BlockType::WITH_BLOCK; + }; }; // Literals? Or literal base? @@ -98,6 +101,8 @@ public: Literal &get_literal () { return literal; } const Literal &get_literal () const { return literal; } + ExprType get_expression_type () const override final { return ExprType::Lit; } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -167,6 +172,11 @@ public: Location get_locus () const override final { return locus; } std::unique_ptr<Expr> &get_expr () { return main_or_left_expr; } + + ExprType get_expression_type () const override final + { + return ExprType::Operator; + } }; /* Unary prefix & or &mut (or && and &&mut) borrow operator. Cannot be @@ -795,6 +805,11 @@ public: return expr_in_parens; } + ExprType get_expression_type () const override final + { + return ExprType::Grouped; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1006,6 +1021,11 @@ public: ArrayElems *get_internal_elements () { return internal_elements.get (); }; + ExprType get_expression_type () const override final + { + return ExprType::Array; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1019,11 +1039,6 @@ protected: } }; -// Aka IndexExpr (also applies to slices) -/* Apparently a[b] is equivalent to *std::ops::Index::index(&a, b) or - * *std::ops::Index::index_mut(&mut a, b) */ -/* Also apparently deref operations on a will be repeatedly applied to find an - * implementation */ class ArrayIndexExpr : public ExprWithoutBlock { std::unique_ptr<Expr> array_expr; @@ -1072,6 +1087,11 @@ public: Expr *get_array_expr () { return array_expr.get (); } Expr *get_index_expr () { return index_expr.get (); } + ExprType get_expression_type () const override final + { + return ExprType::ArrayIndex; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1158,6 +1178,11 @@ public: bool is_unit () const { return tuple_elems.size () == 0; } + ExprType get_expression_type () const override final + { + return ExprType::Tuple; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1171,18 +1196,12 @@ protected: } }; -// aka TupleIndexingExpr -// HIR representation of a tuple indexing expression class TupleIndexExpr : public ExprWithoutBlock { std::unique_ptr<Expr> tuple_expr; - // TupleIndex is a decimal int literal with no underscores or suffix TupleIndex tuple_index; - Location locus; - // i.e. pair.0 - public: std::string as_string () const override; @@ -1227,6 +1246,11 @@ public: return tuple_expr; } + ExprType get_expression_type () const override final + { + return ExprType::TupleIdx; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1260,6 +1284,11 @@ public: PathInExpression &get_struct_name () { return struct_name; } std::string as_string () const override; + + ExprType get_expression_type () const override final + { + return ExprType::Struct; + } }; // Actual HIR node of the struct creator (with no fields). Not abstract! @@ -1648,7 +1677,6 @@ class CallExpr : public ExprWithoutBlock { std::unique_ptr<Expr> function; std::vector<std::unique_ptr<Expr> > params; - Location locus; public: @@ -1710,6 +1738,11 @@ public: return params; } + ExprType get_expression_type () const override final + { + return ExprType::Call; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1729,7 +1762,6 @@ class MethodCallExpr : public ExprWithoutBlock std::unique_ptr<Expr> receiver; PathExprSegment method_name; std::vector<std::unique_ptr<Expr> > params; - Location locus; public: @@ -1794,6 +1826,11 @@ public: return params; } + ExprType get_expression_type () const override final + { + return ExprType::MethodCall; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1865,6 +1902,11 @@ public: Identifier get_field_name () const { return field; } + ExprType get_expression_type () const override final + { + return ExprType::FieldAccess; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1939,10 +1981,7 @@ public: class ClosureExpr : public ExprWithoutBlock { bool has_move; - std::vector<ClosureParam> params; // may be empty - /* also note a double pipe "||" can be used for empty params - does not need a - * space */ - + std::vector<ClosureParam> params; Location locus; protected: @@ -1957,6 +1996,11 @@ public: std::string as_string () const override; Location get_locus () const override final { return locus; } + + ExprType get_expression_type () const override final + { + return ExprType::Closure; + } }; // Represents a non-type-specified closure expression HIR node @@ -2022,10 +2066,8 @@ class BlockExpr : public ExprWithBlock { public: AST::AttrVec inner_attrs; - std::vector<std::unique_ptr<Stmt> > statements; - std::unique_ptr<Expr> expr; // inlined from Statements - + std::unique_ptr<Expr> expr; bool tail_reachable; Location locus; @@ -2109,6 +2151,11 @@ public: std::vector<std::unique_ptr<Stmt> > &get_statements () { return statements; } + ExprType get_expression_type () const final override + { + return ExprType::Block; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -2199,7 +2246,6 @@ protected: // HIR node representing continue expression within loops class ContinueExpr : public ExprWithoutBlock { - // bool has_label; Lifetime label; Location locus; @@ -2222,6 +2268,11 @@ public: Lifetime &get_label () { return label; } + ExprType get_expression_type () const final override + { + return ExprType::Continue; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -2237,7 +2288,6 @@ protected: return new ContinueExpr (*this); } }; -// TODO: merge "break" and "continue"? Or even merge in "return"? // HIR node representing break expression within loops class BreakExpr : public ExprWithoutBlock @@ -2303,6 +2353,11 @@ public: std::unique_ptr<Expr> &get_expr () { return break_expr; } + ExprType get_expression_type () const override final + { + return ExprType::Break; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -2329,6 +2384,11 @@ protected: public: Location get_locus () const override final { return locus; } + + ExprType get_expression_type () const override final + { + return ExprType::Range; + } }; // Range from (inclusive) and to (exclusive) expression HIR node object @@ -2676,6 +2736,11 @@ public: Expr *get_expr () { return return_expr.get (); } + ExprType get_expression_type () const override final + { + return ExprType::Return; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -2697,7 +2762,6 @@ class UnsafeBlockExpr : public ExprWithBlock { // Or just have it extend BlockExpr std::unique_ptr<BlockExpr> expr; - Location locus; public: @@ -2737,6 +2801,11 @@ public: std::unique_ptr<BlockExpr> &get_block_expr () { return expr; } + ExprType get_expression_type () const override final + { + return ExprType::UnsafeBlock; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -2784,10 +2853,7 @@ public: class BaseLoopExpr : public ExprWithBlock { protected: - // protected to allow subclasses better use of them - // bool has_loop_label; LoopLabel loop_label; - std::unique_ptr<BlockExpr> loop_block; private: @@ -2826,6 +2892,11 @@ protected: BaseLoopExpr (BaseLoopExpr &&other) = default; BaseLoopExpr &operator= (BaseLoopExpr &&other) = default; + ExprType get_expression_type () const final override + { + return ExprType::BaseLoop; + } + public: bool has_loop_label () const { return !loop_label.is_error (); } @@ -3128,6 +3199,8 @@ public: Expr *get_if_condition () { return condition.get (); } BlockExpr *get_if_block () { return if_block.get (); } + ExprType get_expression_type () const final override { return ExprType::If; } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -3346,6 +3419,11 @@ public: void accept_vis (HIRFullVisitor &vis) override; + ExprType get_expression_type () const final override + { + return ExprType::IfLet; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -3898,6 +3976,11 @@ public: const std::vector<MatchCase> &get_match_cases () const { return match_arms; } std::vector<MatchCase> &get_match_cases () { return match_arms; } + ExprType get_expression_type () const final override + { + return ExprType::Match; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -3915,7 +3998,6 @@ protected: class AwaitExpr : public ExprWithoutBlock { std::unique_ptr<Expr> awaited_expr; - Location locus; public: @@ -3952,6 +4034,11 @@ public: void accept_vis (HIRFullVisitor &vis) override; + ExprType get_expression_type () const final override + { + return ExprType::Await; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -3964,10 +4051,8 @@ protected: // Async block expression HIR node (block expr that evaluates to a future) class AsyncBlockExpr : public ExprWithBlock { - // TODO: should this extend BlockExpr rather than be a composite of it? bool has_move; std::unique_ptr<BlockExpr> block_expr; - Location locus; public: @@ -4005,6 +4090,11 @@ public: void accept_vis (HIRFullVisitor &vis) override; + ExprType get_expression_type () const final override + { + return ExprType::AsyncBlock; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index 532b95b..de312ee 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -166,12 +166,46 @@ class ExprWithoutBlock; // Base expression HIR node - abstract class Expr { - // TODO: move outer attribute data to derived classes? AST::AttrVec outer_attrs; - Analysis::NodeMapping mappings; public: + enum BlockType + { + WITH_BLOCK, + WITHOUT_BLOCK, + }; + + enum ExprType + { + Lit, + Operator, + Grouped, + Array, + ArrayIndex, + Tuple, + TupleIdx, + Struct, + Call, + MethodCall, + FieldAccess, + Closure, + Block, + Continue, + Break, + Range, + Return, + UnsafeBlock, + BaseLoop, + If, + IfLet, + Match, + Await, + AsyncBlock, + Ident, + Path, + }; + const AST::AttrVec &get_outer_attrs () const { return outer_attrs; } // Unique pointer custom clone function @@ -180,11 +214,6 @@ public: return std::unique_ptr<Expr> (clone_expr_impl ()); } - /* TODO: public methods that could be useful: - * - get_type() - returns type of expression. set_type() may also be useful - * for some? - * - evaluate() - evaluates expression if constant? can_evaluate()? */ - /* HACK: downcasting without dynamic_cast (if possible) via polymorphism - * overrided in subclasses of ExprWithoutBlock */ virtual ExprWithoutBlock *as_expr_without_block () const { return nullptr; } @@ -196,9 +225,6 @@ public: virtual Location get_locus () const = 0; - // HACK: strictly not needed, but faster than full downcast clone - virtual bool is_expr_without_block () const = 0; - virtual void accept_vis (HIRFullVisitor &vis) = 0; const Analysis::NodeMapping &get_mappings () const { return mappings; } @@ -206,6 +232,10 @@ public: // Clone function implementation as pure virtual method virtual Expr *clone_expr_impl () const = 0; + virtual BlockType get_block_expr_type () const = 0; + + virtual ExprType get_expression_type () const = 0; + protected: // Constructor Expr (Analysis::NodeMapping mappings, @@ -242,8 +272,6 @@ protected: return clone_expr_without_block_impl (); } - bool is_expr_without_block () const final override { return true; }; - public: // Unique pointer custom clone function std::unique_ptr<ExprWithoutBlock> clone_expr_without_block () const @@ -257,6 +285,11 @@ public: { return clone_expr_without_block_impl (); } + + BlockType get_block_expr_type () const final override + { + return BlockType::WITHOUT_BLOCK; + }; }; /* HACK: IdentifierExpr, delete when figure out identifier vs expr problem in @@ -294,6 +327,11 @@ public: Identifier get_identifier () const { return ident; } + ExprType get_expression_type () const final override + { + return ExprType::Ident; + } + protected: // Clone method implementation IdentifierExpr *clone_expr_without_block_impl () const override @@ -760,14 +798,17 @@ protected: {} public: - // TODO: think of a better and less hacky way to allow this - /* Replaces the outer attributes of this path expression with the given outer * attributes. */ void replace_outer_attrs (AST::AttrVec outer_attrs) { set_outer_attrs (std::move (outer_attrs)); } + + ExprType get_expression_type () const final override + { + return ExprType::Path; + } }; } // namespace HIR } // namespace Rust |