1 /**
2 * Copyright (c) 2021-2024 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 : 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 : 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 : decorators_) {
51 cb(it);
52 }
53
54 for (auto *it : 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 (func_->IsDeclare()) {
78 dumper->Add("declare ");
79 }
80 if (func_->IsAsyncFunc()) {
81 dumper->Add("async ");
82 }
83 dumper->Add("function ");
84
85 if (func_->IsExtensionMethod()) {
86 for (const auto *param : func_->Params()) {
87 if (param->IsETSParameterExpression() && param->AsETSParameterExpression()->Ident() != nullptr &&
88 param->AsETSParameterExpression()->Ident()->Name() == varbinder::VarBinder::MANDATORY_PARAM_THIS &&
89 param->AsETSParameterExpression()->Ident()->TypeAnnotation() != nullptr &&
90 param->AsETSParameterExpression()->Ident()->TypeAnnotation()->IsETSTypeReference() &&
91 param->AsETSParameterExpression()->Ident()->TypeAnnotation()->AsETSTypeReference()->Part() != nullptr &&
92 param->AsETSParameterExpression()->Ident()->TypeAnnotation()->AsETSTypeReference()->Part()->Name() !=
93 // CC-OFFNXT(G.FMT.02-CPP) project code style
94 nullptr &&
95 param->AsETSParameterExpression()
96 ->Ident()
97 ->TypeAnnotation()
98 ->AsETSTypeReference()
99 ->Part()
100 ->Name()
101 ->IsIdentifier()) {
102 dumper->Add(std::string(param->AsETSParameterExpression()
103 ->Ident()
104 ->TypeAnnotation()
105 ->AsETSTypeReference()
106 ->Part()
107 ->Name()
108 ->AsIdentifier()
109 ->Name()));
110 dumper->Add(".");
111 }
112 }
113 }
114 func_->Id()->Dump(dumper);
115 func_->Dump(dumper);
116 }
117
Compile(compiler::PandaGen * pg) const118 void FunctionDeclaration::Compile(compiler::PandaGen *pg) const
119 {
120 pg->GetAstCompiler()->Compile(this);
121 }
122
Compile(compiler::ETSGen * etsg) const123 void FunctionDeclaration::Compile(compiler::ETSGen *etsg) const
124 {
125 etsg->GetAstCompiler()->Compile(this);
126 }
127
Check(checker::TSChecker * checker)128 checker::Type *FunctionDeclaration::Check(checker::TSChecker *checker)
129 {
130 return checker->GetAnalyzer()->Check(this);
131 }
132
Check(checker::ETSChecker * checker)133 checker::Type *FunctionDeclaration::Check(checker::ETSChecker *checker)
134 {
135 return checker->GetAnalyzer()->Check(this);
136 }
137 } // namespace ark::es2panda::ir
138