1 /** 2 * Copyright (c) 2021 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 panda::es2panda::checker { 23 class TSAnalyzer; 24 class ETSAnalyzer; 25 } // namespace panda::es2panda::checker 26 27 namespace panda::es2panda::compiler { 28 class JSCompiler; 29 class ETSCompiler; 30 class PandaGen; 31 class TryLabelSet; 32 class TryContext; 33 } // namespace panda::es2panda::compiler 34 35 namespace panda::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 const 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 bool HasFinalizer() 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 85 void TransformChildren(const NodeTransformer &cb) override; 86 void SetReturnType(checker::ETSChecker *checker, checker::Type *type) override; 87 88 void Iterate(const NodeTraverser &cb) const override; 89 void Dump(ir::AstDumper *dumper) const override; 90 void Dump(ir::SrcDumper *dumper) const override; 91 void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; 92 void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; 93 checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; 94 checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; 95 Accept(ASTVisitorT * v)96 void Accept(ASTVisitorT *v) override 97 { 98 v->Accept(this); 99 } 100 101 private: 102 BlockStatement *block_; 103 ArenaVector<CatchClause *> catchClauses_; 104 BlockStatement *finalizer_; 105 ArenaVector<std::pair<compiler::LabelPair, const Statement *>> finalizerInsertions_; 106 }; 107 } // namespace panda::es2panda::ir 108 109 #endif 110