• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ES2PANDA_IR_ASTNODE_H
17 #define ES2PANDA_IR_ASTNODE_H
18 
19 #include <functional>
20 #include <macros.h>
21 
22 #include <binder/binder.h>
23 #include <binder/scope.h>
24 #include <ir/astNodeMapping.h>
25 #include <lexer/token/sourceLocation.h>
26 #include <util/enumbitops.h>
27 
28 namespace panda::es2panda::compiler {
29 class PandaGen;
30 }  // namespace panda::es2panda::compiler
31 
32 namespace panda::es2panda::checker {
33 class Checker;
34 class Type;
35 }  // namespace panda::es2panda::checker
36 
37 namespace panda::es2panda::ir {
38 
39 class AstNode;
40 
41 using NodeTraverser = std::function<void(AstNode *)>;
42 
43 using UpdateNodes = std::variant<AstNode *, std::vector<AstNode *>>;
44 using NodeUpdater = std::function<UpdateNodes(AstNode *)>;
45 
46 enum class AstNodeType {
47 #define DECLARE_NODE_TYPES(nodeType, className) nodeType,
48     AST_NODE_MAPPING(DECLARE_NODE_TYPES)
49 #undef DECLARE_NODE_TYPES
50 #define DECLARE_NODE_TYPES(nodeType1, nodeType2, baseClass, reinterpretClass) nodeType1, nodeType2,
51         AST_NODE_REINTERPRET_MAPPING(DECLARE_NODE_TYPES)
52 #undef DECLARE_NODE_TYPES
53 };
54 
55 enum class AstNodeFlags {
56     NO_OPTS = 0,
57     STRICT = (1U << 0U),
58     PARAMETER = (1U << 1U),
59 };
60 
61 DEFINE_BITOPS(AstNodeFlags)
62 
63 enum class ModifierFlags {
64     NONE = 0,
65     STATIC = 1 << 0,
66     ASYNC = 1 << 1,
67     PUBLIC = 1 << 2,
68     PROTECTED = 1 << 3,
69     PRIVATE = 1 << 4,
70     DECLARE = 1 << 5,
71     READONLY = 1 << 6,
72     OPTIONAL = 1 << 7,
73     DEFINITE = 1 << 8,
74     ABSTRACT = 1 << 9,
75     ACCESS = PUBLIC | PROTECTED | PRIVATE,
76     ALL = STATIC | ASYNC | ACCESS | DECLARE | READONLY | ABSTRACT,
77     ALLOWED_IN_CTOR_PARAMETER = ACCESS | READONLY,
78 };
79 
80 DEFINE_BITOPS(ModifierFlags)
81 
82 enum class ScriptFunctionFlags {
83     NONE = 0,
84     GENERATOR = 1 << 0,
85     ASYNC = 1 << 1,
86     ARROW = 1 << 2,
87     EXPRESSION = 1 << 3,
88     OVERLOAD = 1 << 4,
89     CONSTRUCTOR = 1 << 5,
90     METHOD = 1 << 6,
91     CONCURRENT = 1 << 7,
92     SHOW_SOURCE = 1 << 8
93 };
94 
95 DEFINE_BITOPS(ScriptFunctionFlags)
96 
97 enum class TSOperatorType { READONLY, KEYOF, UNIQUE };
98 enum class MappedOption { NO_OPTS, PLUS, MINUS };
99 
100 // Predefinitions
101 class AstDumper;
102 class Expression;
103 class Statement;
104 
105 #define DECLARE_CLASSES(nodeType, className) class className;
106 AST_NODE_MAPPING(DECLARE_CLASSES)
107 #undef DECLARE_CLASSES
108 
109 #define DECLARE_CLASSES(nodeType1, nodeType2, baseClass, reinterpretClass) class baseClass;
AST_NODE_REINTERPRET_MAPPING(DECLARE_CLASSES)110 AST_NODE_REINTERPRET_MAPPING(DECLARE_CLASSES)
111 #undef DECLARE_CLASSES
112 
113 class AstNode {
114 public:
115     explicit AstNode(AstNodeType type) : type_(type) {};
116     virtual ~AstNode() = default;
117     NO_COPY_SEMANTIC(AstNode);
118     NO_MOVE_SEMANTIC(AstNode);
119 
120     bool IsProgram() const
121     {
122         return parent_ == nullptr;
123     }
124 
125 #define DECLARE_IS_CHECKS(nodeType, className) \
126     bool Is##className() const                 \
127     {                                          \
128         return type_ == AstNodeType::nodeType; \
129     }
130     AST_NODE_MAPPING(DECLARE_IS_CHECKS)
131 #undef DECLARE_IS_CHECKS
132 
133 #define DECLARE_IS_CHECKS(nodeType1, nodeType2, baseClass, reinterpretClass) \
134     bool Is##baseClass() const                                               \
135     {                                                                        \
136         return type_ == AstNodeType::nodeType1;                              \
137     }                                                                        \
138     bool Is##reinterpretClass() const                                        \
139     {                                                                        \
140         return type_ == AstNodeType::nodeType2;                              \
141     }
142     AST_NODE_REINTERPRET_MAPPING(DECLARE_IS_CHECKS)
143 #undef DECLARE_IS_CHECKS
144 
145     virtual bool IsStatement() const
146     {
147         return false;
148     }
149 
150     virtual bool IsExpression() const
151     {
152         return false;
153     }
154 
155 #define DECLARE_AS_CASTS(nodeType, className)             \
156     className *As##className()                            \
157     {                                                     \
158         ASSERT(Is##className());                          \
159         return reinterpret_cast<className *>(this);       \
160     }                                                     \
161     const className *As##className() const                \
162     {                                                     \
163         ASSERT(Is##className());                          \
164         return reinterpret_cast<const className *>(this); \
165     }
166     AST_NODE_MAPPING(DECLARE_AS_CASTS)
167 #undef DECLARE_AS_CASTS
168 
169 #define DECLARE_AS_CASTS(nodeType1, nodeType2, baseClass, reinterpretClass) \
170     baseClass *As##baseClass()                                              \
171     {                                                                       \
172         ASSERT(Is##baseClass());                                            \
173         return reinterpret_cast<baseClass *>(this);                         \
174     }                                                                       \
175     baseClass *As##reinterpretClass()                                       \
176     {                                                                       \
177         ASSERT(Is##reinterpretClass());                                     \
178         return reinterpret_cast<baseClass *>(this);                         \
179     }                                                                       \
180     const baseClass *As##baseClass() const                                  \
181     {                                                                       \
182         ASSERT(Is##baseClass());                                            \
183         return reinterpret_cast<const baseClass *>(this);                   \
184     }                                                                       \
185     const baseClass *As##reinterpretClass() const                           \
186     {                                                                       \
187         ASSERT(Is##reinterpretClass());                                     \
188         return reinterpret_cast<const baseClass *>(this);                   \
189     }
190     AST_NODE_REINTERPRET_MAPPING(DECLARE_AS_CASTS)
191 #undef DECLARE_AS_CASTS
192 
193     Expression *AsExpression()
194     {
195         ASSERT(IsExpression());
196         return reinterpret_cast<Expression *>(this);
197     }
198 
199     const Expression *AsExpression() const
200     {
201         ASSERT(IsExpression());
202         return reinterpret_cast<const Expression *>(this);
203     }
204 
205     Statement *AsStatement()
206     {
207         ASSERT(IsStatement());
208         return reinterpret_cast<Statement *>(this);
209     }
210 
211     const Statement *AsStatement() const
212     {
213         ASSERT(IsStatement());
214         return reinterpret_cast<const Statement *>(this);
215     }
216 
217     void SetRange(const lexer::SourceRange &loc)
218     {
219         range_ = loc;
220     }
221 
222     void SetStart(const lexer::SourcePosition &start)
223     {
224         range_.start = start;
225     }
226 
227     void SetEnd(const lexer::SourcePosition &end)
228     {
229         range_.end = end;
230     }
231 
232     const lexer::SourcePosition &Start() const
233     {
234         return range_.start;
235     }
236 
237     const lexer::SourcePosition &End() const
238     {
239         return range_.end;
240     }
241 
242     const lexer::SourceRange &Range() const
243     {
244         return range_;
245     }
246 
247     AstNodeType Type() const
248     {
249         return type_;
250     }
251 
252     const AstNode *Parent()
253     {
254         return parent_;
255     }
256 
257     const AstNode *Parent() const
258     {
259         return parent_;
260     }
261 
262     void SetParent(const AstNode *parent)
263     {
264         parent_ = parent;
265     }
266 
267     const AstNode *Original() const
268     {
269         return original_;
270     }
271 
272     void SetOriginal(const AstNode *original)
273     {
274         original_ = original;
275     }
276 
277     binder::Variable *Variable() const
278     {
279         return variable_;
280     }
281 
282     void SetVariable(binder::Variable *variable)
283     {
284         variable_ = variable;
285     }
286 
287     virtual void Iterate(const NodeTraverser &cb) const = 0;
288     virtual void Dump(ir::AstDumper *dumper) const = 0;
289     virtual void Compile([[maybe_unused]] compiler::PandaGen *pg) const = 0;
290     virtual checker::Type *Check([[maybe_unused]] checker::Checker *checker) const = 0;
291     virtual void UpdateSelf([[maybe_unused]] const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) = 0;
292 
293 protected:
294     void SetType(AstNodeType type)
295     {
296         type_ = type;
297     }
298 
299     const AstNode *parent_ {};
300     lexer::SourceRange range_ {};
301     AstNodeType type_;
302     binder::Variable *variable_ {nullptr};
303     const AstNode *original_ {nullptr};
304 };
305 
306 }  // namespace panda::es2panda::ir
307 #endif
308