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 "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
24namespace ark::es2panda::ir {
25void 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 : cases_) {
33        if (auto *transformedNode = cb(it); it != transformedNode) {
34            it->SetTransformedNode(transformationName, transformedNode);
35            it = transformedNode->AsSwitchCaseStatement();
36        }
37    }
38}
39
40void SwitchStatement::Iterate(const NodeTraverser &cb) const
41{
42    cb(discriminant_);
43
44    for (auto *it : cases_) {
45        cb(it);
46    }
47}
48
49void SwitchStatement::Dump(ir::AstDumper *dumper) const
50{
51    dumper->Add({{"type", "SwitchStatement"}, {"discriminant", discriminant_}, {"cases", cases_}});
52}
53
54void SwitchStatement::Dump(ir::SrcDumper *dumper) const
55{
56    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
74void SwitchStatement::Compile(compiler::PandaGen *pg) const
75{
76    pg->GetAstCompiler()->Compile(this);
77}
78
79void SwitchStatement::Compile(compiler::ETSGen *etsg) const
80{
81    etsg->GetAstCompiler()->Compile(this);
82}
83
84checker::Type *SwitchStatement::Check(checker::TSChecker *checker)
85{
86    return checker->GetAnalyzer()->Check(this);
87}
88
89checker::Type *SwitchStatement::Check(checker::ETSChecker *const checker)
90{
91    return checker->GetAnalyzer()->Check(this);
92}
93
94}  // namespace ark::es2panda::ir
95