1 /**
2 * Copyright (c) 2021-2024 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 "switchCaseStatement.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21 #include "checker/ets/typeRelationContext.h"
22 #include "ir/astDump.h"
23 #include "ir/srcDump.h"
24
25 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)26 void SwitchCaseStatement::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
27 {
28 if (test_ != nullptr) {
29 if (auto *transformedNode = cb(test_); test_ != transformedNode) {
30 test_->SetTransformedNode(transformationName, transformedNode);
31 test_ = transformedNode->AsExpression();
32 }
33 }
34
35 for (auto *&it : consequent_) {
36 if (auto *transformedNode = cb(it); it != transformedNode) {
37 it->SetTransformedNode(transformationName, transformedNode);
38 it = transformedNode->AsStatement();
39 }
40 }
41 }
42
Iterate(const NodeTraverser & cb) const43 void SwitchCaseStatement::Iterate(const NodeTraverser &cb) const
44 {
45 if (test_ != nullptr) {
46 cb(test_);
47 }
48
49 for (auto *it : consequent_) {
50 cb(it);
51 }
52 }
53
Dump(ir::AstDumper * dumper) const54 void SwitchCaseStatement::Dump(ir::AstDumper *dumper) const
55 {
56 dumper->Add({{"type", "SwitchCase"}, {"test", AstDumper::Nullish(test_)}, {"consequent", consequent_}});
57 }
58
Dump(ir::SrcDumper * dumper) const59 void SwitchCaseStatement::Dump(ir::SrcDumper *dumper) const
60 {
61 if (test_ != nullptr) {
62 dumper->Add("case ");
63 test_->Dump(dumper);
64 dumper->Add(":");
65 } else {
66 dumper->Add("default:");
67 }
68 if (!consequent_.empty()) {
69 dumper->IncrIndent();
70 dumper->Endl();
71 for (auto cs : consequent_) {
72 cs->Dump(dumper);
73 }
74 dumper->DecrIndent();
75 }
76 }
77
Compile(compiler::PandaGen * pg) const78 void SwitchCaseStatement::Compile(compiler::PandaGen *pg) const
79 {
80 pg->GetAstCompiler()->Compile(this);
81 }
82
Compile(compiler::ETSGen * etsg) const83 void SwitchCaseStatement::Compile(compiler::ETSGen *etsg) const
84 {
85 etsg->GetAstCompiler()->Compile(this);
86 }
87
Check(checker::TSChecker * checker)88 checker::Type *SwitchCaseStatement::Check(checker::TSChecker *checker)
89 {
90 return checker->GetAnalyzer()->Check(this);
91 }
92
Check(checker::ETSChecker * checker)93 checker::Type *SwitchCaseStatement::Check(checker::ETSChecker *checker)
94 {
95 return checker->GetAnalyzer()->Check(this);
96 }
97
98 // Auxilary function extracted from the 'Check' method for 'SwitchStatement' to reduce function's size.
CheckAndTestCase(checker::ETSChecker * checker,checker::Type * comparedExprType,checker::Type * unboxedDiscType,ir::Expression * node,bool & isDefaultCase)99 void SwitchCaseStatement::CheckAndTestCase(checker::ETSChecker *checker, checker::Type *comparedExprType,
100 checker::Type *unboxedDiscType, ir::Expression *node, bool &isDefaultCase)
101 {
102 if (test_ != nullptr) {
103 auto *caseType = test_->Check(checker);
104 bool validCaseType = true;
105
106 if (caseType->HasTypeFlag(checker::TypeFlag::CHAR)) {
107 validCaseType = comparedExprType->HasTypeFlag(checker::TypeFlag::ETS_INTEGRAL);
108 } else if (caseType->IsETSIntEnumType() && comparedExprType->IsETSIntEnumType()) {
109 validCaseType = comparedExprType->AsETSIntEnumType()->IsSameEnumType(caseType->AsETSIntEnumType());
110 } else if (caseType->IsETSStringEnumType() && comparedExprType->IsETSStringEnumType()) {
111 validCaseType = comparedExprType->AsETSStringEnumType()->IsSameEnumType(caseType->AsETSStringEnumType());
112 } else {
113 checker::AssignmentContext(
114 checker->Relation(), node, caseType, unboxedDiscType, test_->Start(),
115 {"Switch case type '", caseType, "' is not comparable to discriminant type '", comparedExprType, "'"},
116 (comparedExprType->IsETSObjectType() ? checker::TypeRelationFlag::NO_WIDENING
117 : checker::TypeRelationFlag::NO_UNBOXING) |
118 checker::TypeRelationFlag::NO_BOXING);
119 }
120
121 if (!validCaseType) {
122 checker->ThrowTypeError(
123 {"Switch case type '", caseType, "' is not comparable to discriminant type '", comparedExprType, "'"},
124 test_->Start());
125 }
126 } else {
127 isDefaultCase = true;
128 }
129
130 for (auto *caseStmt : consequent_) {
131 caseStmt->Check(checker);
132 }
133 }
134 } // namespace ark::es2panda::ir
135