1 /**
2 * Copyright (c) 2021 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/core/pandagen.h>
20 #include <typescript/checker.h>
21 #include <ir/astDump.h>
22
23 namespace panda::es2panda::ir {
24
Iterate(const NodeTraverser & cb) const25 void ForUpdateStatement::Iterate(const NodeTraverser &cb) const
26 {
27 if (init_) {
28 cb(init_);
29 }
30 if (test_) {
31 cb(test_);
32 }
33 if (update_) {
34 cb(update_);
35 }
36
37 cb(body_);
38 }
39
Dump(ir::AstDumper * dumper) const40 void ForUpdateStatement::Dump(ir::AstDumper *dumper) const
41 {
42 dumper->Add({{"type", "ForUpdateStatement"},
43 {"init", AstDumper::Nullable(init_)},
44 {"test", AstDumper::Nullable(test_)},
45 {"update", AstDumper::Nullable(update_)},
46 {"body", body_}});
47 }
48
Compile(compiler::PandaGen * pg) const49 void ForUpdateStatement::Compile(compiler::PandaGen *pg) const
50 {
51 compiler::LocalRegScope loopRegScope(pg, scope_);
52 compiler::LabelTarget labelTarget(pg);
53 compiler::LoopEnvScope envScope(pg, labelTarget, scope_);
54
55 if (init_) {
56 ASSERT(init_->IsVariableDeclaration() || init_->IsExpression());
57 init_->Compile(pg);
58 }
59
60 auto *startLabel = pg->AllocLabel();
61 pg->SetLabel(this, startLabel);
62
63 {
64 if (test_) {
65 compiler::Condition::Compile(pg, test_, labelTarget.BreakTarget());
66 }
67
68 body_->Compile(pg);
69 pg->SetLabel(this, labelTarget.ContinueTarget());
70 envScope.CopyPerIterationCtx();
71 }
72
73 if (update_) {
74 update_->Compile(pg);
75 }
76
77 pg->Branch(this, startLabel);
78 pg->SetLabel(this, labelTarget.BreakTarget());
79 }
80
Check(checker::Checker * checker) const81 checker::Type *ForUpdateStatement::Check(checker::Checker *checker) const
82 {
83 checker::ScopeContext scopeCtx(checker, scope_);
84
85 if (init_) {
86 init_->Check(checker);
87 }
88
89 if (test_) {
90 checker::Type *testType = test_->Check(checker);
91 checker->CheckTruthinessOfType(testType, Start());
92 }
93
94 if (update_) {
95 update_->Check(checker);
96 }
97
98 body_->Check(checker);
99
100 return nullptr;
101 }
102
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)103 void ForUpdateStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
104 {
105 auto *loopScope = Scope();
106 auto loopCtx = binder::LexicalScope<binder::LoopScope>::Enter(binder, loopScope);
107
108 if (init_) {
109 init_ = std::get<ir::AstNode *>(cb(init_));
110 }
111
112 if (test_) {
113 test_ = std::get<ir::AstNode *>(cb(test_))->AsExpression();
114 }
115
116 if (update_) {
117 update_ = std::get<ir::AstNode *>(cb(update_))->AsExpression();
118 }
119
120 body_ = UpdateChildStatement(cb, binder, body_);
121 }
122
123 } // namespace panda::es2panda::ir
124