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