| //===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===// |
| // |
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| // See https://llvm.org/LICENSE.txt for license information. |
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| // |
| //===----------------------------------------------------------------------===// |
| // |
| // This file implements semantic analysis for Objective C declarations. |
| // |
| //===----------------------------------------------------------------------===// |
| |
| #include "TypeLocBuilder.h" |
| #include "clang/AST/ASTConsumer.h" |
| #include "clang/AST/ASTContext.h" |
| #include "clang/AST/ASTMutationListener.h" |
| #include "clang/AST/DeclObjC.h" |
| #include "clang/AST/Expr.h" |
| #include "clang/AST/ExprObjC.h" |
| #include "clang/AST/RecursiveASTVisitor.h" |
| #include "clang/Basic/SourceManager.h" |
| #include "clang/Sema/DeclSpec.h" |
| #include "clang/Sema/Lookup.h" |
| #include "clang/Sema/Scope.h" |
| #include "clang/Sema/ScopeInfo.h" |
| #include "clang/Sema/SemaInternal.h" |
| #include "llvm/ADT/DenseMap.h" |
| #include "llvm/ADT/DenseSet.h" |
| |
| using namespace clang; |
| |
| /// Check whether the given method, which must be in the 'init' |
| /// family, is a valid member of that family. |
| /// |
| /// \param receiverTypeIfCall - if null, check this as if declaring it; |
| /// if non-null, check this as if making a call to it with the given |
| /// receiver type |
| /// |
| /// \return true to indicate that there was an error and appropriate |
| /// actions were taken |
| bool Sema::checkInitMethod(ObjCMethodDecl *method, |
| QualType receiverTypeIfCall) { |
| if (method->isInvalidDecl()) return true; |
| |
| // This castAs is safe: methods that don't return an object |
| // pointer won't be inferred as inits and will reject an explicit |
| // objc_method_family(init). |
| |
| // We ignore protocols here. Should we? What about Class? |
| |
| const ObjCObjectType *result = |
| method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType(); |
| |
| if (result->isObjCId()) { |
| return false; |
| } else if (result->isObjCClass()) { |
| // fall through: always an error |
| } else { |
| ObjCInterfaceDecl *resultClass = result->getInterface(); |
| assert(resultClass && "unexpected object type!"); |
| |
| // It's okay for the result type to still be a forward declaration |
| // if we're checking an interface declaration. |
| if (!resultClass->hasDefinition()) { |
| if (receiverTypeIfCall.isNull() && |
| !isa<ObjCImplementationDecl>(method->getDeclContext())) |
| return false; |
| |
| // Otherwise, we try to compare class types. |
| } else { |
| // If this method was declared in a protocol, we can't check |
| // anything unless we have a receiver type that's an interface. |
| const ObjCInterfaceDecl *receiverClass = nullptr; |
| if (isa<ObjCProtocolDecl>(method->getDeclContext())) { |
| if (receiverTypeIfCall.isNull()) |
| return false; |
| |
| receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>() |
| ->getInterfaceDecl(); |
| |
| // This can be null for calls to e.g. id<Foo>. |
| if (!receiverClass) return false; |
| } else { |
| receiverClass = method->getClassInterface(); |
| assert(receiverClass && "method not associated with a class!"); |
| } |
| |
| // If either class is a subclass of the other, it's fine. |
| if (receiverClass->isSuperClassOf(resultClass) || |
| resultClass->isSuperClassOf(receiverClass)) |
| return false; |
| } |
| } |
| |
| SourceLocation loc = method->getLocation(); |
| |
| // If we're in a system header, and this is not a call, just make |
| // the method unusable. |
| if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) { |
| method->addAttr(UnavailableAttr::CreateImplicit(Context, "", |
| UnavailableAttr::IR_ARCInitReturnsUnrelated, loc)); |
| return true; |
| } |
| |
| // Otherwise, it's an error. |
| Diag(loc, diag::err_arc_init_method_unrelated_result_type); |
| method->setInvalidDecl(); |
| return true; |
| } |
| |
| /// Issue a warning if the parameter of the overridden method is non-escaping |
| /// but the parameter of the overriding method is not. |
| static bool diagnoseNoescape(const ParmVarDecl *NewD, const ParmVarDecl *OldD, |
| Sema &S) { |
| if (OldD->hasAttr<NoEscapeAttr>() && !NewD->hasAttr<NoEscapeAttr>()) { |
| S.Diag(NewD->getLocation(), diag::warn_overriding_method_missing_noescape); |
| S.Diag(OldD->getLocation(), diag::note_overridden_marked_noescape); |
| return false; |
| } |
| |
| return true; |
| } |
| |
| /// Produce additional diagnostics if a category conforms to a protocol that |
| /// defines a method taking a non-escaping parameter. |
| static void diagnoseNoescape(const ParmVarDecl *NewD, const ParmVarDecl *OldD, |
| const ObjCCategoryDecl *CD, |
| const ObjCProtocolDecl *PD, Sema &S) { |
| if (!diagnoseNoescape(NewD, OldD, S)) |
| S.Diag(CD->getLocation(), diag::note_cat_conform_to_noescape_prot) |
| << CD->IsClassExtension() << PD |
| << cast<ObjCMethodDecl>(NewD->getDeclContext()); |
| } |
| |
| void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod, |
| const ObjCMethodDecl *Overridden) { |
| if (Overridden->hasRelatedResultType() && |
| !NewMethod->hasRelatedResultType()) { |
| // This can only happen when the method follows a naming convention that |
| // implies a related result type, and the original (overridden) method has |
| // a suitable return type, but the new (overriding) method does not have |
| // a suitable return type. |
| QualType ResultType = NewMethod->getReturnType(); |
| SourceRange ResultTypeRange = NewMethod->getReturnTypeSourceRange(); |
| |
| // Figure out which class this method is part of, if any. |
| ObjCInterfaceDecl *CurrentClass |
| = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext()); |
| if (!CurrentClass) { |
| DeclContext *DC = NewMethod->getDeclContext(); |
| if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC)) |
| CurrentClass = Cat->getClassInterface(); |
| else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC)) |
| CurrentClass = Impl->getClassInterface(); |
| else if (ObjCCategoryImplDecl *CatImpl |
| = dyn_cast<ObjCCategoryImplDecl>(DC)) |
| CurrentClass = CatImpl->getClassInterface(); |
| } |
| |
| if (CurrentClass) { |
| Diag(NewMethod->getLocation(), |
| diag::warn_related_result_type_compatibility_class) |
| << Context.getObjCInterfaceType(CurrentClass) |
| << ResultType |
| << ResultTypeRange; |
| } else { |
| Diag(NewMethod->getLocation(), |
| diag::warn_related_result_type_compatibility_protocol) |
| << ResultType |
| << ResultTypeRange; |
| } |
| |
| if (ObjCMethodFamily Family = Overridden->getMethodFamily()) |
| Diag(Overridden->getLocation(), |
| diag::note_related_result_type_family) |
| << /*overridden method*/ 0 |
| << Family; |
| else |
| Diag(Overridden->getLocation(), |
| diag::note_related_result_type_overridden); |
| } |
| |
| if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() != |
| Overridden->hasAttr<NSReturnsRetainedAttr>())) { |
| Diag(NewMethod->getLocation(), |
| getLangOpts().ObjCAutoRefCount |
| ? diag::err_nsreturns_retained_attribute_mismatch |
| : diag::warn_nsreturns_retained_attribute_mismatch) |
| << 1; |
| Diag(Overridden->getLocation(), diag::note_previous_decl) << "method"; |
| } |
| if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() != |
| Overridden->hasAttr<NSReturnsNotRetainedAttr>())) { |
| Diag(NewMethod->getLocation(), |
| getLangOpts().ObjCAutoRefCount |
| ? diag::err_nsreturns_retained_attribute_mismatch |
| : diag::warn_nsreturns_retained_attribute_mismatch) |
| << 0; |
| Diag(Overridden->getLocation(), diag::note_previous_decl) << "method"; |
| } |
| |
| ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(), |
| oe = Overridden->param_end(); |
| for (ObjCMethodDecl::param_iterator ni = NewMethod->param_begin(), |
| ne = NewMethod->param_end(); |
| ni != ne && oi != oe; ++ni, ++oi) { |
| const ParmVarDecl *oldDecl = (*oi); |
| ParmVarDecl *newDecl = (*ni); |
| if (newDecl->hasAttr<NSConsumedAttr>() != |
| oldDecl->hasAttr<NSConsumedAttr>()) { |
| Diag(newDecl->getLocation(), |
| getLangOpts().ObjCAutoRefCount |
| ? diag::err_nsconsumed_attribute_mismatch |
| : diag::warn_nsconsumed_attribute_mismatch); |
| Diag(oldDecl->getLocation(), diag::note_previous_decl) << "parameter"; |
| } |
| |
| diagnoseNoescape(newDecl, oldDecl, *this); |
| } |
| } |
| |
| /// Check a method declaration for compatibility with the Objective-C |
| /// ARC conventions. |
| bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) { |
| ObjCMethodFamily family = method->getMethodFamily(); |
| switch (family) { |
| case OMF_None: |
| case OMF_finalize: |
| case OMF_retain: |
| case OMF_release: |
| case OMF_autorelease: |
| case OMF_retainCount: |
| case OMF_self: |
| case OMF_initialize: |
| case OMF_performSelector: |
| return false; |
| |
| case OMF_dealloc: |
| if (!Context.hasSameType(method->getReturnType(), Context.VoidTy)) { |
| SourceRange ResultTypeRange = method->getReturnTypeSourceRange(); |
| if (ResultTypeRange.isInvalid()) |
| Diag(method->getLocation(), diag::err_dealloc_bad_result_type) |
| << method->getReturnType() |
| << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)"); |
| else |
| Diag(method->getLocation(), diag::err_dealloc_bad_result_type) |
| << method->getReturnType() |
| << FixItHint::CreateReplacement(ResultTypeRange, "void"); |
| return true; |
| } |
| return false; |
| |
| case OMF_init: |
| // If the method doesn't obey the init rules, don't bother annotating it. |
| if (checkInitMethod(method, QualType())) |
| return true; |
| |
| method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context)); |
| |
| // Don't add a second copy of this attribute, but otherwise don't |
| // let it be suppressed. |
| if (method->hasAttr<NSReturnsRetainedAttr>()) |
| return false; |
| break; |
| |
| case OMF_alloc: |
| case OMF_copy: |
| case OMF_mutableCopy: |
| case OMF_new: |
| if (method->hasAttr<NSReturnsRetainedAttr>() || |
| method->hasAttr<NSReturnsNotRetainedAttr>() || |
| method->hasAttr<NSReturnsAutoreleasedAttr>()) |
| return false; |
| break; |
| } |
| |
| method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context)); |
| return false; |
| } |
| |
| static void DiagnoseObjCImplementedDeprecations(Sema &S, const NamedDecl *ND, |
| SourceLocation ImplLoc) { |
| if (!ND) |
| return; |
| bool IsCategory = false; |
| StringRef RealizedPlatform; |
| AvailabilityResult Availability = ND->getAvailability( |
| /*Message=*/nullptr, /*EnclosingVersion=*/VersionTuple(), |
| &RealizedPlatform); |
| if (Availability != AR_Deprecated) { |
| if (isa<ObjCMethodDecl>(ND)) { |
| if (Availability != AR_Unavailable) |
| return; |
| if (RealizedPlatform.empty()) |
| RealizedPlatform = S.Context.getTargetInfo().getPlatformName(); |
| // Warn about implementing unavailable methods, unless the unavailable |
| // is for an app extension. |
| if (RealizedPlatform.endswith("_app_extension")) |
| return; |
| S.Diag(ImplLoc, diag::warn_unavailable_def); |
| S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| << ND->getDeclName(); |
| return; |
| } |
| if (const auto *CD = dyn_cast<ObjCCategoryDecl>(ND)) { |
| if (!CD->getClassInterface()->isDeprecated()) |
| return; |
| ND = CD->getClassInterface(); |
| IsCategory = true; |
| } else |
| return; |
| } |
| S.Diag(ImplLoc, diag::warn_deprecated_def) |
| << (isa<ObjCMethodDecl>(ND) |
| ? /*Method*/ 0 |
| : isa<ObjCCategoryDecl>(ND) || IsCategory ? /*Category*/ 2 |
| : /*Class*/ 1); |
| if (isa<ObjCMethodDecl>(ND)) |
| S.Diag(ND->getLocation(), diag::note_method_declared_at) |
| << ND->getDeclName(); |
| else |
| S.Diag(ND->getLocation(), diag::note_previous_decl) |
| << (isa<ObjCCategoryDecl>(ND) ? "category" : "class"); |
| } |
| |
| /// AddAnyMethodToGlobalPool - Add any method, instance or factory to global |
| /// pool. |
| void Sema::AddAnyMethodToGlobalPool(Decl *D) { |
| ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
| |
| // If we don't have a valid method decl, simply return. |
| if (!MDecl) |
| return; |
| if (MDecl->isInstanceMethod()) |
| AddInstanceMethodToGlobalPool(MDecl, true); |
| else |
| AddFactoryMethodToGlobalPool(MDecl, true); |
| } |
| |
| /// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer |
| /// has explicit ownership attribute; false otherwise. |
| static bool |
| HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) { |
| QualType T = Param->getType(); |
| |
| if (const PointerType *PT = T->getAs<PointerType>()) { |
| T = PT->getPointeeType(); |
| } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) { |
| T = RT->getPointeeType(); |
| } else { |
| return true; |
| } |
| |
| // If we have a lifetime qualifier, but it's local, we must have |
| // inferred it. So, it is implicit. |
| return !T.getLocalQualifiers().hasObjCLifetime(); |
| } |
| |
| /// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible |
| /// and user declared, in the method definition's AST. |
| void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) { |
| ImplicitlyRetainedSelfLocs.clear(); |
| assert((getCurMethodDecl() == nullptr) && "Methodparsing confused"); |
| ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D); |
| |
| PushExpressionEvaluationContext(ExprEvalContexts.back().Context); |
| |
| // If we don't have a valid method decl, simply return. |
| if (!MDecl) |
| return; |
| |
| QualType ResultType = MDecl->getReturnType(); |
| if (!ResultType->isDependentType() && !ResultType->isVoidType() && |
| !MDecl->isInvalidDecl() && |
| RequireCompleteType(MDecl->getLocation(), ResultType, |
| diag::err_func_def_incomplete_result)) |
| MDecl->setInvalidDecl(); |
| |
| // Allow all of Sema to see that we are entering a method definition. |
| PushDeclContext(FnBodyScope, MDecl); |
| PushFunctionScope(); |
| |
| // Create Decl objects for each parameter, entrring them in the scope for |
| // binding to their use. |
| |
| // Insert the invisible arguments, self and _cmd! |
| MDecl->createImplicitParams(Context, MDecl->getClassInterface()); |
| |
| PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope); |
| PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope); |
| |
| // The ObjC parser requires parameter names so there's no need to check. |
| CheckParmsForFunctionDef(MDecl->parameters(), |
| /*CheckParameterNames=*/false); |
| |
| // Introduce all of the other parameters into this scope. |
| for (auto *Param : MDecl->parameters()) { |
| if (!Param->isInvalidDecl() && |
| getLangOpts().ObjCAutoRefCount && |
| !HasExplicitOwnershipAttr(*this, Param)) |
| Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) << |
| Param->getType(); |
| |
| if (Param->getIdentifier()) |
| PushOnScopeChains(Param, FnBodyScope); |
| } |
| |
| // In ARC, disallow definition of retain/release/autorelease/retainCount |
| if (getLangOpts().ObjCAutoRefCount) { |
| switch (MDecl->getMethodFamily()) { |
| case OMF_retain: |
| case OMF_retainCount: |
| case OMF_release: |
| case OMF_autorelease: |
| Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def) |
| << 0 << MDecl->getSelector(); |
| break; |
| |
| case OMF_None: |
| case OMF_dealloc: |
| case OMF_finalize: |
| case OMF_alloc: |
| case OMF_init: |
| case OMF_mutableCopy: |
| case OMF_copy: |
| case OMF_new: |
| case OMF_self: |
| case OMF_initialize: |
| case OMF_performSelector: |
| break; |
| } |
| } |
| |
| // Warn on deprecated methods under -Wdeprecated-implementations, |
| // and prepare for warning on missing super calls. |
| if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) { |
| ObjCMethodDecl *IMD = |
| IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()); |
| |
| if (IMD) { |
| ObjCImplDecl *ImplDeclOfMethodDef = |
| dyn_cast<ObjCImplDecl>(MDecl->getDeclContext()); |
| ObjCContainerDecl *ContDeclOfMethodDecl = |
| dyn_cast<ObjCContainerDecl>(IMD->getDeclContext()); |
| ObjCImplDecl *ImplDeclOfMethodDecl = nullptr; |
| if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl)) |
| ImplDeclOfMethodDecl = OID->getImplementation(); |
| else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) { |
| if (CD->IsClassExtension()) { |
| if (ObjCInterfaceDecl *OID = CD->getClassInterface()) |
| ImplDeclOfMethodDecl = OID->getImplementation(); |
| } else |
| ImplDeclOfMethodDecl = CD->getImplementation(); |
| } |
| // No need to issue deprecated warning if deprecated mehod in class/category |
| // is being implemented in its own implementation (no overriding is involved). |
| if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef) |
| DiagnoseObjCImplementedDeprecations(*this, IMD, MDecl->getLocation()); |
| } |
| |
| if (MDecl->getMethodFamily() == OMF_init) { |
| if (MDecl->isDesignatedInitializerForTheInterface()) { |
| getCurFunction()->ObjCIsDesignatedInit = true; |
| getCurFunction()->ObjCWarnForNoDesignatedInitChain = |
| IC->getSuperClass() != nullptr; |
| } else if (IC->hasDesignatedInitializers()) { |
| getCurFunction()->ObjCIsSecondaryInit = true; |
| getCurFunction()->ObjCWarnForNoInitDelegation = true; |
| } |
| } |
| |
| // If this is "dealloc" or "finalize", set some bit here. |
| // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false. |
| // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set. |
| // Only do this if the current class actually has a superclass. |
| if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) { |
| ObjCMethodFamily Family = MDecl->getMethodFamily(); |
| if (Family == OMF_dealloc) { |
| if (!(getLangOpts().ObjCAutoRefCount || |
| getLangOpts().getGC() == LangOptions::GCOnly)) |
| getCurFunction()->ObjCShouldCallSuper = true; |
| |
| } else if (Family == OMF_finalize) { |
| if (Context.getLangOpts().getGC() != LangOptions::NonGC) |
| getCurFunction()->ObjCShouldCallSuper = true; |
| |
| } else { |
| const ObjCMethodDecl *SuperMethod = |
| SuperClass->lookupMethod(MDecl->getSelector(), |
| MDecl->isInstanceMethod()); |
| getCurFunction()->ObjCShouldCallSuper = |
| (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>()); |
| } |
| } |
| } |
| } |
| |
| namespace { |
| |
| // Callback to only accept typo corrections that are Objective-C classes. |
| // If an ObjCInterfaceDecl* is given to the constructor, then the validation |
| // function will reject corrections to that class. |
| class ObjCInterfaceValidatorCCC final : public CorrectionCandidateCallback { |
| public: |
| ObjCInterfaceValidatorCCC() : CurrentIDecl(nullptr) {} |
| explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl) |
| : CurrentIDecl(IDecl) {} |
| |
| bool ValidateCandidate(const TypoCorrection &candidate) override { |
| ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| return ID && !declaresSameEntity(ID, CurrentIDecl); |
| } |
| |
| std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| return std::make_unique<ObjCInterfaceValidatorCCC>(*this); |
| } |
| |
| private: |
| ObjCInterfaceDecl *CurrentIDecl; |
| }; |
| |
| } // end anonymous namespace |
| |
| static void diagnoseUseOfProtocols(Sema &TheSema, |
| ObjCContainerDecl *CD, |
| ObjCProtocolDecl *const *ProtoRefs, |
| unsigned NumProtoRefs, |
| const SourceLocation *ProtoLocs) { |
| assert(ProtoRefs); |
| // Diagnose availability in the context of the ObjC container. |
| Sema::ContextRAII SavedContext(TheSema, CD); |
| for (unsigned i = 0; i < NumProtoRefs; ++i) { |
| (void)TheSema.DiagnoseUseOfDecl(ProtoRefs[i], ProtoLocs[i], |
| /*UnknownObjCClass=*/nullptr, |
| /*ObjCPropertyAccess=*/false, |
| /*AvoidPartialAvailabilityChecks=*/true); |
| } |
| } |
| |
| void Sema:: |
| ActOnSuperClassOfClassInterface(Scope *S, |
| SourceLocation AtInterfaceLoc, |
| ObjCInterfaceDecl *IDecl, |
| IdentifierInfo *ClassName, |
| SourceLocation ClassLoc, |
| IdentifierInfo *SuperName, |
| SourceLocation SuperLoc, |
| ArrayRef<ParsedType> SuperTypeArgs, |
| SourceRange SuperTypeArgsRange) { |
| // Check if a different kind of symbol declared in this scope. |
| NamedDecl *PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| LookupOrdinaryName); |
| |
| if (!PrevDecl) { |
| // Try to correct for a typo in the superclass name without correcting |
| // to the class we're defining. |
| ObjCInterfaceValidatorCCC CCC(IDecl); |
| if (TypoCorrection Corrected = CorrectTypo( |
| DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, |
| TUScope, nullptr, CCC, CTK_ErrorRecovery)) { |
| diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest) |
| << SuperName << ClassName); |
| PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>(); |
| } |
| } |
| |
| if (declaresSameEntity(PrevDecl, IDecl)) { |
| Diag(SuperLoc, diag::err_recursive_superclass) |
| << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| IDecl->setEndOfDefinitionLoc(ClassLoc); |
| } else { |
| ObjCInterfaceDecl *SuperClassDecl = |
| dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| QualType SuperClassType; |
| |
| // Diagnose classes that inherit from deprecated classes. |
| if (SuperClassDecl) { |
| (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc); |
| SuperClassType = Context.getObjCInterfaceType(SuperClassDecl); |
| } |
| |
| if (PrevDecl && !SuperClassDecl) { |
| // The previous declaration was not a class decl. Check if we have a |
| // typedef. If we do, get the underlying class type. |
| if (const TypedefNameDecl *TDecl = |
| dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
| QualType T = TDecl->getUnderlyingType(); |
| if (T->isObjCObjectType()) { |
| if (NamedDecl *IDecl = T->castAs<ObjCObjectType>()->getInterface()) { |
| SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl); |
| SuperClassType = Context.getTypeDeclType(TDecl); |
| |
| // This handles the following case: |
| // @interface NewI @end |
| // typedef NewI DeprI __attribute__((deprecated("blah"))) |
| // @interface SI : DeprI /* warn here */ @end |
| (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc); |
| } |
| } |
| } |
| |
| // This handles the following case: |
| // |
| // typedef int SuperClass; |
| // @interface MyClass : SuperClass {} @end |
| // |
| if (!SuperClassDecl) { |
| Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName; |
| Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| } |
| } |
| |
| if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) { |
| if (!SuperClassDecl) |
| Diag(SuperLoc, diag::err_undef_superclass) |
| << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc); |
| else if (RequireCompleteType(SuperLoc, |
| SuperClassType, |
| diag::err_forward_superclass, |
| SuperClassDecl->getDeclName(), |
| ClassName, |
| SourceRange(AtInterfaceLoc, ClassLoc))) { |
| SuperClassDecl = nullptr; |
| SuperClassType = QualType(); |
| } |
| } |
| |
| if (SuperClassType.isNull()) { |
| assert(!SuperClassDecl && "Failed to set SuperClassType?"); |
| return; |
| } |
| |
| // Handle type arguments on the superclass. |
| TypeSourceInfo *SuperClassTInfo = nullptr; |
| if (!SuperTypeArgs.empty()) { |
| TypeResult fullSuperClassType = actOnObjCTypeArgsAndProtocolQualifiers( |
| S, |
| SuperLoc, |
| CreateParsedType(SuperClassType, |
| nullptr), |
| SuperTypeArgsRange.getBegin(), |
| SuperTypeArgs, |
| SuperTypeArgsRange.getEnd(), |
| SourceLocation(), |
| { }, |
| { }, |
| SourceLocation()); |
| if (!fullSuperClassType.isUsable()) |
| return; |
| |
| SuperClassType = GetTypeFromParser(fullSuperClassType.get(), |
| &SuperClassTInfo); |
| } |
| |
| if (!SuperClassTInfo) { |
| SuperClassTInfo = Context.getTrivialTypeSourceInfo(SuperClassType, |
| SuperLoc); |
| } |
| |
| IDecl->setSuperClass(SuperClassTInfo); |
| IDecl->setEndOfDefinitionLoc(SuperClassTInfo->getTypeLoc().getEndLoc()); |
| } |
| } |
| |
| DeclResult Sema::actOnObjCTypeParam(Scope *S, |
| ObjCTypeParamVariance variance, |
| SourceLocation varianceLoc, |
| unsigned index, |
| IdentifierInfo *paramName, |
| SourceLocation paramLoc, |
| SourceLocation colonLoc, |
| ParsedType parsedTypeBound) { |
| // If there was an explicitly-provided type bound, check it. |
| TypeSourceInfo *typeBoundInfo = nullptr; |
| if (parsedTypeBound) { |
| // The type bound can be any Objective-C pointer type. |
| QualType typeBound = GetTypeFromParser(parsedTypeBound, &typeBoundInfo); |
| if (typeBound->isObjCObjectPointerType()) { |
| // okay |
| } else if (typeBound->isObjCObjectType()) { |
| // The user forgot the * on an Objective-C pointer type, e.g., |
| // "T : NSView". |
| SourceLocation starLoc = getLocForEndOfToken( |
| typeBoundInfo->getTypeLoc().getEndLoc()); |
| Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
| diag::err_objc_type_param_bound_missing_pointer) |
| << typeBound << paramName |
| << FixItHint::CreateInsertion(starLoc, " *"); |
| |
| // Create a new type location builder so we can update the type |
| // location information we have. |
| TypeLocBuilder builder; |
| builder.pushFullCopy(typeBoundInfo->getTypeLoc()); |
| |
| // Create the Objective-C pointer type. |
| typeBound = Context.getObjCObjectPointerType(typeBound); |
| ObjCObjectPointerTypeLoc newT |
| = builder.push<ObjCObjectPointerTypeLoc>(typeBound); |
| newT.setStarLoc(starLoc); |
| |
| // Form the new type source information. |
| typeBoundInfo = builder.getTypeSourceInfo(Context, typeBound); |
| } else { |
| // Not a valid type bound. |
| Diag(typeBoundInfo->getTypeLoc().getBeginLoc(), |
| diag::err_objc_type_param_bound_nonobject) |
| << typeBound << paramName; |
| |
| // Forget the bound; we'll default to id later. |
| typeBoundInfo = nullptr; |
| } |
| |
| // Type bounds cannot have qualifiers (even indirectly) or explicit |
| // nullability. |
| if (typeBoundInfo) { |
| QualType typeBound = typeBoundInfo->getType(); |
| TypeLoc qual = typeBoundInfo->getTypeLoc().findExplicitQualifierLoc(); |
| if (qual || typeBound.hasQualifiers()) { |
| bool diagnosed = false; |
| SourceRange rangeToRemove; |
| if (qual) { |
| if (auto attr = qual.getAs<AttributedTypeLoc>()) { |
| rangeToRemove = attr.getLocalSourceRange(); |
| if (attr.getTypePtr()->getImmediateNullability()) { |
| Diag(attr.getBeginLoc(), |
| diag::err_objc_type_param_bound_explicit_nullability) |
| << paramName << typeBound |
| << FixItHint::CreateRemoval(rangeToRemove); |
| diagnosed = true; |
| } |
| } |
| } |
| |
| if (!diagnosed) { |
| Diag(qual ? qual.getBeginLoc() |
| : typeBoundInfo->getTypeLoc().getBeginLoc(), |
| diag::err_objc_type_param_bound_qualified) |
| << paramName << typeBound |
| << typeBound.getQualifiers().getAsString() |
| << FixItHint::CreateRemoval(rangeToRemove); |
| } |
| |
| // If the type bound has qualifiers other than CVR, we need to strip |
| // them or we'll probably assert later when trying to apply new |
| // qualifiers. |
| Qualifiers quals = typeBound.getQualifiers(); |
| quals.removeCVRQualifiers(); |
| if (!quals.empty()) { |
| typeBoundInfo = |
| Context.getTrivialTypeSourceInfo(typeBound.getUnqualifiedType()); |
| } |
| } |
| } |
| } |
| |
| // If there was no explicit type bound (or we removed it due to an error), |
| // use 'id' instead. |
| if (!typeBoundInfo) { |
| colonLoc = SourceLocation(); |
| typeBoundInfo = Context.getTrivialTypeSourceInfo(Context.getObjCIdType()); |
| } |
| |
| // Create the type parameter. |
| return ObjCTypeParamDecl::Create(Context, CurContext, variance, varianceLoc, |
| index, paramLoc, paramName, colonLoc, |
| typeBoundInfo); |
| } |
| |
| ObjCTypeParamList *Sema::actOnObjCTypeParamList(Scope *S, |
| SourceLocation lAngleLoc, |
| ArrayRef<Decl *> typeParamsIn, |
| SourceLocation rAngleLoc) { |
| // We know that the array only contains Objective-C type parameters. |
| ArrayRef<ObjCTypeParamDecl *> |
| typeParams( |
| reinterpret_cast<ObjCTypeParamDecl * const *>(typeParamsIn.data()), |
| typeParamsIn.size()); |
| |
| // Diagnose redeclarations of type parameters. |
| // We do this now because Objective-C type parameters aren't pushed into |
| // scope until later (after the instance variable block), but we want the |
| // diagnostics to occur right after we parse the type parameter list. |
| llvm::SmallDenseMap<IdentifierInfo *, ObjCTypeParamDecl *> knownParams; |
| for (auto typeParam : typeParams) { |
| auto known = knownParams.find(typeParam->getIdentifier()); |
| if (known != knownParams.end()) { |
| Diag(typeParam->getLocation(), diag::err_objc_type_param_redecl) |
| << typeParam->getIdentifier() |
| << SourceRange(known->second->getLocation()); |
| |
| typeParam->setInvalidDecl(); |
| } else { |
| knownParams.insert(std::make_pair(typeParam->getIdentifier(), typeParam)); |
| |
| // Push the type parameter into scope. |
| PushOnScopeChains(typeParam, S, /*AddToContext=*/false); |
| } |
| } |
| |
| // Create the parameter list. |
| return ObjCTypeParamList::create(Context, lAngleLoc, typeParams, rAngleLoc); |
| } |
| |
| void Sema::popObjCTypeParamList(Scope *S, ObjCTypeParamList *typeParamList) { |
| for (auto typeParam : *typeParamList) { |
| if (!typeParam->isInvalidDecl()) { |
| S->RemoveDecl(typeParam); |
| IdResolver.RemoveDecl(typeParam); |
| } |
| } |
| } |
| |
| namespace { |
| /// The context in which an Objective-C type parameter list occurs, for use |
| /// in diagnostics. |
| enum class TypeParamListContext { |
| ForwardDeclaration, |
| Definition, |
| Category, |
| Extension |
| }; |
| } // end anonymous namespace |
| |
| /// Check consistency between two Objective-C type parameter lists, e.g., |
| /// between a category/extension and an \@interface or between an \@class and an |
| /// \@interface. |
| static bool checkTypeParamListConsistency(Sema &S, |
| ObjCTypeParamList *prevTypeParams, |
| ObjCTypeParamList *newTypeParams, |
| TypeParamListContext newContext) { |
| // If the sizes don't match, complain about that. |
| if (prevTypeParams->size() != newTypeParams->size()) { |
| SourceLocation diagLoc; |
| if (newTypeParams->size() > prevTypeParams->size()) { |
| diagLoc = newTypeParams->begin()[prevTypeParams->size()]->getLocation(); |
| } else { |
| diagLoc = S.getLocForEndOfToken(newTypeParams->back()->getEndLoc()); |
| } |
| |
| S.Diag(diagLoc, diag::err_objc_type_param_arity_mismatch) |
| << static_cast<unsigned>(newContext) |
| << (newTypeParams->size() > prevTypeParams->size()) |
| << prevTypeParams->size() |
| << newTypeParams->size(); |
| |
| return true; |
| } |
| |
| // Match up the type parameters. |
| for (unsigned i = 0, n = prevTypeParams->size(); i != n; ++i) { |
| ObjCTypeParamDecl *prevTypeParam = prevTypeParams->begin()[i]; |
| ObjCTypeParamDecl *newTypeParam = newTypeParams->begin()[i]; |
| |
| // Check for consistency of the variance. |
| if (newTypeParam->getVariance() != prevTypeParam->getVariance()) { |
| if (newTypeParam->getVariance() == ObjCTypeParamVariance::Invariant && |
| newContext != TypeParamListContext::Definition) { |
| // When the new type parameter is invariant and is not part |
| // of the definition, just propagate the variance. |
| newTypeParam->setVariance(prevTypeParam->getVariance()); |
| } else if (prevTypeParam->getVariance() |
| == ObjCTypeParamVariance::Invariant && |
| !(isa<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) && |
| cast<ObjCInterfaceDecl>(prevTypeParam->getDeclContext()) |
| ->getDefinition() == prevTypeParam->getDeclContext())) { |
| // When the old parameter is invariant and was not part of the |
| // definition, just ignore the difference because it doesn't |
| // matter. |
| } else { |
| { |
| // Diagnose the conflict and update the second declaration. |
| SourceLocation diagLoc = newTypeParam->getVarianceLoc(); |
| if (diagLoc.isInvalid()) |
| diagLoc = newTypeParam->getBeginLoc(); |
| |
| auto diag = S.Diag(diagLoc, |
| diag::err_objc_type_param_variance_conflict) |
| << static_cast<unsigned>(newTypeParam->getVariance()) |
| << newTypeParam->getDeclName() |
| << static_cast<unsigned>(prevTypeParam->getVariance()) |
| << prevTypeParam->getDeclName(); |
| switch (prevTypeParam->getVariance()) { |
| case ObjCTypeParamVariance::Invariant: |
| diag << FixItHint::CreateRemoval(newTypeParam->getVarianceLoc()); |
| break; |
| |
| case ObjCTypeParamVariance::Covariant: |
| case ObjCTypeParamVariance::Contravariant: { |
| StringRef newVarianceStr |
| = prevTypeParam->getVariance() == ObjCTypeParamVariance::Covariant |
| ? "__covariant" |
| : "__contravariant"; |
| if (newTypeParam->getVariance() |
| == ObjCTypeParamVariance::Invariant) { |
| diag << FixItHint::CreateInsertion(newTypeParam->getBeginLoc(), |
| (newVarianceStr + " ").str()); |
| } else { |
| diag << FixItHint::CreateReplacement(newTypeParam->getVarianceLoc(), |
| newVarianceStr); |
| } |
| } |
| } |
| } |
| |
| S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| << prevTypeParam->getDeclName(); |
| |
| // Override the variance. |
| newTypeParam->setVariance(prevTypeParam->getVariance()); |
| } |
| } |
| |
| // If the bound types match, there's nothing to do. |
| if (S.Context.hasSameType(prevTypeParam->getUnderlyingType(), |
| newTypeParam->getUnderlyingType())) |
| continue; |
| |
| // If the new type parameter's bound was explicit, complain about it being |
| // different from the original. |
| if (newTypeParam->hasExplicitBound()) { |
| SourceRange newBoundRange = newTypeParam->getTypeSourceInfo() |
| ->getTypeLoc().getSourceRange(); |
| S.Diag(newBoundRange.getBegin(), diag::err_objc_type_param_bound_conflict) |
| << newTypeParam->getUnderlyingType() |
| << newTypeParam->getDeclName() |
| << prevTypeParam->hasExplicitBound() |
| << prevTypeParam->getUnderlyingType() |
| << (newTypeParam->getDeclName() == prevTypeParam->getDeclName()) |
| << prevTypeParam->getDeclName() |
| << FixItHint::CreateReplacement( |
| newBoundRange, |
| prevTypeParam->getUnderlyingType().getAsString( |
| S.Context.getPrintingPolicy())); |
| |
| S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| << prevTypeParam->getDeclName(); |
| |
| // Override the new type parameter's bound type with the previous type, |
| // so that it's consistent. |
| newTypeParam->setTypeSourceInfo( |
| S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); |
| continue; |
| } |
| |
| // The new type parameter got the implicit bound of 'id'. That's okay for |
| // categories and extensions (overwrite it later), but not for forward |
| // declarations and @interfaces, because those must be standalone. |
| if (newContext == TypeParamListContext::ForwardDeclaration || |
| newContext == TypeParamListContext::Definition) { |
| // Diagnose this problem for forward declarations and definitions. |
| SourceLocation insertionLoc |
| = S.getLocForEndOfToken(newTypeParam->getLocation()); |
| std::string newCode |
| = " : " + prevTypeParam->getUnderlyingType().getAsString( |
| S.Context.getPrintingPolicy()); |
| S.Diag(newTypeParam->getLocation(), |
| diag::err_objc_type_param_bound_missing) |
| << prevTypeParam->getUnderlyingType() |
| << newTypeParam->getDeclName() |
| << (newContext == TypeParamListContext::ForwardDeclaration) |
| << FixItHint::CreateInsertion(insertionLoc, newCode); |
| |
| S.Diag(prevTypeParam->getLocation(), diag::note_objc_type_param_here) |
| << prevTypeParam->getDeclName(); |
| } |
| |
| // Update the new type parameter's bound to match the previous one. |
| newTypeParam->setTypeSourceInfo( |
| S.Context.getTrivialTypeSourceInfo(prevTypeParam->getUnderlyingType())); |
| } |
| |
| return false; |
| } |
| |
| Decl *Sema::ActOnStartClassInterface( |
| Scope *S, SourceLocation AtInterfaceLoc, IdentifierInfo *ClassName, |
| SourceLocation ClassLoc, ObjCTypeParamList *typeParamList, |
| IdentifierInfo *SuperName, SourceLocation SuperLoc, |
| ArrayRef<ParsedType> SuperTypeArgs, SourceRange SuperTypeArgsRange, |
| Decl *const *ProtoRefs, unsigned NumProtoRefs, |
| const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
| const ParsedAttributesView &AttrList) { |
| assert(ClassName && "Missing class identifier"); |
| |
| // Check for another declaration kind with the same name. |
| NamedDecl *PrevDecl = |
| LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
| forRedeclarationInCurContext()); |
| |
| if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
| Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
| Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| } |
| |
| // Create a declaration to describe this @interface. |
| ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| |
| if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) { |
| // A previous decl with a different name is because of |
| // @compatibility_alias, for example: |
| // \code |
| // @class NewImage; |
| // @compatibility_alias OldImage NewImage; |
| // \endcode |
| // A lookup for 'OldImage' will return the 'NewImage' decl. |
| // |
| // In such a case use the real declaration name, instead of the alias one, |
| // otherwise we will break IdentifierResolver and redecls-chain invariants. |
| // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl |
| // has been aliased. |
| ClassName = PrevIDecl->getIdentifier(); |
| } |
| |
| // If there was a forward declaration with type parameters, check |
| // for consistency. |
| if (PrevIDecl) { |
| if (ObjCTypeParamList *prevTypeParamList = PrevIDecl->getTypeParamList()) { |
| if (typeParamList) { |
| // Both have type parameter lists; check for consistency. |
| if (checkTypeParamListConsistency(*this, prevTypeParamList, |
| typeParamList, |
| TypeParamListContext::Definition)) { |
| typeParamList = nullptr; |
| } |
| } else { |
| Diag(ClassLoc, diag::err_objc_parameterized_forward_class_first) |
| << ClassName; |
| Diag(prevTypeParamList->getLAngleLoc(), diag::note_previous_decl) |
| << ClassName; |
| |
| // Clone the type parameter list. |
| SmallVector<ObjCTypeParamDecl *, 4> clonedTypeParams; |
| for (auto typeParam : *prevTypeParamList) { |
| clonedTypeParams.push_back( |
| ObjCTypeParamDecl::Create( |
| Context, |
| CurContext, |
| typeParam->getVariance(), |
| SourceLocation(), |
| typeParam->getIndex(), |
| SourceLocation(), |
| typeParam->getIdentifier(), |
| SourceLocation(), |
| Context.getTrivialTypeSourceInfo(typeParam->getUnderlyingType()))); |
| } |
| |
| typeParamList = ObjCTypeParamList::create(Context, |
| SourceLocation(), |
| clonedTypeParams, |
| SourceLocation()); |
| } |
| } |
| } |
| |
| ObjCInterfaceDecl *IDecl |
| = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName, |
| typeParamList, PrevIDecl, ClassLoc); |
| if (PrevIDecl) { |
| // Class already seen. Was it a definition? |
| if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) { |
| Diag(AtInterfaceLoc, diag::err_duplicate_class_def) |
| << PrevIDecl->getDeclName(); |
| Diag(Def->getLocation(), diag::note_previous_definition); |
| IDecl->setInvalidDecl(); |
| } |
| } |
| |
| ProcessDeclAttributeList(TUScope, IDecl, AttrList); |
| AddPragmaAttributes(TUScope, IDecl); |
| PushOnScopeChains(IDecl, TUScope); |
| |
| // Start the definition of this class. If we're in a redefinition case, there |
| // may already be a definition, so we'll end up adding to it. |
| if (!IDecl->hasDefinition()) |
| IDecl->startDefinition(); |
| |
| if (SuperName) { |
| // Diagnose availability in the context of the @interface. |
| ContextRAII SavedContext(*this, IDecl); |
| |
| ActOnSuperClassOfClassInterface(S, AtInterfaceLoc, IDecl, |
| ClassName, ClassLoc, |
| SuperName, SuperLoc, SuperTypeArgs, |
| SuperTypeArgsRange); |
| } else { // we have a root class. |
| IDecl->setEndOfDefinitionLoc(ClassLoc); |
| } |
| |
| // Check then save referenced protocols. |
| if (NumProtoRefs) { |
| diagnoseUseOfProtocols(*this, IDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| NumProtoRefs, ProtoLocs); |
| IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
| ProtoLocs, Context); |
| IDecl->setEndOfDefinitionLoc(EndProtoLoc); |
| } |
| |
| CheckObjCDeclScope(IDecl); |
| return ActOnObjCContainerStartDefinition(IDecl); |
| } |
| |
| /// ActOnTypedefedProtocols - this action finds protocol list as part of the |
| /// typedef'ed use for a qualified super class and adds them to the list |
| /// of the protocols. |
| void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs, |
| SmallVectorImpl<SourceLocation> &ProtocolLocs, |
| IdentifierInfo *SuperName, |
| SourceLocation SuperLoc) { |
| if (!SuperName) |
| return; |
| NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc, |
| LookupOrdinaryName); |
| if (!IDecl) |
| return; |
| |
| if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) { |
| QualType T = TDecl->getUnderlyingType(); |
| if (T->isObjCObjectType()) |
| if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>()) { |
| ProtocolRefs.append(OPT->qual_begin(), OPT->qual_end()); |
| // FIXME: Consider whether this should be an invalid loc since the loc |
| // is not actually pointing to a protocol name reference but to the |
| // typedef reference. Note that the base class name loc is also pointing |
| // at the typedef. |
| ProtocolLocs.append(OPT->getNumProtocols(), SuperLoc); |
| } |
| } |
| } |
| |
| /// ActOnCompatibilityAlias - this action is called after complete parsing of |
| /// a \@compatibility_alias declaration. It sets up the alias relationships. |
| Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc, |
| IdentifierInfo *AliasName, |
| SourceLocation AliasLocation, |
| IdentifierInfo *ClassName, |
| SourceLocation ClassLocation) { |
| // Look for previous declaration of alias name |
| NamedDecl *ADecl = |
| LookupSingleName(TUScope, AliasName, AliasLocation, LookupOrdinaryName, |
| forRedeclarationInCurContext()); |
| if (ADecl) { |
| Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName; |
| Diag(ADecl->getLocation(), diag::note_previous_declaration); |
| return nullptr; |
| } |
| // Check for class declaration |
| NamedDecl *CDeclU = |
| LookupSingleName(TUScope, ClassName, ClassLocation, LookupOrdinaryName, |
| forRedeclarationInCurContext()); |
| if (const TypedefNameDecl *TDecl = |
| dyn_cast_or_null<TypedefNameDecl>(CDeclU)) { |
| QualType T = TDecl->getUnderlyingType(); |
| if (T->isObjCObjectType()) { |
| if (NamedDecl *IDecl = T->castAs<ObjCObjectType>()->getInterface()) { |
| ClassName = IDecl->getIdentifier(); |
| CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation, |
| LookupOrdinaryName, |
| forRedeclarationInCurContext()); |
| } |
| } |
| } |
| ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU); |
| if (!CDecl) { |
| Diag(ClassLocation, diag::warn_undef_interface) << ClassName; |
| if (CDeclU) |
| Diag(CDeclU->getLocation(), diag::note_previous_declaration); |
| return nullptr; |
| } |
| |
| // Everything checked out, instantiate a new alias declaration AST. |
| ObjCCompatibleAliasDecl *AliasDecl = |
| ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl); |
| |
| if (!CheckObjCDeclScope(AliasDecl)) |
| PushOnScopeChains(AliasDecl, TUScope); |
| |
| return AliasDecl; |
| } |
| |
| bool Sema::CheckForwardProtocolDeclarationForCircularDependency( |
| IdentifierInfo *PName, |
| SourceLocation &Ploc, SourceLocation PrevLoc, |
| const ObjCList<ObjCProtocolDecl> &PList) { |
| |
| bool res = false; |
| for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(), |
| E = PList.end(); I != E; ++I) { |
| if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(), |
| Ploc)) { |
| if (PDecl->getIdentifier() == PName) { |
| Diag(Ploc, diag::err_protocol_has_circular_dependency); |
| Diag(PrevLoc, diag::note_previous_definition); |
| res = true; |
| } |
| |
| if (!PDecl->hasDefinition()) |
| continue; |
| |
| if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc, |
| PDecl->getLocation(), PDecl->getReferencedProtocols())) |
| res = true; |
| } |
| } |
| return res; |
| } |
| |
| Decl *Sema::ActOnStartProtocolInterface( |
| SourceLocation AtProtoInterfaceLoc, IdentifierInfo *ProtocolName, |
| SourceLocation ProtocolLoc, Decl *const *ProtoRefs, unsigned NumProtoRefs, |
| const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
| const ParsedAttributesView &AttrList) { |
| bool err = false; |
| // FIXME: Deal with AttrList. |
| assert(ProtocolName && "Missing protocol identifier"); |
| ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc, |
| forRedeclarationInCurContext()); |
| ObjCProtocolDecl *PDecl = nullptr; |
| if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) { |
| // If we already have a definition, complain. |
| Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName; |
| Diag(Def->getLocation(), diag::note_previous_definition); |
| |
| // Create a new protocol that is completely distinct from previous |
| // declarations, and do not make this protocol available for name lookup. |
| // That way, we'll end up completely ignoring the duplicate. |
| // FIXME: Can we turn this into an error? |
| PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
| ProtocolLoc, AtProtoInterfaceLoc, |
| /*PrevDecl=*/nullptr); |
| |
| // If we are using modules, add the decl to the context in order to |
| // serialize something meaningful. |
| if (getLangOpts().Modules) |
| PushOnScopeChains(PDecl, TUScope); |
| PDecl->startDefinition(); |
| } else { |
| if (PrevDecl) { |
| // Check for circular dependencies among protocol declarations. This can |
| // only happen if this protocol was forward-declared. |
| ObjCList<ObjCProtocolDecl> PList; |
| PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context); |
| err = CheckForwardProtocolDeclarationForCircularDependency( |
| ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList); |
| } |
| |
| // Create the new declaration. |
| PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName, |
| ProtocolLoc, AtProtoInterfaceLoc, |
| /*PrevDecl=*/PrevDecl); |
| |
| PushOnScopeChains(PDecl, TUScope); |
| PDecl->startDefinition(); |
| } |
| |
| ProcessDeclAttributeList(TUScope, PDecl, AttrList); |
| AddPragmaAttributes(TUScope, PDecl); |
| |
| // Merge attributes from previous declarations. |
| if (PrevDecl) |
| mergeDeclAttributes(PDecl, PrevDecl); |
| |
| if (!err && NumProtoRefs ) { |
| /// Check then save referenced protocols. |
| diagnoseUseOfProtocols(*this, PDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| NumProtoRefs, ProtoLocs); |
| PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
| ProtoLocs, Context); |
| } |
| |
| CheckObjCDeclScope(PDecl); |
| return ActOnObjCContainerStartDefinition(PDecl); |
| } |
| |
| static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl, |
| ObjCProtocolDecl *&UndefinedProtocol) { |
| if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) { |
| UndefinedProtocol = PDecl; |
| return true; |
| } |
| |
| for (auto *PI : PDecl->protocols()) |
| if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) { |
| UndefinedProtocol = PI; |
| return true; |
| } |
| return false; |
| } |
| |
| /// FindProtocolDeclaration - This routine looks up protocols and |
| /// issues an error if they are not declared. It returns list of |
| /// protocol declarations in its 'Protocols' argument. |
| void |
| Sema::FindProtocolDeclaration(bool WarnOnDeclarations, bool ForObjCContainer, |
| ArrayRef<IdentifierLocPair> ProtocolId, |
| SmallVectorImpl<Decl *> &Protocols) { |
| for (const IdentifierLocPair &Pair : ProtocolId) { |
| ObjCProtocolDecl *PDecl = LookupProtocol(Pair.first, Pair.second); |
| if (!PDecl) { |
| DeclFilterCCC<ObjCProtocolDecl> CCC{}; |
| TypoCorrection Corrected = CorrectTypo( |
| DeclarationNameInfo(Pair.first, Pair.second), LookupObjCProtocolName, |
| TUScope, nullptr, CCC, CTK_ErrorRecovery); |
| if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) |
| diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest) |
| << Pair.first); |
| } |
| |
| if (!PDecl) { |
| Diag(Pair.second, diag::err_undeclared_protocol) << Pair.first; |
| continue; |
| } |
| // If this is a forward protocol declaration, get its definition. |
| if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition()) |
| PDecl = PDecl->getDefinition(); |
| |
| // For an objc container, delay protocol reference checking until after we |
| // can set the objc decl as the availability context, otherwise check now. |
| if (!ForObjCContainer) { |
| (void)DiagnoseUseOfDecl(PDecl, Pair.second); |
| } |
| |
| // If this is a forward declaration and we are supposed to warn in this |
| // case, do it. |
| // FIXME: Recover nicely in the hidden case. |
| ObjCProtocolDecl *UndefinedProtocol; |
| |
| if (WarnOnDeclarations && |
| NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) { |
| Diag(Pair.second, diag::warn_undef_protocolref) << Pair.first; |
| Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined) |
| << UndefinedProtocol; |
| } |
| Protocols.push_back(PDecl); |
| } |
| } |
| |
| namespace { |
| // Callback to only accept typo corrections that are either |
| // Objective-C protocols or valid Objective-C type arguments. |
| class ObjCTypeArgOrProtocolValidatorCCC final |
| : public CorrectionCandidateCallback { |
| ASTContext &Context; |
| Sema::LookupNameKind LookupKind; |
| public: |
| ObjCTypeArgOrProtocolValidatorCCC(ASTContext &context, |
| Sema::LookupNameKind lookupKind) |
| : Context(context), LookupKind(lookupKind) { } |
| |
| bool ValidateCandidate(const TypoCorrection &candidate) override { |
| // If we're allowed to find protocols and we have a protocol, accept it. |
| if (LookupKind != Sema::LookupOrdinaryName) { |
| if (candidate.getCorrectionDeclAs<ObjCProtocolDecl>()) |
| return true; |
| } |
| |
| // If we're allowed to find type names and we have one, accept it. |
| if (LookupKind != Sema::LookupObjCProtocolName) { |
| // If we have a type declaration, we might accept this result. |
| if (auto typeDecl = candidate.getCorrectionDeclAs<TypeDecl>()) { |
| // If we found a tag declaration outside of C++, skip it. This |
| // can happy because we look for any name when there is no |
| // bias to protocol or type names. |
| if (isa<RecordDecl>(typeDecl) && !Context.getLangOpts().CPlusPlus) |
| return false; |
| |
| // Make sure the type is something we would accept as a type |
| // argument. |
| auto type = Context.getTypeDeclType(typeDecl); |
| if (type->isObjCObjectPointerType() || |
| type->isBlockPointerType() || |
| type->isDependentType() || |
| type->isObjCObjectType()) |
| return true; |
| |
| return false; |
| } |
| |
| // If we have an Objective-C class type, accept it; there will |
| // be another fix to add the '*'. |
| if (candidate.getCorrectionDeclAs<ObjCInterfaceDecl>()) |
| return true; |
| |
| return false; |
| } |
| |
| return false; |
| } |
| |
| std::unique_ptr<CorrectionCandidateCallback> clone() override { |
| return std::make_unique<ObjCTypeArgOrProtocolValidatorCCC>(*this); |
| } |
| }; |
| } // end anonymous namespace |
| |
| void Sema::DiagnoseTypeArgsAndProtocols(IdentifierInfo *ProtocolId, |
| SourceLocation ProtocolLoc, |
| IdentifierInfo *TypeArgId, |
| SourceLocation TypeArgLoc, |
| bool SelectProtocolFirst) { |
| Diag(TypeArgLoc, diag::err_objc_type_args_and_protocols) |
| << SelectProtocolFirst << TypeArgId << ProtocolId |
| << SourceRange(ProtocolLoc); |
| } |
| |
| void Sema::actOnObjCTypeArgsOrProtocolQualifiers( |
| Scope *S, |
| ParsedType baseType, |
| SourceLocation lAngleLoc, |
| ArrayRef<IdentifierInfo *> identifiers, |
| ArrayRef<SourceLocation> identifierLocs, |
| SourceLocation rAngleLoc, |
| SourceLocation &typeArgsLAngleLoc, |
| SmallVectorImpl<ParsedType> &typeArgs, |
| SourceLocation &typeArgsRAngleLoc, |
| SourceLocation &protocolLAngleLoc, |
| SmallVectorImpl<Decl *> &protocols, |
| SourceLocation &protocolRAngleLoc, |
| bool warnOnIncompleteProtocols) { |
| // Local function that updates the declaration specifiers with |
| // protocol information. |
| unsigned numProtocolsResolved = 0; |
| auto resolvedAsProtocols = [&] { |
| assert(numProtocolsResolved == identifiers.size() && "Unresolved protocols"); |
| |
| // Determine whether the base type is a parameterized class, in |
| // which case we want to warn about typos such as |
| // "NSArray<NSObject>" (that should be NSArray<NSObject *>). |
| ObjCInterfaceDecl *baseClass = nullptr; |
| QualType base = GetTypeFromParser(baseType, nullptr); |
| bool allAreTypeNames = false; |
| SourceLocation firstClassNameLoc; |
| if (!base.isNull()) { |
| if (const auto *objcObjectType = base->getAs<ObjCObjectType>()) { |
| baseClass = objcObjectType->getInterface(); |
| if (baseClass) { |
| if (auto typeParams = baseClass->getTypeParamList()) { |
| if (typeParams->size() == numProtocolsResolved) { |
| // Note that we should be looking for type names, too. |
| allAreTypeNames = true; |
| } |
| } |
| } |
| } |
| } |
| |
| for (unsigned i = 0, n = protocols.size(); i != n; ++i) { |
| ObjCProtocolDecl *&proto |
| = reinterpret_cast<ObjCProtocolDecl *&>(protocols[i]); |
| // For an objc container, delay protocol reference checking until after we |
| // can set the objc decl as the availability context, otherwise check now. |
| if (!warnOnIncompleteProtocols) { |
| (void)DiagnoseUseOfDecl(proto, identifierLocs[i]); |
| } |
| |
| // If this is a forward protocol declaration, get its definition. |
| if (!proto->isThisDeclarationADefinition() && proto->getDefinition()) |
| proto = proto->getDefinition(); |
| |
| // If this is a forward declaration and we are supposed to warn in this |
| // case, do it. |
| // FIXME: Recover nicely in the hidden case. |
| ObjCProtocolDecl *forwardDecl = nullptr; |
| if (warnOnIncompleteProtocols && |
| NestedProtocolHasNoDefinition(proto, forwardDecl)) { |
| Diag(identifierLocs[i], diag::warn_undef_protocolref) |
| << proto->getDeclName(); |
| Diag(forwardDecl->getLocation(), diag::note_protocol_decl_undefined) |
| << forwardDecl; |
| } |
| |
| // If everything this far has been a type name (and we care |
| // about such things), check whether this name refers to a type |
| // as well. |
| if (allAreTypeNames) { |
| if (auto *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
| LookupOrdinaryName)) { |
| if (isa<ObjCInterfaceDecl>(decl)) { |
| if (firstClassNameLoc.isInvalid()) |
| firstClassNameLoc = identifierLocs[i]; |
| } else if (!isa<TypeDecl>(decl)) { |
| // Not a type. |
| allAreTypeNames = false; |
| } |
| } else { |
| allAreTypeNames = false; |
| } |
| } |
| } |
| |
| // All of the protocols listed also have type names, and at least |
| // one is an Objective-C class name. Check whether all of the |
| // protocol conformances are declared by the base class itself, in |
| // which case we warn. |
| if (allAreTypeNames && firstClassNameLoc.isValid()) { |
| llvm::SmallPtrSet<ObjCProtocolDecl*, 8> knownProtocols; |
| Context.CollectInheritedProtocols(baseClass, knownProtocols); |
| bool allProtocolsDeclared = true; |
| for (auto proto : protocols) { |
| if (knownProtocols.count(static_cast<ObjCProtocolDecl *>(proto)) == 0) { |
| allProtocolsDeclared = false; |
| break; |
| } |
| } |
| |
| if (allProtocolsDeclared) { |
| Diag(firstClassNameLoc, diag::warn_objc_redundant_qualified_class_type) |
| << baseClass->getDeclName() << SourceRange(lAngleLoc, rAngleLoc) |
| << FixItHint::CreateInsertion(getLocForEndOfToken(firstClassNameLoc), |
| " *"); |
| } |
| } |
| |
| protocolLAngleLoc = lAngleLoc; |
| protocolRAngleLoc = rAngleLoc; |
| assert(protocols.size() == identifierLocs.size()); |
| }; |
| |
| // Attempt to resolve all of the identifiers as protocols. |
| for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| ObjCProtocolDecl *proto = LookupProtocol(identifiers[i], identifierLocs[i]); |
| protocols.push_back(proto); |
| if (proto) |
| ++numProtocolsResolved; |
| } |
| |
| // If all of the names were protocols, these were protocol qualifiers. |
| if (numProtocolsResolved == identifiers.size()) |
| return resolvedAsProtocols(); |
| |
| // Attempt to resolve all of the identifiers as type names or |
| // Objective-C class names. The latter is technically ill-formed, |
| // but is probably something like \c NSArray<NSView *> missing the |
| // \c*. |
| typedef llvm::PointerUnion<TypeDecl *, ObjCInterfaceDecl *> TypeOrClassDecl; |
| SmallVector<TypeOrClassDecl, 4> typeDecls; |
| unsigned numTypeDeclsResolved = 0; |
| for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| NamedDecl *decl = LookupSingleName(S, identifiers[i], identifierLocs[i], |
| LookupOrdinaryName); |
| if (!decl) { |
| typeDecls.push_back(TypeOrClassDecl()); |
| continue; |
| } |
| |
| if (auto typeDecl = dyn_cast<TypeDecl>(decl)) { |
| typeDecls.push_back(typeDecl); |
| ++numTypeDeclsResolved; |
| continue; |
| } |
| |
| if (auto objcClass = dyn_cast<ObjCInterfaceDecl>(decl)) { |
| typeDecls.push_back(objcClass); |
| ++numTypeDeclsResolved; |
| continue; |
| } |
| |
| typeDecls.push_back(TypeOrClassDecl()); |
| } |
| |
| AttributeFactory attrFactory; |
| |
| // Local function that forms a reference to the given type or |
| // Objective-C class declaration. |
| auto resolveTypeReference = [&](TypeOrClassDecl typeDecl, SourceLocation loc) |
| -> TypeResult { |
| // Form declaration specifiers. They simply refer to the type. |
| DeclSpec DS(attrFactory); |
| const char* prevSpec; // unused |
| unsigned diagID; // unused |
| QualType type; |
| if (auto *actualTypeDecl = typeDecl.dyn_cast<TypeDecl *>()) |
| type = Context.getTypeDeclType(actualTypeDecl); |
| else |
| type = Context.getObjCInterfaceType(typeDecl.get<ObjCInterfaceDecl *>()); |
| TypeSourceInfo *parsedTSInfo = Context.getTrivialTypeSourceInfo(type, loc); |
| ParsedType parsedType = CreateParsedType(type, parsedTSInfo); |
| DS.SetTypeSpecType(DeclSpec::TST_typename, loc, prevSpec, diagID, |
| parsedType, Context.getPrintingPolicy()); |
| // Use the identifier location for the type source range. |
| DS.SetRangeStart(loc); |
| DS.SetRangeEnd(loc); |
| |
| // Form the declarator. |
| Declarator D(DS, DeclaratorContext::TypeNameContext); |
| |
| // If we have a typedef of an Objective-C class type that is missing a '*', |
| // add the '*'. |
| if (type->getAs<ObjCInterfaceType>()) { |
| SourceLocation starLoc = getLocForEndOfToken(loc); |
| D.AddTypeInfo(DeclaratorChunk::getPointer(/*TypeQuals=*/0, starLoc, |
| SourceLocation(), |
| SourceLocation(), |
| SourceLocation(), |
| SourceLocation(), |
| SourceLocation()), |
| starLoc); |
| |
| // Diagnose the missing '*'. |
| Diag(loc, diag::err_objc_type_arg_missing_star) |
| << type |
| << FixItHint::CreateInsertion(starLoc, " *"); |
| } |
| |
| // Convert this to a type. |
| return ActOnTypeName(S, D); |
| }; |
| |
| // Local function that updates the declaration specifiers with |
| // type argument information. |
| auto resolvedAsTypeDecls = [&] { |
| // We did not resolve these as protocols. |
| protocols.clear(); |
| |
| assert(numTypeDeclsResolved == identifiers.size() && "Unresolved type decl"); |
| // Map type declarations to type arguments. |
| for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| // Map type reference to a type. |
| TypeResult type = resolveTypeReference(typeDecls[i], identifierLocs[i]); |
| if (!type.isUsable()) { |
| typeArgs.clear(); |
| return; |
| } |
| |
| typeArgs.push_back(type.get()); |
| } |
| |
| typeArgsLAngleLoc = lAngleLoc; |
| typeArgsRAngleLoc = rAngleLoc; |
| }; |
| |
| // If all of the identifiers can be resolved as type names or |
| // Objective-C class names, we have type arguments. |
| if (numTypeDeclsResolved == identifiers.size()) |
| return resolvedAsTypeDecls(); |
| |
| // Error recovery: some names weren't found, or we have a mix of |
| // type and protocol names. Go resolve all of the unresolved names |
| // and complain if we can't find a consistent answer. |
| LookupNameKind lookupKind = LookupAnyName; |
| for (unsigned i = 0, n = identifiers.size(); i != n; ++i) { |
| // If we already have a protocol or type. Check whether it is the |
| // right thing. |
| if (protocols[i] || typeDecls[i]) { |
| // If we haven't figured out whether we want types or protocols |
| // yet, try to figure it out from this name. |
| if (lookupKind == LookupAnyName) { |
| // If this name refers to both a protocol and a type (e.g., \c |
| // NSObject), don't conclude anything yet. |
| if (protocols[i] && typeDecls[i]) |
| continue; |
| |
| // Otherwise, let this name decide whether we'll be correcting |
| // toward types or protocols. |
| lookupKind = protocols[i] ? LookupObjCProtocolName |
| : LookupOrdinaryName; |
| continue; |
| } |
| |
| // If we want protocols and we have a protocol, there's nothing |
| // more to do. |
| if (lookupKind == LookupObjCProtocolName && protocols[i]) |
| continue; |
| |
| // If we want types and we have a type declaration, there's |
| // nothing more to do. |
| if (lookupKind == LookupOrdinaryName && typeDecls[i]) |
| continue; |
| |
| // We have a conflict: some names refer to protocols and others |
| // refer to types. |
| DiagnoseTypeArgsAndProtocols(identifiers[0], identifierLocs[0], |
| identifiers[i], identifierLocs[i], |
| protocols[i] != nullptr); |
| |
| protocols.clear(); |
| typeArgs.clear(); |
| return; |
| } |
| |
| // Perform typo correction on the name. |
| ObjCTypeArgOrProtocolValidatorCCC CCC(Context, lookupKind); |
| TypoCorrection corrected = |
| CorrectTypo(DeclarationNameInfo(identifiers[i], identifierLocs[i]), |
| lookupKind, S, nullptr, CCC, CTK_ErrorRecovery); |
| if (corrected) { |
| // Did we find a protocol? |
| if (auto proto = corrected.getCorrectionDeclAs<ObjCProtocolDecl>()) { |
| diagnoseTypo(corrected, |
| PDiag(diag::err_undeclared_protocol_suggest) |
| << identifiers[i]); |
| lookupKind = LookupObjCProtocolName; |
| protocols[i] = proto; |
| ++numProtocolsResolved; |
| continue; |
| } |
| |
| // Did we find a type? |
| if (auto typeDecl = corrected.getCorrectionDeclAs<TypeDecl>()) { |
| diagnoseTypo(corrected, |
| PDiag(diag::err_unknown_typename_suggest) |
| << identifiers[i]); |
| lookupKind = LookupOrdinaryName; |
| typeDecls[i] = typeDecl; |
| ++numTypeDeclsResolved; |
| continue; |
| } |
| |
| // Did we find an Objective-C class? |
| if (auto objcClass = corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| diagnoseTypo(corrected, |
| PDiag(diag::err_unknown_type_or_class_name_suggest) |
| << identifiers[i] << true); |
| lookupKind = LookupOrdinaryName; |
| typeDecls[i] = objcClass; |
| ++numTypeDeclsResolved; |
| continue; |
| } |
| } |
| |
| // We couldn't find anything. |
| Diag(identifierLocs[i], |
| (lookupKind == LookupAnyName ? diag::err_objc_type_arg_missing |
| : lookupKind == LookupObjCProtocolName ? diag::err_undeclared_protocol |
| : diag::err_unknown_typename)) |
| << identifiers[i]; |
| protocols.clear(); |
| typeArgs.clear(); |
| return; |
| } |
| |
| // If all of the names were (corrected to) protocols, these were |
| // protocol qualifiers. |
| if (numProtocolsResolved == identifiers.size()) |
| return resolvedAsProtocols(); |
| |
| // Otherwise, all of the names were (corrected to) types. |
| assert(numTypeDeclsResolved == identifiers.size() && "Not all types?"); |
| return resolvedAsTypeDecls(); |
| } |
| |
| /// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of |
| /// a class method in its extension. |
| /// |
| void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT, |
| ObjCInterfaceDecl *ID) { |
| if (!ID) |
| return; // Possibly due to previous error |
| |
| llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap; |
| for (auto *MD : ID->methods()) |
| MethodMap[MD->getSelector()] = MD; |
| |
| if (MethodMap.empty()) |
| return; |
| for (const auto *Method : CAT->methods()) { |
| const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()]; |
| if (PrevMethod && |
| (PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) && |
| !MatchTwoMethodDeclarations(Method, PrevMethod)) { |
| Diag(Method->getLocation(), diag::err_duplicate_method_decl) |
| << Method->getDeclName(); |
| Diag(PrevMethod->getLocation(), diag::note_previous_declaration); |
| } |
| } |
| } |
| |
| /// ActOnForwardProtocolDeclaration - Handle \@protocol foo; |
| Sema::DeclGroupPtrTy |
| Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc, |
| ArrayRef<IdentifierLocPair> IdentList, |
| const ParsedAttributesView &attrList) { |
| SmallVector<Decl *, 8> DeclsInGroup; |
| for (const IdentifierLocPair &IdentPair : IdentList) { |
| IdentifierInfo *Ident = IdentPair.first; |
| ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentPair.second, |
| forRedeclarationInCurContext()); |
| ObjCProtocolDecl *PDecl |
| = ObjCProtocolDecl::Create(Context, CurContext, Ident, |
| IdentPair.second, AtProtocolLoc, |
| PrevDecl); |
| |
| PushOnScopeChains(PDecl, TUScope); |
| CheckObjCDeclScope(PDecl); |
| |
| ProcessDeclAttributeList(TUScope, PDecl, attrList); |
| AddPragmaAttributes(TUScope, PDecl); |
| |
| if (PrevDecl) |
| mergeDeclAttributes(PDecl, PrevDecl); |
| |
| DeclsInGroup.push_back(PDecl); |
| } |
| |
| return BuildDeclaratorGroup(DeclsInGroup); |
| } |
| |
| Decl *Sema::ActOnStartCategoryInterface( |
| SourceLocation AtInterfaceLoc, IdentifierInfo *ClassName, |
| SourceLocation ClassLoc, ObjCTypeParamList *typeParamList, |
| IdentifierInfo *CategoryName, SourceLocation CategoryLoc, |
| Decl *const *ProtoRefs, unsigned NumProtoRefs, |
| const SourceLocation *ProtoLocs, SourceLocation EndProtoLoc, |
| const ParsedAttributesView &AttrList) { |
| ObjCCategoryDecl *CDecl; |
| ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
| |
| /// Check that class of this category is already completely declared. |
| |
| if (!IDecl |
| || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| diag::err_category_forward_interface, |
| CategoryName == nullptr)) { |
| // Create an invalid ObjCCategoryDecl to serve as context for |
| // the enclosing method declarations. We mark the decl invalid |
| // to make it clear that this isn't a valid AST. |
| CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
| ClassLoc, CategoryLoc, CategoryName, |
| IDecl, typeParamList); |
| CDecl->setInvalidDecl(); |
| CurContext->addDecl(CDecl); |
| |
| if (!IDecl) |
| Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
| return ActOnObjCContainerStartDefinition(CDecl); |
| } |
| |
| if (!CategoryName && IDecl->getImplementation()) { |
| Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName; |
| Diag(IDecl->getImplementation()->getLocation(), |
| diag::note_implementation_declared); |
| } |
| |
| if (CategoryName) { |
| /// Check for duplicate interface declaration for this category |
| if (ObjCCategoryDecl *Previous |
| = IDecl->FindCategoryDeclaration(CategoryName)) { |
| // Class extensions can be declared multiple times, categories cannot. |
| Diag(CategoryLoc, diag::warn_dup_category_def) |
| << ClassName << CategoryName; |
| Diag(Previous->getLocation(), diag::note_previous_definition); |
| } |
| } |
| |
| // If we have a type parameter list, check it. |
| if (typeParamList) { |
| if (auto prevTypeParamList = IDecl->getTypeParamList()) { |
| if (checkTypeParamListConsistency(*this, prevTypeParamList, typeParamList, |
| CategoryName |
| ? TypeParamListContext::Category |
| : TypeParamListContext::Extension)) |
| typeParamList = nullptr; |
| } else { |
| Diag(typeParamList->getLAngleLoc(), |
| diag::err_objc_parameterized_category_nonclass) |
| << (CategoryName != nullptr) |
| << ClassName |
| << typeParamList->getSourceRange(); |
| |
| typeParamList = nullptr; |
| } |
| } |
| |
| CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc, |
| ClassLoc, CategoryLoc, CategoryName, IDecl, |
| typeParamList); |
| // FIXME: PushOnScopeChains? |
| CurContext->addDecl(CDecl); |
| |
| // Process the attributes before looking at protocols to ensure that the |
| // availability attribute is attached to the category to provide availability |
| // checking for protocol uses. |
| ProcessDeclAttributeList(TUScope, CDecl, AttrList); |
| AddPragmaAttributes(TUScope, CDecl); |
| |
| if (NumProtoRefs) { |
| diagnoseUseOfProtocols(*this, CDecl, (ObjCProtocolDecl*const*)ProtoRefs, |
| NumProtoRefs, ProtoLocs); |
| CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs, |
| ProtoLocs, Context); |
| // Protocols in the class extension belong to the class. |
| if (CDecl->IsClassExtension()) |
| IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs, |
| NumProtoRefs, Context); |
| } |
| |
| CheckObjCDeclScope(CDecl); |
| return ActOnObjCContainerStartDefinition(CDecl); |
| } |
| |
| /// ActOnStartCategoryImplementation - Perform semantic checks on the |
| /// category implementation declaration and build an ObjCCategoryImplDecl |
| /// object. |
| Decl *Sema::ActOnStartCategoryImplementation( |
| SourceLocation AtCatImplLoc, |
| IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| IdentifierInfo *CatName, SourceLocation CatLoc, |
| const ParsedAttributesView &Attrs) { |
| ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true); |
| ObjCCategoryDecl *CatIDecl = nullptr; |
| if (IDecl && IDecl->hasDefinition()) { |
| CatIDecl = IDecl->FindCategoryDeclaration(CatName); |
| if (!CatIDecl) { |
| // Category @implementation with no corresponding @interface. |
| // Create and install one. |
| CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc, |
| ClassLoc, CatLoc, |
| CatName, IDecl, |
| /*typeParamList=*/nullptr); |
| CatIDecl->setImplicit(); |
| } |
| } |
| |
| ObjCCategoryImplDecl *CDecl = |
| ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl, |
| ClassLoc, AtCatImplLoc, CatLoc); |
| /// Check that class of this category is already completely declared. |
| if (!IDecl) { |
| Diag(ClassLoc, diag::err_undef_interface) << ClassName; |
| CDecl->setInvalidDecl(); |
| } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| diag::err_undef_interface)) { |
| CDecl->setInvalidDecl(); |
| } |
| |
| ProcessDeclAttributeList(TUScope, CDecl, Attrs); |
| AddPragmaAttributes(TUScope, CDecl); |
| |
| // FIXME: PushOnScopeChains? |
| CurContext->addDecl(CDecl); |
| |
| // If the interface has the objc_runtime_visible attribute, we |
| // cannot implement a category for it. |
| if (IDecl && IDecl->hasAttr<ObjCRuntimeVisibleAttr>()) { |
| Diag(ClassLoc, diag::err_objc_runtime_visible_category) |
| << IDecl->getDeclName(); |
| } |
| |
| /// Check that CatName, category name, is not used in another implementation. |
| if (CatIDecl) { |
| if (CatIDecl->getImplementation()) { |
| Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName |
| << CatName; |
| Diag(CatIDecl->getImplementation()->getLocation(), |
| diag::note_previous_definition); |
| CDecl->setInvalidDecl(); |
| } else { |
| CatIDecl->setImplementation(CDecl); |
| // Warn on implementating category of deprecated class under |
| // -Wdeprecated-implementations flag. |
| DiagnoseObjCImplementedDeprecations(*this, CatIDecl, |
| CDecl->getLocation()); |
| } |
| } |
| |
| CheckObjCDeclScope(CDecl); |
| return ActOnObjCContainerStartDefinition(CDecl); |
| } |
| |
| Decl *Sema::ActOnStartClassImplementation( |
| SourceLocation AtClassImplLoc, |
| IdentifierInfo *ClassName, SourceLocation ClassLoc, |
| IdentifierInfo *SuperClassname, |
| SourceLocation SuperClassLoc, |
| const ParsedAttributesView &Attrs) { |
| ObjCInterfaceDecl *IDecl = nullptr; |
| // Check for another declaration kind with the same name. |
| NamedDecl *PrevDecl |
| = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName, |
| forRedeclarationInCurContext()); |
| if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
| Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName; |
| Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) { |
| // FIXME: This will produce an error if the definition of the interface has |
| // been imported from a module but is not visible. |
| RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl), |
| diag::warn_undef_interface); |
| } else { |
| // We did not find anything with the name ClassName; try to correct for |
| // typos in the class name. |
| ObjCInterfaceValidatorCCC CCC{}; |
| TypoCorrection Corrected = |
| CorrectTypo(DeclarationNameInfo(ClassName, ClassLoc), |
| LookupOrdinaryName, TUScope, nullptr, CCC, CTK_NonError); |
| if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) { |
| // Suggest the (potentially) correct interface name. Don't provide a |
| // code-modification hint or use the typo name for recovery, because |
| // this is just a warning. The program may actually be correct. |
| diagnoseTypo(Corrected, |
| PDiag(diag::warn_undef_interface_suggest) << ClassName, |
| /*ErrorRecovery*/false); |
| } else { |
| Diag(ClassLoc, diag::warn_undef_interface) << ClassName; |
| } |
| } |
| |
| // Check that super class name is valid class name |
| ObjCInterfaceDecl *SDecl = nullptr; |
| if (SuperClassname) { |
| // Check if a different kind of symbol declared in this scope. |
| PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc, |
| LookupOrdinaryName); |
| if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) { |
| Diag(SuperClassLoc, diag::err_redefinition_different_kind) |
| << SuperClassname; |
| Diag(PrevDecl->getLocation(), diag::note_previous_definition); |
| } else { |
| SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl); |
| if (SDecl && !SDecl->hasDefinition()) |
| SDecl = nullptr; |
| if (!SDecl) |
| Diag(SuperClassLoc, diag::err_undef_superclass) |
| << SuperClassname << ClassName; |
| else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) { |
| // This implementation and its interface do not have the same |
| // super class. |
| Diag(SuperClassLoc, diag::err_conflicting_super_class) |
| << SDecl->getDeclName(); |
| Diag(SDecl->getLocation(), diag::note_previous_definition); |
| } |
| } |
| } |
| |
| if (!IDecl) { |
| // Legacy case of @implementation with no corresponding @interface. |
| // Build, chain & install the interface decl into the identifier. |
| |
| // FIXME: Do we support attributes on the @implementation? If so we should |
| // copy them over. |
| IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc, |
| ClassName, /*typeParamList=*/nullptr, |
| /*PrevDecl=*/nullptr, ClassLoc, |
| true); |
| AddPragmaAttributes(TUScope, IDecl); |
| IDecl->startDefinition(); |
| if (SDecl) { |
| IDecl->setSuperClass(Context.getTrivialTypeSourceInfo( |
| Context.getObjCInterfaceType(SDecl), |
| SuperClassLoc)); |
| IDecl->setEndOfDefinitionLoc(SuperClassLoc); |
| } else { |
| IDecl->setEndOfDefinitionLoc(ClassLoc); |
| } |
| |
| PushOnScopeChains(IDecl, TUScope); |
| } else { |
| // Mark the interface as being completed, even if it was just as |
| // @class ....; |
| // declaration; the user cannot reopen it. |
| if (!IDecl->hasDefinition()) |
| IDecl->startDefinition(); |
| } |
| |
| ObjCImplementationDecl* IMPDecl = |
| ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl, |
| ClassLoc, AtClassImplLoc, SuperClassLoc); |
| |
| ProcessDeclAttributeList(TUScope, IMPDecl, Attrs); |
| AddPragmaAttributes(TUScope, IMPDecl); |
| |
| if (CheckObjCDeclScope(IMPDecl)) |
| return ActOnObjCContainerStartDefinition(IMPDecl); |
| |
| // Check that there is no duplicate implementation of this class. |
| if (IDecl->getImplementation()) { |
| // FIXME: Don't leak everything! |
| Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName; |
| Diag(IDecl->getImplementation()->getLocation(), |
| diag::note_previous_definition); |
| IMPDecl->setInvalidDecl(); |
| } else { // add it to the list. |
| IDecl->setImplementation(IMPDecl); |
| PushOnScopeChains(IMPDecl, TUScope); |
| // Warn on implementating deprecated class under |
| // -Wdeprecated-implementations flag. |
| DiagnoseObjCImplementedDeprecations(*this, IDecl, IMPDecl->getLocation()); |
| } |
| |
| // If the superclass has the objc_runtime_visible attribute, we |
| // cannot implement a subclass of it. |
| if (IDecl->getSuperClass() && |
| IDecl->getSuperClass()->hasAttr<ObjCRuntimeVisibleAttr>()) { |
| Diag(ClassLoc, diag::err_objc_runtime_visible_subclass) |
| << IDecl->getDeclName() |
| << IDecl->getSuperClass()->getDeclName(); |
| } |
| |
| return ActOnObjCContainerStartDefinition(IMPDecl); |
| } |
| |
| Sema::DeclGroupPtrTy |
| Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) { |
| SmallVector<Decl *, 64> DeclsInGroup; |
| DeclsInGroup.reserve(Decls.size() + 1); |
| |
| for (unsigned i = 0, e = Decls.size(); i != e; ++i) { |
| Decl *Dcl = Decls[i]; |
| if (!Dcl) |
| continue; |
| if (Dcl->getDeclContext()->isFileContext()) |
| Dcl->setTopLevelDeclInObjCContainer(); |
| DeclsInGroup.push_back(Dcl); |
| } |
| |
| DeclsInGroup.push_back(ObjCImpDecl); |
| |
| return BuildDeclaratorGroup(DeclsInGroup); |
| } |
| |
| void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl, |
| ObjCIvarDecl **ivars, unsigned numIvars, |
| SourceLocation RBrace) { |
| assert(ImpDecl && "missing implementation decl"); |
| ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface(); |
| if (!IDecl) |
| return; |
| /// Check case of non-existing \@interface decl. |
| /// (legacy objective-c \@implementation decl without an \@interface decl). |
| /// Add implementations's ivar to the synthesize class's ivar list. |
| if (IDecl->isImplicitInterfaceDecl()) { |
| IDecl->setEndOfDefinitionLoc(RBrace); |
| // Add ivar's to class's DeclContext. |
| for (unsigned i = 0, e = numIvars; i != e; ++i) { |
| ivars[i]->setLexicalDeclContext(ImpDecl); |
| IDecl->makeDeclVisibleInContext(ivars[i]); |
| ImpDecl->addDecl(ivars[i]); |
| } |
| |
| return; |
| } |
| // If implementation has empty ivar list, just return. |
| if (numIvars == 0) |
| return; |
| |
| assert(ivars && "missing @implementation ivars"); |
| if (LangOpts.ObjCRuntime.isNonFragile()) { |
| if (ImpDecl->getSuperClass()) |
| Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use); |
| for (unsigned i = 0; i < numIvars; i++) { |
| ObjCIvarDecl* ImplIvar = ivars[i]; |
| if (const ObjCIvarDecl *ClsIvar = |
| IDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| continue; |
| } |
| // Check class extensions (unnamed categories) for duplicate ivars. |
| for (const auto *CDecl : IDecl->visible_extensions()) { |
| if (const ObjCIvarDecl *ClsExtIvar = |
| CDecl->getIvarDecl(ImplIvar->getIdentifier())) { |
| Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration); |
| Diag(ClsExtIvar->getLocation(), diag::note_previous_definition); |
| continue; |
| } |
| } |
| // Instance ivar to Implementation's DeclContext. |
| ImplIvar->setLexicalDeclContext(ImpDecl); |
| IDecl->makeDeclVisibleInContext(ImplIvar); |
| ImpDecl->addDecl(ImplIvar); |
| } |
| return; |
| } |
| // Check interface's Ivar list against those in the implementation. |
| // names and types must match. |
| // |
| unsigned j = 0; |
| ObjCInterfaceDecl::ivar_iterator |
| IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end(); |
| for (; numIvars > 0 && IVI != IVE; ++IVI) { |
| ObjCIvarDecl* ImplIvar = ivars[j++]; |
| ObjCIvarDecl* ClsIvar = *IVI; |
| assert (ImplIvar && "missing implementation ivar"); |
| assert (ClsIvar && "missing class ivar"); |
| |
| // First, make sure the types match. |
| if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) { |
| Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type) |
| << ImplIvar->getIdentifier() |
| << ImplIvar->getType() << ClsIvar->getType(); |
| Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| } else if (ImplIvar->isBitField() && ClsIvar->isBitField() && |
| ImplIvar->getBitWidthValue(Context) != |
| ClsIvar->getBitWidthValue(Context)) { |
| Diag(ImplIvar->getBitWidth()->getBeginLoc(), |
| diag::err_conflicting_ivar_bitwidth) |
| << ImplIvar->getIdentifier(); |
| Diag(ClsIvar->getBitWidth()->getBeginLoc(), |
| diag::note_previous_definition); |
| } |
| // Make sure the names are identical. |
| if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) { |
| Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name) |
| << ImplIvar->getIdentifier() << ClsIvar->getIdentifier(); |
| Diag(ClsIvar->getLocation(), diag::note_previous_definition); |
| } |
| --numIvars; |
| } |
| |
| if (numIvars > 0) |
| Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count); |
| else if (IVI != IVE) |
| Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count); |
| } |
| |
| static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc, |
| ObjCMethodDecl *method, |
| bool &IncompleteImpl, |
| unsigned DiagID, |
| NamedDecl *NeededFor = nullptr) { |
| // No point warning no definition of method which is 'unavailable'. |
| if (method->getAvailability() == AR_Unavailable) |
| return; |
| |
| // FIXME: For now ignore 'IncompleteImpl'. |
| // Previously we grouped all unimplemented methods under a single |
| // warning, but some users strongly voiced that they would prefer |
| // separate warnings. We will give that approach a try, as that |
| // matches what we do with protocols. |
| { |
| const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID); |
| B << method; |
| if (NeededFor) |
| B << NeededFor; |
| } |
| |
| // Issue a note to the original declaration. |
| SourceLocation MethodLoc = method->getBeginLoc(); |
| if (MethodLoc.isValid()) |
| S.Diag(MethodLoc, diag::note_method_declared_at) << method; |
| } |
| |
| /// Determines if type B can be substituted for type A. Returns true if we can |
| /// guarantee that anything that the user will do to an object of type A can |
| /// also be done to an object of type B. This is trivially true if the two |
| /// types are the same, or if B is a subclass of A. It becomes more complex |
| /// in cases where protocols are involved. |
| /// |
| /// Object types in Objective-C describe the minimum requirements for an |
| /// object, rather than providing a complete description of a type. For |
| /// example, if A is a subclass of B, then B* may refer to an instance of A. |
| /// The principle of substitutability means that we may use an instance of A |
| /// anywhere that we may use an instance of B - it will implement all of the |
| /// ivars of B and all of the methods of B. |
| /// |
| /// This substitutability is important when type checking methods, because |
| /// the implementation may have stricter type definitions than the interface. |
| /// The interface specifies minimum requirements, but the implementation may |
| /// have more accurate ones. For example, a method may privately accept |
| /// instances of B, but only publish that it accepts instances of A. Any |
| /// object passed to it will be type checked against B, and so will implicitly |
| /// by a valid A*. Similarly, a method may return a subclass of the class that |
| /// it is declared as returning. |
| /// |
| /// This is most important when considering subclassing. A method in a |
| /// subclass must accept any object as an argument that its superclass's |
| /// implementation accepts. It may, however, accept a more general type |
| /// without breaking substitutability (i.e. you can still use the subclass |
| /// anywhere that you can use the superclass, but not vice versa). The |
| /// converse requirement applies to return types: the return type for a |
| /// subclass method must be a valid object of the kind that the superclass |
| /// advertises, but it may be specified more accurately. This avoids the need |
| /// for explicit down-casting by callers. |
| /// |
| /// Note: This is a stricter requirement than for assignment. |
| static bool isObjCTypeSubstitutable(ASTContext &Context, |
| const ObjCObjectPointerType *A, |
| const ObjCObjectPointerType *B, |
| bool rejectId) { |
| // Reject a protocol-unqualified id. |
| if (rejectId && B->isObjCIdType()) return false; |
| |
| // If B is a qualified id, then A must also be a qualified id and it must |
| // implement all of the protocols in B. It may not be a qualified class. |
| // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a |
| // stricter definition so it is not substitutable for id<A>. |
| if (B->isObjCQualifiedIdType()) { |
| return A->isObjCQualifiedIdType() && |
| Context.ObjCQualifiedIdTypesAreCompatible(A, B, false); |
| } |
| |
| /* |
| // id is a special type that bypasses type checking completely. We want a |
| // warning when it is used in one place but not another. |
| if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false; |
| |
| |
| // If B is a qualified id, then A must also be a qualified id (which it isn't |
| // if we've got this far) |
| if (B->isObjCQualifiedIdType()) return false; |
| */ |
| |
| // Now we know that A and B are (potentially-qualified) class types. The |
| // normal rules for assignment apply. |
| return Context.canAssignObjCInterfaces(A, B); |
| } |
| |
| static SourceRange getTypeRange(TypeSourceInfo *TSI) { |
| return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange()); |
| } |
| |
| /// Determine whether two set of Objective-C declaration qualifiers conflict. |
| static bool objcModifiersConflict(Decl::ObjCDeclQualifier x, |
| Decl::ObjCDeclQualifier y) { |
| return (x & ~Decl::OBJC_TQ_CSNullability) != |
| (y & ~Decl::OBJC_TQ_CSNullability); |
| } |
| |
| static bool CheckMethodOverrideReturn(Sema &S, |
| ObjCMethodDecl *MethodImpl, |
| ObjCMethodDecl *MethodDecl, |
| bool IsProtocolMethodDecl, |
| bool IsOverridingMode, |
| bool Warn) { |
| if (IsProtocolMethodDecl && |
| objcModifiersConflict(MethodDecl->getObjCDeclQualifier(), |
| MethodImpl->getObjCDeclQualifier())) { |
| if (Warn) { |
| S.Diag(MethodImpl->getLocation(), |
| (IsOverridingMode |
| ? diag::warn_conflicting_overriding_ret_type_modifiers |
| : diag::warn_conflicting_ret_type_modifiers)) |
| << MethodImpl->getDeclName() |
| << MethodImpl->getReturnTypeSourceRange(); |
| S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration) |
| << MethodDecl->getReturnTypeSourceRange(); |
| } |
| else |
| return false; |
| } |
| if (Warn && IsOverridingMode && |
| !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) && |
| !S.Context.hasSameNullabilityTypeQualifier(MethodImpl->getReturnType(), |
| MethodDecl->getReturnType(), |
| false)) { |
| auto nullabilityMethodImpl = |
| *MethodImpl->getReturnType()->getNullability(S.Context); |
| auto nullabilityMethodDecl = |
| *MethodDecl->getReturnType()->getNullability(S.Context); |
| S.Diag(MethodImpl->getLocation(), |
| diag::warn_conflicting_nullability_attr_overriding_ret_types) |
| << DiagNullabilityKind( |
| nullabilityMethodImpl, |
| ((MethodImpl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| != 0)) |
| << DiagNullabilityKind( |
| nullabilityMethodDecl, |
| ((MethodDecl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| != 0)); |
| S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
| } |
| |
| if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(), |
| MethodDecl->getReturnType())) |
| return true; |
| if (!Warn) |
| return false; |
| |
| unsigned DiagID = |
| IsOverridingMode ? diag::warn_conflicting_overriding_ret_types |
| : diag::warn_conflicting_ret_types; |
| |
| // Mismatches between ObjC pointers go into a different warning |
| // category, and sometimes they're even completely whitelisted. |
| if (const ObjCObjectPointerType *ImplPtrTy = |
| MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
| if (const ObjCObjectPointerType *IfacePtrTy = |
| MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) { |
| // Allow non-matching return types as long as they don't violate |
| // the principle of substitutability. Specifically, we permit |
| // return types that are subclasses of the declared return type, |
| // or that are more-qualified versions of the declared type. |
| if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false)) |
| return false; |
| |
| DiagID = |
| IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types |
| : diag::warn_non_covariant_ret_types; |
| } |
| } |
| |
| S.Diag(MethodImpl->getLocation(), DiagID) |
| << MethodImpl->getDeclName() << MethodDecl->getReturnType() |
| << MethodImpl->getReturnType() |
| << MethodImpl->getReturnTypeSourceRange(); |
| S.Diag(MethodDecl->getLocation(), IsOverridingMode |
| ? diag::note_previous_declaration |
| : diag::note_previous_definition) |
| << MethodDecl->getReturnTypeSourceRange(); |
| return false; |
| } |
| |
| static bool CheckMethodOverrideParam(Sema &S, |
| ObjCMethodDecl *MethodImpl, |
| ObjCMethodDecl *MethodDecl, |
| ParmVarDecl *ImplVar, |
| ParmVarDecl *IfaceVar, |
| bool IsProtocolMethodDecl, |
| bool IsOverridingMode, |
| bool Warn) { |
| if (IsProtocolMethodDecl && |
| objcModifiersConflict(ImplVar->getObjCDeclQualifier(), |
| IfaceVar->getObjCDeclQualifier())) { |
| if (Warn) { |
| if (IsOverridingMode) |
| S.Diag(ImplVar->getLocation(), |
| diag::warn_conflicting_overriding_param_modifiers) |
| << getTypeRange(ImplVar->getTypeSourceInfo()) |
| << MethodImpl->getDeclName(); |
| else S.Diag(ImplVar->getLocation(), |
| diag::warn_conflicting_param_modifiers) |
| << getTypeRange(ImplVar->getTypeSourceInfo()) |
| << MethodImpl->getDeclName(); |
| S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration) |
| << getTypeRange(IfaceVar->getTypeSourceInfo()); |
| } |
| else |
| return false; |
| } |
| |
| QualType ImplTy = ImplVar->getType(); |
| QualType IfaceTy = IfaceVar->getType(); |
| if (Warn && IsOverridingMode && |
| !isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) && |
| !S.Context.hasSameNullabilityTypeQualifier(ImplTy, IfaceTy, true)) { |
| S.Diag(ImplVar->getLocation(), |
| diag::warn_conflicting_nullability_attr_overriding_param_types) |
| << DiagNullabilityKind( |
| *ImplTy->getNullability(S.Context), |
| ((ImplVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| != 0)) |
| << DiagNullabilityKind( |
| *IfaceTy->getNullability(S.Context), |
| ((IfaceVar->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability) |
| != 0)); |
| S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration); |
| } |
| if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy)) |
| return true; |
| |
| if (!Warn) |
| return false; |
| unsigned DiagID = |
| IsOverridingMode ? diag::warn_conflicting_overriding_param_types |
| : diag::warn_conflicting_param_types; |
| |
| // Mismatches between ObjC pointers go into a different warning |
| // category, and sometimes they're even completely whitelisted. |
| if (const ObjCObjectPointerType *ImplPtrTy = |
| ImplTy->getAs<ObjCObjectPointerType>()) { |
| if (const ObjCObjectPointerType *IfacePtrTy = |
| IfaceTy->getAs<ObjCObjectPointerType>()) { |
| // Allow non-matching argument types as long as they don't |
| // violate the principle of substitutability. Specifically, the |
| // implementation must accept any objects that the superclass |
| // accepts, however it may also accept others. |
| if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true)) |
| return false; |
| |
| DiagID = |
| IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types |
| : diag::warn_non_contravariant_param_types; |
| } |
| } |
| |
| S.Diag(ImplVar->getLocation(), DiagID) |
| << getTypeRange(ImplVar->getTypeSourceInfo()) |
| << MethodImpl->getDeclName() << IfaceTy << ImplTy; |
| S.Diag(IfaceVar->getLocation(), |
| (IsOverridingMode ? diag::note_previous_declaration |
| : diag::note_previous_definition)) |
| << getTypeRange(IfaceVar->getTypeSourceInfo()); |
| return false; |
| } |
| |
| /// In ARC, check whether the conventional meanings of the two methods |
| /// match. If they don't, it's a hard error. |
| static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl, |
| ObjCMethodDecl *decl) { |
| ObjCMethodFamily implFamily = impl->getMethodFamily(); |
| ObjCMethodFamily declFamily = decl->getMethodFamily(); |
| if (implFamily == declFamily) return false; |
| |
| // Since conventions are sorted by selector, the only possibility is |
| // that the types differ enough to cause one selector or the other |
| // to fall out of the family. |
| assert(implFamily == OMF_None || declFamily == OMF_None); |
| |
| // No further diagnostics required on invalid declarations. |
| if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true; |
| |
| const ObjCMethodDecl *unmatched = impl; |
| ObjCMethodFamily family = declFamily; |
| unsigned errorID = diag::err_arc_lost_method_convention; |
| unsigned noteID = diag::note_arc_lost_method_convention; |
| if (declFamily == OMF_None) { |
| unmatched = decl; |
| family = implFamily; |
| errorID = diag::err_arc_gained_method_convention; |
| noteID = diag::note_arc_gained_method_convention; |
| } |
| |
| // Indexes into a %select clause in the diagnostic. |
| enum FamilySelector { |
| F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new |
| }; |
| FamilySelector familySelector = FamilySelector(); |
| |
| switch (family) { |
| case OMF_None: llvm_unreachable("logic error, no method convention"); |
| case OMF_retain: |
| case OMF_release: |
| case OMF_autorelease: |
| case OMF_dealloc: |
| case OMF_finalize: |
| case OMF_retainCount: |
| case OMF_self: |
| case OMF_initialize: |
| case OMF_performSelector: |
| // Mismatches for these methods don't change ownership |
| // conventions, so we don't care. |
| return false; |
| |
| case OMF_init: familySelector = F_init; break; |
| case OMF_alloc: familySelector = F_alloc; break; |
| case OMF_copy: familySelector = F_copy; break; |
| case OMF_mutableCopy: familySelector = F_mutableCopy; break; |
| case OMF_new: familySelector = F_new; break; |
| } |
| |
| enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn }; |
| ReasonSelector reasonSelector; |
| |
| // The only reason these methods don't fall within their families is |
| // due to unusual result types. |
| if (unmatched->getReturnType()->isObjCObjectPointerType()) { |
| reasonSelector = R_UnrelatedReturn; |
| } else { |
| reasonSelector = R_NonObjectReturn; |
| } |
| |
| S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector); |
| S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector); |
| |
| return true; |
| } |
| |
| void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| ObjCMethodDecl *MethodDecl, |
| bool IsProtocolMethodDecl) { |
| if (getLangOpts().ObjCAutoRefCount && |
| checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl)) |
| return; |
| |
| CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
| IsProtocolMethodDecl, false, |
| true); |
| |
| for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
| IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| EF = MethodDecl->param_end(); |
| IM != EM && IF != EF; ++IM, ++IF) { |
| CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF, |
| IsProtocolMethodDecl, false, true); |
| } |
| |
| if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) { |
| Diag(ImpMethodDecl->getLocation(), |
| diag::warn_conflicting_variadic); |
| Diag(MethodDecl->getLocation(), diag::note_previous_declaration); |
| } |
| } |
| |
| void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method, |
| ObjCMethodDecl *Overridden, |
| bool IsProtocolMethodDecl) { |
| |
| CheckMethodOverrideReturn(*this, Method, Overridden, |
| IsProtocolMethodDecl, true, |
| true); |
| |
| for (ObjCMethodDecl::param_iterator IM = Method->param_begin(), |
| IF = Overridden->param_begin(), EM = Method->param_end(), |
| EF = Overridden->param_end(); |
| IM != EM && IF != EF; ++IM, ++IF) { |
| CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF, |
| IsProtocolMethodDecl, true, true); |
| } |
| |
| if (Method->isVariadic() != Overridden->isVariadic()) { |
| Diag(Method->getLocation(), |
| diag::warn_conflicting_overriding_variadic); |
| Diag(Overridden->getLocation(), diag::note_previous_declaration); |
| } |
| } |
| |
| /// WarnExactTypedMethods - This routine issues a warning if method |
| /// implementation declaration matches exactly that of its declaration. |
| void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl, |
| ObjCMethodDecl *MethodDecl, |
| bool IsProtocolMethodDecl) { |
| // don't issue warning when protocol method is optional because primary |
| // class is not required to implement it and it is safe for protocol |
| // to implement it. |
| if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional) |
| return; |
| // don't issue warning when primary class's method is |
| // depecated/unavailable. |
| if (MethodDecl->hasAttr<UnavailableAttr>() || |
| MethodDecl->hasAttr<DeprecatedAttr>()) |
| return; |
| |
| bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl, |
| IsProtocolMethodDecl, false, false); |
| if (match) |
| for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(), |
| IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(), |
| EF = MethodDecl->param_end(); |
| IM != EM && IF != EF; ++IM, ++IF) { |
| match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, |
| *IM, *IF, |
| IsProtocolMethodDecl, false, false); |
| if (!match) |
| break; |
| } |
| if (match) |
| match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic()); |
| if (match) |
| match = !(MethodDecl->isClassMethod() && |
| MethodDecl->getSelector() == GetNullarySelector("load", Context)); |
| |
| if (match) { |
| Diag(ImpMethodDecl->getLocation(), |
| diag::warn_category_method_impl_match); |
| Diag(MethodDecl->getLocation(), diag::note_method_declared_at) |
| << MethodDecl->getDeclName(); |
| } |
| } |
| |
| /// FIXME: Type hierarchies in Objective-C can be deep. We could most likely |
| /// improve the efficiency of selector lookups and type checking by associating |
| /// with each protocol / interface / category the flattened instance tables. If |
| /// we used an immutable set to keep the table then it wouldn't add significant |
| /// memory cost and it would be handy for lookups. |
| |
| typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet; |
| typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet; |
| |
| static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl, |
| ProtocolNameSet &PNS) { |
| if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) |
| PNS.insert(PDecl->getIdentifier()); |
| for (const auto *PI : PDecl->protocols()) |
| findProtocolsWithExplicitImpls(PI, PNS); |
| } |
| |
| /// Recursively populates a set with all conformed protocols in a class |
| /// hierarchy that have the 'objc_protocol_requires_explicit_implementation' |
| /// attribute. |
| static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super, |
| ProtocolNameSet &PNS) { |
| if (!Super) |
| return; |
| |
| for (const auto *I : Super->all_referenced_protocols()) |
| findProtocolsWithExplicitImpls(I, PNS); |
| |
| findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS); |
| } |
| |
| /// CheckProtocolMethodDefs - This routine checks unimplemented methods |
| /// Declared in protocol, and those referenced by it. |
| static void CheckProtocolMethodDefs(Sema &S, |
| SourceLocation ImpLoc, |
| ObjCProtocolDecl *PDecl, |
| bool& IncompleteImpl, |
| const Sema::SelectorSet &InsMap, |
| const Sema::SelectorSet &ClsMap, |
| ObjCContainerDecl *CDecl, |
| LazyProtocolNameSet &ProtocolsExplictImpl) { |
| ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl); |
| ObjCInterfaceDecl *IDecl = C ? C->getClassInterface() |
| : dyn_cast<ObjCInterfaceDecl>(CDecl); |
| assert (IDecl && "CheckProtocolMethodDefs - IDecl is null"); |
| |
| ObjCInterfaceDecl *Super = IDecl->getSuperClass(); |
| ObjCInterfaceDecl *NSIDecl = nullptr; |
| |
| // If this protocol is marked 'objc_protocol_requires_explicit_implementation' |
| // then we should check if any class in the super class hierarchy also |
| // conforms to this protocol, either directly or via protocol inheritance. |
| // If so, we can skip checking this protocol completely because we |
| // know that a parent class already satisfies this protocol. |
| // |
| // Note: we could generalize this logic for all protocols, and merely |
| // add the limit on looking at the super class chain for just |
| // specially marked protocols. This may be a good optimization. This |
| // change is restricted to 'objc_protocol_requires_explicit_implementation' |
| // protocols for now for controlled evaluation. |
| if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) { |
| if (!ProtocolsExplictImpl) { |
| ProtocolsExplictImpl.reset(new ProtocolNameSet); |
| findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl); |
| } |
| if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) != |
| ProtocolsExplictImpl->end()) |
| return; |
| |
| // If no super class conforms to the protocol, we should not search |
| // for methods in the super class to implicitly satisfy the protocol. |
| Super = nullptr; |
| } |
| |
| if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) { |
| // check to see if class implements forwardInvocation method and objects |
| // of this class are derived from 'NSProxy' so that to forward requests |
| // from one object to another. |
| // Under such conditions, which means that every method possible is |
| // implemented in the class, we should not issue "Method definition not |
| // found" warnings. |
| // FIXME: Use a general GetUnarySelector method for this. |
| IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation"); |
| Selector fISelector = S.Context.Selectors.getSelector(1, &II); |
| if (InsMap.count(fISelector)) |
| // Is IDecl derived from 'NSProxy'? If so, no instance methods |
| // need be implemented in the implementation. |
| NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy")); |
| } |
| |
| // If this is a forward protocol declaration, get its definition. |
| if (!PDecl->isThisDeclarationADefinition() && |
| PDecl->getDefinition()) |
| PDecl = PDecl->getDefinition(); |
| |
| // If a method lookup fails locally we still need to look and see if |
| // the method was implemented by a base class or an inherited |
| // protocol. This lookup is slow, but occurs rarely in correct code |
| // and otherwise would terminate in a warning. |
| |
| // check unimplemented instance methods. |
| if (!NSIDecl) |
| for (auto *method : PDecl->instance_methods()) { |
| if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| !method->isPropertyAccessor() && |
| !InsMap.count(method->getSelector()) && |
| (!Super || !Super->lookupMethod(method->getSelector(), |
| true /* instance */, |
| false /* shallowCategory */, |
| true /* followsSuper */, |
| nullptr /* category */))) { |
| // If a method is not implemented in the category implementation but |
| // has been declared in its primary class, superclass, |
| // or in one of their protocols, no need to issue the warning. |
| // This is because method will be implemented in the primary class |
| // or one of its super class implementation. |
| |
| // Ugly, but necessary. Method declared in protocol might have |
| // have been synthesized due to a property declared in the class which |
| // uses the protocol. |
| if (ObjCMethodDecl *MethodInClass = |
| IDecl->lookupMethod(method->getSelector(), |
| true /* instance */, |
| true /* shallowCategoryLookup */, |
| false /* followSuper */)) |
| if (C || MethodInClass->isPropertyAccessor()) |
| continue; |
| unsigned DIAG = diag::warn_unimplemented_protocol_method; |
| if (!S.Diags.isIgnored(DIAG, ImpLoc)) { |
| WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, |
| PDecl); |
| } |
| } |
| } |
| // check unimplemented class methods |
| for (auto *method : PDecl->class_methods()) { |
| if (method->getImplementationControl() != ObjCMethodDecl::Optional && |
| !ClsMap.count(method->getSelector()) && |
| (!Super || !Super->lookupMethod(method->getSelector(), |
| false /* class method */, |
| false /* shallowCategoryLookup */, |
| true /* followSuper */, |
| nullptr /* category */))) { |
| // See above comment for instance method lookups. |
| if (C && IDecl->lookupMethod(method->getSelector(), |
| |