• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (auto *transformedNode = cb(func_); func_ != transformedNode) {
36         func_->SetTransformedNode(transformationName, transformedNode);
37         func_ = transformedNode->AsScriptFunction();
38     }
39 }
40 
Iterate(const NodeTraverser & cb) const41 void FunctionDeclaration::Iterate(const NodeTraverser &cb) const
42 {
43     for (auto *it : decorators_) {
44         cb(it);
45     }
46 
47     cb(func_);
48 }
49 
Dump(ir::AstDumper * dumper) const50 void FunctionDeclaration::Dump(ir::AstDumper *dumper) const
51 {
52     dumper->Add({{"type", func_->IsOverload() ? "TSDeclareFunction" : "FunctionDeclaration"},
53                  {"decorators", AstDumper::Optional(decorators_)},
54                  {"function", func_}});
55 }
56 
Dump(ir::SrcDumper * dumper) const57 void FunctionDeclaration::Dump(ir::SrcDumper *dumper) const
58 {
59     dumper->Add("function ");
60     func_->Id()->Dump(dumper);
61     func_->Dump(dumper);
62 }
63 
Compile(compiler::PandaGen * pg) const64 void FunctionDeclaration::Compile(compiler::PandaGen *pg) const
65 {
66     pg->GetAstCompiler()->Compile(this);
67 }
68 
Compile(compiler::ETSGen * etsg) const69 void FunctionDeclaration::Compile(compiler::ETSGen *etsg) const
70 {
71     etsg->GetAstCompiler()->Compile(this);
72 }
73 
Check(checker::TSChecker * checker)74 checker::Type *FunctionDeclaration::Check(checker::TSChecker *checker)
75 {
76     return checker->GetAnalyzer()->Check(this);
77 }
78 
Check(checker::ETSChecker * checker)79 checker::Type *FunctionDeclaration::Check(checker::ETSChecker *checker)
80 {
81     return checker->GetAnalyzer()->Check(this);
82 }
83 }  // namespace ark::es2panda::ir
84