1/** 2 * Copyright (c) 2021 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 <binder/binder.h> 19#include <binder/scope.h> 20#include <compiler/core/labelTarget.h> 21#include <compiler/core/switchBuilder.h> 22#include <compiler/core/pandagen.h> 23#include <typescript/checker.h> 24#include <ir/astDump.h> 25#include <ir/expression.h> 26#include <ir/statements/switchCaseStatement.h> 27 28namespace panda::es2panda::ir { 29 30void SwitchStatement::Iterate(const NodeTraverser &cb) const 31{ 32 cb(discriminant_); 33 34 for (auto *it : cases_) { 35 cb(it); 36 } 37} 38 39void SwitchStatement::Dump(ir::AstDumper *dumper) const 40{ 41 dumper->Add({{"type", "SwitchStatement"}, {"discriminant", discriminant_}, {"cases", cases_}}); 42} 43 44void SwitchStatement::Compile(compiler::PandaGen *pg) const 45{ 46 compiler::SwitchBuilder builder(pg, this); 47 compiler::VReg tag = pg->AllocReg(); 48 49 builder.CompileTagOfSwitch(tag); 50 51 compiler::LocalRegScope lrs(pg, scope_); 52 uint32_t defaultIndex = 0; 53 54 if (cases_.size() == 0) { 55 return; 56 } 57 58 for (size_t i = 0; i < cases_.size(); i++) { 59 const auto *clause = cases_[i]; 60 61 if (!clause->Test()) { 62 defaultIndex = i; 63 continue; 64 } 65 66 builder.JumpIfCase(tag, i); 67 } 68 69 if (!cases_[defaultIndex]->Test()) { 70 builder.JumpToDefault(defaultIndex); 71 } else { 72 builder.Break(); 73 } 74 75 for (size_t i = 0; i < cases_.size(); i++) { 76 builder.SetCaseTarget(i); 77 builder.CompileCaseStatements(i); 78 } 79} 80 81checker::Type *SwitchStatement::Check(checker::Checker *checker) const 82{ 83 checker::ScopeContext scopeCtx(checker, scope_); 84 85 checker::Type *exprType = discriminant_->Check(checker); 86 bool exprIsLiteral = checker::Checker::IsLiteralType(exprType); 87 88 for (auto *it : cases_) { 89 if (it->Test()) { 90 checker::Type *caseType = it->Test()->Check(checker); 91 bool caseIsLiteral = checker::Checker::IsLiteralType(caseType); 92 checker::Type *comparedExprType = exprType; 93 94 if (!caseIsLiteral || !exprIsLiteral) { 95 caseType = caseIsLiteral ? checker->GetBaseTypeOfLiteralType(caseType) : caseType; 96 comparedExprType = checker->GetBaseTypeOfLiteralType(exprType); 97 } 98 99 if (!checker->IsTypeEqualityComparableTo(comparedExprType, caseType) && 100 !checker->IsTypeComparableTo(caseType, comparedExprType)) { 101 checker->ThrowTypeError({"Type ", caseType, " is not comparable to type ", comparedExprType}, 102 it->Test()->Start()); 103 } 104 } 105 106 for (auto *caseStmt : it->Consequent()) { 107 caseStmt->Check(checker); 108 } 109 } 110 111 return nullptr; 112} 113 114void SwitchStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) 115{ 116 auto scopeCtx = binder::LexicalScope<binder::LocalScope>::Enter(binder, scope_); 117 118 discriminant_ = std::get<ir::AstNode *>(cb(discriminant_))->AsExpression(); 119 120 for (auto iter = cases_.begin(); iter != cases_.end(); iter++) { 121 *iter = std::get<ir::AstNode *>(cb(*iter))->AsSwitchCaseStatement(); 122 } 123} 124 125} // namespace panda::es2panda::ir 126