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