1 /*
2 * Copyright (c) 2021-2024 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 #ifndef ES2PANDA_COMPILER_CHECKER_ETS_FUNCTION_HELPERS_H
17 #define ES2PANDA_COMPILER_CHECKER_ETS_FUNCTION_HELPERS_H
18
19 #include "varbinder/varbinder.h"
20 #include "varbinder/declaration.h"
21 #include "varbinder/ETSBinder.h"
22 #include "varbinder/scope.h"
23 #include "varbinder/variable.h"
24 #include "varbinder/variableFlags.h"
25 #include "checker/ETSchecker.h"
26 #include "checker/ets/typeRelationContext.h"
27 #include "checker/types/ets/etsAsyncFuncReturnType.h"
28 #include "checker/types/ets/etsObjectType.h"
29 #include "checker/types/type.h"
30 #include "checker/types/typeFlag.h"
31 #include "ir/astNode.h"
32 #include "ir/typeNode.h"
33 #include "ir/base/catchClause.h"
34 #include "ir/base/classDefinition.h"
35 #include "ir/base/classProperty.h"
36 #include "ir/base/methodDefinition.h"
37 #include "ir/base/scriptFunction.h"
38 #include "ir/base/spreadElement.h"
39 #include "ir/ets/etsFunctionType.h"
40 #include "ir/ets/etsParameterExpression.h"
41 #include "ir/ets/etsTypeReference.h"
42 #include "ir/ets/etsTypeReferencePart.h"
43 #include "ir/expressions/arrowFunctionExpression.h"
44 #include "ir/expressions/assignmentExpression.h"
45 #include "ir/expressions/callExpression.h"
46 #include "ir/expressions/functionExpression.h"
47 #include "ir/expressions/identifier.h"
48 #include "ir/expressions/literals/numberLiteral.h"
49 #include "ir/expressions/memberExpression.h"
50 #include "ir/expressions/objectExpression.h"
51 #include "ir/expressions/thisExpression.h"
52 #include "ir/statements/blockStatement.h"
53 #include "ir/statements/doWhileStatement.h"
54 #include "ir/statements/expressionStatement.h"
55 #include "ir/statements/forInStatement.h"
56 #include "ir/statements/forOfStatement.h"
57 #include "ir/statements/forUpdateStatement.h"
58 #include "ir/statements/returnStatement.h"
59 #include "ir/statements/switchStatement.h"
60 #include "ir/statements/whileStatement.h"
61 #include "ir/ts/tsArrayType.h"
62 #include "ir/ts/tsInterfaceBody.h"
63 #include "ir/ts/tsTypeAliasDeclaration.h"
64 #include "ir/ts/tsTypeParameter.h"
65 #include "ir/ts/tsTypeParameterInstantiation.h"
66 #include "parser/program/program.h"
67 #include "util/helpers.h"
68 #include "util/language.h"
69
70 namespace ark::es2panda::checker {
71
MaybeBoxedType(ETSChecker * checker,Type * type,ir::Expression * expr)72 static Type *MaybeBoxedType(ETSChecker *checker, Type *type, ir::Expression *expr)
73 {
74 if (!type->HasTypeFlag(TypeFlag::ETS_PRIMITIVE)) {
75 return type;
76 }
77 auto *relation = checker->Relation();
78 auto *oldNode = relation->GetNode();
79 relation->SetNode(expr);
80 auto *res = checker->PrimitiveTypeAsETSBuiltinType(type);
81 relation->SetNode(oldNode);
82 return res;
83 }
84
BuildImplicitSubstitutionForArguments(ETSChecker * checker,Signature * signature,const ArenaVector<ir::Expression * > & arguments)85 static const Substitution *BuildImplicitSubstitutionForArguments(ETSChecker *checker, Signature *signature,
86 const ArenaVector<ir::Expression *> &arguments)
87 {
88 Substitution *substitution = checker->NewSubstitution();
89 auto *sigInfo = signature->GetSignatureInfo();
90 auto &sigParams = signature->GetSignatureInfo()->typeParams;
91
92 for (size_t ix = 0; ix < arguments.size(); ix++) {
93 auto *arg = arguments[ix];
94 if (arg->IsObjectExpression()) {
95 continue;
96 }
97
98 auto *const argType = arg->IsSpreadElement()
99 ? MaybeBoxedType(checker, arg->AsSpreadElement()->Argument()->Check(checker),
100 arg->AsSpreadElement()->Argument())
101 : MaybeBoxedType(checker, arg->Check(checker), arg);
102 auto *const paramType = (ix < signature->MinArgCount()) ? sigInfo->params[ix]->TsType()
103 : sigInfo->restVar != nullptr ? sigInfo->restVar->TsType()
104 : nullptr;
105
106 if (paramType == nullptr) {
107 continue;
108 }
109
110 if (!checker->EnhanceSubstitutionForType(sigInfo->typeParams, paramType, argType, substitution)) {
111 return nullptr;
112 }
113 }
114
115 if (substitution->size() != sigParams.size()) {
116 for (const auto typeParam : sigParams) {
117 auto newTypeParam = typeParam->AsETSTypeParameter();
118 if (auto it = substitution->find(newTypeParam); it != substitution->cend()) {
119 continue;
120 }
121 if (newTypeParam->GetDefaultType() == nullptr) {
122 return nullptr;
123 }
124 auto dflt = newTypeParam->GetDefaultType()->Substitute(checker->Relation(), substitution);
125 if (!checker->EnhanceSubstitutionForType(sigInfo->typeParams, newTypeParam, dflt, substitution)) {
126 return nullptr;
127 }
128 }
129
130 if (substitution->size() != sigParams.size() &&
131 (signature->Function()->ReturnTypeAnnotation() == nullptr ||
132 !checker->EnhanceSubstitutionForType(sigInfo->typeParams,
133 signature->Function()->ReturnTypeAnnotation()->TsType(),
134 signature->ReturnType(), substitution))) {
135 return nullptr;
136 }
137 }
138
139 return substitution;
140 }
141
BuildExplicitSubstitutionForArguments(ETSChecker * checker,Signature * signature,const ArenaVector<ir::TypeNode * > & params,const lexer::SourcePosition & pos,TypeRelationFlag flags)142 static const Substitution *BuildExplicitSubstitutionForArguments(ETSChecker *checker, Signature *signature,
143 const ArenaVector<ir::TypeNode *> ¶ms,
144 const lexer::SourcePosition &pos,
145 TypeRelationFlag flags)
146 {
147 auto &sigParams = signature->GetSignatureInfo()->typeParams;
148 auto *substitution = checker->NewSubstitution();
149 auto *constraintsSubstitution = checker->NewSubstitution();
150 ArenaVector<Type *> instArgs {checker->Allocator()->Adapter()};
151
152 for (size_t ix = 0; ix < params.size(); ++ix) {
153 instArgs.push_back(MaybeBoxedType(checker, params[ix]->GetType(checker), params[ix]));
154 if (ix < sigParams.size()) {
155 ETSChecker::EmplaceSubstituted(constraintsSubstitution, sigParams[ix]->AsETSTypeParameter(), instArgs[ix]);
156 }
157 }
158 for (size_t ix = instArgs.size(); ix < sigParams.size(); ++ix) {
159 auto *dflt = sigParams[ix]->AsETSTypeParameter()->GetDefaultType();
160 if (dflt == nullptr) {
161 break;
162 }
163
164 dflt = dflt->Substitute(checker->Relation(), constraintsSubstitution);
165 instArgs.push_back(dflt);
166 ETSChecker::EmplaceSubstituted(constraintsSubstitution, sigParams[ix]->AsETSTypeParameter(), instArgs[ix]);
167 }
168 if (sigParams.size() != instArgs.size()) {
169 if ((flags & TypeRelationFlag::NO_THROW) != 0) {
170 return nullptr;
171 }
172 checker->ThrowTypeError({"Expected ", sigParams.size(), " type arguments, got ", instArgs.size(), " ."}, pos);
173 }
174
175 for (size_t ix = 0; ix < sigParams.size(); ix++) {
176 if (!checker->IsCompatibleTypeArgument(sigParams[ix]->AsETSTypeParameter(), instArgs[ix],
177 constraintsSubstitution)) {
178 return nullptr;
179 }
180 ETSChecker::EmplaceSubstituted(substitution, sigParams[ix]->AsETSTypeParameter(), instArgs[ix]);
181 }
182 return substitution;
183 }
184
MaybeSubstituteTypeParameters(ETSChecker * checker,Signature * signature,const ir::TSTypeParameterInstantiation * typeArguments,const ArenaVector<ir::Expression * > & arguments,const lexer::SourcePosition & pos,TypeRelationFlag flags)185 static Signature *MaybeSubstituteTypeParameters(ETSChecker *checker, Signature *signature,
186 const ir::TSTypeParameterInstantiation *typeArguments,
187 const ArenaVector<ir::Expression *> &arguments,
188 const lexer::SourcePosition &pos, TypeRelationFlag flags)
189 {
190 if (typeArguments == nullptr && signature->GetSignatureInfo()->typeParams.empty()) {
191 return signature;
192 }
193
194 const Substitution *substitution =
195 (typeArguments != nullptr)
196 ? BuildExplicitSubstitutionForArguments(checker, signature, typeArguments->Params(), pos, flags)
197 : BuildImplicitSubstitutionForArguments(checker, signature, arguments);
198
199 return (substitution == nullptr) ? nullptr : signature->Substitute(checker->Relation(), substitution);
200 }
201
CmpAssemblerTypesWithRank(Signature * sig1,Signature * sig2)202 static bool CmpAssemblerTypesWithRank(Signature *sig1, Signature *sig2)
203 {
204 for (size_t ix = 0; ix < sig1->MinArgCount(); ix++) {
205 std::stringstream s1;
206 std::stringstream s2;
207 sig1->Params()[ix]->TsType()->ToAssemblerTypeWithRank(s1);
208 sig2->Params()[ix]->TsType()->ToAssemblerTypeWithRank(s2);
209 if (s1.str() != s2.str()) {
210 return false;
211 break;
212 }
213 }
214 return true;
215 }
216
HasSameAssemblySignature(ETSFunctionType * func1,ETSFunctionType * func2)217 static bool HasSameAssemblySignature(ETSFunctionType *func1, ETSFunctionType *func2)
218 {
219 for (auto *sig1 : func1->CallSignatures()) {
220 for (auto *sig2 : func2->CallSignatures()) {
221 if (sig1->MinArgCount() != sig2->MinArgCount()) {
222 continue;
223 }
224 bool allSame = CmpAssemblerTypesWithRank(sig1, sig2);
225 if (!allSame) {
226 continue;
227 }
228 auto *rv1 = sig1->RestVar();
229 auto *rv2 = sig2->RestVar();
230 if (rv1 == nullptr && rv2 == nullptr) {
231 return true;
232 }
233 if (rv1 == nullptr || rv2 == nullptr) { // exactly one of them is null
234 return false;
235 }
236 std::stringstream s1;
237 std::stringstream s2;
238 rv1->TsType()->ToAssemblerTypeWithRank(s1);
239 rv2->TsType()->ToAssemblerTypeWithRank(s2);
240 if (s1.str() == s2.str()) {
241 return true;
242 }
243 }
244 }
245 return false;
246 }
247
CheckInterfaceOverride(ETSChecker * const checker,ETSObjectType * const interface,Signature * const signature)248 static bool CheckInterfaceOverride(ETSChecker *const checker, ETSObjectType *const interface,
249 Signature *const signature)
250 {
251 bool isOverriding = checker->CheckOverride(signature, interface);
252
253 for (auto *const superInterface : interface->Interfaces()) {
254 isOverriding |= CheckInterfaceOverride(checker, superInterface, signature);
255 }
256
257 return isOverriding;
258 }
259
NodeScope(ir::AstNode * ast)260 static varbinder::Scope *NodeScope(ir::AstNode *ast)
261 {
262 if (ast->IsBlockStatement()) {
263 return ast->AsBlockStatement()->Scope();
264 }
265 if (ast->IsDoWhileStatement()) {
266 return ast->AsDoWhileStatement()->Scope();
267 }
268 if (ast->IsForInStatement()) {
269 return ast->AsForInStatement()->Scope();
270 }
271 if (ast->IsForOfStatement()) {
272 return ast->AsForOfStatement()->Scope();
273 }
274 if (ast->IsForUpdateStatement()) {
275 return ast->AsForUpdateStatement()->Scope();
276 }
277 if (ast->IsSwitchStatement()) {
278 return ast->AsSwitchStatement()->Scope();
279 }
280 if (ast->IsWhileStatement()) {
281 return ast->AsWhileStatement()->Scope();
282 }
283 if (ast->IsCatchClause()) {
284 return ast->AsCatchClause()->Scope();
285 }
286 if (ast->IsClassDefinition()) {
287 return ast->AsClassDefinition()->Scope();
288 }
289 if (ast->IsScriptFunction()) {
290 return ast->AsScriptFunction()->Scope()->ParamScope();
291 }
292 return nullptr;
293 }
294
295 } // namespace ark::es2panda::checker
296
297 #endif
298