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 "binaryExpression.h"
17
18 #include "compiler/core/pandagen.h"
19 #include "compiler/core/ETSGen.h"
20 #include "checker/TSchecker.h"
21 #include "ir/astDump.h"
22 #include "ir/srcDump.h"
23
24 namespace panda::es2panda::ir {
TransformChildren(const NodeTransformer & cb)25 void BinaryExpression::TransformChildren(const NodeTransformer &cb)
26 {
27 left_ = cb(left_)->AsExpression();
28 right_ = cb(right_)->AsExpression();
29 }
30
Iterate(const NodeTraverser & cb) const31 void BinaryExpression::Iterate(const NodeTraverser &cb) const
32 {
33 cb(left_);
34 cb(right_);
35 }
36
Dump(ir::AstDumper * dumper) const37 void BinaryExpression::Dump(ir::AstDumper *dumper) const
38 {
39 dumper->Add({{"type", IsLogical() ? "LogicalExpression" : "BinaryExpression"},
40 {"operator", operator_},
41 {"left", left_},
42 {"right", right_}});
43 }
44
Dump(ir::SrcDumper * dumper) const45 void BinaryExpression::Dump(ir::SrcDumper *dumper) const
46 {
47 ASSERT(left_ != nullptr);
48 ASSERT(right_ != nullptr);
49 dumper->Add("(");
50 left_->Dump(dumper);
51 dumper->Add(" ");
52 dumper->Add(TokenToString(operator_));
53 dumper->Add(" ");
54 right_->Dump(dumper);
55 dumper->Add(")");
56 }
57
Compile(compiler::PandaGen * pg) const58 void BinaryExpression::Compile(compiler::PandaGen *pg) const
59 {
60 pg->GetAstCompiler()->Compile(this);
61 }
62
Compile(compiler::ETSGen * etsg) const63 void BinaryExpression::Compile(compiler::ETSGen *etsg) const
64 {
65 etsg->GetAstCompiler()->Compile(this);
66 }
67
Check(checker::TSChecker * checker)68 checker::Type *BinaryExpression::Check(checker::TSChecker *checker)
69 {
70 return checker->GetAnalyzer()->Check(this);
71 }
72
Check(checker::ETSChecker * checker)73 checker::Type *BinaryExpression::Check(checker::ETSChecker *checker)
74 {
75 return checker->GetAnalyzer()->Check(this);
76 }
77
78 // NOLINTNEXTLINE(google-default-arguments)
Clone(ArenaAllocator * const allocator,AstNode * const parent)79 BinaryExpression *BinaryExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
80 {
81 auto *const left = left_ != nullptr ? left_->Clone(allocator)->AsExpression() : nullptr;
82 auto *const right = right_ != nullptr ? right_->Clone(allocator)->AsExpression() : nullptr;
83
84 if (auto *const clone = allocator->New<BinaryExpression>(left, right, operator_); clone != nullptr) {
85 if (operationType_ != nullptr) {
86 clone->SetOperationType(operationType_);
87 }
88 if (right != nullptr) {
89 right->SetParent(clone);
90 }
91 if (left != nullptr) {
92 left->SetParent(clone);
93 }
94 if (parent != nullptr) {
95 clone->SetParent(parent);
96 }
97 return clone;
98 }
99
100 throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
101 }
102 } // namespace panda::es2panda::ir
103