• 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 <binder/variable.h>
19 #include <compiler/core/pandagen.h>
20 #include <compiler/core/regScope.h>
21 #include <typescript/checker.h>
22 #include <ir/astDump.h>
23 #include <lexer/token/tokenType.h>
24 
25 namespace panda::es2panda::ir {
26 
Iterate(const NodeTraverser & cb) const27 void BinaryExpression::Iterate(const NodeTraverser &cb) const
28 {
29     cb(left_);
30     cb(right_);
31 }
32 
Dump(ir::AstDumper * dumper) const33 void BinaryExpression::Dump(ir::AstDumper *dumper) const
34 {
35     dumper->Add({{"type", IsLogical() ? "LogicalExpression" : "BinaryExpression"},
36                  {"operator", operator_},
37                  {"left", left_},
38                  {"right", right_}});
39 }
40 
CompileLogical(compiler::PandaGen * pg) const41 void BinaryExpression::CompileLogical(compiler::PandaGen *pg) const
42 {
43     compiler::RegScope rs(pg);
44     compiler::VReg lhs = pg->AllocReg();
45 
46     ASSERT(operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_AND ||
47            operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_OR ||
48            operator_ == lexer::TokenType::PUNCTUATOR_NULLISH_COALESCING);
49     auto *skipRight = pg->AllocLabel();
50     auto *endLabel = pg->AllocLabel();
51 
52     // left -> acc -> lhs -> toboolean -> acc -> bool_lhs
53     left_->Compile(pg);
54     pg->StoreAccumulator(this, lhs);
55 
56     if (operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_AND) {
57         pg->BranchIfFalse(this, skipRight);
58     } else if (operator_ == lexer::TokenType::PUNCTUATOR_LOGICAL_OR) {
59         pg->BranchIfTrue(this, skipRight);
60     } else {
61         ASSERT(operator_ == lexer::TokenType::PUNCTUATOR_NULLISH_COALESCING);
62         auto *nullish = pg->AllocLabel();
63         // if lhs === null
64         pg->BranchIfStrictNull(this, nullish);
65         pg->LoadAccumulator(this, lhs);
66         // if lhs === undefined
67         pg->BranchIfStrictNotUndefined(this, skipRight);
68         pg->SetLabel(this, nullish);
69     }
70 
71     // left is true/false(and/or) then right -> acc
72     right_->Compile(pg);
73     pg->Branch(this, endLabel);
74 
75     // left is false/true(and/or) then lhs -> acc
76     pg->SetLabel(this, skipRight);
77     pg->LoadAccumulator(this, lhs);
78     pg->SetLabel(this, endLabel);
79 }
80 
Compile(compiler::PandaGen * pg) const81 void BinaryExpression::Compile(compiler::PandaGen *pg) const
82 {
83     if (IsLogical()) {
84         CompileLogical(pg);
85         return;
86     }
87 
88     compiler::RegScope rs(pg);
89     compiler::VReg lhs = pg->AllocReg();
90 
91     left_->Compile(pg);
92     pg->StoreAccumulator(this, lhs);
93     right_->Compile(pg);
94 
95     pg->Binary(this, operator_, lhs);
96 }
97 
Check(checker::Checker * checker) const98 checker::Type *BinaryExpression::Check(checker::Checker *checker) const
99 {
100     auto *leftType = left_->Check(checker);
101     auto *rightType = right_->Check(checker);
102 
103     switch (operator_) {
104         case lexer::TokenType::PUNCTUATOR_MULTIPLY:
105         case lexer::TokenType::PUNCTUATOR_EXPONENTIATION:
106         case lexer::TokenType::PUNCTUATOR_DIVIDE:
107         case lexer::TokenType::PUNCTUATOR_MOD:
108         case lexer::TokenType::PUNCTUATOR_MINUS:
109         case lexer::TokenType::PUNCTUATOR_LEFT_SHIFT:
110         case lexer::TokenType::PUNCTUATOR_RIGHT_SHIFT:
111         case lexer::TokenType::PUNCTUATOR_UNSIGNED_RIGHT_SHIFT:
112         case lexer::TokenType::PUNCTUATOR_BITWISE_AND:
113         case lexer::TokenType::PUNCTUATOR_BITWISE_XOR:
114         case lexer::TokenType::PUNCTUATOR_BITWISE_OR: {
115             return checker->CheckBinaryOperator(leftType, rightType, left_, right_, this, operator_);
116         }
117         case lexer::TokenType::PUNCTUATOR_PLUS: {
118             return checker->CheckPlusOperator(leftType, rightType, left_, right_, this, operator_);
119         }
120         case lexer::TokenType::PUNCTUATOR_LESS_THAN:
121         case lexer::TokenType::PUNCTUATOR_GREATER_THAN: {
122             return checker->CheckCompareOperator(leftType, rightType, left_, right_, this, operator_);
123         }
124         case lexer::TokenType::PUNCTUATOR_EQUAL:
125         case lexer::TokenType::PUNCTUATOR_NOT_EQUAL:
126         case lexer::TokenType::PUNCTUATOR_STRICT_EQUAL:
127         case lexer::TokenType::PUNCTUATOR_NOT_STRICT_EQUAL: {
128             if (checker->IsTypeEqualityComparableTo(leftType, rightType) ||
129                 checker->IsTypeEqualityComparableTo(rightType, leftType)) {
130                 return checker->GlobalBooleanType();
131             }
132 
133             checker->ThrowBinaryLikeError(operator_, leftType, rightType, Start());
134         }
135         case lexer::TokenType::KEYW_INSTANCEOF: {
136             return checker->CheckInstanceofExpression(leftType, rightType, right_, this);
137         }
138         case lexer::TokenType::KEYW_IN: {
139             return checker->CheckInExpression(leftType, rightType, left_, right_, this);
140         }
141         case lexer::TokenType::PUNCTUATOR_LOGICAL_AND: {
142             return checker->CheckAndOperator(leftType, rightType, left_);
143         }
144         case lexer::TokenType::PUNCTUATOR_LOGICAL_OR: {
145             return checker->CheckOrOperator(leftType, rightType, left_);
146         }
147         case lexer::TokenType::PUNCTUATOR_NULLISH_COALESCING: {
148             // TODO(Csaba Repasi): Implement checker for nullish coalescing
149             return checker->GlobalAnyType();
150         }
151         case lexer::TokenType::PUNCTUATOR_SUBSTITUTION: {
152             checker->CheckAssignmentOperator(operator_, left_, leftType, rightType);
153             return rightType;
154         }
155         default: {
156             UNREACHABLE();
157             break;
158         }
159     }
160 
161     return nullptr;
162 }
163 
UpdateSelf(const NodeUpdater & cb,binder::Binder * binder)164 void BinaryExpression::UpdateSelf(const NodeUpdater &cb, [[maybe_unused]] binder::Binder *binder)
165 {
166     left_ = std::get<ir::AstNode *>(cb(left_))->AsExpression();
167     right_ = std::get<ir::AstNode *>(cb(right_))->AsExpression();
168 }
169 
170 }  // namespace panda::es2panda::ir
171