1 /**
2 * Copyright (c) 2021-2025 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 "switchStatement.h"
17
18 #include "compiler/core/pandagen.h"
19 #include "compiler/core/ETSGen.h"
20 #include "checker/TSchecker.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23
24 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)25 void SwitchStatement::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
26 {
27 if (auto *transformedNode = cb(discriminant_); discriminant_ != transformedNode) {
28 discriminant_->SetTransformedNode(transformationName, transformedNode);
29 discriminant_ = transformedNode->AsExpression();
30 }
31
32 for (auto *&it : VectorIterationGuard(cases_)) {
33 if (auto *transformedNode = cb(it); it != transformedNode) {
34 it->SetTransformedNode(transformationName, transformedNode);
35 it = transformedNode->AsSwitchCaseStatement();
36 }
37 }
38 }
39
Iterate(const NodeTraverser & cb) const40 void SwitchStatement::Iterate(const NodeTraverser &cb) const
41 {
42 cb(discriminant_);
43
44 for (auto *it : VectorIterationGuard(cases_)) {
45 cb(it);
46 }
47 }
48
Dump(ir::AstDumper * dumper) const49 void SwitchStatement::Dump(ir::AstDumper *dumper) const
50 {
51 dumper->Add({{"type", "SwitchStatement"}, {"discriminant", discriminant_}, {"cases", cases_}});
52 }
53
Dump(ir::SrcDumper * dumper) const54 void SwitchStatement::Dump(ir::SrcDumper *dumper) const
55 {
56 ES2PANDA_ASSERT(discriminant_);
57 dumper->Add("switch (");
58 discriminant_->Dump(dumper);
59 dumper->Add(") {");
60 if (!cases_.empty()) {
61 dumper->IncrIndent();
62 dumper->Endl();
63 for (auto cs : cases_) {
64 cs->Dump(dumper);
65 if (cs == cases_.back()) {
66 dumper->DecrIndent();
67 }
68 dumper->Endl();
69 }
70 }
71 dumper->Add("}");
72 }
73
Compile(compiler::PandaGen * pg) const74 void SwitchStatement::Compile(compiler::PandaGen *pg) const
75 {
76 pg->GetAstCompiler()->Compile(this);
77 }
78
Compile(compiler::ETSGen * etsg) const79 void SwitchStatement::Compile(compiler::ETSGen *etsg) const
80 {
81 etsg->GetAstCompiler()->Compile(this);
82 }
83
Check(checker::TSChecker * checker)84 checker::Type *SwitchStatement::Check(checker::TSChecker *checker)
85 {
86 return checker->GetAnalyzer()->Check(this);
87 }
88
Check(checker::ETSChecker * const checker)89 checker::VerifiedType SwitchStatement::Check(checker::ETSChecker *const checker)
90 {
91 return {this, checker->GetAnalyzer()->Check(this)};
92 }
93
Clone(ArenaAllocator * const allocator,AstNode * const parent)94 SwitchStatement *SwitchStatement::Clone(ArenaAllocator *const allocator, AstNode *const parent)
95 {
96 auto *const discriminant = discriminant_->Clone(allocator, nullptr)->AsExpression();
97 ArenaVector<SwitchCaseStatement *> cases(allocator->Adapter());
98
99 for (auto currentCase : cases_) {
100 cases.push_back(currentCase->Clone(allocator, nullptr));
101 }
102
103 auto clone = util::NodeAllocator::ForceSetParent<ir::SwitchStatement>(allocator, discriminant, std::move(cases));
104
105 clone->SetParent(parent);
106 clone->SetRange(Range());
107 return clone;
108 }
109
110 } // namespace ark::es2panda::ir
111