• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2024-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 #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 
28 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)29 void 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 : VectorIterationGuard(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 
Iterate(const NodeTraverser & cb) const51 void TryStatement::Iterate(const NodeTraverser &cb) const
52 {
53     cb(block_);
54 
55     for (auto *it : VectorIterationGuard(catchClauses_)) {
56         cb(it);
57     }
58 
59     if (finalizer_ != nullptr) {
60         cb(finalizer_);
61     }
62 }
63 
Dump(ir::AstDumper * dumper) const64 void 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 
Dump(ir::SrcDumper * dumper) const72 void TryStatement::Dump(ir::SrcDumper *dumper) const
73 {
74     ES2PANDA_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 
HasDefaultCatchClause() const97 bool TryStatement::HasDefaultCatchClause() const
98 {
99     return (!catchClauses_.empty() && catchClauses_.back()->IsDefaultCatchClause());
100 }
101 
Compile(compiler::PandaGen * pg) const102 void TryStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const
103 {
104     pg->GetAstCompiler()->Compile(this);
105 }
106 
Compile(compiler::ETSGen * etsg) const107 void TryStatement::Compile(compiler::ETSGen *etsg) const
108 {
109     etsg->GetAstCompiler()->Compile(this);
110 }
111 
Check(checker::TSChecker * checker)112 checker::Type *TryStatement::Check([[maybe_unused]] checker::TSChecker *checker)
113 {
114     return checker->GetAnalyzer()->Check(this);
115 }
116 
Check(checker::ETSChecker * checker)117 checker::VerifiedType TryStatement::Check([[maybe_unused]] checker::ETSChecker *checker)
118 {
119     return {this, checker->GetAnalyzer()->Check(this)};
120 }
121 
TryStatement(TryStatement const & other,ArenaAllocator * allocator)122 TryStatement::TryStatement(TryStatement const &other, ArenaAllocator *allocator)
123     : Statement(other),
124       catchClauses_(allocator->Adapter()),
125       finalizerInsertions_(allocator->Adapter()),
126       finallyCanCompleteNormally_(other.finallyCanCompleteNormally_)
127 {
128     block_ = other.block_ == nullptr ? nullptr : other.block_->Clone(allocator, this)->AsBlockStatement();
129     for (auto &cc : other.catchClauses_) {
130         if (cc == nullptr) {
131             catchClauses_.push_back(nullptr);
132             continue;
133         }
134         auto *ccClone = cc->Clone(allocator, this);
135         ES2PANDA_ASSERT(ccClone != nullptr);
136         catchClauses_.push_back(ccClone->AsCatchClause());
137     }
138     if (other.finalizer_ == nullptr) {
139         finalizer_ = nullptr;
140     } else {
141         auto *otherFinalizerClone = other.finalizer_->Clone(allocator, this);
142         ES2PANDA_ASSERT(otherFinalizerClone != nullptr);
143         finalizer_ = otherFinalizerClone->AsBlockStatement();
144     }
145     for (auto &[label, st] : other.finalizerInsertions_) {
146         finalizerInsertions_.emplace_back(label, st);
147     }
148 }
149 
Clone(ArenaAllocator * const allocator,AstNode * const parent)150 TryStatement *TryStatement::Clone(ArenaAllocator *const allocator, AstNode *const parent)
151 {
152     auto *const clone = allocator->New<TryStatement>(*this, allocator);
153     ES2PANDA_ASSERT(clone != nullptr);
154     if (parent != nullptr) {
155         clone->SetParent(parent);
156     }
157 
158     clone->SetRange(Range());
159     return clone;
160 }
161 
162 }  // namespace ark::es2panda::ir
163