• 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 "binaryExpression.h"
17 
18 #include <compiler/core/pandagen.h>
19 #include <typescript/checker.h>
20 #include <ir/astDump.h>
21 #include <ir/expressions/privateIdentifier.h>
22 
23 namespace panda::es2panda::ir {
24 
Iterate(const NodeTraverser & cb) const25 void BinaryExpression::Iterate(const NodeTraverser &cb) const
26 {
27     cb(left_);
28     cb(right_);
29 }
30 
Dump(ir::AstDumper * dumper) const31 void BinaryExpression::Dump(ir::AstDumper *dumper) const
32 {
33     dumper->Add({{"type", IsLogical() ? "LogicalExpression" : "BinaryExpression"},
34                  {"operator", operator_},
35                  {"left", left_},
36                  {"right", right_}});
37 }
38 
CompileLogical(compiler::PandaGen * pg) const39 void BinaryExpression::CompileLogical(compiler::PandaGen *pg) const
40 {
41     compiler::RegScope rs(pg);
42     compiler::VReg lhs = pg->AllocReg();
43 
44     ASSERT(operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_AND ||
45            operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_OR ||
46            operator_ == lexer::TokenType::PUNCTUATOR_NULLISH_COALESCING);
47     auto *skipRight = pg->AllocLabel();
48     auto *endLabel = pg->AllocLabel();
49 
50     // left -> acc -> lhs -> toboolean -> acc -> bool_lhs
51     left_->Compile(pg);
52     pg->StoreAccumulator(this, lhs);
53 
54     if (operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_AND) {
55         pg->BranchIfFalse(this, skipRight);
56     } else if (operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_OR) {
57         pg->BranchIfTrue(this, skipRight);
58     } else {
59         ASSERT(operator_ == lexer::TokenType::PUNCTUATOR_NULLISH_COALESCING);
60         auto *nullish = pg->AllocLabel();
61         // if lhs === null
62         pg->BranchIfStrictNull(this, nullish);
63         pg->LoadAccumulator(this, lhs);
64         // if lhs === undefined
65         pg->BranchIfStrictNotUndefined(this, skipRight);
66         pg->SetLabel(this, nullish);
67     }
68 
69     // left is true/false(and/or) then right -> acc
70     right_->Compile(pg);
71     pg->Branch(this, endLabel);
72 
73     // left is false/true(and/or) then lhs -> acc
74     pg->SetLabel(this, skipRight);
75     pg->LoadAccumulator(this, lhs);
76     pg->SetLabel(this, endLabel);
77 }
78 
CompilePrivateIn(compiler::PandaGen * pg) const79 void BinaryExpression::CompilePrivateIn(compiler::PandaGen *pg) const
80 {
81     ASSERT(operator_ == lexer::TokenType::KEYW_IN);
82     auto name = left_->AsPrivateIdentifier()->Name();
83     auto result = pg->Scope()->FindPrivateName(name);
84 
85     right_->Compile(pg);
86     if (!result.result.isMethod) {
87         pg->TestIn(this, result.lexLevel, result.result.slot);
88         return;
89     }
90     // Instance private method check symbol("#method")
91     if (!result.result.isStatic) {
92         pg->TestIn(this, result.lexLevel, result.result.validateMethodSlot);
93         return;
94     }
95     // Static private method check whether equals the class object
96     compiler::RegScope rs(pg);
97     compiler::VReg rhs = pg->AllocReg();
98     pg->StoreAccumulator(right_, rhs);
99     pg->LoadLexicalVar(this, result.lexLevel, result.result.validateMethodSlot);
100     pg->Equal(this, rhs);
101 }
102 
Compile(compiler::PandaGen * pg) const103 void BinaryExpression::Compile(compiler::PandaGen *pg) const
104 {
105     if (left_->IsPrivateIdentifier()) {
106         CompilePrivateIn(pg);
107         return;
108     }
109 
110     if (IsLogical()) {
111         CompileLogical(pg);
112         return;
113     }
114 
115     compiler::RegScope rs(pg);
116     compiler::VReg lhs = pg->AllocReg();
117 
118     left_->Compile(pg);
119     pg->StoreAccumulator(right_, lhs);
120     right_->Compile(pg);
121 
122     pg->Binary(right_, operator_, lhs);
123 }
124 
Check(checker::Checker * checker) const125 checker::Type *BinaryExpression::Check(checker::Checker *checker) const
126 {
127     auto *leftType = left_->Check(checker);
128     auto *rightType = right_->Check(checker);
129 
130     switch (operator_) {
131         case lexer::TokenType::PUNCTUATOR_MULTIPLY:
132         case lexer::TokenType::PUNCTUATOR_EXPONENTIATION:
133         case lexer::TokenType::PUNCTUATOR_DIVIDE:
134         case lexer::TokenType::PUNCTUATOR_MOD:
135         case lexer::TokenType::PUNCTUATOR_MINUS:
136         case lexer::TokenType::PUNCTUATOR_LEFT_SHIFT:
137         case lexer::TokenType::PUNCTUATOR_RIGHT_SHIFT:
138         case lexer::TokenType::PUNCTUATOR_UNSIGNED_RIGHT_SHIFT:
139         case lexer::TokenType::PUNCTUATOR_BITWISE_AND:
140         case lexer::TokenType::PUNCTUATOR_BITWISE_XOR:
141         case lexer::TokenType::PUNCTUATOR_BITWISE_OR: {
142             return checker->CheckBinaryOperator(leftType, rightType, left_, right_, this, operator_);
143         }
144         case lexer::TokenType::PUNCTUATOR_PLUS: {
145             return checker->CheckPlusOperator(leftType, rightType, left_, right_, this, operator_);
146         }
147         case lexer::TokenType::PUNCTUATOR_LESS_THAN:
148         case lexer::TokenType::PUNCTUATOR_GREATER_THAN: {
149             return checker->CheckCompareOperator(leftType, rightType, left_, right_, this, operator_);
150         }
151         case lexer::TokenType::PUNCTUATOR_EQUAL:
152         case lexer::TokenType::PUNCTUATOR_NOT_EQUAL:
153         case lexer::TokenType::PUNCTUATOR_STRICT_EQUAL:
154         case lexer::TokenType::PUNCTUATOR_NOT_STRICT_EQUAL: {
155             if (checker->IsTypeEqualityComparableTo(leftType, rightType) ||
156                 checker->IsTypeEqualityComparableTo(rightType, leftType)) {
157                 return checker->GlobalBooleanType();
158             }
159 
160             checker->ThrowBinaryLikeError(operator_, leftType, rightType, Start());
161         }
162         case lexer::TokenType::KEYW_INSTANCEOF: {
163             return checker->CheckInstanceofExpression(leftType, rightType, right_, this);
164         }
165         case lexer::TokenType::KEYW_IN: {
166             return checker->CheckInExpression(leftType, rightType, left_, right_, this);
167         }
168         case lexer::TokenType::PUNCTUATOR_LOGICAL_AND: {
169             return checker->CheckAndOperator(leftType, rightType, left_);
170         }
171         case lexer::TokenType::PUNCTUATOR_LOGICAL_OR: {
172             return checker->CheckOrOperator(leftType, rightType, left_);
173         }
174         case lexer::TokenType::PUNCTUATOR_NULLISH_COALESCING: {
175             // TODO(Csaba Repasi): Implement checker for nullish coalescing
176             return checker->GlobalAnyType();
177         }
178         case lexer::TokenType::PUNCTUATOR_SUBSTITUTION: {
179             checker->CheckAssignmentOperator(operator_, left_, leftType, rightType);
180             return rightType;
181         }
182         default: {
183             UNREACHABLE();
184             break;
185         }
186     }
187 
188     return nullptr;
189 }
190 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)191 void BinaryExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
192 {
193     left_ = std::get<ir::AstNode *>(cb(left_))->AsExpression();
194     right_ = std::get<ir::AstNode *>(cb(right_))->AsExpression();
195 }
196 
197 }  // namespace panda::es2panda::ir
198