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
28 namespace panda::es2panda::ir {
29
Iterate(const NodeTraverser & cb) const30 void SwitchStatement::Iterate(const NodeTraverser &cb) const
31 {
32 cb(discriminant_);
33
34 for (auto *it : cases_) {
35 cb(it);
36 }
37 }
38
Dump(ir::AstDumper * dumper) const39 void SwitchStatement::Dump(ir::AstDumper *dumper) const
40 {
41 dumper->Add({{"type", "SwitchStatement"}, {"discriminant", discriminant_}, {"cases", cases_}});
42 }
43
Compile(compiler::PandaGen * pg) const44 void 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 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 (defaultIndex > 0) {
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