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