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