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 #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 22 namespace ark::es2panda::checker { 23 class TSAnalyzer; 24 class ETSAnalyzer; 25 } // namespace ark::es2panda::checker 26 27 namespace ark::es2panda::compiler { 28 class JSCompiler; 29 class ETSCompiler; 30 class PandaGen; 31 class TryLabelSet; 32 class TryContext; 33 } // namespace ark::es2panda::compiler 34 35 namespace ark::es2panda::ir { 36 class BlockStatement; 37 class CatchClause; 38 39 class TryStatement : public Statement { 40 public: TryStatement(BlockStatement * block,ArenaVector<CatchClause * > && catchClauses,BlockStatement * finalizer,ArenaVector<std::pair<compiler::LabelPair,const Statement * >> finalizerInsertions)41 explicit TryStatement(BlockStatement *block, ArenaVector<CatchClause *> &&catchClauses, BlockStatement *finalizer, 42 ArenaVector<std::pair<compiler::LabelPair, const Statement *>> finalizerInsertions) 43 : Statement(AstNodeType::TRY_STATEMENT), 44 block_(block), 45 catchClauses_(std::move(catchClauses)), 46 finalizer_(finalizer), 47 finalizerInsertions_(std::move(finalizerInsertions)) 48 { 49 } 50 51 friend class checker::TSAnalyzer; 52 friend class checker::ETSAnalyzer; 53 friend class compiler::JSCompiler; 54 friend class compiler::ETSCompiler; 55 FinallyBlock()56 BlockStatement *FinallyBlock() const 57 { 58 return finalizer_; 59 } 60 Block()61 BlockStatement *Block() const 62 { 63 return block_; 64 } 65 AddFinalizerInsertion(compiler::LabelPair insertion,const Statement * insertionStmt)66 std::pair<compiler::LabelPair, const Statement *> AddFinalizerInsertion(compiler::LabelPair insertion, 67 const Statement *insertionStmt) 68 { 69 finalizerInsertions_.push_back(std::pair<compiler::LabelPair, const Statement *>(insertion, insertionStmt)); 70 return finalizerInsertions_.back(); 71 } 72 HasFinalizer()73 [[nodiscard]] bool HasFinalizer() const noexcept 74 { 75 return finalizer_ != nullptr; 76 } 77 78 bool HasDefaultCatchClause() const; 79 CatchClauses()80 const ArenaVector<CatchClause *> &CatchClauses() const 81 { 82 return catchClauses_; 83 } 84 FinallyCanCompleteNormally()85 bool FinallyCanCompleteNormally() const 86 { 87 return finallyCanCompleteNormally_; 88 } 89 SetFinallyCanCompleteNormally(bool finallyCanCompleteNormally)90 void SetFinallyCanCompleteNormally(bool finallyCanCompleteNormally) 91 { 92 finallyCanCompleteNormally_ = finallyCanCompleteNormally; 93 } 94 95 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 96 97 void Iterate(const NodeTraverser &cb) const override; 98 void Dump(ir::AstDumper *dumper) const override; 99 void Dump(ir::SrcDumper *dumper) const override; 100 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 101 void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; 102 checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; 103 checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; 104 Accept(ASTVisitorT * v)105 void Accept(ASTVisitorT *v) override 106 { 107 v->Accept(this); 108 } 109 110 private: 111 BlockStatement *block_; 112 ArenaVector<CatchClause *> catchClauses_; 113 BlockStatement *finalizer_; 114 ArenaVector<std::pair<compiler::LabelPair, const Statement *>> finalizerInsertions_; 115 bool finallyCanCompleteNormally_ {}; 116 }; 117 } // namespace ark::es2panda::ir 118 119 #endif 120