1 /** 2 * Copyright (c) 2021-2025 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 #ifndef ES2PANDA_IR_STATEMENT_TRY_STATEMENT_H 17 #define ES2PANDA_IR_STATEMENT_TRY_STATEMENT_H 18 19 #include "compiler/core/labelPair.h" 20 #include "ir/statement.h" 21 #include "util/helpers.h" 22 #include <memory> 23 24 namespace ark::es2panda::checker { 25 class TSAnalyzer; 26 class ETSAnalyzer; 27 } // namespace ark::es2panda::checker 28 29 namespace ark::es2panda::compiler { 30 class JSCompiler; 31 class ETSCompiler; 32 class PandaGen; 33 class TryLabelSet; 34 class TryContext; 35 } // namespace ark::es2panda::compiler 36 37 namespace ark::es2panda::ir { 38 class BlockStatement; 39 class CatchClause; 40 41 class TryStatement : public Statement { 42 public: TryStatement(BlockStatement * block,ArenaVector<CatchClause * > && catchClauses,BlockStatement * finalizer,ArenaVector<std::pair<compiler::LabelPair,const Statement * >> finalizerInsertions)43 explicit TryStatement(BlockStatement *block, ArenaVector<CatchClause *> &&catchClauses, BlockStatement *finalizer, 44 ArenaVector<std::pair<compiler::LabelPair, const Statement *>> finalizerInsertions) 45 : Statement(AstNodeType::TRY_STATEMENT), 46 block_(block), 47 catchClauses_(std::move(catchClauses)), 48 finalizer_(finalizer), 49 finalizerInsertions_(std::move(finalizerInsertions)) 50 { 51 } 52 53 // Special constructor need by ArktsPluginAPI TryStatement(BlockStatement * block,ArenaVector<CatchClause * > && catchClauses,BlockStatement * finalizer,ArenaVector<compiler::LabelPair> finalizerInsertionsLabelPair,ArenaVector<Statement * > finalizerInsertionsStatement)54 explicit TryStatement(BlockStatement *block, ArenaVector<CatchClause *> &&catchClauses, BlockStatement *finalizer, 55 ArenaVector<compiler::LabelPair> finalizerInsertionsLabelPair, 56 ArenaVector<Statement *> finalizerInsertionsStatement) 57 : Statement(AstNodeType::TRY_STATEMENT), 58 block_(block), 59 catchClauses_(std::move(catchClauses)), 60 finalizer_(finalizer), 61 finalizerInsertions_(catchClauses_.get_allocator()) 62 { 63 ES2PANDA_ASSERT(finalizerInsertionsLabelPair.size() == finalizerInsertionsStatement.size()); 64 65 for (uint64_t i = 0; i < finalizerInsertionsLabelPair.size(); i++) { 66 finalizerInsertions_.push_back( 67 std::make_pair(finalizerInsertionsLabelPair[i], finalizerInsertionsStatement[i])); 68 } 69 } 70 71 explicit TryStatement(TryStatement const &other, ArenaAllocator *allocator); 72 friend class checker::TSAnalyzer; 73 friend class checker::ETSAnalyzer; 74 friend class compiler::JSCompiler; 75 friend class compiler::ETSCompiler; 76 FinallyBlock()77 BlockStatement *FinallyBlock() const 78 { 79 return finalizer_; 80 } 81 Block()82 BlockStatement *Block() const 83 { 84 return block_; 85 } 86 AddFinalizerInsertion(compiler::LabelPair insertion,const Statement * insertionStmt)87 std::pair<compiler::LabelPair, const Statement *> AddFinalizerInsertion(compiler::LabelPair insertion, 88 const Statement *insertionStmt) 89 { 90 finalizerInsertions_.push_back(std::pair<compiler::LabelPair, const Statement *>(insertion, insertionStmt)); 91 return finalizerInsertions_.back(); 92 } 93 HasFinalizer()94 [[nodiscard]] bool HasFinalizer() const noexcept 95 { 96 return finalizer_ != nullptr; 97 } 98 99 bool HasDefaultCatchClause() const; 100 CatchClauses()101 const ArenaVector<CatchClause *> &CatchClauses() const 102 { 103 return catchClauses_; 104 } 105 FinallyCanCompleteNormally()106 bool FinallyCanCompleteNormally() const 107 { 108 return finallyCanCompleteNormally_; 109 } 110 SetFinallyCanCompleteNormally(bool finallyCanCompleteNormally)111 void SetFinallyCanCompleteNormally(bool finallyCanCompleteNormally) 112 { 113 finallyCanCompleteNormally_ = finallyCanCompleteNormally; 114 } 115 116 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 117 118 void Iterate(const NodeTraverser &cb) const override; 119 void Dump(ir::AstDumper *dumper) const override; 120 void Dump(ir::SrcDumper *dumper) const override; 121 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 122 void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; 123 checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; 124 checker::VerifiedType Check([[maybe_unused]] checker::ETSChecker *checker) override; 125 Accept(ASTVisitorT * v)126 void Accept(ASTVisitorT *v) override 127 { 128 v->Accept(this); 129 } 130 131 [[nodiscard]] TryStatement *Clone(ArenaAllocator *allocator, AstNode *parent) override; 132 133 private: 134 BlockStatement *block_; 135 ArenaVector<CatchClause *> catchClauses_; 136 BlockStatement *finalizer_; 137 ArenaVector<std::pair<compiler::LabelPair, const Statement *>> finalizerInsertions_; 138 bool finallyCanCompleteNormally_ {}; 139 }; 140 } // namespace ark::es2panda::ir 141 142 #endif 143