• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "forOfStatement.h"
17 
18 #include <binder/binder.h>
19 #include <binder/scope.h>
20 #include <compiler/base/iterators.h>
21 #include <compiler/base/lreference.h>
22 #include <compiler/core/labelTarget.h>
23 #include <compiler/core/pandagen.h>
24 #include <typescript/checker.h>
25 #include <ir/astDump.h>
26 #include <ir/expression.h>
27 
28 namespace panda::es2panda::ir {
29 
Iterate(const NodeTraverser & cb) const30 void ForOfStatement::Iterate(const NodeTraverser &cb) const
31 {
32     cb(left_);
33     cb(right_);
34     cb(body_);
35 }
36 
Dump(ir::AstDumper * dumper) const37 void ForOfStatement::Dump(ir::AstDumper *dumper) const
38 {
39     dumper->Add({{"type", "ForOfStatement"}, {"await", isAwait_}, {"left", left_}, {"right", right_}, {"body", body_}});
40 }
41 
Compile(compiler::PandaGen * pg) const42 void ForOfStatement::Compile(compiler::PandaGen *pg) const
43 {
44     compiler::LocalRegScope regScope(pg, scope_);
45 
46     if (scope_->NeedLexEnv()) {
47         pg->NewLexEnv(this, scope_->LexicalSlots());
48     }
49 
50     right_->Compile(pg); // if poplexenv next, acc will not be affected, no need to store the value to a vreg
51 
52     if (scope_->NeedLexEnv()) {
53         pg->PopLexEnv(this);
54     }
55 
56     compiler::LabelTarget labelTarget(pg);
57     auto iterator_type = isAwait_ ? compiler::IteratorType::ASYNC : compiler::IteratorType::SYNC;
58     compiler::Iterator iterator(pg, this, iterator_type);
59 
60     pg->SetLabel(this, labelTarget.ContinueTarget());
61 
62     iterator.Next();
63     iterator.Complete();
64     pg->BranchIfTrue(this, labelTarget.BreakTarget());
65 
66     compiler::VReg value = pg->AllocReg();
67     iterator.Value();
68     pg->StoreAccumulator(this, value);
69 
70     auto lref = compiler::LReference::CreateLRef(pg, left_, false);
71     {
72         compiler::IteratorContext forOfCtx(pg, iterator, labelTarget);
73         compiler::LoopEnvScope envScope(pg, scope_, {});
74         pg->LoadAccumulator(this, value);
75         lref.SetValue();
76 
77         body_->Compile(pg);
78     }
79 
80     pg->Branch(this, labelTarget.ContinueTarget());
81     pg->SetLabel(this, labelTarget.BreakTarget());
82 }
83 
Check(checker::Checker * checker) const84 checker::Type *ForOfStatement::Check([[maybe_unused]] checker::Checker *checker) const
85 {
86     return nullptr;
87 }
88 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)89 void ForOfStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
90 {
91     auto *loopScope = Scope();
92     auto loopCtx = binder::LexicalScope<binder::LoopScope>::Enter(binder, loopScope);
93     left_ = std::get<ir::AstNode *>(cb(left_));
94     right_ = std::get<ir::AstNode *>(cb(right_))->AsExpression();
95 
96     body_ = std::get<ir::AstNode *>(cb(body_))->AsStatement();
97 }
98 
99 }  // namespace panda::es2panda::ir
100