• 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 "whileStatement.h"
17 
18 #include <compiler/base/condition.h>
19 #include <compiler/core/pandagen.h>
20 #include <typescript/checker.h>
21 #include <ir/astDump.h>
22 
23 namespace panda::es2panda::ir {
24 
Iterate(const NodeTraverser & cb) const25 void WhileStatement::Iterate(const NodeTraverser &cb) const
26 {
27     cb(test_);
28     cb(body_);
29 }
30 
Dump(ir::AstDumper * dumper) const31 void WhileStatement::Dump(ir::AstDumper *dumper) const
32 {
33     dumper->Add({{"type", "WhileStatement"}, {"test", test_}, {"body", body_}});
34 }
35 
Compile(compiler::PandaGen * pg) const36 void WhileStatement::Compile(compiler::PandaGen *pg) const
37 {
38     compiler::LabelTarget labelTarget(pg);
39 
40     pg->SetLabel(this, labelTarget.ContinueTarget());
41     compiler::Condition::Compile(pg, test_, labelTarget.BreakTarget());
42 
43     {
44         compiler::LoopEnvScope envScope(pg, labelTarget, scope_);
45         body_->Compile(pg);
46     }
47 
48     pg->Branch(this, labelTarget.ContinueTarget());
49     pg->SetLabel(this, labelTarget.BreakTarget());
50 }
51 
Check(checker::Checker * checker) const52 checker::Type *WhileStatement::Check(checker::Checker *checker) const
53 {
54     checker::ScopeContext scopeCtx(checker, scope_);
55 
56     checker::Type *testType = test_->Check(checker);
57     checker->CheckTruthinessOfType(testType, Start());
58 
59     body_->Check(checker);
60 
61     return nullptr;
62 }
63 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)64 void WhileStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
65 {
66     test_ = std::get<ir::AstNode *>(cb(test_))->AsExpression();
67 
68     auto loopScopeCtx = binder::LexicalScope<binder::LoopScope>::Enter(binder, scope_);
69     body_ = UpdateChildStatement(cb, binder, body_);
70 }
71 
72 }  // namespace panda::es2panda::ir
73