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 "ir/astDump.h"
21 #include "ir/statements/blockStatement.h"
22 #include "typescript/checker.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, binder::Binder *binder)
79 {
80 test_ = std::get<ir::AstNode *>(cb(test_))->AsExpression();
81 consequent_ = UpdateIfStatementChildStatement(cb, binder, consequent_);
82
83 if (alternate_) {
84 alternate_ = UpdateIfStatementChildStatement(cb, binder, alternate_);
85 }
86 }
87
UpdateIfStatementChildStatement(const NodeUpdater & cb,const binder::Binder * binder,Statement * statement) const88 Statement *IfStatement::UpdateIfStatementChildStatement(const NodeUpdater &cb,
89 const binder::Binder *binder,
90 Statement *statement) const
91 {
92 auto newStatement = cb(statement);
93 if (std::holds_alternative<ir::AstNode *>(newStatement)) {
94 return std::get<ir::AstNode *>(newStatement)->AsStatement();
95 }
96
97 ASSERT(std::holds_alternative<std::vector<ir::AstNode *>>(newStatement));
98 ArenaVector<ir::Statement *> statements(binder->Allocator()->Adapter());
99 auto newStatements = std::get<std::vector<ir::AstNode *>>(newStatement);
100 for (auto *it : newStatements) {
101 statements.push_back(it->AsStatement());
102 }
103 return binder->Allocator()->New<ir::BlockStatement>(nullptr, std::move(statements));
104 }
105
106 } // namespace panda::es2panda::ir
107