1 /**
2 * Copyright (c) 2021-2025 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
22 namespace ark::es2panda::ir {
TransformChildren(const NodeTransformer & cb,std::string_view const transformationName)23 void BinaryExpression::TransformChildren(const NodeTransformer &cb, std::string_view const transformationName)
24 {
25 if (auto *transformedNode = cb(left_); left_ != transformedNode) {
26 left_->SetTransformedNode(transformationName, transformedNode);
27 left_ = transformedNode->AsExpression();
28 }
29
30 if (auto *transformedNode = cb(right_); right_ != transformedNode) {
31 right_->SetTransformedNode(transformationName, transformedNode);
32 right_ = transformedNode->AsExpression();
33 }
34 }
35
Iterate(const NodeTraverser & cb) const36 void BinaryExpression::Iterate(const NodeTraverser &cb) const
37 {
38 cb(left_);
39 cb(right_);
40 }
41
Dump(ir::AstDumper * dumper) const42 void BinaryExpression::Dump(ir::AstDumper *dumper) const
43 {
44 dumper->Add({{"type", IsLogical() ? "LogicalExpression" : "BinaryExpression"},
45 {"operator", operator_},
46 {"left", left_},
47 {"right", right_}});
48 }
49
Dump(ir::SrcDumper * dumper) const50 void BinaryExpression::Dump(ir::SrcDumper *dumper) const
51 {
52 ES2PANDA_ASSERT(left_ != nullptr);
53 ES2PANDA_ASSERT(right_ != nullptr);
54 dumper->Add("((");
55 left_->Dump(dumper);
56 dumper->Add(") ");
57 dumper->Add(TokenToString(operator_));
58 dumper->Add(" (");
59 right_->Dump(dumper);
60 dumper->Add("))");
61 }
62
Compile(compiler::PandaGen * pg) const63 void BinaryExpression::Compile(compiler::PandaGen *pg) const
64 {
65 pg->GetAstCompiler()->Compile(this);
66 }
67
Compile(compiler::ETSGen * etsg) const68 void BinaryExpression::Compile(compiler::ETSGen *etsg) const
69 {
70 etsg->GetAstCompiler()->Compile(this);
71 }
72
CompileOperands(compiler::ETSGen * etsg,compiler::VReg lhs) const73 void BinaryExpression::CompileOperands(compiler::ETSGen *etsg, compiler::VReg lhs) const
74 {
75 left_->Compile(etsg);
76
77 if (operator_ == lexer::TokenType::KEYW_INSTANCEOF) {
78 etsg->StoreAccumulator(left_, lhs);
79 } else {
80 etsg->ApplyConversionAndStoreAccumulator(left_, lhs, operationType_);
81 }
82
83 right_->Compile(etsg);
84 etsg->ApplyConversion(right_, operationType_);
85 }
86
Check(checker::TSChecker * checker)87 checker::Type *BinaryExpression::Check(checker::TSChecker *checker)
88 {
89 return checker->GetAnalyzer()->Check(this);
90 }
91
Check(checker::ETSChecker * checker)92 checker::VerifiedType BinaryExpression::Check(checker::ETSChecker *checker)
93 {
94 return {this, checker->GetAnalyzer()->Check(this)};
95 }
96
Clone(ArenaAllocator * const allocator,AstNode * const parent)97 BinaryExpression *BinaryExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
98 {
99 auto *const left = left_ != nullptr ? left_->Clone(allocator, nullptr)->AsExpression() : nullptr;
100 auto *const right = right_ != nullptr ? right_->Clone(allocator, nullptr)->AsExpression() : nullptr;
101 auto *const clone = allocator->New<BinaryExpression>(left, right, operator_);
102 ES2PANDA_ASSERT(clone);
103
104 if (operationType_ != nullptr) {
105 clone->SetOperationType(operationType_);
106 }
107
108 if (right != nullptr) {
109 right->SetParent(clone);
110 }
111
112 if (left != nullptr) {
113 left->SetParent(clone);
114 }
115
116 if (parent != nullptr) {
117 clone->SetParent(parent);
118 }
119
120 clone->SetRange(Range());
121 return clone;
122 }
123 } // namespace ark::es2panda::ir
124