• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)29 void TryStatement::TransformChildren(const NodeTransformer &cb)
30 {
31     block_ = cb(block_)->AsBlockStatement();
32 
33     for (auto *&it : catchClauses_) {
34         it = cb(it)->AsCatchClause();
35     }
36 
37     if (finalizer_ != nullptr) {
38         finalizer_ = cb(finalizer_)->AsBlockStatement();
39     }
40 }
41 
Iterate(const NodeTraverser & cb) const42 void TryStatement::Iterate(const NodeTraverser &cb) const
43 {
44     cb(block_);
45 
46     for (auto *it : catchClauses_) {
47         cb(it);
48     }
49 
50     if (finalizer_ != nullptr) {
51         cb(finalizer_);
52     }
53 }
54 
Dump(ir::AstDumper * dumper) const55 void TryStatement::Dump(ir::AstDumper *dumper) const
56 {
57     dumper->Add({{"type", "TryStatement"},
58                  {"block", block_},
59                  {"handler", catchClauses_},
60                  {"finalizer", AstDumper::Nullish(finalizer_)}});
61 }
62 
Dump(ir::SrcDumper * dumper) const63 void TryStatement::Dump(ir::SrcDumper *dumper) const
64 {
65     ASSERT(block_ != nullptr);
66     dumper->Add("try {");
67     dumper->IncrIndent();
68     dumper->Endl();
69     block_->Dump(dumper);
70     dumper->DecrIndent();
71     dumper->Endl();
72     dumper->Add("}");
73     for (auto clause : catchClauses_) {
74         dumper->Add(" catch ");
75         clause->Dump(dumper);
76     }
77     if (finalizer_ != nullptr) {
78         dumper->Add(" finally {");
79         dumper->IncrIndent();
80         dumper->Endl();
81         finalizer_->Dump(dumper);
82         dumper->DecrIndent();
83         dumper->Endl();
84         dumper->Add("}");
85     }
86 }
87 
HasDefaultCatchClause() const88 bool TryStatement::HasDefaultCatchClause() const
89 {
90     return (!catchClauses_.empty() && catchClauses_.back()->IsDefaultCatchClause());
91 }
92 
Compile(compiler::PandaGen * pg) const93 void TryStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const
94 {
95     pg->GetAstCompiler()->Compile(this);
96 }
97 
Compile(compiler::ETSGen * etsg) const98 void TryStatement::Compile(compiler::ETSGen *etsg) const
99 {
100     etsg->GetAstCompiler()->Compile(this);
101 }
102 
Check(checker::TSChecker * checker)103 checker::Type *TryStatement::Check([[maybe_unused]] checker::TSChecker *checker)
104 {
105     return checker->GetAnalyzer()->Check(this);
106 }
107 
Check(checker::ETSChecker * checker)108 checker::Type *TryStatement::Check([[maybe_unused]] checker::ETSChecker *checker)
109 {
110     return checker->GetAnalyzer()->Check(this);
111 }
112 
SetReturnType(checker::ETSChecker * checker,checker::Type * type)113 void TryStatement::SetReturnType(checker::ETSChecker *checker, checker::Type *type)
114 {
115     if (block_ != nullptr) {
116         block_->SetReturnType(checker, type);
117     }
118     if (finalizer_ != nullptr) {
119         finalizer_->SetReturnType(checker, type);
120     }
121     for (auto *catchClause : catchClauses_) {
122         if (catchClause != nullptr) {
123             catchClause->SetReturnType(checker, type);
124         }
125     }
126 }
127 }  // namespace panda::es2panda::ir
128