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 "variableDeclarator.h" 17 18#include "compiler/base/lreference.h" 19#include "compiler/core/pandagen.h" 20#include "compiler/core/ETSGen.h" 21#include "ir/astDump.h" 22#include "ir/srcDump.h" 23#include "ir/astNode.h" 24#include "ir/typeNode.h" 25#include "ir/expression.h" 26#include "ir/statements/variableDeclaration.h" 27 28#include "checker/TSchecker.h" 29#include "checker/ETSchecker.h" 30#include "checker/ts/destructuringContext.h" 31 32namespace ark::es2panda::ir { 33void VariableDeclarator::TransformChildren(const NodeTransformer &cb, std::string_view transformationName) 34{ 35 if (auto *transformedNode = cb(id_); id_ != transformedNode) { 36 id_->SetTransformedNode(transformationName, transformedNode); 37 id_ = transformedNode->AsExpression(); 38 } 39 40 if (init_ != nullptr) { 41 if (auto *transformedNode = cb(init_); init_ != transformedNode) { 42 init_->SetTransformedNode(transformationName, transformedNode); 43 init_ = transformedNode->AsExpression(); 44 } 45 } 46} 47 48void VariableDeclarator::Iterate(const NodeTraverser &cb) const 49{ 50 cb(id_); 51 52 if (init_ != nullptr) { 53 cb(init_); 54 } 55} 56 57void VariableDeclarator::Dump(ir::AstDumper *dumper) const 58{ 59 dumper->Add({{"type", "VariableDeclarator"}, {"id", id_}, {"init", AstDumper::Nullish(init_)}}); 60} 61 62void VariableDeclarator::Dump(ir::SrcDumper *dumper) const 63{ 64 if (id_ != nullptr) { 65 id_->Dump(dumper); 66 if (id_->IsOptionalDeclaration()) { 67 dumper->Add("?"); 68 } 69 if (id_->IsAnnotatedExpression()) { 70 auto *type = id_->AsAnnotatedExpression()->TypeAnnotation(); 71 if (type != nullptr) { 72 dumper->Add(": "); 73 type->Dump(dumper); 74 } 75 } 76 } 77 if (init_ != nullptr) { 78 dumper->Add(" = "); 79 init_->Dump(dumper); 80 } 81} 82 83VariableDeclarator *VariableDeclarator::Clone(ArenaAllocator *const allocator, AstNode *const parent) 84{ 85 auto *const id = id_ != nullptr ? id_->Clone(allocator, nullptr)->AsExpression() : nullptr; 86 auto *const init = init_ != nullptr ? init_->Clone(allocator, nullptr)->AsExpression() : nullptr; 87 88 if (auto *const clone = allocator->New<VariableDeclarator>(flag_, id, init); clone != nullptr) { 89 if (id != nullptr) { 90 id->SetParent(clone); 91 } 92 if (init != nullptr) { 93 init->SetParent(clone); 94 } 95 if (parent != nullptr) { 96 clone->SetParent(parent); 97 } 98 return clone; 99 } 100 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); 101} 102 103void VariableDeclarator::Compile([[maybe_unused]] compiler::PandaGen *pg) const 104{ 105 pg->GetAstCompiler()->Compile(this); 106} 107 108void VariableDeclarator::Compile(compiler::ETSGen *etsg) const 109{ 110 etsg->GetAstCompiler()->Compile(this); 111} 112 113checker::Type *VariableDeclarator::Check([[maybe_unused]] checker::TSChecker *checker) 114{ 115 return checker->GetAnalyzer()->Check(this); 116} 117 118checker::Type *VariableDeclarator::Check(checker::ETSChecker *checker) 119{ 120 return checker->GetAnalyzer()->Check(this); 121} 122} // namespace ark::es2panda::ir 123