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 "forUpdateStatement.h"
17
18 #include "compiler/base/condition.h"
19 #include "compiler/base/lreference.h"
20 #include "compiler/core/labelTarget.h"
21 #include "compiler/core/pandagen.h"
22 #include "compiler/core/ETSGen.h"
23 #include "checker/TSchecker.h"
24 #include "ir/astDump.h"
25 #include "ir/srcDump.h"
26
27 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)28 void ForUpdateStatement::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
29 {
30 if (init_ != nullptr) {
31 if (auto *transformedNode = cb(init_); init_ != transformedNode) {
32 init_->SetTransformedNode(transformationName, transformedNode);
33 init_ = transformedNode;
34 }
35 }
36
37 if (test_ != nullptr) {
38 if (auto *transformedNode = cb(test_); test_ != transformedNode) {
39 test_->SetTransformedNode(transformationName, transformedNode);
40 test_ = transformedNode->AsExpression();
41 }
42 }
43
44 if (update_ != nullptr) {
45 if (auto *transformedNode = cb(update_); update_ != transformedNode) {
46 update_->SetTransformedNode(transformationName, transformedNode);
47 update_ = transformedNode->AsExpression();
48 }
49 }
50
51 if (auto *transformedNode = cb(body_); body_ != transformedNode) {
52 body_->SetTransformedNode(transformationName, transformedNode);
53 body_ = transformedNode->AsStatement();
54 }
55 }
56
Iterate(const NodeTraverser & cb) const57 void ForUpdateStatement::Iterate(const NodeTraverser &cb) const
58 {
59 if (init_ != nullptr) {
60 cb(init_);
61 }
62 if (test_ != nullptr) {
63 cb(test_);
64 }
65 if (update_ != nullptr) {
66 cb(update_);
67 }
68
69 cb(body_);
70 }
71
Dump(ir::AstDumper * dumper) const72 void ForUpdateStatement::Dump(ir::AstDumper *dumper) const
73 {
74 dumper->Add({{"type", "ForUpdateStatement"},
75 {"init", AstDumper::Nullish(init_)},
76 {"test", AstDumper::Nullish(test_)},
77 {"update", AstDumper::Nullish(update_)},
78 {"body", body_}});
79 }
80
Dump(ir::SrcDumper * dumper) const81 void ForUpdateStatement::Dump(ir::SrcDumper *dumper) const
82 {
83 dumper->Add("for ");
84 dumper->Add("(");
85 if (init_ != nullptr) {
86 init_->Dump(dumper);
87 }
88 dumper->Add(";");
89 if (test_ != nullptr) {
90 test_->Dump(dumper);
91 }
92 dumper->Add(";");
93 if (update_ != nullptr) {
94 update_->Dump(dumper);
95 }
96 dumper->Add(") ");
97 dumper->Add("{");
98 if (body_ != nullptr) {
99 dumper->IncrIndent();
100 dumper->Endl();
101 body_->Dump(dumper);
102 dumper->DecrIndent();
103 dumper->Endl();
104 }
105 dumper->Add("}");
106 }
107
Compile(compiler::PandaGen * pg) const108 void ForUpdateStatement::Compile(compiler::PandaGen *pg) const
109 {
110 pg->GetAstCompiler()->Compile(this);
111 }
112
Compile(compiler::ETSGen * etsg) const113 void ForUpdateStatement::Compile(compiler::ETSGen *etsg) const
114 {
115 etsg->GetAstCompiler()->Compile(this);
116 }
117
Check(checker::TSChecker * checker)118 checker::Type *ForUpdateStatement::Check(checker::TSChecker *checker)
119 {
120 return checker->GetAnalyzer()->Check(this);
121 }
122
Check(checker::ETSChecker * checker)123 checker::VerifiedType ForUpdateStatement::Check(checker::ETSChecker *checker)
124 {
125 return {this, checker->GetAnalyzer()->Check(this)};
126 }
127
Clone(ArenaAllocator * const allocator,AstNode * const parent)128 ForUpdateStatement *ForUpdateStatement::Clone(ArenaAllocator *const allocator, AstNode *const parent)
129 {
130 ir::AstNode *init = nullptr;
131 if (init_ != nullptr) {
132 init = init_->Clone(allocator, nullptr);
133 }
134
135 ir::Expression *test = nullptr;
136 if (test_ != nullptr) {
137 test = test_->Clone(allocator, nullptr)->AsExpression();
138 }
139
140 ir::Expression *update = nullptr;
141 if (update_ != nullptr) {
142 update = update_->Clone(allocator, nullptr)->AsExpression();
143 }
144 auto *const body = body_->Clone(allocator, nullptr)->AsStatement();
145 auto *const clone = util::NodeAllocator::ForceSetParent<ForUpdateStatement>(allocator, init, test, update, body);
146
147 if (parent != nullptr) {
148 clone->SetParent(parent);
149 }
150
151 clone->SetRange(Range());
152 ES2PANDA_ASSERT(clone->Scope() == nullptr);
153 return clone;
154 }
155 } // namespace ark::es2panda::ir
156