• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 #include "functionDeclaration.h"
17 
18 #include "varbinder/variable.h"
19 #include "compiler/core/ETSGen.h"
20 #include "checker/TSchecker.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23 #include "compiler/core/pandagen.h"
24 
25 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)26 void FunctionDeclaration::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
27 {
28     for (auto *&it : VectorIterationGuard(decorators_)) {
29         if (auto *transformedNode = cb(it); it != transformedNode) {
30             it->SetTransformedNode(transformationName, transformedNode);
31             it = transformedNode->AsDecorator();
32         }
33     }
34 
35     for (auto *&it : VectorIterationGuard(Annotations())) {
36         if (auto *transformedNode = cb(it); it != transformedNode) {
37             it->SetTransformedNode(transformationName, transformedNode);
38             it = transformedNode->AsAnnotationUsage();
39         }
40     }
41 
42     if (auto *transformedNode = cb(func_); func_ != transformedNode) {
43         func_->SetTransformedNode(transformationName, transformedNode);
44         func_ = transformedNode->AsScriptFunction();
45     }
46 }
47 
Iterate(const NodeTraverser & cb) const48 void FunctionDeclaration::Iterate(const NodeTraverser &cb) const
49 {
50     for (auto *it : VectorIterationGuard(decorators_)) {
51         cb(it);
52     }
53 
54     for (auto *it : VectorIterationGuard(Annotations())) {
55         cb(it);
56     }
57 
58     cb(func_);
59 }
60 
Dump(ir::AstDumper * dumper) const61 void FunctionDeclaration::Dump(ir::AstDumper *dumper) const
62 {
63     dumper->Add({{"type", func_->IsOverload() ? "TSDeclareFunction" : "FunctionDeclaration"},
64                  {"decorators", AstDumper::Optional(decorators_)},
65                  {"annotations", AstDumper::Optional(Annotations())},
66                  {"function", func_}});
67 }
68 
Dump(ir::SrcDumper * dumper) const69 void FunctionDeclaration::Dump(ir::SrcDumper *dumper) const
70 {
71     for (auto *anno : Annotations()) {
72         anno->Dump(dumper);
73     }
74     if (func_->IsNative()) {
75         dumper->Add("native ");
76     }
77     if (IsExported()) {
78         dumper->Add("export ");
79     } else if (IsDefaultExported()) {
80         dumper->Add("export default ");
81     }
82     if (func_->IsDeclare() && !(parent_ != nullptr && parent_->IsDeclare())) {
83         dumper->Add("declare ");
84     }
85     if (func_->IsAsyncFunc()) {
86         dumper->Add("async ");
87     }
88     dumper->Add("function ");
89 
90     func_->Id()->Dump(dumper);
91     func_->Dump(dumper);
92 }
93 
Compile(compiler::PandaGen * pg) const94 void FunctionDeclaration::Compile(compiler::PandaGen *pg) const
95 {
96     pg->GetAstCompiler()->Compile(this);
97 }
98 
Compile(compiler::ETSGen * etsg) const99 void FunctionDeclaration::Compile(compiler::ETSGen *etsg) const
100 {
101     etsg->GetAstCompiler()->Compile(this);
102 }
103 
Check(checker::TSChecker * checker)104 checker::Type *FunctionDeclaration::Check(checker::TSChecker *checker)
105 {
106     return checker->GetAnalyzer()->Check(this);
107 }
108 
Check(checker::ETSChecker * checker)109 checker::VerifiedType FunctionDeclaration::Check(checker::ETSChecker *checker)
110 {
111     return {this, checker->GetAnalyzer()->Check(this)};
112 }
113 
Construct(ArenaAllocator * allocator)114 FunctionDeclaration *FunctionDeclaration::Construct(ArenaAllocator *allocator)
115 {
116     return allocator->New<FunctionDeclaration>(allocator, nullptr);
117 }
118 
CopyTo(AstNode * other) const119 void FunctionDeclaration::CopyTo(AstNode *other) const
120 {
121     auto otherImpl = other->AsFunctionDeclaration();
122 
123     otherImpl->decorators_ = decorators_;
124     otherImpl->func_ = func_;
125     otherImpl->isAnonymous_ = isAnonymous_;
126 
127     JsDocAllowed<AnnotationAllowed<Statement>>::CopyTo(other);
128 }
129 
130 }  // namespace ark::es2panda::ir
131