Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 Free Software Foundation, Inc.
2 : :
3 : : // This file is part of GCC.
4 : :
5 : : // GCC is free software; you can redistribute it and/or modify it under
6 : : // the terms of the GNU General Public License as published by the Free
7 : : // Software Foundation; either version 3, or (at your option) any later
8 : : // version.
9 : :
10 : : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : : // for more details.
14 : :
15 : : // You should have received a copy of the GNU General Public License
16 : : // along with GCC; see the file COPYING3. If not see
17 : : // <http://www.gnu.org/licenses/>.
18 : :
19 : : #ifndef RUST_HIR_EXPR_ABSTRACT_H
20 : : #define RUST_HIR_EXPR_ABSTRACT_H
21 : :
22 : : #include "rust-ast.h"
23 : : #include "rust-hir-visitable.h"
24 : : #include "rust-hir-node.h"
25 : :
26 : : namespace Rust {
27 : : namespace HIR {
28 : :
29 : : // Base expression HIR node - abstract
30 : 0 : class Expr : public Node, virtual public FullVisitable
31 : : {
32 : : public:
33 : : using FullVisitable::accept_vis;
34 : :
35 : : protected:
36 : : AST::AttrVec outer_attrs;
37 : : Analysis::NodeMapping mappings;
38 : :
39 : : public:
40 : : enum BlockType
41 : : {
42 : : WITH_BLOCK,
43 : : WITHOUT_BLOCK,
44 : : };
45 : :
46 : : enum ExprType
47 : : {
48 : : Lit,
49 : : Operator,
50 : : Grouped,
51 : : Array,
52 : : ArrayIndex,
53 : : Tuple,
54 : : TupleIdx,
55 : : Struct,
56 : : Call,
57 : : MethodCall,
58 : : FieldAccess,
59 : : Closure,
60 : : Block,
61 : : Continue,
62 : : Break,
63 : : Range,
64 : : Return,
65 : : UnsafeBlock,
66 : : BaseLoop,
67 : : If,
68 : : IfLet,
69 : : Match,
70 : : Await,
71 : : AsyncBlock,
72 : : Path,
73 : : InlineAsm,
74 : : LlvmInlineAsm,
75 : : };
76 : :
77 : 0 : BaseKind get_hir_kind () override final { return Node::BaseKind::EXPR; }
78 : :
79 : 0 : const AST::AttrVec &get_outer_attrs () const { return outer_attrs; }
80 : :
81 : : // Unique pointer custom clone function
82 : 28127 : std::unique_ptr<Expr> clone_expr () const
83 : : {
84 : 28596 : return std::unique_ptr<Expr> (clone_expr_impl ());
85 : : }
86 : :
87 : : // TODO: make pure virtual if move out outer attributes to derived classes
88 : : virtual std::string as_string () const;
89 : :
90 : 10087 : virtual ~Expr () {}
91 : :
92 : : virtual location_t get_locus () const = 0;
93 : :
94 : 537094 : const Analysis::NodeMapping &get_mappings () const { return mappings; }
95 : :
96 : : // Clone function implementation as pure virtual method
97 : : virtual Expr *clone_expr_impl () const = 0;
98 : :
99 : : virtual BlockType get_block_expr_type () const = 0;
100 : :
101 : : virtual ExprType get_expression_type () const = 0;
102 : :
103 : : virtual void accept_vis (HIRExpressionVisitor &vis) = 0;
104 : :
105 : : protected:
106 : : // Constructor
107 : : Expr (Analysis::NodeMapping mappings,
108 : : AST::AttrVec outer_attribs = AST::AttrVec ());
109 : :
110 : : // TODO: think of less hacky way to implement this kind of thing
111 : : // Sets outer attributes.
112 : : void set_outer_attrs (AST::AttrVec outer_attrs_to_set)
113 : : {
114 : : outer_attrs = std::move (outer_attrs_to_set);
115 : : }
116 : : };
117 : :
118 : : // HIR node for an expression without an accompanying block - abstract
119 : 0 : class ExprWithoutBlock : public Expr
120 : : {
121 : : protected:
122 : : // Constructor
123 : : ExprWithoutBlock (Analysis::NodeMapping mappings,
124 : : AST::AttrVec outer_attribs = AST::AttrVec ());
125 : :
126 : : // pure virtual clone implementation
127 : : virtual ExprWithoutBlock *clone_expr_without_block_impl () const = 0;
128 : :
129 : : /* Save having to specify two clone methods in derived classes by making expr
130 : : * clone return exprwithoutblock clone. Hopefully won't affect performance too
131 : : * much. */
132 : 1001 : ExprWithoutBlock *clone_expr_impl () const override
133 : : {
134 : 1001 : return clone_expr_without_block_impl ();
135 : : }
136 : :
137 : : public:
138 : : // Unique pointer custom clone function
139 : : std::unique_ptr<ExprWithoutBlock> clone_expr_without_block () const
140 : : {
141 : : return std::unique_ptr<ExprWithoutBlock> (clone_expr_without_block_impl ());
142 : : }
143 : :
144 : 0 : BlockType get_block_expr_type () const final override
145 : : {
146 : 0 : return BlockType::WITHOUT_BLOCK;
147 : : };
148 : : };
149 : :
150 : : // Base path expression HIR node - abstract
151 : : class PathExpr : public ExprWithoutBlock
152 : : {
153 : : protected:
154 : 37939 : PathExpr (Analysis::NodeMapping mappings, AST::AttrVec outer_attribs)
155 : 37939 : : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs))
156 : 37939 : {}
157 : :
158 : : public:
159 : : /* Replaces the outer attributes of this path expression with the given outer
160 : : * attributes. */
161 : : void replace_outer_attrs (AST::AttrVec outer_attrs)
162 : : {
163 : : set_outer_attrs (std::move (outer_attrs));
164 : : }
165 : :
166 : 286 : ExprType get_expression_type () const final override
167 : : {
168 : 286 : return ExprType::Path;
169 : : }
170 : : };
171 : :
172 : : } // namespace HIR
173 : : } // namespace Rust
174 : :
175 : : #endif
|