• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2025 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 : VectorIterationGuard(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 : VectorIterationGuard(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->Add(" {");
70         dumper->IncrIndent();
71         dumper->Endl();
72         for (auto cs : consequent_) {
73             cs->Dump(dumper);
74             if (!cs->IsBlockStatement() && cs != consequent_.back()) {
75                 dumper->Endl();
76             }
77         }
78         dumper->DecrIndent();
79         dumper->Endl();
80         dumper->Add("}");
81     }
82 }
83 
Compile(compiler::PandaGen * pg) const84 void SwitchCaseStatement::Compile(compiler::PandaGen *pg) const
85 {
86     pg->GetAstCompiler()->Compile(this);
87 }
88 
Compile(compiler::ETSGen * etsg) const89 void SwitchCaseStatement::Compile(compiler::ETSGen *etsg) const
90 {
91     etsg->GetAstCompiler()->Compile(this);
92 }
93 
Check(checker::TSChecker * checker)94 checker::Type *SwitchCaseStatement::Check(checker::TSChecker *checker)
95 {
96     return checker->GetAnalyzer()->Check(this);
97 }
98 
Check(checker::ETSChecker * checker)99 checker::VerifiedType SwitchCaseStatement::Check(checker::ETSChecker *checker)
100 {
101     return {this, checker->GetAnalyzer()->Check(this)};
102 }
103 
104 // 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)105 void SwitchCaseStatement::CheckAndTestCase(checker::ETSChecker *checker, checker::Type *comparedExprType,
106                                            checker::Type *unboxedDiscType, ir::Expression *node, bool &isDefaultCase)
107 {
108     if (test_ != nullptr) {
109         auto caseType = test_->Check(checker);
110         bool validCaseType = true;
111 
112         if (caseType->HasTypeFlag(checker::TypeFlag::CHAR)) {
113             validCaseType = comparedExprType->HasTypeFlag(checker::TypeFlag::ETS_INTEGRAL);
114         } else if (caseType->IsETSEnumType() || comparedExprType->IsETSEnumType()) {
115             validCaseType = checker->Relation()->IsIdenticalTo(caseType, comparedExprType);
116         } else {
117             const checker::AssignmentContext ctx {
118                 checker->Relation(),
119                 node,
120                 caseType,
121                 unboxedDiscType,
122                 test_->Start(),
123                 std::nullopt,
124                 (comparedExprType->IsETSObjectType() ? checker::TypeRelationFlag::NO_WIDENING
125                                                      : checker::TypeRelationFlag::NO_UNBOXING) |
126                     checker::TypeRelationFlag::NO_BOXING | checker::TypeRelationFlag::NO_THROW};
127             if (!ctx.IsAssignable()) {
128                 checker->LogError(diagnostic::SWITCH_CASE_TYPE_INCOMPARABLE, {caseType, comparedExprType},
129                                   test_->Start());
130                 return;
131             }
132         }
133 
134         if (!validCaseType) {
135             checker->LogError(diagnostic::SWITCH_CASE_TYPE_INCOMPARABLE, {caseType, comparedExprType}, test_->Start());
136             return;
137         }
138     } else {
139         isDefaultCase = true;
140     }
141 
142     for (auto *caseStmt : consequent_) {
143         caseStmt->Check(checker);
144     }
145 }
146 
Clone(ArenaAllocator * const allocator,AstNode * const parent)147 SwitchCaseStatement *SwitchCaseStatement::Clone(ArenaAllocator *const allocator, AstNode *const parent)
148 {
149     auto *const test = test_->Clone(allocator, nullptr)->AsExpression();
150     ArenaVector<Statement *> consequent(allocator->Adapter());
151 
152     for (auto *statement : consequent_) {
153         consequent.push_back(statement->Clone(allocator, nullptr)->AsStatement());
154     }
155 
156     auto clone = util::NodeAllocator::ForceSetParent<ir::SwitchCaseStatement>(allocator, test, std::move(consequent));
157 
158     clone->SetParent(parent);
159     clone->SetRange(Range());
160     return clone;
161 }
162 
163 }  // namespace ark::es2panda::ir
164