• 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 "doWhileStatement.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 DoWhileStatement::Iterate(const NodeTraverser &cb) const
26 {
27     cb(body_);
28     cb(test_);
29 }
30 
Dump(ir::AstDumper * dumper) const31 void DoWhileStatement::Dump(ir::AstDumper *dumper) const
32 {
33     dumper->Add({{"type", "DoWhileStatement"}, {"body", body_}, {"test", test_}});
34 }
35 
Compile(compiler::PandaGen * pg) const36 void DoWhileStatement::Compile(compiler::PandaGen *pg) const
37 {
38     auto *startLabel = pg->AllocLabel();
39     compiler::LabelTarget labelTarget(pg);
40 
41     pg->SetLabel(this, startLabel);
42 
43     {
44         compiler::LoopEnvScope envScope(pg, labelTarget, scope_);
45         body_->Compile(pg);
46     }
47 
48     pg->SetLabel(this, labelTarget.ContinueTarget());
49     compiler::Condition::Compile(pg, this->Test(), labelTarget.BreakTarget());
50 
51     pg->Branch(this, startLabel);
52     pg->SetLabel(this, labelTarget.BreakTarget());
53 }
54 
Check(checker::Checker * checker) const55 checker::Type *DoWhileStatement::Check(checker::Checker *checker) const
56 {
57     checker::ScopeContext scopeCtx(checker, scope_);
58 
59     checker::Type *testType = test_->Check(checker);
60     checker->CheckTruthinessOfType(testType, this->Start());
61     body_->Check(checker);
62 
63     return nullptr;
64 }
65 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)66 void DoWhileStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
67 {
68     {
69         auto loopScopeCtx = binder::LexicalScope<binder::LoopScope>::Enter(binder, scope_);
70         body_ = UpdateChildStatement(cb, binder, body_);
71     }
72     test_ = std::get<ir::AstNode *>(cb(test_))->AsExpression();
73 }
74 
75 }  // namespace panda::es2panda::ir
76