• 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 "whileStatement.h"
17 
18 #include "compiler/base/condition.h"
19 #include "compiler/core/labelTarget.h"
20 #include "compiler/core/pandagen.h"
21 #include "compiler/core/ETSGen.h"
22 #include "compiler/core/regScope.h"
23 #include "checker/TSchecker.h"
24 #include "ir/astDump.h"
25 #include "ir/srcDump.h"
26 #include "ir/expression.h"
27 
28 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view transformationName)29 void WhileStatement::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
30 {
31     if (auto *transformedNode = cb(test_); test_ != transformedNode) {
32         test_->SetTransformedNode(transformationName, transformedNode);
33         test_ = transformedNode->AsExpression();
34     }
35 
36     if (auto *transformedNode = cb(body_); body_ != transformedNode) {
37         body_->SetTransformedNode(transformationName, transformedNode);
38         body_ = transformedNode->AsStatement();
39     }
40 }
41 
Iterate(const NodeTraverser & cb) const42 void WhileStatement::Iterate(const NodeTraverser &cb) const
43 {
44     cb(test_);
45     cb(body_);
46 }
47 
Dump(ir::AstDumper * dumper) const48 void WhileStatement::Dump(ir::AstDumper *dumper) const
49 {
50     dumper->Add({{"type", "WhileStatement"}, {"test", test_}, {"body", body_}});
51 }
52 
Dump(ir::SrcDumper * dumper) const53 void WhileStatement::Dump(ir::SrcDumper *dumper) const
54 {
55     dumper->Add("while (");
56     if (test_ != nullptr) {
57         test_->Dump(dumper);
58     }
59     dumper->Add(") {");
60     if (body_ != nullptr) {
61         dumper->IncrIndent();
62         dumper->Endl();
63         body_->Dump(dumper);
64         dumper->DecrIndent();
65         dumper->Endl();
66     }
67     dumper->Add("}");
68 }
69 
Compile(compiler::PandaGen * pg) const70 void WhileStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const
71 {
72     pg->GetAstCompiler()->Compile(this);
73 }
74 
Compile(compiler::ETSGen * etsg) const75 void WhileStatement::Compile([[maybe_unused]] compiler::ETSGen *etsg) const
76 {
77     etsg->GetAstCompiler()->Compile(this);
78 }
79 
Check(checker::TSChecker * checker)80 checker::Type *WhileStatement::Check([[maybe_unused]] checker::TSChecker *checker)
81 {
82     return checker->GetAnalyzer()->Check(this);
83 }
84 
Check(checker::ETSChecker * checker)85 checker::VerifiedType WhileStatement::Check([[maybe_unused]] checker::ETSChecker *checker)
86 {
87     return {this, checker->GetAnalyzer()->Check(this)};
88 }
89 
Clone(ArenaAllocator * const allocator,AstNode * const parent)90 WhileStatement *WhileStatement::Clone(ArenaAllocator *const allocator, AstNode *const parent)
91 {
92     auto *const test = test_->Clone(allocator, nullptr)->AsExpression();
93     auto *const body = body_->Clone(allocator, nullptr)->AsStatement();
94     auto *const clone = util::NodeAllocator::ForceSetParent<WhileStatement>(allocator, test, body);
95 
96     if (parent != nullptr) {
97         clone->SetParent(parent);
98     }
99 
100     clone->SetRange(Range());
101     ES2PANDA_ASSERT(clone->Scope() == nullptr);
102     return clone;
103 }
104 }  // namespace ark::es2panda::ir
105