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