Branch data Line data Source code
1 : : // Copyright (C) 2020-2026 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 class 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 : : AnonConst,
62 : : ConstBlock,
63 : : Continue,
64 : : Break,
65 : : Range,
66 : : Return,
67 : : UnsafeBlock,
68 : : BaseLoop,
69 : : If,
70 : : IfLet,
71 : : Match,
72 : : Await,
73 : : AsyncBlock,
74 : : Path,
75 : : InlineAsm,
76 : : LlvmInlineAsm,
77 : : OffsetOf,
78 : : };
79 : :
80 : 0 : BaseKind get_hir_kind () override final { return Node::BaseKind::EXPR; }
81 : :
82 : 45444 : const AST::AttrVec &get_outer_attrs () const { return outer_attrs; }
83 : :
84 : : // Unique pointer custom clone function
85 : 90402 : std::unique_ptr<Expr> clone_expr () const
86 : : {
87 : 99849 : return std::unique_ptr<Expr> (clone_expr_impl ());
88 : : }
89 : :
90 : : // TODO: make pure virtual if move out outer attributes to derived classes
91 : : virtual std::string to_string () const;
92 : :
93 : : std::string to_debug_string () const
94 : : {
95 : : return to_string () + mappings.as_string ();
96 : : }
97 : :
98 : 13564 : virtual ~Expr () {}
99 : :
100 : : virtual location_t get_locus () const = 0;
101 : :
102 : 860927 : const Analysis::NodeMapping &get_mappings () const { return mappings; }
103 : :
104 : : // Clone function implementation as pure virtual method
105 : : virtual Expr *clone_expr_impl () const = 0;
106 : :
107 : : virtual BlockType get_block_expr_type () const = 0;
108 : :
109 : : virtual ExprType get_expression_type () const = 0;
110 : :
111 : : virtual void accept_vis (HIRExpressionVisitor &vis) = 0;
112 : :
113 : : protected:
114 : : // Constructor
115 : : Expr (Analysis::NodeMapping mappings,
116 : : AST::AttrVec outer_attribs = AST::AttrVec ());
117 : :
118 : : // TODO: think of less hacky way to implement this kind of thing
119 : : // Sets outer attributes.
120 : : void set_outer_attrs (AST::AttrVec outer_attrs_to_set)
121 : : {
122 : : outer_attrs = std::move (outer_attrs_to_set);
123 : : }
124 : : };
125 : :
126 : : // HIR node for an expression without an accompanying block - abstract
127 : 0 : class ExprWithoutBlock : public Expr
128 : : {
129 : : protected:
130 : : // Constructor
131 : : ExprWithoutBlock (Analysis::NodeMapping mappings,
132 : : AST::AttrVec outer_attribs = AST::AttrVec ());
133 : :
134 : : // pure virtual clone implementation
135 : : virtual ExprWithoutBlock *clone_expr_without_block_impl () const = 0;
136 : :
137 : : /* Save having to specify two clone methods in derived classes by making expr
138 : : * clone return exprwithoutblock clone. Hopefully won't affect performance too
139 : : * much. */
140 : 821 : ExprWithoutBlock *clone_expr_impl () const override
141 : : {
142 : 821 : return clone_expr_without_block_impl ();
143 : : }
144 : :
145 : : public:
146 : : // Unique pointer custom clone function
147 : : std::unique_ptr<ExprWithoutBlock> clone_expr_without_block () const
148 : : {
149 : : return std::unique_ptr<ExprWithoutBlock> (clone_expr_without_block_impl ());
150 : : }
151 : :
152 : 0 : BlockType get_block_expr_type () const final override
153 : : {
154 : 0 : return BlockType::WITHOUT_BLOCK;
155 : : };
156 : : };
157 : :
158 : : // Base path expression HIR node - abstract
159 : : class PathExpr : public ExprWithoutBlock
160 : : {
161 : : protected:
162 : 48812 : PathExpr (Analysis::NodeMapping mappings, AST::AttrVec outer_attribs)
163 : 48812 : : ExprWithoutBlock (std::move (mappings), std::move (outer_attribs))
164 : 48812 : {}
165 : :
166 : : public:
167 : : /* Replaces the outer attributes of this path expression with the given outer
168 : : * attributes. */
169 : : void replace_outer_attrs (AST::AttrVec outer_attrs)
170 : : {
171 : : set_outer_attrs (std::move (outer_attrs));
172 : : }
173 : :
174 : 71 : ExprType get_expression_type () const final override
175 : : {
176 : 71 : return ExprType::Path;
177 : : }
178 : : };
179 : :
180 : : } // namespace HIR
181 : : } // namespace Rust
182 : :
183 : : #endif
|