• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "catchClause.h"
17 
18 #include "checker/TSchecker.h"
19 #include "compiler/core/pandagen.h"
20 #include "compiler/core/ETSGen.h"
21 
22 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)23 void CatchClause::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
24 {
25     if (param_ != nullptr) {
26         if (auto *transformedNode = cb(param_); param_ != transformedNode) {
27             param_->SetTransformedNode(transformationName, transformedNode);
28             param_ = transformedNode->AsExpression();
29         }
30     }
31 
32     if (auto *transformedNode = cb(body_); body_ != transformedNode) {
33         body_->SetTransformedNode(transformationName, transformedNode);
34         body_ = transformedNode->AsBlockStatement();
35     }
36 }
37 
Iterate(const NodeTraverser & cb) const38 void CatchClause::Iterate(const NodeTraverser &cb) const
39 {
40     if (param_ != nullptr) {
41         cb(param_);
42     }
43 
44     cb(body_);
45 }
46 
Dump(ir::AstDumper * dumper) const47 void CatchClause::Dump(ir::AstDumper *dumper) const
48 {
49     dumper->Add({{"type", "CatchClause"}, {"body", body_}, {"param", AstDumper::Nullish(param_)}});
50 }
51 
Dump(ir::SrcDumper * dumper) const52 void CatchClause::Dump(ir::SrcDumper *dumper) const
53 {
54     ES2PANDA_ASSERT(body_ != nullptr);
55     dumper->Add("(");
56     if (param_ != nullptr) {
57         param_->Dump(dumper);
58         if (param_->IsIdentifier() && param_->AsIdentifier()->TypeAnnotation() != nullptr) {
59             dumper->Add(": ");
60             param_->AsIdentifier()->TypeAnnotation()->Dump(dumper);
61         }
62     }
63     dumper->Add(") {");
64     dumper->IncrIndent();
65     dumper->Endl();
66     body_->Dump(dumper);
67     dumper->DecrIndent();
68     dumper->Endl();
69     dumper->Add("}");
70 }
71 
IsDefaultCatchClause() const72 bool CatchClause::IsDefaultCatchClause() const
73 {
74     return param_ != nullptr && param_->AsIdentifier()->TypeAnnotation() == nullptr;
75 }
76 
Compile(compiler::PandaGen * pg) const77 void CatchClause::Compile(compiler::PandaGen *pg) const
78 {
79     pg->GetAstCompiler()->Compile(this);
80 }
81 
Compile(compiler::ETSGen * etsg) const82 void CatchClause::Compile(compiler::ETSGen *etsg) const
83 {
84     etsg->GetAstCompiler()->Compile(this);
85 }
86 
Check(checker::TSChecker * checker)87 checker::Type *CatchClause::Check(checker::TSChecker *checker)
88 {
89     return checker->GetAnalyzer()->Check(this);
90 }
91 
Check(checker::ETSChecker * checker)92 checker::VerifiedType CatchClause::Check(checker::ETSChecker *checker)
93 {
94     return {this, checker->GetAnalyzer()->Check(this)};
95 }
96 
CatchClause(CatchClause const & other,ArenaAllocator * allocator)97 CatchClause::CatchClause(CatchClause const &other, ArenaAllocator *allocator) : TypedStatement(other)
98 {
99     param_ = nullptr;
100     body_ = nullptr;
101     auto *const cloneParam = other.param_ != nullptr ? other.param_->Clone(allocator, this) : nullptr;
102     auto *const cloneBody = other.body_ != nullptr ? other.body_->Clone(allocator, this) : nullptr;
103     if (cloneParam != nullptr) {
104         param_ = cloneParam->AsExpression();
105     }
106     if (cloneBody != nullptr) {
107         body_ = cloneBody->AsBlockStatement();
108     }
109 }
110 
Clone(ArenaAllocator * const allocator,AstNode * const parent)111 CatchClause *CatchClause::Clone(ArenaAllocator *const allocator, AstNode *const parent)
112 {
113     auto *const clone = allocator->New<CatchClause>(*this, allocator);
114     ES2PANDA_ASSERT(clone != nullptr);
115     if (parent != nullptr) {
116         clone->SetParent(parent);
117     }
118 
119     clone->SetRange(Range());
120     return clone;
121 }
122 
123 }  // namespace ark::es2panda::ir
124