• 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 <compiler/base/catchTable.h>
19 #include <compiler/base/lreference.h>
20 #include <compiler/core/pandagen.h>
21 #include <ir/astDump.h>
22 
23 namespace panda::es2panda::ir {
24 
Iterate(const NodeTraverser & cb) const25 void ForOfStatement::Iterate(const NodeTraverser &cb) const
26 {
27     cb(left_);
28     cb(right_);
29     cb(body_);
30 }
31 
Dump(ir::AstDumper * dumper) const32 void ForOfStatement::Dump(ir::AstDumper *dumper) const
33 {
34     dumper->Add({{"type", "ForOfStatement"}, {"await", isAwait_}, {"left", left_}, {"right", right_}, {"body", body_}});
35 }
36 
Compile(compiler::PandaGen * pg) const37 void ForOfStatement::Compile(compiler::PandaGen *pg) const
38 {
39     compiler::LocalRegScope regScope(pg, scope_);
40 
41     if (scope_->NeedLexEnv()) {
42         pg->NewLexEnv(this, scope_->LexicalSlots());
43     }
44 
45     {
46         compiler::TryContext iterInitTryCtx(pg);
47         const auto &labelSet = iterInitTryCtx.LabelSet();
48         pg->SetLabel(right_, labelSet.TryBegin());
49         right_->Compile(pg);
50         pg->SetLabel(right_, labelSet.TryEnd());
51         pg->Branch(right_, labelSet.CatchEnd());
52 
53         pg->SetLabel(right_, labelSet.CatchBegin());
54         compiler::VReg exception = pg->AllocReg();
55         pg->StoreAccumulator(right_, exception);
56         if (scope_->NeedLexEnv()) {
57             pg->PopLexEnv(this);
58         }
59         pg->LoadAccumulator(right_, exception);
60         pg->EmitThrow(right_);
61         pg->SetLabel(right_, labelSet.CatchEnd());
62     }
63 
64     if (scope_->NeedLexEnv()) {
65         pg->PopLexEnv(this);
66     }
67 
68     compiler::LabelTarget labelTarget(pg);
69     auto iterator_type = isAwait_ ? compiler::IteratorType::ASYNC : compiler::IteratorType::SYNC;
70     compiler::Iterator iterator(pg, this, iterator_type);
71 
72     pg->SetLabel(this, labelTarget.ContinueTarget());
73 
74     iterator.Next();
75     iterator.Complete();
76     pg->BranchIfTrue(this, labelTarget.BreakTarget());
77 
78     compiler::VReg value = pg->AllocReg();
79     iterator.Value();
80     pg->StoreAccumulator(this, value);
81 
82     auto lref = compiler::LReference::CreateLRef(pg, left_, false);
83     {
84         compiler::IteratorContext forOfCtx(pg, iterator, labelTarget);
85         compiler::LoopEnvScope envScope(pg, scope_, {});
86         pg->LoadAccumulator(this, value);
87         lref.SetValue();
88 
89         body_->Compile(pg);
90     }
91 
92     pg->Branch(this, labelTarget.ContinueTarget());
93     pg->SetLabel(this, labelTarget.BreakTarget());
94 }
95 
Check(checker::Checker * checker) const96 checker::Type *ForOfStatement::Check([[maybe_unused]] checker::Checker *checker) const
97 {
98     return nullptr;
99 }
100 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)101 void ForOfStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
102 {
103     auto *loopScope = Scope();
104     auto loopCtx = binder::LexicalScope<binder::LoopScope>::Enter(binder, loopScope);
105     left_ = std::get<ir::AstNode *>(cb(left_));
106     right_ = std::get<ir::AstNode *>(cb(right_))->AsExpression();
107 
108     body_ = UpdateChildStatement(cb, binder, body_);
109 }
110 
111 }  // namespace panda::es2panda::ir
112