• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021 - 2023 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_PARSER_INCLUDE_AST_CLASS_DEFINITION_H
17 #define ES2PANDA_PARSER_INCLUDE_AST_CLASS_DEFINITION_H
18 
19 #include "varbinder/scope.h"
20 #include "varbinder/variable.h"
21 #include "ir/astNode.h"
22 #include "util/bitset.h"
23 #include "util/language.h"
24 
25 namespace panda::es2panda::ir {
26 class ClassElement;
27 class Identifier;
28 class MethodDefinition;
29 class TSTypeParameterDeclaration;
30 class TSTypeParameterInstantiation;
31 class TSClassImplements;
32 class TSIndexSignature;
33 
34 enum class ClassDefinitionModifiers : uint32_t {
35     NONE = 0,
36     DECLARATION = 1U << 0U,
37     ID_REQUIRED = 1U << 1U,
38     GLOBAL = 1U << 2U,
39     HAS_SUPER = 1U << 3U,
40     SET_CTOR_ID = 1U << 4U,
41     EXTERN = 1U << 5U,
42     ANONYMOUS = 1U << 6U,
43     GLOBAL_INITIALIZED = 1U << 7U,
44     CLASS_DECL = 1U << 8U,
45     INNER = 1U << 9U,
46     FROM_EXTERNAL = 1U << 10U,
47     DECLARATION_ID_REQUIRED = DECLARATION | ID_REQUIRED
48 };
49 
DEFINE_BITOPS(ClassDefinitionModifiers)50 DEFINE_BITOPS(ClassDefinitionModifiers)
51 
52 class ClassDefinition : public TypedAstNode {
53 public:
54     ClassDefinition() = delete;
55     ~ClassDefinition() override = default;
56 
57     NO_COPY_SEMANTIC(ClassDefinition);
58     NO_MOVE_SEMANTIC(ClassDefinition);
59 
60     explicit ClassDefinition(const util::StringView &privateId, Identifier *ident,
61                              TSTypeParameterDeclaration *typeParams, TSTypeParameterInstantiation *superTypeParams,
62                              ArenaVector<TSClassImplements *> &&implements, MethodDefinition *ctor,
63                              Expression *superClass, ArenaVector<AstNode *> &&body, ClassDefinitionModifiers modifiers,
64                              ModifierFlags flags, Language lang)
65         : TypedAstNode(AstNodeType::CLASS_DEFINITION, flags),
66           privateId_(privateId),
67           ident_(ident),
68           typeParams_(typeParams),
69           superTypeParams_(superTypeParams),
70           implements_(std::move(implements)),
71           ctor_(ctor),
72           superClass_(superClass),
73           body_(std::move(body)),
74           modifiers_(modifiers),
75           lang_(lang)
76     {
77     }
78 
79     explicit ClassDefinition(ArenaAllocator *allocator, Identifier *ident, ArenaVector<AstNode *> &&body,
80                              ClassDefinitionModifiers modifiers, Language lang)
81         : TypedAstNode(AstNodeType::CLASS_DEFINITION),
82           ident_(ident),
83           implements_(allocator->Adapter()),
84           body_(std::move(body)),
85           modifiers_(modifiers),
86           lang_(lang)
87     {
88     }
89 
90     explicit ClassDefinition(ArenaAllocator *allocator, Identifier *ident, ClassDefinitionModifiers modifiers,
91                              ModifierFlags flags, Language lang)
92         : TypedAstNode(AstNodeType::CLASS_DEFINITION, flags),
93           ident_(ident),
94           implements_(allocator->Adapter()),
95           body_(allocator->Adapter()),
96           modifiers_(modifiers),
97           lang_(lang)
98     {
99     }
100 
101     bool IsScopeBearer() const override
102     {
103         return true;
104     }
105 
106     varbinder::LocalScope *Scope() const override
107     {
108         return scope_;
109     }
110 
111     void SetScope(varbinder::LocalScope *scope)
112     {
113         scope_ = scope;
114     }
115 
116     [[nodiscard]] const Identifier *Ident() const noexcept
117     {
118         return ident_;
119     }
120 
121     [[nodiscard]] Identifier *Ident() noexcept
122     {
123         return ident_;
124     }
125 
126     void SetIdent(ir::Identifier *ident) noexcept
127     {
128         ident_ = ident;
129     }
130 
131     [[nodiscard]] const util::StringView &PrivateId() const noexcept
132     {
133         return privateId_;
134     }
135 
136     [[nodiscard]] const util::StringView &InternalName() const noexcept
137     {
138         return privateId_;
139     }
140 
141     void SetInternalName(util::StringView internalName) noexcept
142     {
143         privateId_ = internalName;
144     }
145 
146     [[nodiscard]] Expression *Super() noexcept
147     {
148         return superClass_;
149     }
150 
151     [[nodiscard]] const Expression *Super() const noexcept
152     {
153         return superClass_;
154     }
155 
156     void SetSuper(Expression *superClass)
157     {
158         superClass_ = superClass;
159     }
160 
161     [[nodiscard]] bool IsGlobal() const noexcept
162     {
163         return (modifiers_ & ClassDefinitionModifiers::GLOBAL) != 0;
164     }
165 
166     [[nodiscard]] bool IsExtern() const noexcept
167     {
168         return (modifiers_ & ClassDefinitionModifiers::EXTERN) != 0;
169     }
170 
171     [[nodiscard]] bool IsFromExternal() const noexcept
172     {
173         return (modifiers_ & ClassDefinitionModifiers::FROM_EXTERNAL) != 0;
174     }
175     [[nodiscard]] bool IsInner() const noexcept
176     {
177         return (modifiers_ & ClassDefinitionModifiers::INNER) != 0;
178     }
179 
180     [[nodiscard]] bool IsGlobalInitialized() const noexcept
181     {
182         return (modifiers_ & ClassDefinitionModifiers::GLOBAL_INITIALIZED) != 0;
183     }
184 
185     [[nodiscard]] es2panda::Language Language() const noexcept
186     {
187         return lang_;
188     }
189 
190     void SetGlobalInitialized() noexcept
191     {
192         modifiers_ |= ClassDefinitionModifiers::GLOBAL_INITIALIZED;
193     }
194 
195     void SetInnerModifier() noexcept
196     {
197         modifiers_ |= ClassDefinitionModifiers::INNER;
198     }
199 
200     [[nodiscard]] ClassDefinitionModifiers Modifiers() const noexcept
201     {
202         return modifiers_;
203     }
204 
205     void AddProperties(ArenaVector<AstNode *> &&body)
206     {
207         for (auto *prop : body) {
208             prop->SetParent(this);
209         }
210 
211         body_.insert(body_.end(), body.begin(), body.end());
212     }
213 
214     [[nodiscard]] ArenaVector<AstNode *> &Body() noexcept
215     {
216         return body_;
217     }
218 
219     [[nodiscard]] const ArenaVector<AstNode *> &Body() const noexcept
220     {
221         return body_;
222     }
223 
224     [[nodiscard]] MethodDefinition *Ctor() noexcept
225     {
226         return ctor_;
227     }
228 
229     void SetCtor(MethodDefinition *ctor)
230     {
231         ctor_ = ctor;
232     }
233 
234     [[nodiscard]] ArenaVector<ir::TSClassImplements *> &Implements() noexcept
235     {
236         return implements_;
237     }
238 
239     [[nodiscard]] const ArenaVector<ir::TSClassImplements *> &Implements() const noexcept
240     {
241         return implements_;
242     }
243 
244     [[nodiscard]] const ir::TSTypeParameterDeclaration *TypeParams() const noexcept
245     {
246         return typeParams_;
247     }
248 
249     [[nodiscard]] ir::TSTypeParameterDeclaration *TypeParams() noexcept
250     {
251         return typeParams_;
252     }
253 
254     void SetTypeParams(ir::TSTypeParameterDeclaration *typeParams)
255     {
256         typeParams_ = typeParams;
257     }
258 
259     const TSTypeParameterInstantiation *SuperTypeParams() const
260     {
261         return superTypeParams_;
262     }
263 
264     TSTypeParameterInstantiation *SuperTypeParams()
265     {
266         return superTypeParams_;
267     }
268 
269     const FunctionExpression *Ctor() const;
270     bool HasPrivateMethod() const;
271     bool HasComputedInstanceField() const;
272     bool HasMatchingPrivateKey(const util::StringView &name) const;
273 
274     void TransformChildren(const NodeTransformer &cb) override;
275     void Iterate(const NodeTraverser &cb) const override;
276 
277     void Dump(ir::AstDumper *dumper) const override;
278     void Dump(ir::SrcDumper *dumper) const override;
279     void Compile(compiler::PandaGen *pg) const override;
280     void Compile(compiler::ETSGen *etsg) const override;
281     checker::Type *Check(checker::TSChecker *checker) override;
282     checker::Type *Check(checker::ETSChecker *checker) override;
283 
284     void Accept(ASTVisitorT *v) override
285     {
286         v->Accept(this);
287     }
288 
289 private:
290     void CompileStaticFieldInitializers(compiler::PandaGen *pg, compiler::VReg classReg,
291                                         const std::vector<compiler::VReg> &staticComputedFieldKeys) const;
292 
293     varbinder::LocalScope *scope_ {nullptr};
294     util::StringView privateId_ {};
295     Identifier *ident_ {};
296     TSTypeParameterDeclaration *typeParams_ {};
297     TSTypeParameterInstantiation *superTypeParams_ {};
298     ArenaVector<TSClassImplements *> implements_;
299     MethodDefinition *ctor_ {};
300     Expression *superClass_ {};
301     ArenaVector<AstNode *> body_;
302     ClassDefinitionModifiers modifiers_;
303     es2panda::Language lang_;
304 };
305 }  // namespace panda::es2panda::ir
306 
307 #endif
308