13af6ab5fSopenharmony_ci/**
23af6ab5fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#include "switchStatement.h"
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ci#include <binder/binder.h>
193af6ab5fSopenharmony_ci#include <binder/scope.h>
203af6ab5fSopenharmony_ci#include <compiler/core/labelTarget.h>
213af6ab5fSopenharmony_ci#include <compiler/core/switchBuilder.h>
223af6ab5fSopenharmony_ci#include <compiler/core/pandagen.h>
233af6ab5fSopenharmony_ci#include <typescript/checker.h>
243af6ab5fSopenharmony_ci#include <ir/astDump.h>
253af6ab5fSopenharmony_ci#include <ir/expression.h>
263af6ab5fSopenharmony_ci#include <ir/statements/switchCaseStatement.h>
273af6ab5fSopenharmony_ci
283af6ab5fSopenharmony_cinamespace panda::es2panda::ir {
293af6ab5fSopenharmony_ci
303af6ab5fSopenharmony_civoid SwitchStatement::Iterate(const NodeTraverser &cb) const
313af6ab5fSopenharmony_ci{
323af6ab5fSopenharmony_ci    cb(discriminant_);
333af6ab5fSopenharmony_ci
343af6ab5fSopenharmony_ci    for (auto *it : cases_) {
353af6ab5fSopenharmony_ci        cb(it);
363af6ab5fSopenharmony_ci    }
373af6ab5fSopenharmony_ci}
383af6ab5fSopenharmony_ci
393af6ab5fSopenharmony_civoid SwitchStatement::Dump(ir::AstDumper *dumper) const
403af6ab5fSopenharmony_ci{
413af6ab5fSopenharmony_ci    dumper->Add({{"type", "SwitchStatement"}, {"discriminant", discriminant_}, {"cases", cases_}});
423af6ab5fSopenharmony_ci}
433af6ab5fSopenharmony_ci
443af6ab5fSopenharmony_civoid SwitchStatement::Compile(compiler::PandaGen *pg) const
453af6ab5fSopenharmony_ci{
463af6ab5fSopenharmony_ci    compiler::SwitchBuilder builder(pg, this);
473af6ab5fSopenharmony_ci    compiler::VReg tag = pg->AllocReg();
483af6ab5fSopenharmony_ci
493af6ab5fSopenharmony_ci    builder.CompileTagOfSwitch(tag);
503af6ab5fSopenharmony_ci
513af6ab5fSopenharmony_ci    compiler::LocalRegScope lrs(pg, scope_);
523af6ab5fSopenharmony_ci    uint32_t defaultIndex = 0;
533af6ab5fSopenharmony_ci
543af6ab5fSopenharmony_ci    if (cases_.size() == 0) {
553af6ab5fSopenharmony_ci        return;
563af6ab5fSopenharmony_ci    }
573af6ab5fSopenharmony_ci
583af6ab5fSopenharmony_ci    for (size_t i = 0; i < cases_.size(); i++) {
593af6ab5fSopenharmony_ci        const auto *clause = cases_[i];
603af6ab5fSopenharmony_ci
613af6ab5fSopenharmony_ci        if (!clause->Test()) {
623af6ab5fSopenharmony_ci            defaultIndex = i;
633af6ab5fSopenharmony_ci            continue;
643af6ab5fSopenharmony_ci        }
653af6ab5fSopenharmony_ci
663af6ab5fSopenharmony_ci        builder.JumpIfCase(tag, i);
673af6ab5fSopenharmony_ci    }
683af6ab5fSopenharmony_ci
693af6ab5fSopenharmony_ci    if (!cases_[defaultIndex]->Test()) {
703af6ab5fSopenharmony_ci        builder.JumpToDefault(defaultIndex);
713af6ab5fSopenharmony_ci    } else {
723af6ab5fSopenharmony_ci        builder.Break();
733af6ab5fSopenharmony_ci    }
743af6ab5fSopenharmony_ci
753af6ab5fSopenharmony_ci    for (size_t i = 0; i < cases_.size(); i++) {
763af6ab5fSopenharmony_ci        builder.SetCaseTarget(i);
773af6ab5fSopenharmony_ci        builder.CompileCaseStatements(i);
783af6ab5fSopenharmony_ci    }
793af6ab5fSopenharmony_ci}
803af6ab5fSopenharmony_ci
813af6ab5fSopenharmony_cichecker::Type *SwitchStatement::Check(checker::Checker *checker) const
823af6ab5fSopenharmony_ci{
833af6ab5fSopenharmony_ci    checker::ScopeContext scopeCtx(checker, scope_);
843af6ab5fSopenharmony_ci
853af6ab5fSopenharmony_ci    checker::Type *exprType = discriminant_->Check(checker);
863af6ab5fSopenharmony_ci    bool exprIsLiteral = checker::Checker::IsLiteralType(exprType);
873af6ab5fSopenharmony_ci
883af6ab5fSopenharmony_ci    for (auto *it : cases_) {
893af6ab5fSopenharmony_ci        if (it->Test()) {
903af6ab5fSopenharmony_ci            checker::Type *caseType = it->Test()->Check(checker);
913af6ab5fSopenharmony_ci            bool caseIsLiteral = checker::Checker::IsLiteralType(caseType);
923af6ab5fSopenharmony_ci            checker::Type *comparedExprType = exprType;
933af6ab5fSopenharmony_ci
943af6ab5fSopenharmony_ci            if (!caseIsLiteral || !exprIsLiteral) {
953af6ab5fSopenharmony_ci                caseType = caseIsLiteral ? checker->GetBaseTypeOfLiteralType(caseType) : caseType;
963af6ab5fSopenharmony_ci                comparedExprType = checker->GetBaseTypeOfLiteralType(exprType);
973af6ab5fSopenharmony_ci            }
983af6ab5fSopenharmony_ci
993af6ab5fSopenharmony_ci            if (!checker->IsTypeEqualityComparableTo(comparedExprType, caseType) &&
1003af6ab5fSopenharmony_ci                !checker->IsTypeComparableTo(caseType, comparedExprType)) {
1013af6ab5fSopenharmony_ci                checker->ThrowTypeError({"Type ", caseType, " is not comparable to type ", comparedExprType},
1023af6ab5fSopenharmony_ci                                        it->Test()->Start());
1033af6ab5fSopenharmony_ci            }
1043af6ab5fSopenharmony_ci        }
1053af6ab5fSopenharmony_ci
1063af6ab5fSopenharmony_ci        for (auto *caseStmt : it->Consequent()) {
1073af6ab5fSopenharmony_ci            caseStmt->Check(checker);
1083af6ab5fSopenharmony_ci        }
1093af6ab5fSopenharmony_ci    }
1103af6ab5fSopenharmony_ci
1113af6ab5fSopenharmony_ci    return nullptr;
1123af6ab5fSopenharmony_ci}
1133af6ab5fSopenharmony_ci
1143af6ab5fSopenharmony_civoid SwitchStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
1153af6ab5fSopenharmony_ci{
1163af6ab5fSopenharmony_ci    auto scopeCtx = binder::LexicalScope<binder::LocalScope>::Enter(binder, scope_);
1173af6ab5fSopenharmony_ci
1183af6ab5fSopenharmony_ci    discriminant_ = std::get<ir::AstNode *>(cb(discriminant_))->AsExpression();
1193af6ab5fSopenharmony_ci
1203af6ab5fSopenharmony_ci    for (auto iter = cases_.begin(); iter != cases_.end(); iter++) {
1213af6ab5fSopenharmony_ci        *iter = std::get<ir::AstNode *>(cb(*iter))->AsSwitchCaseStatement();
1223af6ab5fSopenharmony_ci    }
1233af6ab5fSopenharmony_ci}
1243af6ab5fSopenharmony_ci
1253af6ab5fSopenharmony_ci}  // namespace panda::es2panda::ir
126