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 "forInStatement.h"
17
18 #include <compiler/base/catchTable.h>
19 #include <compiler/base/lreference.h>
20 #include <compiler/core/pandagen.h>
21
22 #include <ir/astDump.h>
23
24 namespace panda::es2panda::ir {
25
Iterate(const NodeTraverser & cb) const26 void ForInStatement::Iterate(const NodeTraverser &cb) const
27 {
28 cb(left_);
29 cb(right_);
30 cb(body_);
31 }
32
Dump(ir::AstDumper * dumper) const33 void ForInStatement::Dump(ir::AstDumper *dumper) const
34 {
35 dumper->Add({{"type", "ForInStatement"}, {"left", left_}, {"right", right_}, {"body", body_}});
36 }
37
Compile(compiler::PandaGen * pg) const38 void ForInStatement::Compile(compiler::PandaGen *pg) const
39 {
40 if (scope_->NeedLexEnv()) {
41 pg->NewLexEnv(this, scope_->LexicalSlots());
42 }
43
44 compiler::LocalRegScope loopRegScope(pg, scope_);
45 compiler::VReg iter = pg->AllocReg();
46 compiler::VReg propName = pg->AllocReg();
47
48 // create enumerator
49 {
50 compiler::TryContext enumeratorInitTryCtx(pg);
51 const auto &labelSet = enumeratorInitTryCtx.LabelSet();
52 pg->SetLabel(right_, labelSet.TryBegin());
53 right_->Compile(pg);
54 pg->GetPropIterator(this);
55 pg->StoreAccumulator(this, iter);
56 pg->SetLabel(right_, labelSet.TryEnd());
57 pg->Branch(right_, labelSet.CatchEnd());
58
59 pg->SetLabel(right_, labelSet.CatchBegin());
60 compiler::VReg exception = pg->AllocReg();
61 pg->StoreAccumulator(right_, exception);
62 if (scope_->NeedLexEnv()) {
63 pg->PopLexEnv(this);
64 }
65 pg->LoadAccumulator(right_, exception);
66 pg->EmitThrow(right_);
67 pg->SetLabel(right_, labelSet.CatchEnd());
68 }
69
70 if (scope_->NeedLexEnv()) {
71 pg->PopLexEnv(this);
72 }
73
74 compiler::LabelTarget labelTarget(pg);
75 // loop start
76 pg->SetLabel(this, labelTarget.ContinueTarget());
77
78 // get next prop of enumerator
79 pg->GetNextPropName(this, iter);
80 pg->StoreAccumulator(this, propName);
81 pg->BranchIfUndefined(this, labelTarget.BreakTarget());
82
83 auto lref = compiler::LReference::CreateLRef(pg, left_, false);
84 {
85 compiler::LoopEnvScope envScope(pg, scope_, labelTarget);
86 pg->LoadAccumulator(this, propName);
87 lref.SetValue();
88 body_->Compile(pg);
89 }
90
91 pg->Branch(this, labelTarget.ContinueTarget());
92 pg->SetLabel(this, labelTarget.BreakTarget());
93 }
94
Check(checker::Checker * checker) const95 checker::Type *ForInStatement::Check([[maybe_unused]] checker::Checker *checker) const
96 {
97 return nullptr;
98 }
99
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)100 void ForInStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
101 {
102 auto *loopScope = Scope();
103 auto loopCtx = binder::LexicalScope<binder::LoopScope>::Enter(binder, loopScope);
104 left_ = std::get<ir::AstNode *>(cb(left_));
105 right_ = std::get<ir::AstNode *>(cb(right_))->AsExpression();
106
107 body_ = UpdateChildStatement(cb, binder, body_);
108 }
109
110 } // namespace panda::es2panda::ir
111