1 /**
2 * Copyright (c) 2021-2022 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 "switchCaseStatement.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23
24 namespace panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)25 void SwitchCaseStatement::TransformChildren(const NodeTransformer &cb)
26 {
27 if (test_ != nullptr) {
28 test_ = cb(test_)->AsExpression();
29 }
30
31 for (auto *&it : consequent_) {
32 it = cb(it)->AsStatement();
33 }
34 }
35
Iterate(const NodeTraverser & cb) const36 void SwitchCaseStatement::Iterate(const NodeTraverser &cb) const
37 {
38 if (test_ != nullptr) {
39 cb(test_);
40 }
41
42 for (auto *it : consequent_) {
43 cb(it);
44 }
45 }
46
Dump(ir::AstDumper * dumper) const47 void SwitchCaseStatement::Dump(ir::AstDumper *dumper) const
48 {
49 dumper->Add({{"type", "SwitchCase"}, {"test", AstDumper::Nullish(test_)}, {"consequent", consequent_}});
50 }
51
Dump(ir::SrcDumper * dumper) const52 void SwitchCaseStatement::Dump(ir::SrcDumper *dumper) const
53 {
54 if (test_ != nullptr) {
55 dumper->Add("case ");
56 test_->Dump(dumper);
57 dumper->Add(":");
58 } else {
59 dumper->Add("default:");
60 }
61 if (!consequent_.empty()) {
62 dumper->IncrIndent();
63 dumper->Endl();
64 for (auto cs : consequent_) {
65 cs->Dump(dumper);
66 }
67 dumper->DecrIndent();
68 }
69 }
70
Compile(compiler::PandaGen * pg) const71 void SwitchCaseStatement::Compile(compiler::PandaGen *pg) const
72 {
73 pg->GetAstCompiler()->Compile(this);
74 }
75
Compile(compiler::ETSGen * etsg) const76 void SwitchCaseStatement::Compile(compiler::ETSGen *etsg) const
77 {
78 etsg->GetAstCompiler()->Compile(this);
79 }
80
Check(checker::TSChecker * checker)81 checker::Type *SwitchCaseStatement::Check(checker::TSChecker *checker)
82 {
83 return checker->GetAnalyzer()->Check(this);
84 }
85
Check(checker::ETSChecker * checker)86 checker::Type *SwitchCaseStatement::Check(checker::ETSChecker *checker)
87 {
88 return checker->GetAnalyzer()->Check(this);
89 }
90 } // namespace panda::es2panda::ir
91