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 "ir/expressions/assignmentExpression.h"
17 #include "ir/expressions/memberExpression.h"
18
19 #include "checker/TSchecker.h"
20
21 namespace panda::es2panda::checker {
CheckBinaryOperator(Type * leftType,Type * rightType,ir::Expression * leftExpr,ir::Expression * rightExpr,ir::AstNode * expr,lexer::TokenType op)22 Type *TSChecker::CheckBinaryOperator(Type *leftType, Type *rightType, ir::Expression *leftExpr,
23 ir::Expression *rightExpr, ir::AstNode *expr, lexer::TokenType op)
24 {
25 CheckNonNullType(leftType, leftExpr->Start());
26 CheckNonNullType(rightType, rightExpr->Start());
27
28 if (leftType->HasTypeFlag(TypeFlag::BOOLEAN_LIKE) && rightType->HasTypeFlag(TypeFlag::BOOLEAN_LIKE)) {
29 lexer::TokenType suggestedOp;
30 switch (op) {
31 case lexer::TokenType::PUNCTUATOR_BITWISE_OR:
32 case lexer::TokenType::PUNCTUATOR_BITWISE_OR_EQUAL: {
33 suggestedOp = lexer::TokenType::PUNCTUATOR_LOGICAL_OR;
34 break;
35 }
36 case lexer::TokenType::PUNCTUATOR_BITWISE_AND:
37 case lexer::TokenType::PUNCTUATOR_BITWISE_AND_EQUAL: {
38 suggestedOp = lexer::TokenType::PUNCTUATOR_LOGICAL_AND;
39 break;
40 }
41 case lexer::TokenType::PUNCTUATOR_BITWISE_XOR:
42 case lexer::TokenType::PUNCTUATOR_BITWISE_XOR_EQUAL: {
43 suggestedOp = lexer::TokenType::PUNCTUATOR_NOT_STRICT_EQUAL;
44 break;
45 }
46 default: {
47 suggestedOp = lexer::TokenType::EOS;
48 break;
49 }
50 }
51
52 if (suggestedOp != lexer::TokenType::EOS) {
53 ThrowTypeError(
54 {"The ", op, " operator is not allowed for boolean types. Consider using ", suggestedOp, " instead"},
55 expr->Start());
56 }
57 }
58
59 if (!leftType->HasTypeFlag(TypeFlag::VALID_ARITHMETIC_TYPE)) {
60 ThrowTypeError(
61 "The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an "
62 "enum "
63 "type.",
64 expr->Start());
65 }
66
67 if (!rightType->HasTypeFlag(TypeFlag::VALID_ARITHMETIC_TYPE)) {
68 ThrowTypeError(
69 "The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an "
70 "enum "
71 "type.",
72 rightExpr->Start());
73 }
74
75 Type *resultType = nullptr;
76 if ((leftType->IsAnyType() && rightType->IsAnyType()) ||
77 !(leftType->HasTypeFlag(TypeFlag::BIGINT_LIKE) || rightType->HasTypeFlag(TypeFlag::BIGINT_LIKE))) {
78 resultType = GlobalNumberType();
79 } else if (leftType->HasTypeFlag(TypeFlag::BIGINT_LIKE) && rightType->HasTypeFlag(TypeFlag::BIGINT_LIKE)) {
80 if (op == lexer::TokenType::PUNCTUATOR_UNSIGNED_RIGHT_SHIFT ||
81 op == lexer::TokenType::PUNCTUATOR_UNSIGNED_RIGHT_SHIFT_EQUAL) {
82 ThrowTypeError({"operator ", op, " cannot be applied to types 'bigint' and 'bigint'"}, expr->Start());
83 }
84 resultType = GlobalBigintType();
85 } else {
86 ThrowBinaryLikeError(op, leftType, rightType, expr->Start());
87 }
88
89 CheckAssignmentOperator(op, leftExpr, leftType, resultType);
90 return resultType;
91 }
92
CheckPlusOperator(Type * leftType,Type * rightType,ir::Expression * leftExpr,ir::Expression * rightExpr,ir::AstNode * expr,lexer::TokenType op)93 Type *TSChecker::CheckPlusOperator(Type *leftType, Type *rightType, ir::Expression *leftExpr, ir::Expression *rightExpr,
94 ir::AstNode *expr, lexer::TokenType op)
95 {
96 if (!leftType->HasTypeFlag(TypeFlag::STRING_LIKE) && !rightType->HasTypeFlag(TypeFlag::STRING_LIKE)) {
97 CheckNonNullType(leftType, leftExpr->Start());
98 CheckNonNullType(rightType, rightExpr->Start());
99 }
100
101 Type *resultType = nullptr;
102 if (IsTypeAssignableTo(leftType, GlobalNumberType()) && IsTypeAssignableTo(rightType, GlobalNumberType())) {
103 resultType = GlobalNumberType();
104 } else if (IsTypeAssignableTo(leftType, GlobalBigintType()) && IsTypeAssignableTo(rightType, GlobalBigintType())) {
105 resultType = GlobalBigintType();
106 } else if (IsTypeAssignableTo(leftType, GlobalStringType()) || IsTypeAssignableTo(rightType, GlobalStringType())) {
107 resultType = GlobalStringType();
108 } else if (MaybeTypeOfKind(leftType, TypeFlag::UNKNOWN)) {
109 ThrowTypeError("object is of type 'unknown'", leftExpr->Start());
110 } else if (MaybeTypeOfKind(rightType, TypeFlag::UNKNOWN)) {
111 ThrowTypeError("object is of type 'unknown'", rightExpr->Start());
112 } else if (leftType->IsAnyType() || rightType->IsAnyType()) {
113 resultType = GlobalAnyType();
114 } else {
115 ThrowBinaryLikeError(op, leftType, rightType, expr->Start());
116 }
117
118 if (op == lexer::TokenType::PUNCTUATOR_PLUS_EQUAL) {
119 CheckAssignmentOperator(op, leftExpr, leftType, resultType);
120 }
121
122 return resultType;
123 }
124
CheckCompareOperator(Type * leftType,Type * rightType,ir::Expression * leftExpr,ir::Expression * rightExpr,ir::AstNode * expr,lexer::TokenType op)125 Type *TSChecker::CheckCompareOperator(Type *leftType, Type *rightType, ir::Expression *leftExpr,
126 ir::Expression *rightExpr, ir::AstNode *expr, lexer::TokenType op)
127 {
128 CheckNonNullType(leftType, leftExpr->Start());
129 CheckNonNullType(rightType, rightExpr->Start());
130
131 if (AreTypesComparable(leftType, rightType) || (IsTypeAssignableTo(leftType, GlobalNumberOrBigintType()) &&
132 IsTypeAssignableTo(rightType, GlobalNumberOrBigintType()))) {
133 return GlobalBooleanType();
134 }
135
136 ThrowBinaryLikeError(op, leftType, rightType, expr->Start());
137
138 return GlobalAnyType();
139 }
140
CheckAndOperator(Type * leftType,Type * rightType,ir::Expression * leftExpr)141 Type *TSChecker::CheckAndOperator(Type *leftType, Type *rightType, ir::Expression *leftExpr)
142 {
143 CheckTruthinessOfType(leftType, leftExpr->Start());
144
145 if ((static_cast<uint64_t>(leftType->GetTypeFacts()) & static_cast<uint64_t>(TypeFacts::TRUTHY)) != 0U) {
146 Type *resultType = CreateUnionType({ExtractDefinitelyFalsyTypes(rightType), rightType});
147 return resultType;
148 }
149
150 return leftType;
151 }
152
CheckOrOperator(Type * leftType,Type * rightType,ir::Expression * leftExpr)153 Type *TSChecker::CheckOrOperator(Type *leftType, Type *rightType, ir::Expression *leftExpr)
154 {
155 CheckTruthinessOfType(leftType, leftExpr->Start());
156
157 if ((static_cast<uint64_t>(leftType->GetTypeFacts()) & static_cast<uint64_t>(TypeFacts::FALSY)) != 0U) {
158 // NOTE: aszilagyi. subtype reduction in the result union
159 Type *resultType = CreateUnionType({RemoveDefinitelyFalsyTypes(leftType), rightType});
160 return resultType;
161 }
162
163 return leftType;
164 }
165
TypeHasCallOrConstructSignatures(Type * type)166 static bool TypeHasCallOrConstructSignatures(Type *type)
167 {
168 return type->IsObjectType() &&
169 (!type->AsObjectType()->CallSignatures().empty() || !type->AsObjectType()->ConstructSignatures().empty());
170 }
171
CheckInstanceofExpression(Type * leftType,Type * rightType,ir::Expression * rightExpr,ir::AstNode * expr)172 Type *TSChecker::CheckInstanceofExpression(Type *leftType, Type *rightType, ir::Expression *rightExpr,
173 ir::AstNode *expr)
174 {
175 if (leftType->TypeFlags() != TypeFlag::ANY && IsAllTypesAssignableTo(leftType, GlobalPrimitiveType())) {
176 ThrowTypeError({"The left-hand side of an 'instanceof' expression must be of type 'any',",
177 " an object type or a type parameter."},
178 expr->Start());
179 }
180
181 // NOTE: aszilagyi. Check if right type is subtype of globalFunctionType
182 if (rightType->TypeFlags() != TypeFlag::ANY && !TypeHasCallOrConstructSignatures(rightType)) {
183 ThrowTypeError({"The right-hand side of an 'instanceof' expression must be of type 'any'",
184 " or of a type assignable to the 'Function' interface type."},
185 rightExpr->Start());
186 }
187
188 return GlobalBooleanType();
189 }
190
CheckInExpression(Type * leftType,Type * rightType,ir::Expression * leftExpr,ir::Expression * rightExpr,ir::AstNode * expr)191 Type *TSChecker::CheckInExpression(Type *leftType, Type *rightType, ir::Expression *leftExpr, ir::Expression *rightExpr,
192 ir::AstNode *expr)
193 {
194 CheckNonNullType(leftType, leftExpr->Start());
195 CheckNonNullType(rightType, rightExpr->Start());
196
197 // NOTE: aszilagyi. Check IsAllTypesAssignableTo with ESSymbol too
198 if (leftType->TypeFlags() != TypeFlag::ANY && !IsAllTypesAssignableTo(leftType, GlobalStringOrNumberType())) {
199 ThrowTypeError(
200 {"The left-hand side of an 'in' expression must be of type 'any',", " 'string', 'number', or 'symbol'."},
201 expr->Start());
202 }
203
204 // NOTE: aszilagyi. Handle type parameters
205 if (!IsAllTypesAssignableTo(rightType, GlobalNonPrimitiveType())) {
206 ThrowTypeError("The right-hand side of an 'in' expression must not be a primitive.", rightExpr->Start());
207 }
208
209 return GlobalBooleanType();
210 }
211
CheckAssignmentOperator(lexer::TokenType op,ir::Expression * leftExpr,Type * leftType,Type * valueType)212 void TSChecker::CheckAssignmentOperator(lexer::TokenType op, ir::Expression *leftExpr, Type *leftType, Type *valueType)
213 {
214 if (IsAssignmentOperator(op)) {
215 CheckReferenceExpression(
216 leftExpr, "the left hand side of an assignment expression must be a variable or a property access",
217 "The left-hand side of an assignment expression may not be an optional property access.");
218
219 if (!IsTypeAssignableTo(valueType, leftType)) {
220 ThrowAssignmentError(valueType, leftType, leftExpr->Start(),
221 leftExpr->Parent()->AsAssignmentExpression()->Right()->IsMemberExpression() ||
222 leftExpr->Parent()->AsAssignmentExpression()->Right()->IsChainExpression());
223 }
224 }
225 }
226 } // namespace panda::es2panda::checker
227