• 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_SCRIPT_FUNCTION_H
17 #define ES2PANDA_PARSER_INCLUDE_AST_SCRIPT_FUNCTION_H
18 
19 #include "ir/astNode.h"
20 #include "varbinder/scope.h"
21 #include "util/enumbitops.h"
22 #include "util/language.h"
23 #include "scriptFunctionSignature.h"
24 
25 namespace panda::es2panda::checker {
26 class Signature;
27 
28 }  // namespace panda::es2panda::checker
29 namespace panda::es2panda::compiler {
30 class ScopesInitPhase;
31 }  // namespace panda::es2panda::compiler
32 
33 namespace panda::es2panda::ir {
34 class TSTypeParameterDeclaration;
35 class TypeNode;
36 
37 class ScriptFunction : public AstNode {
38 public:
39     ScriptFunction() = delete;
40     ~ScriptFunction() override = default;
41 
42     NO_COPY_SEMANTIC(ScriptFunction);
43     NO_MOVE_SEMANTIC(ScriptFunction);
44 
ScriptFunction(FunctionSignature && signature,AstNode * body,ir::ScriptFunctionFlags funcFlags,bool declare,Language lang)45     explicit ScriptFunction(FunctionSignature &&signature, AstNode *body, ir::ScriptFunctionFlags funcFlags,
46                             bool declare, Language lang)
47         : AstNode(AstNodeType::SCRIPT_FUNCTION),
48           irSignature_(std::move(signature)),
49           body_(body),
50           funcFlags_(funcFlags),
51           declare_(declare),
52           lang_(lang)
53     {
54     }
55 
ScriptFunction(FunctionSignature && signature,AstNode * body,ir::ScriptFunctionFlags funcFlags,ir::ModifierFlags flags,bool declare,Language lang)56     explicit ScriptFunction(FunctionSignature &&signature, AstNode *body, ir::ScriptFunctionFlags funcFlags,
57                             ir::ModifierFlags flags, bool declare, Language lang)
58         : AstNode(AstNodeType::SCRIPT_FUNCTION, flags),
59           irSignature_(std::move(signature)),
60           body_(body),
61           funcFlags_(funcFlags),
62           declare_(declare),
63           lang_(lang)
64     {
65     }
66 
Id()67     [[nodiscard]] const Identifier *Id() const noexcept
68     {
69         return id_;
70     }
71 
Id()72     [[nodiscard]] Identifier *Id() noexcept
73     {
74         return id_;
75     }
76 
Signature()77     [[nodiscard]] const checker::Signature *Signature() const noexcept
78     {
79         return signature_;
80     }
81 
Signature()82     [[nodiscard]] checker::Signature *Signature() noexcept
83     {
84         return signature_;
85     }
86 
Params()87     [[nodiscard]] const ArenaVector<Expression *> &Params() const noexcept
88     {
89         return irSignature_.Params();
90     }
91 
Params()92     [[nodiscard]] ArenaVector<Expression *> &Params() noexcept
93     {
94         return irSignature_.Params();
95     }
96 
TypeParams()97     [[nodiscard]] const TSTypeParameterDeclaration *TypeParams() const noexcept
98     {
99         return irSignature_.TypeParams();
100     }
101 
TypeParams()102     [[nodiscard]] TSTypeParameterDeclaration *TypeParams() noexcept
103     {
104         return irSignature_.TypeParams();
105     }
106 
Body()107     [[nodiscard]] const AstNode *Body() const noexcept
108     {
109         return body_;
110     }
111 
Body()112     [[nodiscard]] AstNode *Body() noexcept
113     {
114         return body_;
115     }
116 
SetBody(AstNode * body)117     void SetBody(AstNode *body) noexcept
118     {
119         body_ = body;
120     }
121 
ReturnTypeAnnotation()122     [[nodiscard]] const TypeNode *ReturnTypeAnnotation() const noexcept
123     {
124         return irSignature_.ReturnType();
125     }
126 
ReturnTypeAnnotation()127     [[nodiscard]] TypeNode *ReturnTypeAnnotation() noexcept
128     {
129         return irSignature_.ReturnType();
130     }
131 
SetReturnTypeAnnotation(TypeNode * node)132     void SetReturnTypeAnnotation(TypeNode *node) noexcept
133     {
134         irSignature_.SetReturnType(node);
135     }
136 
IsEntryPoint()137     [[nodiscard]] bool IsEntryPoint() const noexcept
138     {
139         return (funcFlags_ & ir::ScriptFunctionFlags::ENTRY_POINT) != 0;
140     }
141 
IsGenerator()142     [[nodiscard]] bool IsGenerator() const noexcept
143     {
144         return (funcFlags_ & ir::ScriptFunctionFlags::GENERATOR) != 0;
145     }
146 
IsAsyncFunc()147     [[nodiscard]] bool IsAsyncFunc() const noexcept
148     {
149         return (funcFlags_ & ir::ScriptFunctionFlags::ASYNC) != 0;
150     }
151 
IsArrow()152     [[nodiscard]] bool IsArrow() const noexcept
153     {
154         return (funcFlags_ & ir::ScriptFunctionFlags::ARROW) != 0;
155     }
156 
IsOverload()157     [[nodiscard]] bool IsOverload() const noexcept
158     {
159         return (funcFlags_ & ir::ScriptFunctionFlags::OVERLOAD) != 0;
160     }
161 
IsConstructor()162     [[nodiscard]] bool IsConstructor() const noexcept
163     {
164         return (funcFlags_ & ir::ScriptFunctionFlags::CONSTRUCTOR) != 0;
165     }
166 
IsGetter()167     [[nodiscard]] bool IsGetter() const noexcept
168     {
169         return (funcFlags_ & ir::ScriptFunctionFlags::GETTER) != 0;
170     }
171 
IsSetter()172     [[nodiscard]] bool IsSetter() const noexcept
173     {
174         return (funcFlags_ & ir::ScriptFunctionFlags::SETTER) != 0;
175     }
176 
IsMethod()177     [[nodiscard]] bool IsMethod() const noexcept
178     {
179         return (funcFlags_ & ir::ScriptFunctionFlags::METHOD) != 0;
180     }
181 
IsProxy()182     [[nodiscard]] bool IsProxy() const noexcept
183     {
184         return (funcFlags_ & ir::ScriptFunctionFlags::PROXY) != 0;
185     }
186 
IsStaticBlock()187     [[nodiscard]] bool IsStaticBlock() const noexcept
188     {
189         return (funcFlags_ & ir::ScriptFunctionFlags::STATIC_BLOCK) != 0;
190     }
191 
IsEnum()192     [[nodiscard]] bool IsEnum() const noexcept
193     {
194         return (funcFlags_ & ir::ScriptFunctionFlags::ENUM) != 0;
195     }
196 
IsHidden()197     [[nodiscard]] bool IsHidden() const noexcept
198     {
199         return (funcFlags_ & ir::ScriptFunctionFlags::HIDDEN) != 0;
200     }
201 
IsExternal()202     [[nodiscard]] bool IsExternal() const noexcept
203     {
204         return (funcFlags_ & ir::ScriptFunctionFlags::EXTERNAL) != 0;
205     }
206 
IsImplicitSuperCallNeeded()207     [[nodiscard]] bool IsImplicitSuperCallNeeded() const noexcept
208     {
209         return (funcFlags_ & ir::ScriptFunctionFlags::IMPLICIT_SUPER_CALL_NEEDED) != 0;
210     }
211 
HasBody()212     [[nodiscard]] bool HasBody() const noexcept
213     {
214         return body_ != nullptr;
215     }
216 
IsThrowing()217     [[nodiscard]] bool IsThrowing() const noexcept
218     {
219         return (funcFlags_ & ir::ScriptFunctionFlags::THROWS) != 0;
220     }
221 
IsRethrowing()222     [[nodiscard]] bool IsRethrowing() const noexcept
223     {
224         return (funcFlags_ & ir::ScriptFunctionFlags::RETHROWS) != 0;
225     }
226 
IsDefaultParamProxy()227     [[nodiscard]] bool IsDefaultParamProxy() const noexcept
228     {
229         return (funcFlags_ & ir::ScriptFunctionFlags::DEFAULT_PARAM_PROXY) != 0;
230     }
231 
SetDefaultParamProxy()232     void SetDefaultParamProxy() noexcept
233     {
234         AddFlag(ir::ScriptFunctionFlags::DEFAULT_PARAM_PROXY);
235     }
236 
IsDynamic()237     [[nodiscard]] bool IsDynamic() const noexcept
238     {
239         return lang_.IsDynamic();
240     }
241 
IsExtensionMethod()242     [[nodiscard]] bool IsExtensionMethod() const noexcept
243     {
244         return (funcFlags_ & ir::ScriptFunctionFlags::INSTANCE_EXTENSION_METHOD) != 0;
245     }
246 
Declare()247     [[nodiscard]] bool Declare() const noexcept
248     {
249         return declare_;
250     }
251 
Flags()252     [[nodiscard]] ir::ScriptFunctionFlags Flags() const noexcept
253     {
254         return funcFlags_;
255     }
256 
SetIdent(Identifier * id)257     void SetIdent(Identifier *id) noexcept
258     {
259         id_ = id;
260     }
261 
SetSignature(checker::Signature * signature)262     void SetSignature(checker::Signature *signature) noexcept
263     {
264         signature_ = signature;
265     }
266 
AddFlag(ir::ScriptFunctionFlags flags)267     void AddFlag(ir::ScriptFunctionFlags flags) noexcept
268     {
269         funcFlags_ |= flags;
270     }
271 
AddModifier(ir::ModifierFlags flags)272     void AddModifier(ir::ModifierFlags flags) noexcept
273     {
274         flags_ |= flags;
275     }
276 
277     [[nodiscard]] std::size_t FormalParamsLength() const noexcept;
278 
IsScopeBearer()279     bool IsScopeBearer() const override
280     {
281         return true;
282     }
283 
Scope()284     varbinder::FunctionScope *Scope() const override
285     {
286         return scope_;
287     }
288 
SetScope(varbinder::FunctionScope * scope)289     void SetScope(varbinder::FunctionScope *scope)
290     {
291         scope_ = scope;
292     }
293 
Language()294     [[nodiscard]] es2panda::Language Language() const
295     {
296         return lang_;
297     }
298 
299     void TransformChildren(const NodeTransformer &cb) override;
300     void Iterate(const NodeTraverser &cb) const override;
301 
302     void Dump(ir::AstDumper *dumper) const override;
303     void Dump(ir::SrcDumper *dumper) const override;
304     void Compile(compiler::PandaGen *pg) const override;
305     void Compile(compiler::ETSGen *etsg) const override;
306     checker::Type *Check(checker::TSChecker *checker) override;
307     checker::Type *Check(checker::ETSChecker *checker) override;
308 
Accept(ASTVisitorT * v)309     void Accept(ASTVisitorT *v) override
310     {
311         v->Accept(this);
312     }
313 
314 private:
315     friend panda::es2panda::compiler::ScopesInitPhase;
316 
317 private:
318     Identifier *id_ {};
319     FunctionSignature irSignature_;
320     AstNode *body_;
321     varbinder::FunctionScope *scope_ {nullptr};
322     ir::ScriptFunctionFlags funcFlags_;
323     checker::Signature *signature_ {};
324     bool declare_;
325     es2panda::Language lang_;
326 };
327 }  // namespace panda::es2panda::ir
328 
329 #endif
330