1 /**
2 * Copyright (c) 2021 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 panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)27 void CatchClause::TransformChildren(const NodeTransformer &cb)
28 {
29 if (param_ != nullptr) {
30 param_ = cb(param_)->AsExpression();
31 }
32
33 body_ = cb(body_)->AsBlockStatement();
34 }
35
Iterate(const NodeTraverser & cb) const36 void CatchClause::Iterate(const NodeTraverser &cb) const
37 {
38 if (param_ != nullptr) {
39 cb(param_);
40 }
41
42 cb(body_);
43 }
44
Dump(ir::AstDumper * dumper) const45 void CatchClause::Dump(ir::AstDumper *dumper) const
46 {
47 dumper->Add({{"type", "CatchClause"}, {"body", body_}, {"param", AstDumper::Nullish(param_)}});
48 }
49
Dump(ir::SrcDumper * dumper) const50 void CatchClause::Dump(ir::SrcDumper *dumper) const
51 {
52 ASSERT(body_ != nullptr);
53 dumper->Add("(");
54 if (param_ != nullptr) {
55 param_->Dump(dumper);
56 }
57 dumper->Add(") {");
58 dumper->IncrIndent();
59 dumper->Endl();
60 body_->Dump(dumper);
61 dumper->DecrIndent();
62 dumper->Endl();
63 dumper->Add("}");
64 }
65
IsDefaultCatchClause() const66 bool CatchClause::IsDefaultCatchClause() const
67 {
68 return param_->AsIdentifier()->TypeAnnotation() == nullptr;
69 }
70
Compile(compiler::PandaGen * pg) const71 void CatchClause::Compile(compiler::PandaGen *pg) const
72 {
73 pg->GetAstCompiler()->Compile(this);
74 }
75
Compile(compiler::ETSGen * etsg) const76 void CatchClause::Compile(compiler::ETSGen *etsg) const
77 {
78 etsg->GetAstCompiler()->Compile(this);
79 }
80
Check(checker::TSChecker * checker)81 checker::Type *CatchClause::Check(checker::TSChecker *checker)
82 {
83 return checker->GetAnalyzer()->Check(this);
84 }
85
Check(checker::ETSChecker * checker)86 checker::Type *CatchClause::Check(checker::ETSChecker *checker)
87 {
88 return checker->GetAnalyzer()->Check(this);
89 }
90 } // namespace panda::es2panda::ir
91