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 25namespace ark::es2panda::ir { 26void 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 41void FunctionDeclaration::Iterate(const NodeTraverser &cb) const 42{ 43 for (auto *it : decorators_) { 44 cb(it); 45 } 46 47 cb(func_); 48} 49 50void 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 57void FunctionDeclaration::Dump(ir::SrcDumper *dumper) const 58{ 59 if (func_->IsNative()) { 60 dumper->Add("native "); 61 } 62 if (func_->Declare()) { 63 dumper->Add("declare "); 64 } 65 if (func_->IsAsyncFunc()) { 66 dumper->Add("async "); 67 } 68 dumper->Add("function "); 69 70 if (func_->IsExtensionMethod()) { 71 for (const auto *param : func_->Params()) { 72 if (param->IsETSParameterExpression() && param->AsETSParameterExpression()->Ident() != nullptr && 73 param->AsETSParameterExpression()->Ident()->Name() == varbinder::VarBinder::MANDATORY_PARAM_THIS && 74 param->AsETSParameterExpression()->Ident()->TypeAnnotation() != nullptr && 75 param->AsETSParameterExpression()->Ident()->TypeAnnotation()->IsETSTypeReference() && 76 param->AsETSParameterExpression()->Ident()->TypeAnnotation()->AsETSTypeReference()->Part() != nullptr && 77 param->AsETSParameterExpression()->Ident()->TypeAnnotation()->AsETSTypeReference()->Part()->Name() != 78 nullptr && 79 param->AsETSParameterExpression() 80 ->Ident() 81 ->TypeAnnotation() 82 ->AsETSTypeReference() 83 ->Part() 84 ->Name() 85 ->IsIdentifier()) { 86 dumper->Add(std::string(param->AsETSParameterExpression() 87 ->Ident() 88 ->TypeAnnotation() 89 ->AsETSTypeReference() 90 ->Part() 91 ->Name() 92 ->AsIdentifier() 93 ->Name())); 94 dumper->Add("."); 95 } 96 } 97 } 98 func_->Id()->Dump(dumper); 99 func_->Dump(dumper); 100} 101 102void FunctionDeclaration::Compile(compiler::PandaGen *pg) const 103{ 104 pg->GetAstCompiler()->Compile(this); 105} 106 107void FunctionDeclaration::Compile(compiler::ETSGen *etsg) const 108{ 109 etsg->GetAstCompiler()->Compile(this); 110} 111 112checker::Type *FunctionDeclaration::Check(checker::TSChecker *checker) 113{ 114 return checker->GetAnalyzer()->Check(this); 115} 116 117checker::Type *FunctionDeclaration::Check(checker::ETSChecker *checker) 118{ 119 return checker->GetAnalyzer()->Check(this); 120} 121} // namespace ark::es2panda::ir 122