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 #include "catchClause.h"
17 
18 #include "checker/TSchecker.h"
19 #include "compiler/core/pandagen.h"
20 #include "compiler/core/ETSGen.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23 #include "ir/expressions/identifier.h"
24 #include "ir/statements/blockStatement.h"
25 
26 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer &cb, std::string_view transformationName)27 void CatchClause::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
28 {
29     if (param_ != nullptr) {
30         if (auto *transformedNode = cb(param_); param_ != transformedNode) {
31             param_->SetTransformedNode(transformationName, transformedNode);
32             param_ = transformedNode->AsExpression();
33         }
34     }
35 
36     if (auto *transformedNode = cb(body_); body_ != transformedNode) {
37         body_->SetTransformedNode(transformationName, transformedNode);
38         body_ = transformedNode->AsBlockStatement();
39     }
40 }
41 
Iterate(const NodeTraverser &cb) const42 void CatchClause::Iterate(const NodeTraverser &cb) const
43 {
44     if (param_ != nullptr) {
45         cb(param_);
46     }
47 
48     cb(body_);
49 }
50 
Dump(ir::AstDumper *dumper) const51 void CatchClause::Dump(ir::AstDumper *dumper) const
52 {
53     dumper->Add({{"type", "CatchClause"}, {"body", body_}, {"param", AstDumper::Nullish(param_)}});
54 }
55 
Dump(ir::SrcDumper *dumper) const56 void CatchClause::Dump(ir::SrcDumper *dumper) const
57 {
58     ASSERT(body_ != nullptr);
59     dumper->Add("(");
60     if (param_ != nullptr) {
61         param_->Dump(dumper);
62         if (param_->IsIdentifier() && param_->AsIdentifier()->TypeAnnotation() != nullptr) {
63             dumper->Add(": ");
64             param_->AsIdentifier()->TypeAnnotation()->Dump(dumper);
65         }
66     }
67     dumper->Add(") {");
68     dumper->IncrIndent();
69     dumper->Endl();
70     body_->Dump(dumper);
71     dumper->DecrIndent();
72     dumper->Endl();
73     dumper->Add("}");
74 }
75 
IsDefaultCatchClause() const76 bool CatchClause::IsDefaultCatchClause() const
77 {
78     return param_->AsIdentifier()->TypeAnnotation() == nullptr;
79 }
80 
Compile(compiler::PandaGen *pg) const81 void CatchClause::Compile(compiler::PandaGen *pg) const
82 {
83     pg->GetAstCompiler()->Compile(this);
84 }
85 
Compile(compiler::ETSGen *etsg) const86 void CatchClause::Compile(compiler::ETSGen *etsg) const
87 {
88     etsg->GetAstCompiler()->Compile(this);
89 }
90 
Check(checker::TSChecker *checker)91 checker::Type *CatchClause::Check(checker::TSChecker *checker)
92 {
93     return checker->GetAnalyzer()->Check(this);
94 }
95 
Check(checker::ETSChecker *checker)96 checker::Type *CatchClause::Check(checker::ETSChecker *checker)
97 {
98     return checker->GetAnalyzer()->Check(this);
99 }
100 }  // namespace ark::es2panda::ir
101