• Home
  • Raw
  • Download

Lines Matching +full:checker +full:-

2  * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
7 * http://www.apache.org/licenses/LICENSE-2.0
17 #include "checker/types/ets/etsAsyncFuncReturnType.h"
19 namespace ark::es2panda::checker { namespace
27 if (thisType->IsETSArrayType() || thisType->IsETSTypeParameter()) { in IsValidReceiverParameter()
31 if (!thisType->IsETSObjectType() || thisType->IsETSEnumType()) { in IsValidReceiverParameter()
35 auto *const thisObjectType = thisType->AsETSObjectType(); in IsValidReceiverParameter()
36 return thisObjectType->HasObjectFlag(ETSObjectFlags::CLASS | ETSObjectFlags::INTERFACE); in IsValidReceiverParameter()
39 void CheckExtensionIsShadowedInCurrentClassOrInterface(checker::ETSChecker *checker, checker::ETSOb… in CheckExtensionIsShadowedInCurrentClassOrInterface() argument
40 … ir::ScriptFunction *extensionFunc, checker::Signature *signature) in CheckExtensionIsShadowedInCurrentClassOrInterface()
42 const auto methodName = extensionFunc->Id()->Name(); in CheckExtensionIsShadowedInCurrentClassOrInterface()
45 …auto *const fieldVariable = objType->GetOwnProperty<checker::PropertyType::INSTANCE_FIELD>(methodN… in CheckExtensionIsShadowedInCurrentClassOrInterface()
47checker->LogError(diagnostic::EXTENSION_NAME_CONFLICT_WITH_FIELD, {methodName, objType->Name()}, in CheckExtensionIsShadowedInCurrentClassOrInterface()
48 extensionFunc->Body()->Start()); in CheckExtensionIsShadowedInCurrentClassOrInterface()
53 …auto *const methodVariable = objType->GetOwnProperty<checker::PropertyType::INSTANCE_METHOD>(metho… in CheckExtensionIsShadowedInCurrentClassOrInterface()
54 if (methodVariable == nullptr || methodVariable->TsType()->IsTypeError()) { in CheckExtensionIsShadowedInCurrentClassOrInterface()
58 const auto *const funcType = methodVariable->TsType()->AsETSFunctionType(); in CheckExtensionIsShadowedInCurrentClassOrInterface()
59 for (auto *funcSignature : funcType->CallSignatures()) { in CheckExtensionIsShadowedInCurrentClassOrInterface()
61 signature->SetReturnType(funcSignature->ReturnType()); in CheckExtensionIsShadowedInCurrentClassOrInterface()
62 if (!checker->Relation()->SignatureIsSupertypeOf(signature, funcSignature) && in CheckExtensionIsShadowedInCurrentClassOrInterface()
63 !checker->HasSameAssemblySignature(signature, funcSignature)) { in CheckExtensionIsShadowedInCurrentClassOrInterface()
67 if (signature->Function()->IsGetter() || signature->Function()->IsSetter()) { in CheckExtensionIsShadowedInCurrentClassOrInterface()
68checker->LogError(diagnostic::EXTENSION_NAME_CONFLICT_WITH_METHOD, {funcType->Name(), objType->Nam… in CheckExtensionIsShadowedInCurrentClassOrInterface()
69 extensionFunc->Body()->Start()); in CheckExtensionIsShadowedInCurrentClassOrInterface()
70 } else if (funcSignature->HasSignatureFlag(SignatureFlags::PUBLIC)) { in CheckExtensionIsShadowedInCurrentClassOrInterface()
71checker->LogError(diagnostic::EXTENSION_FUNC_NAME_CONFLICT_WITH_METH, {funcType->Name(), objType->… in CheckExtensionIsShadowedInCurrentClassOrInterface()
72 extensionFunc->Body()->Start()); in CheckExtensionIsShadowedInCurrentClassOrInterface()
74checker->LogDiagnostic(diagnostic::EXTENSION_NONPUBLIC_COLLISION, {funcType->Name(), objType->Name… in CheckExtensionIsShadowedInCurrentClassOrInterface()
75 extensionFunc->Body()->Start()); in CheckExtensionIsShadowedInCurrentClassOrInterface()
81 void CheckExtensionIsShadowedByMethod(checker::ETSChecker *checker, checker::ETSObjectType *objType, in CheckExtensionIsShadowedByMethod() argument
82 … ir::ScriptFunction *extensionFunc, checker::Signature *signature) in CheckExtensionIsShadowedByMethod()
88 CheckExtensionIsShadowedInCurrentClassOrInterface(checker, objType, extensionFunc, signature); in CheckExtensionIsShadowedByMethod()
90 for (auto *interface : objType->Interfaces()) { in CheckExtensionIsShadowedByMethod()
91 CheckExtensionIsShadowedByMethod(checker, interface, extensionFunc, signature); in CheckExtensionIsShadowedByMethod()
94 CheckExtensionIsShadowedByMethod(checker, objType->SuperType(), extensionFunc, signature); in CheckExtensionIsShadowedByMethod()
97 static void ReplaceThisInExtensionMethod(checker::ETSChecker *checker, ir::ScriptFunction *extensio… in ReplaceThisInExtensionMethod() argument
100 if (extensionFunc->Params().empty() || in ReplaceThisInExtensionMethod()
101 !extensionFunc->Params()[0]->AsETSParameterExpression()->Ident()->IsReceiver()) { in ReplaceThisInExtensionMethod()
102 ES2PANDA_ASSERT(checker->IsAnyError()); in ReplaceThisInExtensionMethod()
106 …if (extensionFunc->ReturnTypeAnnotation() != nullptr && extensionFunc->ReturnTypeAnnotation()->IsT… in ReplaceThisInExtensionMethod()
109 auto *const thisType = extensionFunc->Signature()->Params()[0]->TsType(); in ReplaceThisInExtensionMethod()
111 extensionFunc->Params()[0]->AsETSParameterExpression()->Ident()->TypeAnnotation(); in ReplaceThisInExtensionMethod()
112 extensionFunc->Signature()->SetReturnType(thisType); in ReplaceThisInExtensionMethod()
113 …extensionFunc->SetReturnTypeAnnotation(thisTypeAnnotation->Clone(checker->ProgramAllocator(), exte… in ReplaceThisInExtensionMethod()
116 auto thisVariable = extensionFunc->Params()[0]->Variable(); in ReplaceThisInExtensionMethod()
117 extensionFunc->Body()->TransformChildrenRecursively( in ReplaceThisInExtensionMethod()
119 if (ast->IsThisExpression()) { in ReplaceThisInExtensionMethod()
120 auto *thisParam = checker->ProgramAllocator()->New<ir::Identifier>( in ReplaceThisInExtensionMethod()
121 varbinder::TypedBinder::MANDATORY_PARAM_THIS, checker->ProgramAllocator()); in ReplaceThisInExtensionMethod()
122 thisParam->SetRange(ast->Range()); in ReplaceThisInExtensionMethod()
123 thisParam->SetParent(ast->Parent()); in ReplaceThisInExtensionMethod()
124 thisParam->SetVariable(thisVariable); in ReplaceThisInExtensionMethod()
129 "replace-this-in-extension-method"); in ReplaceThisInExtensionMethod()
132 void CheckExtensionMethod(checker::ETSChecker *checker, ir::ScriptFunction *extensionFunc, ir::AstN… in CheckExtensionMethod() argument
135 …!extensionFunc->Signature()->Params().empty() ? extensionFunc->Signature()->Params()[0]->TsType() … in CheckExtensionMethod()
140 if (!thisType->IsETSArrayType() && !thisType->IsETSTypeParameter() && in CheckExtensionMethod()
141 thisType->Variable()->Declaration()->Node()->IsClassDefinition() && in CheckExtensionMethod()
142 … !thisType->Variable()->Declaration()->Node()->AsClassDefinition()->IsClassDefinitionChecked()) { in CheckExtensionMethod()
143 thisType->Variable()->Declaration()->Node()->Check(checker); in CheckExtensionMethod()
146 ReplaceThisInExtensionMethod(checker, extensionFunc); in CheckExtensionMethod()
147 if (extensionFunc->IsArrow()) { in CheckExtensionMethod()
151checker::SignatureInfo *originalExtensionSigInfo = checker->ProgramAllocator()->New<checker::Signa… in CheckExtensionMethod()
152 extensionFunc->Signature()->GetSignatureInfo(), checker->ProgramAllocator()); in CheckExtensionMethod()
154 originalExtensionSigInfo->minArgCount -= 1U; in CheckExtensionMethod()
155 originalExtensionSigInfo->params.erase(originalExtensionSigInfo->params.begin()); in CheckExtensionMethod()
156 checker::Signature *originalExtensionSignature = in CheckExtensionMethod()
157checker->CreateSignature(originalExtensionSigInfo, extensionFunc->Signature()->ReturnType(), exten… in CheckExtensionMethod()
161 if (thisType->IsETSObjectType()) { in CheckExtensionMethod()
162 CheckExtensionIsShadowedByMethod(checker, thisType->AsETSObjectType(), extensionFunc, in CheckExtensionMethod()
167 if (thisType->IsETSArrayType()) { in CheckExtensionMethod()
168 CheckExtensionIsShadowedByMethod(checker, checker->GlobalETSObjectType(), extensionFunc, in CheckExtensionMethod()
172 checker->LogError(diagnostic::EXTENSION_FUNC_ON_INEXETENSIBLE, {}, node->Start()); in CheckExtensionMethod()
176 static void CheckMethodBodyForNativeAbstractDeclare(ETSChecker *checker, ir::MethodDefinition *node, in CheckMethodBodyForNativeAbstractDeclare() argument
179 if ((node->IsNative() && !node->IsConstructor()) || node->IsAbstract() || node->IsDeclare()) { in CheckMethodBodyForNativeAbstractDeclare()
180 checker->LogError(diagnostic::UNEXPECTED_FUNC_BODY, {}, scriptFunc->Body()->Start()); in CheckMethodBodyForNativeAbstractDeclare()
184 static void CheckNativeConstructorBody(ETSChecker *checker, ir::MethodDefinition *node, ir::ScriptF… in CheckNativeConstructorBody() argument
186 if (node->IsNative() && node->IsConstructor()) { in CheckNativeConstructorBody()
187 checker->LogError(diagnostic::NATIVE_WITH_BODY, {}, scriptFunc->Body()->Start()); in CheckNativeConstructorBody()
191 void DoBodyTypeChecking(ETSChecker *checker, ir::MethodDefinition *node, ir::ScriptFunction *script… in DoBodyTypeChecking() argument
193 if (scriptFunc->HasBody()) { in DoBodyTypeChecking()
194 CheckMethodBodyForNativeAbstractDeclare(checker, node, scriptFunc); in DoBodyTypeChecking()
195 CheckNativeConstructorBody(checker, node, scriptFunc); in DoBodyTypeChecking()
198 if (!scriptFunc->HasBody() || (scriptFunc->IsExternal() && !scriptFunc->IsExternalOverload())) { in DoBodyTypeChecking()
202 checker::ScopeContext scopeCtx(checker, scriptFunc->Scope()); in DoBodyTypeChecking()
203 checker::SavedCheckerContext savedContext(checker, checker->Context().Status(), in DoBodyTypeChecking()
204 checker->Context().ContainingClass()); in DoBodyTypeChecking()
205 checker->Context().SetContainingSignature(checker->GetSignatureFromMethodDefinition(node)); in DoBodyTypeChecking()
207 if (node->IsStatic() && !node->IsConstructor() && in DoBodyTypeChecking()
208 !checker->Context().ContainingClass()->HasObjectFlag(checker::ETSObjectFlags::GLOBAL)) { in DoBodyTypeChecking()
209 checker->AddStatus(checker::CheckerStatus::IN_STATIC_CONTEXT); in DoBodyTypeChecking()
212 if (node->IsConstructor()) { in DoBodyTypeChecking()
213 checker->AddStatus(checker::CheckerStatus::IN_CONSTRUCTOR); in DoBodyTypeChecking()
216 if (node->IsExtensionMethod() && scriptFunc->Signature() != nullptr) { in DoBodyTypeChecking()
217 CheckExtensionMethod(checker, scriptFunc, node); in DoBodyTypeChecking()
220 scriptFunc->Body()->Check(checker); in DoBodyTypeChecking()
222 if (scriptFunc->ReturnTypeAnnotation() == nullptr) { in DoBodyTypeChecking()
223 if (scriptFunc->IsAsyncFunc()) { in DoBodyTypeChecking()
224 …auto returnType = checker->CreateETSAsyncFuncReturnTypeFromBaseType(scriptFunc->Signature()->Retur… in DoBodyTypeChecking()
226 scriptFunc->Signature()->SetReturnType(returnType->PromiseType()); in DoBodyTypeChecking()
227 for (auto &returnStatement : scriptFunc->ReturnStatements()) { in DoBodyTypeChecking()
228 returnStatement->SetReturnType(checker, returnType); in DoBodyTypeChecking()
231 if (scriptFunc->IsAsyncImplFunc()) { in DoBodyTypeChecking()
232 ComposeAsyncImplFuncReturnType(checker, scriptFunc); in DoBodyTypeChecking()
235 for (auto &returnStatement : scriptFunc->ReturnStatements()) { in DoBodyTypeChecking()
236 returnStatement->SetReturnType(checker, scriptFunc->Signature()->ReturnType()); in DoBodyTypeChecking()
241 checker->Context().SetContainingSignature(nullptr); in DoBodyTypeChecking()
244 void ComposeAsyncImplFuncReturnType(ETSChecker *checker, ir::ScriptFunction *scriptFunc) in ComposeAsyncImplFuncReturnType() argument
246 …auto const promiseType = checker->CreatePromiseOf(checker->MaybeBoxType(scriptFunc->Signature()->R… in ComposeAsyncImplFuncReturnType()
248 …auto *objectId = checker->ProgramAllocNode<ir::Identifier>(compiler::Signatures::BUILTIN_OBJECT_CL… in ComposeAsyncImplFuncReturnType()
249 checker->ProgramAllocator()); in ComposeAsyncImplFuncReturnType()
250 checker->VarBinder()->AsETSBinder()->LookupTypeReference(objectId, false); in ComposeAsyncImplFuncReturnType()
251 auto *returnType = checker->ProgramAllocNode<ir::ETSTypeReference>( in ComposeAsyncImplFuncReturnType()
252checker->ProgramAllocNode<ir::ETSTypeReferencePart>(objectId, nullptr, nullptr, checker->ProgramAl… in ComposeAsyncImplFuncReturnType()
253 checker->ProgramAllocator()); in ComposeAsyncImplFuncReturnType()
255 objectId->SetParent(returnType->Part()); in ComposeAsyncImplFuncReturnType()
256 returnType->Part()->SetParent(returnType); in ComposeAsyncImplFuncReturnType()
257 …returnType->SetTsType(checker->ProgramAllocator()->New<ETSAsyncFuncReturnType>(checker->ProgramAll… in ComposeAsyncImplFuncReturnType()
258checker->Relation(), promiseType)); in ComposeAsyncImplFuncReturnType()
259 returnType->Check(checker); in ComposeAsyncImplFuncReturnType()
260 scriptFunc->Signature()->SetReturnType(returnType->TsType()); in ComposeAsyncImplFuncReturnType()
263 void CheckPredefinedMethodReturnType(ETSChecker *checker, ir::ScriptFunction *scriptFunc) in CheckPredefinedMethodReturnType() argument
265 if (scriptFunc->Signature() == nullptr) { in CheckPredefinedMethodReturnType()
266 ES2PANDA_ASSERT(checker->IsAnyError()); in CheckPredefinedMethodReturnType()
270 auto const &position = scriptFunc->Start(); in CheckPredefinedMethodReturnType()
272 …if (scriptFunc->IsGetter() && (scriptFunc->Signature()->ReturnType() == checker->GlobalVoidType())… in CheckPredefinedMethodReturnType()
273 checker->LogError(diagnostic::GETTER_VOID, {}, position); in CheckPredefinedMethodReturnType()
276 auto const name = scriptFunc->Id()->Name(); in CheckPredefinedMethodReturnType()
280 if (scriptFunc->Signature()->ReturnType() == checker->GlobalVoidType()) { in CheckPredefinedMethodReturnType()
281checker->LogError(diagnostic::UNEXPECTED_VOID, {util::StringView(methodName)}, position); in CheckPredefinedMethodReturnType()
284 if (scriptFunc->Signature()->ReturnType() != checker->GlobalVoidType()) { in CheckPredefinedMethodReturnType()
285checker->LogError(diagnostic::UNEXPECTED_NONVOID, {util::StringView(methodName)}, position); in CheckPredefinedMethodReturnType()
288 CheckIteratorMethodReturnType(checker, scriptFunc, position, methodName); in CheckPredefinedMethodReturnType()
295 auto &&iteratorInterfaceImpl) -> bool { in HasIteratorInterface()
296 if (checkType->Name().Is(ir::ITERATOR_INTERFACE_NAME)) { in HasIteratorInterface()
299 for (const auto *const interface : checkType->Interfaces()) { in HasIteratorInterface()
310 void CheckIteratorMethodReturnType(ETSChecker *checker, ir::ScriptFunction *scriptFunc, in CheckIteratorMethodReturnType() argument
313 const auto *returnType = scriptFunc->Signature()->ReturnType(); in CheckIteratorMethodReturnType()
316checker->LogError(diagnostic::MISSING_RETURN_TYPE_2, {util::StringView(methodName)}, position); in CheckIteratorMethodReturnType()
320 if (returnType->IsETSTypeParameter()) { in CheckIteratorMethodReturnType()
321 … returnType = checker->GetApparentType(returnType->AsETSTypeParameter()->GetConstraintType()); in CheckIteratorMethodReturnType()
325 if (returnType->IsETSObjectType() && HasIteratorInterface(returnType->AsETSObjectType())) { in CheckIteratorMethodReturnType()
329 while (returnType->IsETSObjectType() && returnType->AsETSObjectType()->SuperType() != nullptr) { in CheckIteratorMethodReturnType()
330 returnType = returnType->AsETSObjectType()->SuperType(); in CheckIteratorMethodReturnType()
331 if (returnType->IsETSObjectType() && HasIteratorInterface(returnType->AsETSObjectType())) { in CheckIteratorMethodReturnType()
336 checker->LogError(diagnostic::RETURN_ISNT_ITERATOR, {scriptFunc->Id()->Name()}, position); in CheckIteratorMethodReturnType()
339 static void SwitchMethodCallToFunctionCall(checker::ETSChecker *checker, ir::CallExpression *expr, … in SwitchMethodCallToFunctionCall() argument
341 ES2PANDA_ASSERT(expr->Callee()->IsMemberExpression()); in SwitchMethodCallToFunctionCall()
342 auto *memberExpr = expr->Callee()->AsMemberExpression(); in SwitchMethodCallToFunctionCall()
343 if (expr->Arguments().empty() || expr->Arguments()[0] != memberExpr->Object()) { in SwitchMethodCallToFunctionCall()
344 expr->Arguments().insert(expr->Arguments().begin(), memberExpr->Object()); in SwitchMethodCallToFunctionCall()
346 expr->SetSignature(signature); in SwitchMethodCallToFunctionCall()
347 expr->SetCallee(memberExpr->Property()); in SwitchMethodCallToFunctionCall()
348 memberExpr->Property()->AsIdentifier()->SetParent(expr); in SwitchMethodCallToFunctionCall()
349 expr->Arguments()[0]->SetParent(expr); in SwitchMethodCallToFunctionCall()
350 checker->HandleUpdatedCallExpressionNode(expr); in SwitchMethodCallToFunctionCall()
352 expr->Callee()->Check(checker); in SwitchMethodCallToFunctionCall()
355 checker::Signature *ResolveCallExtensionFunction(checker::Type *functionType, checker::ETSChecker * in ResolveCallExtensionFunction() argument
361 auto &signatures = expr->IsETSConstructorCall() in ResolveCallExtensionFunction()
362 ? functionType->AsETSObjectType()->ConstructSignatures() in ResolveCallExtensionFunction()
363 : functionType->AsETSFunctionType()->CallSignaturesOfMethodOrArrow(); in ResolveCallExtensionFunction()
364 if (expr->Callee()->IsMemberExpression()) { in ResolveCallExtensionFunction()
368 auto *memberExpr = expr->Callee()->AsMemberExpression(); in ResolveCallExtensionFunction()
369 expr->Arguments().insert(expr->Arguments().begin(), memberExpr->Object()); in ResolveCallExtensionFunction()
370 …auto *signature = checker->ResolveCallExpressionAndTrailingLambda(signatures, expr, expr->Start(),… in ResolveCallExtensionFunction()
372 expr->Arguments().erase(expr->Arguments().begin()); in ResolveCallExtensionFunction()
375 if (!signature->HasSignatureFlag(SignatureFlags::EXTENSION_FUNCTION)) { in ResolveCallExtensionFunction()
376 checker->LogError(diagnostic::PROPERTY_NONEXISTENT, in ResolveCallExtensionFunction()
377 … {memberExpr->Property()->AsIdentifier()->Name(), memberExpr->ObjType()->Name()}, in ResolveCallExtensionFunction()
378 memberExpr->Property()->Start()); in ResolveCallExtensionFunction()
382 SwitchMethodCallToFunctionCall(checker, expr, signature); in ResolveCallExtensionFunction()
386 return checker->ResolveCallExpressionAndTrailingLambda(signatures, expr, expr->Start()); in ResolveCallExtensionFunction()
389 checker::Signature *ResolveCallForClassMethod(checker::ETSExtensionFuncHelperType *type, checker::E… in ResolveCallForClassMethod() argument
392 ES2PANDA_ASSERT(expr->Callee()->IsMemberExpression()); in ResolveCallForClassMethod()
394 …auto signature = checker->ResolveCallExpressionAndTrailingLambda(type->ClassMethodType()->CallSign… in ResolveCallForClassMethod()
395 expr->Start(), reportFlag); in ResolveCallForClassMethod()
397 auto *memberExpr = expr->Callee()->AsMemberExpression(); in ResolveCallForClassMethod()
398 auto *var = type->ClassMethodType()->Variable(); in ResolveCallForClassMethod()
399 memberExpr->Property()->AsIdentifier()->SetVariable(var); in ResolveCallForClassMethod()
404 checker::Signature *GetMostSpecificSigFromExtensionFuncAndClassMethod(checker::ETSExtensionFuncHelp… in GetMostSpecificSigFromExtensionFuncAndClassMethod()
405 checker::ETSChecker *checker, in GetMostSpecificSigFromExtensionFuncAndClassMethod() argument
412 ArenaVector<Signature *> signatures(checker->ProgramAllocator()->Adapter()); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
413 signatures.insert(signatures.end(), type->ClassMethodType()->CallSignatures().begin(), in GetMostSpecificSigFromExtensionFuncAndClassMethod()
414 type->ClassMethodType()->CallSignatures().end()); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
415 signatures.insert(signatures.end(), type->ExtensionMethodType()->CallSignatures().begin(), in GetMostSpecificSigFromExtensionFuncAndClassMethod()
416 type->ExtensionMethodType()->CallSignatures().end()); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
418 auto *memberExpr = expr->Callee()->AsMemberExpression(); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
419 auto *dummyReceiver = memberExpr->Object(); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
420 auto *dummyReceiverVar = type->ExtensionMethodType()->CallSignatures()[0]->Params()[0]; in GetMostSpecificSigFromExtensionFuncAndClassMethod()
421 expr->Arguments().insert(expr->Arguments().begin(), dummyReceiver); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
422 const bool typeParamsNeeded = dummyReceiverVar->TsType()->IsETSObjectType(); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
424 for (auto *methodCallSig : type->ClassMethodType()->CallSignatures()) { in GetMostSpecificSigFromExtensionFuncAndClassMethod()
425 methodCallSig->GetSignatureInfo()->minArgCount++; in GetMostSpecificSigFromExtensionFuncAndClassMethod()
426 auto &paramsVar = methodCallSig->Params(); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
428 auto &params = methodCallSig->Function()->Params(); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
431 auto &typeParams = methodCallSig->TypeParams(); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
432 …typeParams.insert(typeParams.end(), dummyReceiverVar->TsType()->AsETSObjectType()->TypeArguments()… in GetMostSpecificSigFromExtensionFuncAndClassMethod()
433 dummyReceiverVar->TsType()->AsETSObjectType()->TypeArguments().end()); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
437 … auto *signature = checker->ResolveCallExpressionAndTrailingLambda(signatures, expr, expr->Start(), in GetMostSpecificSigFromExtensionFuncAndClassMethod()
438checker::TypeRelationFlag::NO_THROW); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
440 for (auto *methodCallSig : type->ClassMethodType()->CallSignatures()) { in GetMostSpecificSigFromExtensionFuncAndClassMethod()
441 methodCallSig->GetSignatureInfo()->minArgCount--; in GetMostSpecificSigFromExtensionFuncAndClassMethod()
442 auto &paramsVar = methodCallSig->Params(); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
444 auto &params = methodCallSig->Function()->Params(); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
447 auto &typeParams = methodCallSig->TypeParams(); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
448 typeParams.resize(typeParams.size() - in GetMostSpecificSigFromExtensionFuncAndClassMethod()
449 … dummyReceiverVar->TsType()->AsETSObjectType()->TypeArguments().size()); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
452 expr->Arguments().erase(expr->Arguments().begin()); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
455 if (signature->Owner()->GetDeclNode()->IsClassDefinition() && in GetMostSpecificSigFromExtensionFuncAndClassMethod()
456 signature->Owner()->GetDeclNode()->AsClassDefinition()->IsGlobal()) { in GetMostSpecificSigFromExtensionFuncAndClassMethod()
457 SwitchMethodCallToFunctionCall(checker, expr, signature); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
459 auto *var = type->ClassMethodType()->Variable(); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
460 memberExpr->Property()->AsIdentifier()->SetVariable(var); in GetMostSpecificSigFromExtensionFuncAndClassMethod()
466 checker::Signature *ResolveCallForETSExtensionFuncHelperType(checker::ETSExtensionFuncHelperType *t… in ResolveCallForETSExtensionFuncHelperType()
467checker::ETSChecker *checker, ir::CallExpression *expr) in ResolveCallForETSExtensionFuncHelperType() argument
469 ES2PANDA_ASSERT(expr->Callee()->IsMemberExpression()); in ResolveCallForETSExtensionFuncHelperType()
470 auto *calleeObj = expr->Callee()->AsMemberExpression()->Object(); in ResolveCallForETSExtensionFuncHelperType()
471 …bool isCalleeObjETSGlobal = calleeObj->TsType()->AsETSObjectType()->GetDeclNode()->IsClassDefiniti… in ResolveCallForETSExtensionFuncHelperType()
472 … calleeObj->TsType()->AsETSObjectType()->GetDeclNode()->AsClassDefinition()->IsGlobal(); in ResolveCallForETSExtensionFuncHelperType()
478 …if (checker->IsTypeIdenticalTo(checker->Context().ContainingClass(), calleeObj->TsType()) || isCal… in ResolveCallForETSExtensionFuncHelperType()
480 … signature = ResolveCallForClassMethod(type, checker, expr, checker::TypeRelationFlag::NO_THROW); in ResolveCallForETSExtensionFuncHelperType()
486 signature = GetMostSpecificSigFromExtensionFuncAndClassMethod(type, checker, expr); in ResolveCallForETSExtensionFuncHelperType()
488checker->ThrowSignatureMismatch(type->ExtensionMethodType()->CallSignatures(), expr->Arguments(), … in ResolveCallForETSExtensionFuncHelperType()
495 ArenaVector<checker::Signature *> GetUnionTypeSignatures(ETSChecker *checker, checker::ETSUnionType… in GetUnionTypeSignatures() argument
497 ArenaVector<checker::Signature *> callSignatures(checker->ProgramAllocator()->Adapter()); in GetUnionTypeSignatures()
499 for (auto *constituentType : etsUnionType->ConstituentTypes()) { in GetUnionTypeSignatures()
500 if (constituentType->IsETSFunctionType()) { in GetUnionTypeSignatures()
501 … ArenaVector<checker::Signature *> tmpCallSignatures(checker->ProgramAllocator()->Adapter()); in GetUnionTypeSignatures()
502 tmpCallSignatures = constituentType->AsETSFunctionType()->CallSignatures(); in GetUnionTypeSignatures()
505 if (constituentType->IsETSUnionType()) { in GetUnionTypeSignatures()
506 … ArenaVector<checker::Signature *> tmpCallSignatures(checker->ProgramAllocator()->Adapter()); in GetUnionTypeSignatures()
507 tmpCallSignatures = GetUnionTypeSignatures(checker, constituentType->AsETSUnionType()); in GetUnionTypeSignatures()
515 void ProcessExclamationMark(ETSChecker *checker, ir::UnaryExpression *expr, checker::Type *operandT… in ProcessExclamationMark() argument
517 if (checker->IsNullLikeOrVoidExpression(expr->Argument())) { in ProcessExclamationMark()
518 auto tsType = checker->CreateETSBooleanType(true); in ProcessExclamationMark()
520 tsType->AddTypeFlag(checker::TypeFlag::CONSTANT); in ProcessExclamationMark()
521 expr->SetTsType(tsType); in ProcessExclamationMark()
525 if (operandType == nullptr || operandType->IsTypeError()) { in ProcessExclamationMark()
526 expr->SetTsType(checker->GlobalTypeError()); in ProcessExclamationMark()
530 auto exprRes = operandType->ResolveConditionExpr(); in ProcessExclamationMark()
532 auto tsType = checker->CreateETSBooleanType(!std::get<1>(exprRes)); in ProcessExclamationMark()
533 tsType->AddTypeFlag(checker::TypeFlag::CONSTANT); in ProcessExclamationMark()
534 expr->SetTsType(tsType); in ProcessExclamationMark()
537 expr->SetTsType(checker->GlobalETSBooleanType()); in ProcessExclamationMark()
540 void SetTsTypeForUnaryExpression(ETSChecker *checker, ir::UnaryExpression *expr, checker::Type *ope… in SetTsTypeForUnaryExpression() argument
542 switch (expr->OperatorType()) { in SetTsTypeForUnaryExpression()
545 …if (operandType == nullptr || !operandType->HasTypeFlag(checker::TypeFlag::ETS_CONVERTIBLE_TO_NUME… in SetTsTypeForUnaryExpression()
546 checker->LogError(diagnostic::OPERAND_NOT_NUMERIC, {}, expr->Argument()->Start()); in SetTsTypeForUnaryExpression()
547 expr->SetTsType(checker->GlobalTypeError()); in SetTsTypeForUnaryExpression()
551 if (operandType->HasTypeFlag(checker::TypeFlag::CONSTANT) && in SetTsTypeForUnaryExpression()
552 expr->OperatorType() == lexer::TokenType::PUNCTUATOR_MINUS) { in SetTsTypeForUnaryExpression()
553 expr->SetTsType(checker->NegateNumericType(operandType, expr)); in SetTsTypeForUnaryExpression()
557 expr->SetTsType(operandType); in SetTsTypeForUnaryExpression()
561 …if (operandType == nullptr || !operandType->HasTypeFlag(checker::TypeFlag::ETS_CONVERTIBLE_TO_NUME… in SetTsTypeForUnaryExpression()
562 checker->LogError(diagnostic::OPERAND_NOT_NUMERIC, {}, expr->Argument()->Start()); in SetTsTypeForUnaryExpression()
563 expr->SetTsType(checker->GlobalTypeError()); in SetTsTypeForUnaryExpression()
567 …expr->Argument()->SetTsType(expr->SetTsType(checker->SelectGlobalIntegerTypeForNumeric(operandType… in SetTsTypeForUnaryExpression()
571 ProcessExclamationMark(checker, expr, operandType); in SetTsTypeForUnaryExpression()
580 checker::Type *GetIteratorType(ETSChecker *checker, checker::Type *elemType, ir::AstNode *left) in GetIteratorType() argument
583 // CC-OFFNXT(G.FMT.14-CPP) project code style in GetIteratorType()
584 …auto const getIterType = [checker, elemType](ir::VariableDeclarator *const declarator) -> checker:… in GetIteratorType()
585 if (declarator->TsType() == nullptr) { in GetIteratorType()
586 …if (auto *resolved = checker->FindVariableInFunctionScope(declarator->Id()->AsIdentifier()->Name()… in GetIteratorType()
588 resolved->SetTsType(elemType); in GetIteratorType()
592 return declarator->TsType(); in GetIteratorType()
594 return checker->GlobalTypeError(); in GetIteratorType()
597 checker::Type *iterType = nullptr; in GetIteratorType()
598 if (left->IsIdentifier()) { in GetIteratorType()
599 auto *const variable = left->Variable(); in GetIteratorType()
600 ES2PANDA_ASSERT(variable != nullptr && variable->Declaration() != nullptr); in GetIteratorType()
602 auto *decl = variable->Declaration(); in GetIteratorType()
603 if (decl->IsConstDecl() || decl->IsReadonlyDecl()) { in GetIteratorType()
605 …decl->IsConstDecl() ? diagnostic::INVALID_CONST_ASSIGNMENT : diagnostic::INVALID_READONLY_ASSIGNME… in GetIteratorType()
606 checker->LogError(errorMsg, {variable->Name()}, decl->Node()->Start()); in GetIteratorType()
608 iterType = left->AsIdentifier()->TsType(); in GetIteratorType()
609 } else if (left->IsVariableDeclaration()) { in GetIteratorType()
610 …if (auto const &declarators = left->AsVariableDeclaration()->Declarators(); !declarators.empty()) { in GetIteratorType()
616 checker->LogError(diagnostic::ITERATOR_TYPE_ABSENT, {}, left->Start()); in GetIteratorType()
617 return checker->GlobalTypeError(); in GetIteratorType()
619 return checker->GetNonConstantType(iterType); in GetIteratorType()
622 bool CheckArgumentVoidType(checker::Type *&funcReturnType, ETSChecker *checker, const std::string &… in CheckArgumentVoidType() argument
626 if (!funcReturnType->IsETSVoidType() && !funcReturnType->IsIntType()) { in CheckArgumentVoidType()
627 checker->LogError(diagnostic::MAIN_BAD_RETURN, {}, st->Start()); in CheckArgumentVoidType()
633 bool CheckReturnType(ETSChecker *checker, checker::Type *funcReturnType, checker::Type *argumentTyp… in CheckReturnType() argument
636 if (funcReturnType->IsETSVoidType() || funcReturnType == checker->GlobalVoidType()) { in CheckReturnType()
637 if (argumentType != checker->GlobalVoidType()) { in CheckReturnType()
638 checker->LogError(diagnostic::UNEXPECTED_VALUE_RETURN, {}, stArgument->Start()); in CheckReturnType()
641 … if (!checker::AssignmentContext(checker->Relation(), stArgument, argumentType, funcReturnType, in CheckReturnType()
642 stArgument->Start(), std::nullopt, in CheckReturnType()
643checker::TypeRelationFlag::DIRECT_RETURN | checker::TypeRelationFlag::NO_THROW) in CheckReturnType()
644 // CC-OFFNXT(G.FMT.02) project code style in CheckReturnType()
646 checker->LogError(diagnostic::RETURN_TYPE_MISMATCH, {}, stArgument->Start()); in CheckReturnType()
652 if (containingFunc->IsAsyncFunc() && funcReturnType->IsETSObjectType() && in CheckReturnType()
653 … funcReturnType->AsETSObjectType()->GetOriginalBaseType() == checker->GlobalBuiltinPromiseType()) { in CheckReturnType()
654 auto promiseArg = funcReturnType->AsETSObjectType()->TypeArguments()[0]; in CheckReturnType()
655checker::AssignmentContext(checker->Relation(), stArgument, argumentType, promiseArg, stArgument->… in CheckReturnType()
657checker::TypeRelationFlag::DIRECT_RETURN | checker::TypeRelationFlag::NO_THROW); in CheckReturnType()
658 if (checker->Relation()->IsTrue()) { in CheckReturnType()
663 …if (!checker::AssignmentContext(checker->Relation(), stArgument, argumentType, funcReturnType, stA… in CheckReturnType()
665checker::TypeRelationFlag::DIRECT_RETURN | checker::TypeRelationFlag::NO_THROW) in CheckReturnType()
666 // CC-OFFNXT(G.FMT.02) project code style in CheckReturnType()
668checker->LogError(diagnostic::ARROW_TYPE_MISMATCH, {argumentType, funcReturnType}, stArgument->Sta… in CheckReturnType()
674 void InferReturnType(ETSChecker *checker, ir::ScriptFunction *containingFunc, checker::Type *&funcR… in InferReturnType() argument
679 …stArgument == nullptr ? checker->GlobalVoidType() : checker->GetNonConstantType(stArgument->Check( in InferReturnType()
681 if (funcReturnType->IsTypeError()) { in InferReturnType()
682 containingFunc->Signature()->RemoveSignatureFlag(checker::SignatureFlags::NEED_RETURN_TYPE); in InferReturnType()
693 if (stArgument != nullptr && stArgument->IsArrowFunctionExpression()) { in InferReturnType()
694 auto arrowFunc = stArgument->AsArrowFunctionExpression(); in InferReturnType()
695 auto typeAnnotation = arrowFunc->CreateTypeAnnotation(checker); in InferReturnType()
697 auto *argumentType = arrowFunc->TsType(); in InferReturnType()
699 funcReturnType = typeAnnotation->GetType(checker); in InferReturnType()
700 … if (!checker::AssignmentContext(checker->Relation(), arrowFunc, argumentType, funcReturnType, in InferReturnType()
701 stArgument->Start(), std::nullopt, in InferReturnType()
702checker::TypeRelationFlag::DIRECT_RETURN | checker::TypeRelationFlag::NO_THROW) in InferReturnType()
703 // CC-OFFNXT(G.FMT.02) project code style in InferReturnType()
705checker->LogError(diagnostic::ARROW_TYPE_MISMATCH, {argumentType, funcReturnType}, stArgument->Sta… in InferReturnType()
706 funcReturnType = checker->GlobalTypeError(); in InferReturnType()
711 containingFunc->Signature()->SetReturnType(funcReturnType); in InferReturnType()
712 containingFunc->Signature()->RemoveSignatureFlag(checker::SignatureFlags::NEED_RETURN_TYPE); in InferReturnType()
713 containingFunc->Signature()->AddSignatureFlag(checker::SignatureFlags::INFERRED_RETURN_TYPE); in InferReturnType()
714 checker->VarBinder()->AsETSBinder()->BuildFunctionName(containingFunc); in InferReturnType()
716 if (stArgument != nullptr && stArgument->IsObjectExpression()) { in InferReturnType()
717 stArgument->AsObjectExpression()->SetPreferredType(funcReturnType); in InferReturnType()
721 bool IsArrayExpressionValidInitializerForType(ETSChecker *checker, const Type *const arrayExprPrefe… in IsArrayExpressionValidInitializerForType() argument
724 … || arrayExprPreferredType->IsETSArrayType() // valid for fixed array type in IsArrayExpressionValidInitializerForType()
725 … || arrayExprPreferredType->IsETSResizableArrayType() // valid for resizable array type in IsArrayExpressionValidInitializerForType()
726 … || arrayExprPreferredType->IsETSTupleType() // valid for tuple type in IsArrayExpressionValidInitializerForType()
727 … || checker->Relation()->IsSupertypeOf(arrayExprPreferredType, // valid for 'Object' in IsArrayExpressionValidInitializerForType()
728checker->GlobalETSObjectType()); in IsArrayExpressionValidInitializerForType()
733 void CastPossibleTupleOnRHS(ETSChecker *checker, ir::AssignmentExpression *expr) in CastPossibleTupleOnRHS() argument
735 if (expr->Left()->IsMemberExpression() && in CastPossibleTupleOnRHS()
736 expr->Left()->AsMemberExpression()->Object()->TsType()->IsETSTupleType() && in CastPossibleTupleOnRHS()
737 expr->OperatorType() == lexer::TokenType::PUNCTUATOR_SUBSTITUTION) { in CastPossibleTupleOnRHS()
738 auto *storedTupleType = expr->Left()->AsMemberExpression()->Object()->TsType(); in CastPossibleTupleOnRHS()
740 const checker::CastingContext tupleCast( in CastPossibleTupleOnRHS()
741 checker->Relation(), diagnostic::CAST_FAIL_UNREACHABLE, {}, in CastPossibleTupleOnRHS()
742checker::CastingContext::ConstructorData {expr->Right(), expr->Right()->TsType(), storedTupleType, in CastPossibleTupleOnRHS()
743 … expr->Right()->Start(), TypeRelationFlag::NO_THROW}); in CastPossibleTupleOnRHS()
747 void ProcessReturnStatements(ETSChecker *checker, ir::ScriptFunction *containingFunc, checker::Type… in ProcessReturnStatements() argument
750 funcReturnType = containingFunc->Signature()->ReturnType(); in ProcessReturnStatements()
754 if (!funcReturnType->IsETSVoidType() && funcReturnType != checker->GlobalVoidType()) { in ProcessReturnStatements()
755 checker->LogError(diagnostic::MIXED_VOID_NONVOID, {}, st->Start()); in ProcessReturnStatements()
759 if (stArgument->IsObjectExpression()) { in ProcessReturnStatements()
760 stArgument->AsObjectExpression()->SetPreferredType(funcReturnType); in ProcessReturnStatements()
763 if (stArgument->IsMemberExpression()) { in ProcessReturnStatements()
764checker->SetArrayPreferredTypeForNestedMemberExpressions(stArgument->AsMemberExpression(), funcRet… in ProcessReturnStatements()
767 checker::Type *argumentType = checker->GetNonConstantType(stArgument->Check(checker)); in ProcessReturnStatements()
771 if (funcReturnType->IsETSVoidType() && !argumentType->IsETSVoidType()) { in ProcessReturnStatements()
772 checker->LogError(diagnostic::MIXED_VOID_NONVOID, {}, stArgument->Start()); in ProcessReturnStatements()
776 const auto name = containingFunc->Scope()->InternalName().Mutf8(); in ProcessReturnStatements()
777 if (!CheckArgumentVoidType(funcReturnType, checker, name, st)) { in ProcessReturnStatements()
781 auto *const relation = checker->Relation(); in ProcessReturnStatements()
782 relation->SetNode(stArgument); in ProcessReturnStatements()
784 if (!relation->IsIdenticalTo(funcReturnType, argumentType)) { in ProcessReturnStatements()
785 checker->ResolveReturnStatement(funcReturnType, argumentType, containingFunc, st); in ProcessReturnStatements()
788 relation->SetNode(nullptr); in ProcessReturnStatements()
789 relation->SetFlags(checker::TypeRelationFlag::NONE); in ProcessReturnStatements()
796 auto *scriptFunc = node->Function(); in CheckReturnTypeNecessity()
797 needReturnType &= (node->IsNative() || node->IsDeclare()); in CheckReturnTypeNecessity()
798 needReturnType &= !node->IsConstructor(); in CheckReturnTypeNecessity()
800 needReturnType &= !scriptFunc->IsSetter(); in CheckReturnTypeNecessity()
804 void CheckAllConstPropertyInitialized(checker::ETSChecker *checker, ir::ETSModule *pkg) in CheckAllConstPropertyInitialized() argument
806 …auto globalDecl = std::find_if(pkg->Statements().begin(), pkg->Statements().end(), [](ir::AstNode … in CheckAllConstPropertyInitialized()
807 return node->IsClassDeclaration() && node->AsClassDeclaration()->Definition()->IsGlobal(); in CheckAllConstPropertyInitialized()
809 if (globalDecl == pkg->Statements().end()) { in CheckAllConstPropertyInitialized()
813 …auto const &globalClassBody = (*globalDecl)->AsClassDeclaration()->Definition()->AsClassDefinition… in CheckAllConstPropertyInitialized()
815 if (!prop->IsClassProperty()) { in CheckAllConstPropertyInitialized()
819 …if (prop->AsClassProperty()->Key()->Variable()->HasFlag(varbinder::VariableFlags::INIT_IN_STATIC_B… in CheckAllConstPropertyInitialized()
820 … !prop->AsClassProperty()->Key()->Variable()->HasFlag(varbinder::VariableFlags::INITIALIZED)) { in CheckAllConstPropertyInitialized()
821 checker->LogError(diagnostic::MISSING_INIT_FOR_CONST_PACKAGE_PROP, {}, prop->Start()); in CheckAllConstPropertyInitialized()
826 } // namespace ark::es2panda::checker