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