• 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     ACCESSOR = 1 << 10,
76     OVERRIDE = 1 << 11,
77     ACCESS = PUBLIC | PROTECTED | PRIVATE,
78     ALL = STATIC | ASYNC | ACCESS | DECLARE | READONLY | ABSTRACT | ACCESSOR | OVERRIDE,
79     ALLOWED_IN_CTOR_PARAMETER = ACCESS | READONLY | OVERRIDE,
80 };
81 
82 DEFINE_BITOPS(ModifierFlags)
83 
84 enum class ScriptFunctionFlags {
85     NONE = 0,
86     GENERATOR = 1 << 0,
87     ASYNC = 1 << 1,
88     ARROW = 1 << 2,
89     EXPRESSION = 1 << 3,
90     OVERLOAD = 1 << 4,
91     CONSTRUCTOR = 1 << 5,
92     METHOD = 1 << 6,
93     CONCURRENT = 1 << 7,
94     SHOW_SOURCE = 1 << 8,
95     STATIC_INITIALIZER = 1 << 9,
96     INSTANCE_INITIALIZER = 1 << 10,
97 };
98 
99 DEFINE_BITOPS(ScriptFunctionFlags)
100 
101 enum class TSOperatorType { READONLY, KEYOF, UNIQUE };
102 enum class MappedOption { NO_OPTS, PLUS, MINUS };
103 
104 // Predefinitions
105 class AstDumper;
106 class Expression;
107 class Statement;
108 
109 #define DECLARE_CLASSES(nodeType, className) class className;
110 AST_NODE_MAPPING(DECLARE_CLASSES)
111 #undef DECLARE_CLASSES
112 
113 #define DECLARE_CLASSES(nodeType1, nodeType2, baseClass, reinterpretClass) class baseClass;
AST_NODE_REINTERPRET_MAPPING(DECLARE_CLASSES)114 AST_NODE_REINTERPRET_MAPPING(DECLARE_CLASSES)
115 #undef DECLARE_CLASSES
116 
117 class AstNode {
118 public:
119     explicit AstNode(AstNodeType type) : type_(type) {};
120     virtual ~AstNode() = default;
121     NO_COPY_SEMANTIC(AstNode);
122     NO_MOVE_SEMANTIC(AstNode);
123 
124     bool IsProgram() const
125     {
126         return parent_ == nullptr;
127     }
128 
129 #define DECLARE_IS_CHECKS(nodeType, className) \
130     bool Is##className() const                 \
131     {                                          \
132         return type_ == AstNodeType::nodeType; \
133     }
134     AST_NODE_MAPPING(DECLARE_IS_CHECKS)
135 #undef DECLARE_IS_CHECKS
136 
137 #define DECLARE_IS_CHECKS(nodeType1, nodeType2, baseClass, reinterpretClass) \
138     bool Is##baseClass() const                                               \
139     {                                                                        \
140         return type_ == AstNodeType::nodeType1;                              \
141     }                                                                        \
142     bool Is##reinterpretClass() const                                        \
143     {                                                                        \
144         return type_ == AstNodeType::nodeType2;                              \
145     }
146     AST_NODE_REINTERPRET_MAPPING(DECLARE_IS_CHECKS)
147 #undef DECLARE_IS_CHECKS
148 
149     virtual bool IsStatement() const
150     {
151         return false;
152     }
153 
154     virtual bool IsExpression() const
155     {
156         return false;
157     }
158 
159 #define DECLARE_AS_CASTS(nodeType, className)             \
160     className *As##className()                            \
161     {                                                     \
162         ASSERT(Is##className());                          \
163         return reinterpret_cast<className *>(this);       \
164     }                                                     \
165     const className *As##className() const                \
166     {                                                     \
167         ASSERT(Is##className());                          \
168         return reinterpret_cast<const className *>(this); \
169     }
170     AST_NODE_MAPPING(DECLARE_AS_CASTS)
171 #undef DECLARE_AS_CASTS
172 
173 #define DECLARE_AS_CASTS(nodeType1, nodeType2, baseClass, reinterpretClass) \
174     baseClass *As##baseClass()                                              \
175     {                                                                       \
176         ASSERT(Is##baseClass());                                            \
177         return reinterpret_cast<baseClass *>(this);                         \
178     }                                                                       \
179     baseClass *As##reinterpretClass()                                       \
180     {                                                                       \
181         ASSERT(Is##reinterpretClass());                                     \
182         return reinterpret_cast<baseClass *>(this);                         \
183     }                                                                       \
184     const baseClass *As##baseClass() const                                  \
185     {                                                                       \
186         ASSERT(Is##baseClass());                                            \
187         return reinterpret_cast<const baseClass *>(this);                   \
188     }                                                                       \
189     const baseClass *As##reinterpretClass() const                           \
190     {                                                                       \
191         ASSERT(Is##reinterpretClass());                                     \
192         return reinterpret_cast<const baseClass *>(this);                   \
193     }
194     AST_NODE_REINTERPRET_MAPPING(DECLARE_AS_CASTS)
195 #undef DECLARE_AS_CASTS
196 
197     Expression *AsExpression()
198     {
199         ASSERT(IsExpression());
200         return reinterpret_cast<Expression *>(this);
201     }
202 
203     const Expression *AsExpression() const
204     {
205         ASSERT(IsExpression());
206         return reinterpret_cast<const Expression *>(this);
207     }
208 
209     Statement *AsStatement()
210     {
211         ASSERT(IsStatement());
212         return reinterpret_cast<Statement *>(this);
213     }
214 
215     const Statement *AsStatement() const
216     {
217         ASSERT(IsStatement());
218         return reinterpret_cast<const Statement *>(this);
219     }
220 
221     void SetRange(const lexer::SourceRange &loc)
222     {
223         range_ = loc;
224     }
225 
226     void SetStart(const lexer::SourcePosition &start)
227     {
228         range_.start = start;
229     }
230 
231     void SetEnd(const lexer::SourcePosition &end)
232     {
233         range_.end = end;
234     }
235 
236     const lexer::SourcePosition &Start() const
237     {
238         return range_.start;
239     }
240 
241     const lexer::SourcePosition &End() const
242     {
243         return range_.end;
244     }
245 
246     const lexer::SourceRange &Range() const
247     {
248         return range_;
249     }
250 
251     AstNodeType Type() const
252     {
253         return type_;
254     }
255 
256     const AstNode *Parent()
257     {
258         return parent_;
259     }
260 
261     const AstNode *Parent() const
262     {
263         return parent_;
264     }
265 
266     void SetParent(const AstNode *parent)
267     {
268         parent_ = parent;
269     }
270 
271     const AstNode *Original() const
272     {
273         return original_;
274     }
275 
276     void SetOriginal(const AstNode *original)
277     {
278         original_ = original;
279     }
280 
281     binder::Variable *Variable() const
282     {
283         return variable_;
284     }
285 
286     void SetVariable(binder::Variable *variable)
287     {
288         variable_ = variable;
289     }
290 
291     virtual void Iterate(const NodeTraverser &cb) const = 0;
292     virtual void Dump(ir::AstDumper *dumper) const = 0;
293     virtual void Compile([[maybe_unused]] compiler::PandaGen *pg) const = 0;
294     virtual checker::Type *Check([[maybe_unused]] checker::Checker *checker) const = 0;
295     virtual void UpdateSelf([[maybe_unused]] const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder) = 0;
296 
297 protected:
298     void SetType(AstNodeType type)
299     {
300         type_ = type;
301     }
302 
303     const AstNode *parent_ {};
304     lexer::SourceRange range_ {};
305     AstNodeType type_;
306     binder::Variable *variable_ {nullptr};
307     const AstNode *original_ {nullptr};
308 };
309 
310 }  // namespace panda::es2panda::ir
311 #endif
312