• 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 "ifStatement.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 #include <ir/expression.h>
23 
24 namespace panda::es2panda::ir {
25 
Iterate(const NodeTraverser & cb) const26 void IfStatement::Iterate(const NodeTraverser &cb) const
27 {
28     cb(test_);
29     cb(consequent_);
30 
31     if (alternate_) {
32         cb(alternate_);
33     }
34 }
35 
Dump(ir::AstDumper * dumper) const36 void IfStatement::Dump(ir::AstDumper *dumper) const
37 {
38     dumper->Add({{"type", "IfStatement"},
39                  {"test", test_},
40                  {"consequent", consequent_},
41                  {"alternate", AstDumper::Nullable(alternate_)}});
42 }
43 
Compile(compiler::PandaGen * pg) const44 void IfStatement::Compile(compiler::PandaGen *pg) const
45 {
46     auto *consequentEnd = pg->AllocLabel();
47     compiler::Label *statementEnd = consequentEnd;
48 
49     compiler::Condition::Compile(pg, test_, consequentEnd);
50     consequent_->Compile(pg);
51 
52     if (alternate_) {
53         statementEnd = pg->AllocLabel();
54         pg->Branch(pg->Insns().back()->Node(), statementEnd);
55 
56         pg->SetLabel(this, consequentEnd);
57         alternate_->Compile(pg);
58     }
59 
60     pg->SetLabel(this, statementEnd);
61 }
62 
Check(checker::Checker * checker) const63 checker::Type *IfStatement::Check(checker::Checker *checker) const
64 {
65     checker::Type *testType = test_->Check(checker);
66     checker->CheckTruthinessOfType(testType, Start());
67     checker->CheckTestingKnownTruthyCallableOrAwaitableType(test_, testType, consequent_);
68 
69     consequent_->Check(checker);
70 
71     if (alternate_) {
72         alternate_->Check(checker);
73     }
74 
75     return nullptr;
76 }
77 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)78 void IfStatement::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
79 {
80     test_ = std::get<ir::AstNode *>(cb(test_))->AsExpression();
81     consequent_ = std::get<ir::AstNode *>(cb(consequent_))->AsStatement();
82 
83     if (alternate_) {
84         alternate_ = std::get<ir::AstNode *>(cb(alternate_))->AsStatement();
85     }
86 }
87 
88 }  // namespace panda::es2panda::ir
89