1/** 2 * Copyright (c) 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 "tryStatement.h" 17 18#include "checker/TSchecker.h" 19#include "compiler/core/pandagen.h" 20#include "compiler/core/ETSGen.h" 21#include "compiler/core/dynamicContext.h" 22#include "compiler/base/catchTable.h" 23#include "ir/astDump.h" 24#include "ir/srcDump.h" 25#include "ir/base/catchClause.h" 26#include "ir/statements/blockStatement.h" 27 28namespace ark::es2panda::ir { 29void TryStatement::TransformChildren(const NodeTransformer &cb, std::string_view transformationName) 30{ 31 if (auto *transformedNode = cb(block_); block_ != transformedNode) { 32 block_->SetTransformedNode(transformationName, transformedNode); 33 block_ = transformedNode->AsBlockStatement(); 34 } 35 36 for (auto *&it : catchClauses_) { 37 if (auto *transformedNode = cb(it); it != transformedNode) { 38 it->SetTransformedNode(transformationName, transformedNode); 39 it = transformedNode->AsCatchClause(); 40 } 41 } 42 43 if (finalizer_ != nullptr) { 44 if (auto *transformedNode = cb(finalizer_); finalizer_ != transformedNode) { 45 finalizer_->SetTransformedNode(transformationName, transformedNode); 46 finalizer_ = transformedNode->AsBlockStatement(); 47 } 48 } 49} 50 51void TryStatement::Iterate(const NodeTraverser &cb) const 52{ 53 cb(block_); 54 55 for (auto *it : catchClauses_) { 56 cb(it); 57 } 58 59 if (finalizer_ != nullptr) { 60 cb(finalizer_); 61 } 62} 63 64void TryStatement::Dump(ir::AstDumper *dumper) const 65{ 66 dumper->Add({{"type", "TryStatement"}, 67 {"block", block_}, 68 {"handler", catchClauses_}, 69 {"finalizer", AstDumper::Nullish(finalizer_)}}); 70} 71 72void TryStatement::Dump(ir::SrcDumper *dumper) const 73{ 74 ASSERT(block_ != nullptr); 75 dumper->Add("try {"); 76 dumper->IncrIndent(); 77 dumper->Endl(); 78 block_->Dump(dumper); 79 dumper->DecrIndent(); 80 dumper->Endl(); 81 dumper->Add("}"); 82 for (auto clause : catchClauses_) { 83 dumper->Add(" catch "); 84 clause->Dump(dumper); 85 } 86 if (finalizer_ != nullptr) { 87 dumper->Add(" finally {"); 88 dumper->IncrIndent(); 89 dumper->Endl(); 90 finalizer_->Dump(dumper); 91 dumper->DecrIndent(); 92 dumper->Endl(); 93 dumper->Add("}"); 94 } 95} 96 97bool TryStatement::HasDefaultCatchClause() const 98{ 99 return (!catchClauses_.empty() && catchClauses_.back()->IsDefaultCatchClause()); 100} 101 102void TryStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const 103{ 104 pg->GetAstCompiler()->Compile(this); 105} 106 107void TryStatement::Compile(compiler::ETSGen *etsg) const 108{ 109 etsg->GetAstCompiler()->Compile(this); 110} 111 112checker::Type *TryStatement::Check([[maybe_unused]] checker::TSChecker *checker) 113{ 114 return checker->GetAnalyzer()->Check(this); 115} 116 117checker::Type *TryStatement::Check([[maybe_unused]] checker::ETSChecker *checker) 118{ 119 return checker->GetAnalyzer()->Check(this); 120} 121 122} // namespace ark::es2panda::ir 123