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 "ifStatement.h"
17
18 #include "checker/TSchecker.h"
19 #include "compiler/core/ETSGen.h"
20 #include "compiler/core/pandagen.h"
21
22 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)23 void IfStatement::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
24 {
25 if (auto *transformedNode = cb(test_); test_ != transformedNode) {
26 test_->SetTransformedNode(transformationName, transformedNode);
27 test_ = transformedNode->AsExpression();
28 }
29
30 if (auto *transformedNode = cb(consequent_); consequent_ != transformedNode) {
31 consequent_->SetTransformedNode(transformationName, transformedNode);
32 consequent_ = transformedNode->AsStatement();
33 }
34
35 if (alternate_ != nullptr) {
36 if (auto *transformedNode = cb(alternate_); alternate_ != transformedNode) {
37 alternate_->SetTransformedNode(transformationName, transformedNode);
38 alternate_ = transformedNode->AsStatement();
39 }
40 }
41 }
42
Iterate(const NodeTraverser & cb) const43 void IfStatement::Iterate(const NodeTraverser &cb) const
44 {
45 cb(test_);
46 cb(consequent_);
47
48 if (alternate_ != nullptr) {
49 cb(alternate_);
50 }
51 }
52
Dump(ir::AstDumper * dumper) const53 void IfStatement::Dump(ir::AstDumper *dumper) const
54 {
55 dumper->Add({{"type", "IfStatement"},
56 {"test", test_},
57 {"consequent", consequent_},
58 {"alternate", AstDumper::Nullish(alternate_)}});
59 }
60
Dump(ir::SrcDumper * dumper) const61 void IfStatement::Dump(ir::SrcDumper *dumper) const
62 {
63 ES2PANDA_ASSERT(test_);
64 ES2PANDA_ASSERT(consequent_ != nullptr);
65 dumper->Add("if (");
66 test_->Dump(dumper);
67 dumper->Add(") {");
68 dumper->IncrIndent();
69 dumper->Endl();
70 consequent_->Dump(dumper);
71 dumper->DecrIndent();
72 dumper->Endl();
73 dumper->Add("}");
74 if (alternate_ != nullptr) {
75 dumper->Add(" else {");
76 dumper->IncrIndent();
77 dumper->Endl();
78 alternate_->Dump(dumper);
79 dumper->DecrIndent();
80 dumper->Endl();
81 dumper->Add("}");
82 }
83 }
84
Compile(compiler::PandaGen * pg) const85 void IfStatement::Compile(compiler::PandaGen *pg) const
86 {
87 pg->GetAstCompiler()->Compile(this);
88 }
89
Compile(compiler::ETSGen * etsg) const90 void IfStatement::Compile(compiler::ETSGen *etsg) const
91 {
92 etsg->GetAstCompiler()->Compile(this);
93 }
94
Check(checker::TSChecker * checker)95 checker::Type *IfStatement::Check([[maybe_unused]] checker::TSChecker *checker)
96 {
97 return checker->GetAnalyzer()->Check(this);
98 }
99
Check(checker::ETSChecker * checker)100 checker::VerifiedType IfStatement::Check([[maybe_unused]] checker::ETSChecker *checker)
101 {
102 return {this, checker->GetAnalyzer()->Check(this)};
103 }
104
Clone(ArenaAllocator * const allocator,AstNode * const parent)105 IfStatement *IfStatement::Clone(ArenaAllocator *const allocator, AstNode *const parent)
106 {
107 auto *const test = test_->Clone(allocator, nullptr)->AsExpression();
108 auto *const consequent = consequent_->Clone(allocator, nullptr)->AsStatement();
109 auto *const alternate = alternate_ != nullptr ? consequent_->Clone(allocator, nullptr)->AsStatement() : nullptr;
110 auto *const clone = allocator->New<IfStatement>(test, consequent, alternate);
111
112 if (parent != nullptr) {
113 clone->SetParent(parent);
114 }
115
116 test->SetParent(clone);
117 consequent->SetParent(clone);
118 if (alternate != nullptr) {
119 alternate->SetParent(clone);
120 }
121
122 clone->SetRange(Range());
123 return clone;
124 }
125 } // namespace ark::es2panda::ir
126