• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     }
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_->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::Type *CatchClause::Check(checker::ETSChecker *checker)
93 {
94     return checker->GetAnalyzer()->Check(this);
95 }
96 }  // namespace ark::es2panda::ir
97