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 "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([[maybe_unused]] compiler::PandaGen *pg) const70 void WhileStatement::Compile([[maybe_unused]] compiler::PandaGen *pg) const
71 {
72 pg->GetAstCompiler()->Compile(this);
73 }
74
Compile([[maybe_unused]] compiler::ETSGen *etsg) const75 void WhileStatement::Compile([[maybe_unused]] compiler::ETSGen *etsg) const
76 {
77 etsg->GetAstCompiler()->Compile(this);
78 }
79
Check([[maybe_unused]] checker::TSChecker *checker)80 checker::Type *WhileStatement::Check([[maybe_unused]] checker::TSChecker *checker)
81 {
82 return checker->GetAnalyzer()->Check(this);
83 }
84
Check([[maybe_unused]] checker::ETSChecker *checker)85 checker::Type *WhileStatement::Check([[maybe_unused]] checker::ETSChecker *checker)
86 {
87 return checker->GetAnalyzer()->Check(this);
88 }
89 } // namespace ark::es2panda::ir
90