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