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 "blockStatement.h" 17 18#include "compiler/core/pandagen.h" 19#include "compiler/core/regScope.h" 20#include "compiler/core/ETSGen.h" 21#include "checker/TSchecker.h" 22#include "checker/ETSchecker.h" 23#include "ir/astDump.h" 24#include "ir/srcDump.h" 25 26namespace ark::es2panda::ir { 27void BlockStatement::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName) 28{ 29 for (auto *&it : statements_) { 30 if (auto *transformedNode = cb(it); it != transformedNode) { 31 it->SetTransformedNode(transformationName, transformedNode); 32 it = transformedNode->AsStatement(); 33 } 34 } 35} 36 37AstNode *BlockStatement::Clone(ArenaAllocator *const allocator, AstNode *const parent) 38{ 39 ArenaVector<Statement *> statements(allocator->Adapter()); 40 41 for (auto *statement : this->statements_) { 42 statements.push_back(statement->Clone(allocator, parent)->AsStatement()); 43 } 44 45 auto retVal = util::NodeAllocator::ForceSetParent<ir::BlockStatement>(allocator, allocator, std::move(statements)); 46 retVal->SetParent(parent); 47 48 return retVal; 49} 50 51void BlockStatement::Iterate(const NodeTraverser &cb) const 52{ 53 // This will survive pushing element to the back of statements_ in the process 54 // NOLINTNEXTLINE(modernize-loop-convert) 55 for (size_t ix = 0; ix < statements_.size(); ix++) { 56 cb(statements_[ix]); 57 } 58} 59 60void BlockStatement::Dump(ir::AstDumper *dumper) const 61{ 62 dumper->Add({{"type", IsProgram() ? "Program" : "BlockStatement"}, {"statements", statements_}}); 63} 64 65void BlockStatement::Dump(ir::SrcDumper *dumper) const 66{ 67 // NOTE(nsizov): trailing blocks 68 if (Parent() != nullptr && (Parent()->IsBlockStatement() || Parent()->IsCallExpression())) { 69 dumper->Add("{"); 70 if (!statements_.empty()) { 71 dumper->IncrIndent(); 72 dumper->Endl(); 73 } 74 } 75 for (auto statement : statements_) { 76 statement->Dump(dumper); 77 if (statement != statements_.back()) { 78 dumper->Endl(); 79 } 80 } 81 if (Parent() != nullptr && (Parent()->IsBlockStatement() || Parent()->IsCallExpression())) { 82 if (!statements_.empty()) { 83 dumper->DecrIndent(); 84 dumper->Endl(); 85 } 86 dumper->Add("}"); 87 } 88} 89 90void BlockStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const 91{ 92 pg->GetAstCompiler()->Compile(this); 93} 94 95void BlockStatement::Compile([[maybe_unused]] compiler::ETSGen *etsg) const 96{ 97 etsg->GetAstCompiler()->Compile(this); 98} 99 100checker::Type *BlockStatement::Check([[maybe_unused]] checker::TSChecker *checker) 101{ 102 return checker->GetAnalyzer()->Check(this); 103} 104 105checker::Type *BlockStatement::Check([[maybe_unused]] checker::ETSChecker *checker) 106{ 107 return checker->GetAnalyzer()->Check(this); 108} 109} // namespace ark::es2panda::ir 110