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 #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         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     friend class checker::TSAnalyzer;
72     friend class checker::ETSAnalyzer;
73     friend class compiler::JSCompiler;
74     friend class compiler::ETSCompiler;
75 
FinallyBlock() const76     BlockStatement *FinallyBlock() const
77     {
78         return finalizer_;
79     }
80 
Block() const81     BlockStatement *Block() const
82     {
83         return block_;
84     }
85 
AddFinalizerInsertion(compiler::LabelPair insertion, const Statement *insertionStmt)86     std::pair<compiler::LabelPair, const Statement *> AddFinalizerInsertion(compiler::LabelPair insertion,
87                                                                             const Statement *insertionStmt)
88     {
89         finalizerInsertions_.push_back(std::pair<compiler::LabelPair, const Statement *>(insertion, insertionStmt));
90         return finalizerInsertions_.back();
91     }
92 
93     [[nodiscard]] bool HasFinalizer() const noexcept
94     {
95         return finalizer_ != nullptr;
96     }
97 
98     bool HasDefaultCatchClause() const;
99 
CatchClauses() const100     const ArenaVector<CatchClause *> &CatchClauses() const
101     {
102         return catchClauses_;
103     }
104 
FinallyCanCompleteNormally() const105     bool FinallyCanCompleteNormally() const
106     {
107         return finallyCanCompleteNormally_;
108     }
109 
SetFinallyCanCompleteNormally(bool finallyCanCompleteNormally)110     void SetFinallyCanCompleteNormally(bool finallyCanCompleteNormally)
111     {
112         finallyCanCompleteNormally_ = finallyCanCompleteNormally;
113     }
114 
115     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
116 
117     void Iterate(const NodeTraverser &cb) const override;
118     void Dump(ir::AstDumper *dumper) const override;
119     void Dump(ir::SrcDumper *dumper) const override;
120     void Compile([[maybe_unused]] compiler::PandaGen *pg) const override;
121     void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override;
122     checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override;
123     checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override;
124 
125     void Accept(ASTVisitorT *v) override
126     {
127         v->Accept(this);
128     }
129 
130 private:
131     BlockStatement *block_;
132     ArenaVector<CatchClause *> catchClauses_;
133     BlockStatement *finalizer_;
134     ArenaVector<std::pair<compiler::LabelPair, const Statement *>> finalizerInsertions_;
135     bool finallyCanCompleteNormally_ {};
136 };
137 }  // namespace ark::es2panda::ir
138 
139 #endif
140