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 #ifndef ES2PANDA_IR_STATEMENT_SWITCH_STATEMENT_H
17 #define ES2PANDA_IR_STATEMENT_SWITCH_STATEMENT_H
18 
19 #include "varbinder/scope.h"
20 #include "ir/statement.h"
21 
22 namespace ark::es2panda::checker {
23 class TSAnalyzer;
24 class ETSAnalyzer;
25 }  // namespace ark::es2panda::checker
26 
27 namespace ark::es2panda::ir {
28 class Expression;
29 class SwitchCaseStatement;
30 
31 class SwitchStatement : public Statement {
32 public:
33     SwitchStatement() = delete;
34     ~SwitchStatement() override = default;
35 
36     NO_COPY_SEMANTIC(SwitchStatement);
37     NO_MOVE_SEMANTIC(SwitchStatement);
38 
SwitchStatement(Expression *discriminant, ArenaVector<SwitchCaseStatement *> &&cases)39     explicit SwitchStatement(Expression *discriminant, ArenaVector<SwitchCaseStatement *> &&cases)
40         : Statement(AstNodeType::SWITCH_STATEMENT), discriminant_(discriminant), cases_(std::move(cases))
41     {
42     }
43 
44     [[nodiscard]] const Expression *Discriminant() const noexcept
45     {
46         return discriminant_;
47     }
48 
49     [[nodiscard]] Expression *Discriminant() noexcept
50     {
51         return discriminant_;
52     }
53 
54     [[nodiscard]] const ArenaVector<SwitchCaseStatement *> &Cases() const noexcept
55     {
56         return cases_;
57     }
58 
59     [[nodiscard]] ArenaVector<SwitchCaseStatement *> &Cases() noexcept
60     {
61         return cases_;
62     }
63 
64     [[nodiscard]] bool IsScopeBearer() const noexcept override
65     {
66         return true;
67     }
68 
69     [[nodiscard]] varbinder::LocalScope *Scope() const noexcept override
70     {
71         return scope_;
72     }
73 
74     void SetScope(varbinder::LocalScope *scope) noexcept
75     {
76         ASSERT(scope_ == nullptr);
77         scope_ = scope;
78     }
79 
80     void ClearScope() noexcept override
81     {
82         scope_ = nullptr;
83     }
84 
85     void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override;
86 
87     void Iterate(const NodeTraverser &cb) const override;
88     void Dump(ir::AstDumper *dumper) const override;
89     void Dump(ir::SrcDumper *dumper) const override;
90     void Compile(compiler::PandaGen *pg) const override;
91     void Compile(compiler::ETSGen *etsg) const override;
92     checker::Type *Check(checker::TSChecker *checker) override;
93     checker::Type *Check(checker::ETSChecker *checker) override;
94 
95     void Accept(ASTVisitorT *v) override
96     {
97         v->Accept(this);
98     }
99 
100 private:
101     varbinder::LocalScope *scope_ {nullptr};
102     Expression *discriminant_;
103     ArenaVector<SwitchCaseStatement *> cases_;
104 };
105 }  // namespace ark::es2panda::ir
106 
107 #endif
108