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