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 {
48 compiler::LoopEnvScope envScope(pg, labelTarget, scope_);
49 body_->Compile(pg);
50 }
51
52 pg->SetLabel(this, labelTarget.ContinueTarget());
53 compiler::Condition::Compile(pg, this->Test(), labelTarget.BreakTarget());
54
55 pg->Branch(this, startLabel);
56 pg->SetLabel(this, labelTarget.BreakTarget());
57 }
58
Check(checker::Checker *checker) const59 checker::Type *DoWhileStatement::Check(checker::Checker *checker) const
60 {
61 checker::ScopeContext scopeCtx(checker, scope_);
62
63 checker::Type *testType = test_->Check(checker);
64 checker->CheckTruthinessOfType(testType, this->Start());
65 body_->Check(checker);
66
67 return nullptr;
68 }
69
UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)70 void DoWhileStatement::UpdateSelf(const NodeUpdater &cb, binder::Binder *binder)
71 {
72 {
73 auto loopScopeCtx = binder::LexicalScope<binder::LoopScope>::Enter(binder, scope_);
74 body_ = UpdateChildStatement(cb, binder, body_);
75 }
76 test_ = std::get<ir::AstNode *>(cb(test_))->AsExpression();
77 }
78
79 } // namespace panda::es2panda::ir
80