2007-12-12 15:09:47 +08:00
|
|
|
//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-12-12 15:09:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements semantic analysis for Objective C declarations.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "TypeLocBuilder.h"
|
2011-06-16 07:02:42 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2007-12-12 15:09:47 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2011-11-13 05:07:52 +08:00
|
|
|
#include "clang/AST/ASTMutationListener.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/ExprObjC.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/AST/RecursiveASTVisitor.h"
|
2011-06-16 07:02:42 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Sema/DeclSpec.h"
|
|
|
|
#include "clang/Sema/Lookup.h"
|
|
|
|
#include "clang/Sema/Scope.h"
|
|
|
|
#include "clang/Sema/ScopeInfo.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Sema/SemaInternal.h"
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2010-08-25 15:03:20 +08:00
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
/// 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?
|
|
|
|
|
2014-01-26 00:55:45 +08:00
|
|
|
const ObjCObjectType *result =
|
|
|
|
method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType();
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
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.
|
2011-12-16 04:29:51 +08:00
|
|
|
if (!resultClass->hasDefinition()) {
|
2011-06-16 07:02:42 +08:00
|
|
|
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.
|
2014-05-26 14:22:03 +08:00
|
|
|
const ObjCInterfaceDecl *receiverClass = nullptr;
|
2011-06-16 07:02:42 +08:00
|
|
|
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)) {
|
2015-10-28 13:03:19 +08:00
|
|
|
method->addAttr(UnavailableAttr::CreateImplicit(Context, "",
|
|
|
|
UnavailableAttr::IR_ARCInitReturnsUnrelated, loc));
|
2011-06-16 07:02:42 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, it's an error.
|
|
|
|
Diag(loc, diag::err_arc_init_method_unrelated_result_type);
|
|
|
|
method->setInvalidDecl();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-28 06:35:36 +08:00
|
|
|
void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
|
2013-01-16 06:43:08 +08:00
|
|
|
const ObjCMethodDecl *Overridden) {
|
2011-06-11 09:09:30 +08:00
|
|
|
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.
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType ResultType = NewMethod->getReturnType();
|
2014-08-01 21:20:09 +08:00
|
|
|
SourceRange ResultTypeRange = NewMethod->getReturnTypeSourceRange();
|
2011-06-11 09:09:30 +08:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2011-09-08 09:46:34 +08:00
|
|
|
if (ObjCMethodFamily Family = Overridden->getMethodFamily())
|
|
|
|
Diag(Overridden->getLocation(),
|
2013-03-19 15:04:25 +08:00
|
|
|
diag::note_related_result_type_family)
|
|
|
|
<< /*overridden method*/ 0
|
2011-09-08 09:46:34 +08:00
|
|
|
<< Family;
|
|
|
|
else
|
|
|
|
Diag(Overridden->getLocation(),
|
|
|
|
diag::note_related_result_type_overridden);
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount) {
|
2011-09-28 06:35:36 +08:00
|
|
|
if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
|
|
|
|
Overridden->hasAttr<NSReturnsRetainedAttr>())) {
|
|
|
|
Diag(NewMethod->getLocation(),
|
|
|
|
diag::err_nsreturns_retained_attribute_mismatch) << 1;
|
|
|
|
Diag(Overridden->getLocation(), diag::note_previous_decl)
|
|
|
|
<< "method";
|
|
|
|
}
|
|
|
|
if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
|
|
|
|
Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
|
|
|
|
Diag(NewMethod->getLocation(),
|
|
|
|
diag::err_nsreturns_retained_attribute_mismatch) << 0;
|
|
|
|
Diag(Overridden->getLocation(), diag::note_previous_decl)
|
|
|
|
<< "method";
|
|
|
|
}
|
2012-05-18 07:13:29 +08:00
|
|
|
ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
|
|
|
|
oe = Overridden->param_end();
|
2011-10-03 14:37:04 +08:00
|
|
|
for (ObjCMethodDecl::param_iterator
|
|
|
|
ni = NewMethod->param_begin(), ne = NewMethod->param_end();
|
2012-05-18 07:13:29 +08:00
|
|
|
ni != ne && oi != oe; ++ni, ++oi) {
|
2011-10-03 14:37:04 +08:00
|
|
|
const ParmVarDecl *oldDecl = (*oi);
|
2011-09-28 06:35:36 +08:00
|
|
|
ParmVarDecl *newDecl = (*ni);
|
|
|
|
if (newDecl->hasAttr<NSConsumedAttr>() !=
|
|
|
|
oldDecl->hasAttr<NSConsumedAttr>()) {
|
|
|
|
Diag(newDecl->getLocation(),
|
|
|
|
diag::err_nsconsumed_attribute_mismatch);
|
|
|
|
Diag(oldDecl->getLocation(), diag::note_previous_decl)
|
|
|
|
<< "parameter";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
/// \brief Check a method declaration for compatibility with the Objective-C
|
|
|
|
/// ARC conventions.
|
2013-04-04 09:38:37 +08:00
|
|
|
bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) {
|
2011-06-16 07:02:42 +08:00
|
|
|
ObjCMethodFamily family = method->getMethodFamily();
|
|
|
|
switch (family) {
|
|
|
|
case OMF_None:
|
2011-08-29 06:35:17 +08:00
|
|
|
case OMF_finalize:
|
2011-06-16 07:02:42 +08:00
|
|
|
case OMF_retain:
|
|
|
|
case OMF_release:
|
|
|
|
case OMF_autorelease:
|
|
|
|
case OMF_retainCount:
|
|
|
|
case OMF_self:
|
2014-08-23 00:57:26 +08:00
|
|
|
case OMF_initialize:
|
2011-07-22 10:45:48 +08:00
|
|
|
case OMF_performSelector:
|
2011-06-16 07:02:42 +08:00
|
|
|
return false;
|
|
|
|
|
2012-07-31 04:52:48 +08:00
|
|
|
case OMF_dealloc:
|
2014-01-26 00:55:45 +08:00
|
|
|
if (!Context.hasSameType(method->getReturnType(), Context.VoidTy)) {
|
2014-08-01 21:20:09 +08:00
|
|
|
SourceRange ResultTypeRange = method->getReturnTypeSourceRange();
|
2012-07-31 04:52:48 +08:00
|
|
|
if (ResultTypeRange.isInvalid())
|
2014-01-26 00:55:45 +08:00
|
|
|
Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
|
|
|
|
<< method->getReturnType()
|
|
|
|
<< FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
|
2012-07-31 04:52:48 +08:00
|
|
|
else
|
2014-01-26 00:55:45 +08:00
|
|
|
Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
|
|
|
|
<< method->getReturnType()
|
|
|
|
<< FixItHint::CreateReplacement(ResultTypeRange, "void");
|
2012-07-31 04:52:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
case OMF_init:
|
|
|
|
// If the method doesn't obey the init rules, don't bother annotating it.
|
2013-04-04 09:38:37 +08:00
|
|
|
if (checkInitMethod(method, QualType()))
|
2011-06-16 07:02:42 +08:00
|
|
|
return true;
|
|
|
|
|
2014-01-16 21:03:14 +08:00
|
|
|
method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context));
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-01-16 21:03:14 +08:00
|
|
|
method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context));
|
2011-06-16 07:02:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-02-16 01:49:58 +08:00
|
|
|
static void DiagnoseObjCImplementedDeprecations(Sema &S,
|
|
|
|
NamedDecl *ND,
|
|
|
|
SourceLocation ImplLoc,
|
|
|
|
int select) {
|
Implement a new 'availability' attribute, that allows one to specify
which versions of an OS provide a certain facility. For example,
void foo()
__attribute__((availability(macosx,introduced=10.2,deprecated=10.4,obsoleted=10.6)));
says that the function "foo" was introduced in 10.2, deprecated in
10.4, and completely obsoleted in 10.6. This attribute ties in with
the deployment targets (e.g., -mmacosx-version-min=10.1 specifies that
we want to deploy back to Mac OS X 10.1). There are several concrete
behaviors that this attribute enables, as illustrated with the
function foo() above:
- If we choose a deployment target >= Mac OS X 10.4, uses of "foo"
will result in a deprecation warning, as if we had placed
attribute((deprecated)) on it (but with a better diagnostic)
- If we choose a deployment target >= Mac OS X 10.6, uses of "foo"
will result in an "unavailable" warning (in C)/error (in C++), as
if we had placed attribute((unavailable)) on it
- If we choose a deployment target prior to 10.2, foo() is
weak-imported (if it is a kind of entity that can be weak
imported), as if we had placed the weak_import attribute on it.
Naturally, there can be multiple availability attributes on a
declaration, for different platforms; only the current platform
matters when checking availability attributes.
The only platforms this attribute currently works for are "ios" and
"macosx", since we already have -mxxxx-version-min flags for them and we
have experience there with macro tricks translating down to the
deprecated/unavailable/weak_import attributes. The end goal is to open
this up to other platforms, and even extension to other "platforms"
that are really libraries (say, through a #pragma clang
define_system), but that hasn't yet been designed and we may want to
shake out more issues with this narrower problem first.
Addresses <rdar://problem/6690412>.
As a drive-by bug-fix, if an entity is both deprecated and
unavailable, we only emit the "unavailable" diagnostic.
llvm-svn: 128127
2011-03-23 08:50:03 +08:00
|
|
|
if (ND && ND->isDeprecated()) {
|
2011-02-16 08:30:31 +08:00
|
|
|
S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
|
2011-02-16 01:49:58 +08:00
|
|
|
if (select == 0)
|
2012-02-28 06:55:11 +08:00
|
|
|
S.Diag(ND->getLocation(), diag::note_method_declared_at)
|
|
|
|
<< ND->getDeclName();
|
2011-02-16 01:49:58 +08:00
|
|
|
else
|
|
|
|
S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-01 01:37:55 +08:00
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2012-09-14 02:53:14 +08:00
|
|
|
/// 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();
|
|
|
|
}
|
|
|
|
|
2012-08-09 07:41:08 +08:00
|
|
|
/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
|
|
|
|
/// and user declared, in the method definition's AST.
|
|
|
|
void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
|
2014-05-26 14:22:03 +08:00
|
|
|
assert((getCurMethodDecl() == nullptr) && "Methodparsing confused");
|
2010-08-21 17:40:31 +08:00
|
|
|
ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
|
2012-07-03 07:37:09 +08:00
|
|
|
|
2008-07-26 01:57:26 +08:00
|
|
|
// If we don't have a valid method decl, simply return.
|
|
|
|
if (!MDecl)
|
|
|
|
return;
|
2007-12-18 09:30:32 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// Allow all of Sema to see that we are entering a method definition.
|
2008-12-12 00:49:14 +08:00
|
|
|
PushDeclContext(FnBodyScope, MDecl);
|
Keep an explicit stack of function and block scopes, each element of
which has the label map, switch statement stack, etc. Previously, we
had a single set of maps in Sema (for the function) along with a stack
of block scopes. However, this lead to funky behavior with nested
functions, e.g., in the member functions of local classes.
The explicit-stack approach is far cleaner, and we retain a 1-element
cache so that we're not malloc/free'ing every time we enter a
function. Fixes PR6382.
Also, tweaked the unused-variable warning suppression logic to look at
errors within a given Scope rather than within a given function. The
prior code wasn't looking at the right number-of-errors count when
dealing with blocks, since the block's count would be deallocated
before we got to ActOnPopScope. This approach works with nested
blocks/functions, and gives tighter error recovery.
llvm-svn: 97518
2010-03-02 07:15:13 +08:00
|
|
|
PushFunctionScope();
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// Create Decl objects for each parameter, entrring them in the scope for
|
|
|
|
// binding to their use.
|
|
|
|
|
|
|
|
// Insert the invisible arguments, self and _cmd!
|
2008-12-10 04:23:04 +08:00
|
|
|
MDecl->createImplicitParams(Context, MDecl->getClassInterface());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-26 14:07:48 +08:00
|
|
|
PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
|
|
|
|
PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
|
2008-04-08 12:40:51 +08:00
|
|
|
|
2013-06-24 22:38:26 +08:00
|
|
|
// The ObjC parser requires parameter names so there's no need to check.
|
2016-06-24 12:05:48 +08:00
|
|
|
CheckParmsForFunctionDef(MDecl->parameters(),
|
2013-06-24 22:38:26 +08:00
|
|
|
/*CheckParameterNames=*/false);
|
|
|
|
|
2008-04-10 10:22:51 +08:00
|
|
|
// Introduce all of the other parameters into this scope.
|
2016-06-24 12:05:48 +08:00
|
|
|
for (auto *Param : MDecl->parameters()) {
|
2012-09-14 02:53:14 +08:00
|
|
|
if (!Param->isInvalidDecl() &&
|
|
|
|
getLangOpts().ObjCAutoRefCount &&
|
|
|
|
!HasExplicitOwnershipAttr(*this, Param))
|
|
|
|
Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
|
|
|
|
Param->getType();
|
2012-08-31 07:56:02 +08:00
|
|
|
|
2014-03-08 01:50:17 +08:00
|
|
|
if (Param->getIdentifier())
|
|
|
|
PushOnScopeChains(Param, FnBodyScope);
|
2010-09-18 06:07:07 +08:00
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
// In ARC, disallow definition of retain/release/autorelease/retainCount
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount) {
|
2011-06-16 07:02:42 +08:00
|
|
|
switch (MDecl->getMethodFamily()) {
|
|
|
|
case OMF_retain:
|
|
|
|
case OMF_retainCount:
|
|
|
|
case OMF_release:
|
|
|
|
case OMF_autorelease:
|
|
|
|
Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
|
2013-05-17 03:08:44 +08:00
|
|
|
<< 0 << MDecl->getSelector();
|
2011-06-16 07:02:42 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OMF_None:
|
|
|
|
case OMF_dealloc:
|
2011-08-29 06:35:17 +08:00
|
|
|
case OMF_finalize:
|
2011-06-16 07:02:42 +08:00
|
|
|
case OMF_alloc:
|
|
|
|
case OMF_init:
|
|
|
|
case OMF_mutableCopy:
|
|
|
|
case OMF_copy:
|
|
|
|
case OMF_new:
|
|
|
|
case OMF_self:
|
2014-08-23 00:57:26 +08:00
|
|
|
case OMF_initialize:
|
2011-07-06 06:38:59 +08:00
|
|
|
case OMF_performSelector:
|
2011-06-16 07:02:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-23 01:25:57 +08:00
|
|
|
// Warn on deprecated methods under -Wdeprecated-implementations,
|
|
|
|
// and prepare for warning on missing super calls.
|
|
|
|
if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
|
2012-09-08 07:46:23 +08:00
|
|
|
ObjCMethodDecl *IMD =
|
|
|
|
IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
|
|
|
|
|
2012-11-18 04:53:53 +08:00
|
|
|
if (IMD) {
|
|
|
|
ObjCImplDecl *ImplDeclOfMethodDef =
|
|
|
|
dyn_cast<ObjCImplDecl>(MDecl->getDeclContext());
|
|
|
|
ObjCContainerDecl *ContDeclOfMethodDecl =
|
|
|
|
dyn_cast<ObjCContainerDecl>(IMD->getDeclContext());
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCImplDecl *ImplDeclOfMethodDecl = nullptr;
|
2012-11-18 04:53:53 +08:00
|
|
|
if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
|
|
|
|
ImplDeclOfMethodDecl = OID->getImplementation();
|
2014-03-19 00:25:22 +08:00
|
|
|
else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) {
|
|
|
|
if (CD->IsClassExtension()) {
|
|
|
|
if (ObjCInterfaceDecl *OID = CD->getClassInterface())
|
|
|
|
ImplDeclOfMethodDecl = OID->getImplementation();
|
|
|
|
} else
|
|
|
|
ImplDeclOfMethodDecl = CD->getImplementation();
|
2014-03-18 08:10:37 +08:00
|
|
|
}
|
2012-11-18 04:53:53 +08:00
|
|
|
// No need to issue deprecated warning if deprecated mehod in class/category
|
|
|
|
// is being implemented in its own implementation (no overriding is involved).
|
2014-03-19 00:25:22 +08:00
|
|
|
if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef)
|
2012-11-18 04:53:53 +08:00
|
|
|
DiagnoseObjCImplementedDeprecations(*this,
|
2011-02-16 01:49:58 +08:00
|
|
|
dyn_cast<NamedDecl>(IMD),
|
|
|
|
MDecl->getLocation(), 0);
|
2012-11-18 04:53:53 +08:00
|
|
|
}
|
2011-08-23 01:25:57 +08:00
|
|
|
|
2013-12-04 05:11:49 +08:00
|
|
|
if (MDecl->getMethodFamily() == OMF_init) {
|
|
|
|
if (MDecl->isDesignatedInitializerForTheInterface()) {
|
|
|
|
getCurFunction()->ObjCIsDesignatedInit = true;
|
2014-03-15 07:30:18 +08:00
|
|
|
getCurFunction()->ObjCWarnForNoDesignatedInitChain =
|
2014-05-26 14:22:03 +08:00
|
|
|
IC->getSuperClass() != nullptr;
|
2013-12-04 05:11:49 +08:00
|
|
|
} else if (IC->hasDesignatedInitializers()) {
|
|
|
|
getCurFunction()->ObjCIsSecondaryInit = true;
|
2014-03-15 07:30:18 +08:00
|
|
|
getCurFunction()->ObjCWarnForNoInitDelegation = true;
|
2013-12-04 05:11:49 +08:00
|
|
|
}
|
|
|
|
}
|
2013-12-04 05:11:36 +08:00
|
|
|
|
2011-08-29 06:35:17 +08:00
|
|
|
// If this is "dealloc" or "finalize", set some bit here.
|
2011-08-23 01:25:57 +08:00
|
|
|
// 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.
|
2013-03-05 09:27:54 +08:00
|
|
|
if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) {
|
2012-10-20 00:05:26 +08:00
|
|
|
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;
|
|
|
|
|
2013-11-05 08:28:21 +08:00
|
|
|
} else {
|
2012-10-20 00:05:26 +08:00
|
|
|
const ObjCMethodDecl *SuperMethod =
|
2013-03-05 09:27:54 +08:00
|
|
|
SuperClass->lookupMethod(MDecl->getSelector(),
|
|
|
|
MDecl->isInstanceMethod());
|
2012-10-20 00:05:26 +08:00
|
|
|
getCurFunction()->ObjCShouldCallSuper =
|
|
|
|
(SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
|
2012-09-11 02:04:25 +08:00
|
|
|
}
|
2011-08-29 06:35:17 +08:00
|
|
|
}
|
2011-08-23 01:25:57 +08:00
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2012-01-13 09:32:50 +08:00
|
|
|
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 : public CorrectionCandidateCallback {
|
|
|
|
public:
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCInterfaceValidatorCCC() : CurrentIDecl(nullptr) {}
|
2012-01-13 09:32:50 +08:00
|
|
|
explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
|
|
|
|
: CurrentIDecl(IDecl) {}
|
|
|
|
|
2014-03-12 12:55:44 +08:00
|
|
|
bool ValidateCandidate(const TypoCorrection &candidate) override {
|
2012-01-13 09:32:50 +08:00
|
|
|
ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
|
|
|
|
return ID && !declaresSameEntity(ID, CurrentIDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ObjCInterfaceDecl *CurrentIDecl;
|
|
|
|
};
|
|
|
|
|
2015-10-07 07:40:43 +08:00
|
|
|
} // end anonymous namespace
|
2012-01-13 09:32:50 +08:00
|
|
|
|
2015-04-20 04:15:55 +08:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 11:57:35 +08:00
|
|
|
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.
|
|
|
|
if (TypoCorrection Corrected = CorrectTypo(
|
|
|
|
DeclarationNameInfo(SuperName, SuperLoc),
|
|
|
|
LookupOrdinaryName, TUScope,
|
2015-10-07 07:40:43 +08:00
|
|
|
nullptr, llvm::make_unique<ObjCInterfaceValidatorCCC>(IDecl),
|
2015-07-07 11:57:35 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-10-07 07:40:43 +08:00
|
|
|
if (PrevDecl && !SuperClassDecl) {
|
2015-07-07 11:57:35 +08:00
|
|
|
// 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->getAs<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))) {
|
2015-10-07 07:40:43 +08:00
|
|
|
SuperClassDecl = nullptr;
|
2015-07-07 11:57:35 +08:00
|
|
|
SuperClassType = QualType();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SuperClassType.isNull()) {
|
|
|
|
assert(!SuperClassDecl && "Failed to set SuperClassType?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle type arguments on the superclass.
|
|
|
|
TypeSourceInfo *SuperClassTInfo = nullptr;
|
2015-07-07 11:58:14 +08:00
|
|
|
if (!SuperTypeArgs.empty()) {
|
|
|
|
TypeResult fullSuperClassType = actOnObjCTypeArgsAndProtocolQualifiers(
|
|
|
|
S,
|
|
|
|
SuperLoc,
|
|
|
|
CreateParsedType(SuperClassType,
|
|
|
|
nullptr),
|
|
|
|
SuperTypeArgsRange.getBegin(),
|
|
|
|
SuperTypeArgs,
|
|
|
|
SuperTypeArgsRange.getEnd(),
|
|
|
|
SourceLocation(),
|
|
|
|
{ },
|
|
|
|
{ },
|
|
|
|
SourceLocation());
|
2015-07-07 11:57:35 +08:00
|
|
|
if (!fullSuperClassType.isUsable())
|
|
|
|
return;
|
|
|
|
|
|
|
|
SuperClassType = GetTypeFromParser(fullSuperClassType.get(),
|
|
|
|
&SuperClassTInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SuperClassTInfo) {
|
|
|
|
SuperClassTInfo = Context.getTrivialTypeSourceInfo(SuperClassType,
|
|
|
|
SuperLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
IDecl->setSuperClass(SuperClassTInfo);
|
|
|
|
IDecl->setEndOfDefinitionLoc(SuperClassTInfo->getTypeLoc().getLocEnd());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 11:58:54 +08:00
|
|
|
DeclResult Sema::actOnObjCTypeParam(Scope *S,
|
|
|
|
ObjCTypeParamVariance variance,
|
|
|
|
SourceLocation varianceLoc,
|
|
|
|
unsigned index,
|
Substitute type arguments into uses of Objective-C interface members.
When messaging a method that was defined in an Objective-C class (or
category or extension thereof) that has type parameters, substitute
the type arguments for those type parameters. Similarly, substitute
into property accesses, instance variables, and other references.
This includes general infrastructure for substituting the type
arguments associated with an ObjCObject(Pointer)Type into a type
referenced within a particular context, handling all of the
substitutions required to deal with (e.g.) inheritance involving
parameterized classes. In cases where no type arguments are available
(e.g., because we're messaging via some unspecialized type, id, etc.),
we substitute in the type bounds for the type parameters instead.
Example:
@interface NSSet<T : id<NSCopying>> : NSObject <NSCopying>
- (T)firstObject;
@end
void f(NSSet<NSString *> *stringSet, NSSet *anySet) {
[stringSet firstObject]; // produces NSString*
[anySet firstObject]; // produces id<NSCopying> (the bound)
}
When substituting for the type parameters given an unspecialized
context (i.e., no specific type arguments were given), substituting
the type bounds unconditionally produces type signatures that are too
strong compared to the pre-generics signatures. Instead, use the
following rule:
- In covariant positions, such as method return types, replace type
parameters with “id” or “Class” (the latter only when the type
parameter bound is “Class” or qualified class, e.g,
“Class<NSCopying>”)
- In other positions (e.g., parameter types), replace type
parameters with their type bounds.
- When a specialized Objective-C object or object pointer type
contains a type parameter in its type arguments (e.g.,
NSArray<T>*, but not NSArray<NSString *> *), replace the entire
object/object pointer type with its unspecialized version (e.g.,
NSArray *).
llvm-svn: 241543
2015-07-07 11:57:53 +08:00
|
|
|
IdentifierInfo *paramName,
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
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".
|
2015-11-15 10:31:46 +08:00
|
|
|
SourceLocation starLoc = getLocForEndOfToken(
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
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 {
|
2015-07-07 11:57:35 +08:00
|
|
|
// Not a valid type bound.
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
Diag(typeBoundInfo->getTypeLoc().getBeginLoc(),
|
|
|
|
diag::err_objc_type_param_bound_nonobject)
|
|
|
|
<< typeBound << paramName;
|
|
|
|
|
|
|
|
// Forget the bound; we'll default to id later.
|
|
|
|
typeBoundInfo = nullptr;
|
|
|
|
}
|
Substitute type arguments into uses of Objective-C interface members.
When messaging a method that was defined in an Objective-C class (or
category or extension thereof) that has type parameters, substitute
the type arguments for those type parameters. Similarly, substitute
into property accesses, instance variables, and other references.
This includes general infrastructure for substituting the type
arguments associated with an ObjCObject(Pointer)Type into a type
referenced within a particular context, handling all of the
substitutions required to deal with (e.g.) inheritance involving
parameterized classes. In cases where no type arguments are available
(e.g., because we're messaging via some unspecialized type, id, etc.),
we substitute in the type bounds for the type parameters instead.
Example:
@interface NSSet<T : id<NSCopying>> : NSObject <NSCopying>
- (T)firstObject;
@end
void f(NSSet<NSString *> *stringSet, NSSet *anySet) {
[stringSet firstObject]; // produces NSString*
[anySet firstObject]; // produces id<NSCopying> (the bound)
}
When substituting for the type parameters given an unspecialized
context (i.e., no specific type arguments were given), substituting
the type bounds unconditionally produces type signatures that are too
strong compared to the pre-generics signatures. Instead, use the
following rule:
- In covariant positions, such as method return types, replace type
parameters with “id” or “Class” (the latter only when the type
parameter bound is “Class” or qualified class, e.g,
“Class<NSCopying>”)
- In other positions (e.g., parameter types), replace type
parameters with their type bounds.
- When a specialized Objective-C object or object pointer type
contains a type parameter in its type arguments (e.g.,
NSArray<T>*, but not NSArray<NSString *> *), replace the entire
object/object pointer type with its unspecialized version (e.g.,
NSArray *).
llvm-svn: 241543
2015-07-07 11:57:53 +08:00
|
|
|
|
2015-09-24 06:14:21 +08:00
|
|
|
// Type bounds cannot have qualifiers (even indirectly) or explicit
|
|
|
|
// nullability.
|
Substitute type arguments into uses of Objective-C interface members.
When messaging a method that was defined in an Objective-C class (or
category or extension thereof) that has type parameters, substitute
the type arguments for those type parameters. Similarly, substitute
into property accesses, instance variables, and other references.
This includes general infrastructure for substituting the type
arguments associated with an ObjCObject(Pointer)Type into a type
referenced within a particular context, handling all of the
substitutions required to deal with (e.g.) inheritance involving
parameterized classes. In cases where no type arguments are available
(e.g., because we're messaging via some unspecialized type, id, etc.),
we substitute in the type bounds for the type parameters instead.
Example:
@interface NSSet<T : id<NSCopying>> : NSObject <NSCopying>
- (T)firstObject;
@end
void f(NSSet<NSString *> *stringSet, NSSet *anySet) {
[stringSet firstObject]; // produces NSString*
[anySet firstObject]; // produces id<NSCopying> (the bound)
}
When substituting for the type parameters given an unspecialized
context (i.e., no specific type arguments were given), substituting
the type bounds unconditionally produces type signatures that are too
strong compared to the pre-generics signatures. Instead, use the
following rule:
- In covariant positions, such as method return types, replace type
parameters with “id” or “Class” (the latter only when the type
parameter bound is “Class” or qualified class, e.g,
“Class<NSCopying>”)
- In other positions (e.g., parameter types), replace type
parameters with their type bounds.
- When a specialized Objective-C object or object pointer type
contains a type parameter in its type arguments (e.g.,
NSArray<T>*, but not NSArray<NSString *> *), replace the entire
object/object pointer type with its unspecialized version (e.g.,
NSArray *).
llvm-svn: 241543
2015-07-07 11:57:53 +08:00
|
|
|
if (typeBoundInfo) {
|
2015-09-24 06:14:21 +08:00
|
|
|
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.getLocStart(),
|
|
|
|
diag::err_objc_type_param_bound_explicit_nullability)
|
|
|
|
<< paramName << typeBound
|
|
|
|
<< FixItHint::CreateRemoval(rangeToRemove);
|
|
|
|
diagnosed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!diagnosed) {
|
|
|
|
Diag(qual ? qual.getLocStart()
|
|
|
|
: typeBoundInfo->getTypeLoc().getLocStart(),
|
|
|
|
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());
|
|
|
|
}
|
Substitute type arguments into uses of Objective-C interface members.
When messaging a method that was defined in an Objective-C class (or
category or extension thereof) that has type parameters, substitute
the type arguments for those type parameters. Similarly, substitute
into property accesses, instance variables, and other references.
This includes general infrastructure for substituting the type
arguments associated with an ObjCObject(Pointer)Type into a type
referenced within a particular context, handling all of the
substitutions required to deal with (e.g.) inheritance involving
parameterized classes. In cases where no type arguments are available
(e.g., because we're messaging via some unspecialized type, id, etc.),
we substitute in the type bounds for the type parameters instead.
Example:
@interface NSSet<T : id<NSCopying>> : NSObject <NSCopying>
- (T)firstObject;
@end
void f(NSSet<NSString *> *stringSet, NSSet *anySet) {
[stringSet firstObject]; // produces NSString*
[anySet firstObject]; // produces id<NSCopying> (the bound)
}
When substituting for the type parameters given an unspecialized
context (i.e., no specific type arguments were given), substituting
the type bounds unconditionally produces type signatures that are too
strong compared to the pre-generics signatures. Instead, use the
following rule:
- In covariant positions, such as method return types, replace type
parameters with “id” or “Class” (the latter only when the type
parameter bound is “Class” or qualified class, e.g,
“Class<NSCopying>”)
- In other positions (e.g., parameter types), replace type
parameters with their type bounds.
- When a specialized Objective-C object or object pointer type
contains a type parameter in its type arguments (e.g.,
NSArray<T>*, but not NSArray<NSString *> *), replace the entire
object/object pointer type with its unspecialized version (e.g.,
NSArray *).
llvm-svn: 241543
2015-07-07 11:57:53 +08:00
|
|
|
}
|
|
|
|
}
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2015-07-07 11:58:54 +08:00
|
|
|
return ObjCTypeParamDecl::Create(Context, CurContext, variance, varianceLoc,
|
|
|
|
index, paramLoc, paramName, colonLoc,
|
|
|
|
typeBoundInfo);
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
};
|
2015-10-07 07:40:43 +08:00
|
|
|
} // end anonymous namespace
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
|
|
|
|
/// Check consistency between two Objective-C type parameter lists, e.g.,
|
2015-07-08 10:35:56 +08:00
|
|
|
/// between a category/extension and an \@interface or between an \@class and an
|
|
|
|
/// \@interface.
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
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 {
|
2015-11-15 10:31:46 +08:00
|
|
|
diagLoc = S.getLocForEndOfToken(newTypeParams->back()->getLocEnd());
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
2015-07-07 11:58:54 +08:00
|
|
|
// 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->getLocStart();
|
|
|
|
|
|
|
|
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->getLocStart(),
|
|
|
|
(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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
// 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
|
2015-11-15 10:31:46 +08:00
|
|
|
= S.getLocForEndOfToken(newTypeParam->getLocation());
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::
|
2015-07-07 11:57:35 +08:00
|
|
|
ActOnStartClassInterface(Scope *S, SourceLocation AtInterfaceLoc,
|
2008-07-22 06:17:28 +08:00
|
|
|
IdentifierInfo *ClassName, SourceLocation ClassLoc,
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
ObjCTypeParamList *typeParamList,
|
2008-07-22 06:17:28 +08:00
|
|
|
IdentifierInfo *SuperName, SourceLocation SuperLoc,
|
2015-07-07 11:57:35 +08:00
|
|
|
ArrayRef<ParsedType> SuperTypeArgs,
|
|
|
|
SourceRange SuperTypeArgsRange,
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl * const *ProtoRefs, unsigned NumProtoRefs,
|
2010-01-16 23:02:53 +08:00
|
|
|
const SourceLocation *ProtoLocs,
|
2008-07-22 06:17:28 +08:00
|
|
|
SourceLocation EndProtoLoc, AttributeList *AttrList) {
|
2007-12-12 15:09:47 +08:00
|
|
|
assert(ClassName && "Missing class identifier");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// Check for another declaration kind with the same name.
|
2010-04-16 06:33:43 +08:00
|
|
|
NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
|
2010-04-16 07:40:53 +08:00
|
|
|
LookupOrdinaryName, ForRedeclaration);
|
2008-12-06 02:15:24 +08:00
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_definition);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-12-16 04:29:51 +08:00
|
|
|
// Create a declaration to describe this @interface.
|
2011-12-16 11:12:41 +08:00
|
|
|
ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
|
2013-06-19 05:26:33 +08:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
// 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,
|
2015-07-07 11:58:54 +08:00
|
|
|
typeParam->getVariance(),
|
|
|
|
SourceLocation(),
|
Substitute type arguments into uses of Objective-C interface members.
When messaging a method that was defined in an Objective-C class (or
category or extension thereof) that has type parameters, substitute
the type arguments for those type parameters. Similarly, substitute
into property accesses, instance variables, and other references.
This includes general infrastructure for substituting the type
arguments associated with an ObjCObject(Pointer)Type into a type
referenced within a particular context, handling all of the
substitutions required to deal with (e.g.) inheritance involving
parameterized classes. In cases where no type arguments are available
(e.g., because we're messaging via some unspecialized type, id, etc.),
we substitute in the type bounds for the type parameters instead.
Example:
@interface NSSet<T : id<NSCopying>> : NSObject <NSCopying>
- (T)firstObject;
@end
void f(NSSet<NSString *> *stringSet, NSSet *anySet) {
[stringSet firstObject]; // produces NSString*
[anySet firstObject]; // produces id<NSCopying> (the bound)
}
When substituting for the type parameters given an unspecialized
context (i.e., no specific type arguments were given), substituting
the type bounds unconditionally produces type signatures that are too
strong compared to the pre-generics signatures. Instead, use the
following rule:
- In covariant positions, such as method return types, replace type
parameters with “id” or “Class” (the latter only when the type
parameter bound is “Class” or qualified class, e.g,
“Class<NSCopying>”)
- In other positions (e.g., parameter types), replace type
parameters with their type bounds.
- When a specialized Objective-C object or object pointer type
contains a type parameter in its type arguments (e.g.,
NSArray<T>*, but not NSArray<NSString *> *), replace the entire
object/object pointer type with its unspecialized version (e.g.,
NSArray *).
llvm-svn: 241543
2015-07-07 11:57:53 +08:00
|
|
|
typeParam->getIndex(),
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
SourceLocation(),
|
|
|
|
typeParam->getIdentifier(),
|
|
|
|
SourceLocation(),
|
|
|
|
Context.getTrivialTypeSourceInfo(typeParam->getUnderlyingType())));
|
|
|
|
}
|
|
|
|
|
|
|
|
typeParamList = ObjCTypeParamList::create(Context,
|
|
|
|
SourceLocation(),
|
|
|
|
clonedTypeParams,
|
|
|
|
SourceLocation());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-16 04:29:51 +08:00
|
|
|
ObjCInterfaceDecl *IDecl
|
|
|
|
= ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
typeParamList, PrevIDecl, ClassLoc);
|
2011-12-16 04:29:51 +08:00
|
|
|
if (PrevIDecl) {
|
|
|
|
// Class already seen. Was it a definition?
|
|
|
|
if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
|
|
|
|
Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
|
|
|
|
<< PrevIDecl->getDeclName();
|
2011-12-15 13:27:12 +08:00
|
|
|
Diag(Def->getLocation(), diag::note_previous_definition);
|
2011-12-16 04:29:51 +08:00
|
|
|
IDecl->setInvalidDecl();
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2010-08-11 20:19:30 +08:00
|
|
|
}
|
2011-12-16 04:29:51 +08:00
|
|
|
|
|
|
|
if (AttrList)
|
|
|
|
ProcessDeclAttributeList(TUScope, IDecl, AttrList);
|
|
|
|
PushOnScopeChains(IDecl, TUScope);
|
2010-08-10 05:55:28 +08:00
|
|
|
|
2011-12-16 04:29:51 +08:00
|
|
|
// 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.
|
2011-12-15 13:27:12 +08:00
|
|
|
if (!IDecl->hasDefinition())
|
|
|
|
IDecl->startDefinition();
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
if (SuperName) {
|
2015-07-07 11:57:35 +08:00
|
|
|
// Diagnose availability in the context of the @interface.
|
|
|
|
ContextRAII SavedContext(*this, IDecl);
|
2010-01-04 02:01:57 +08:00
|
|
|
|
2015-07-07 11:57:35 +08:00
|
|
|
ActOnSuperClassOfClassInterface(S, AtInterfaceLoc, IDecl,
|
|
|
|
ClassName, ClassLoc,
|
|
|
|
SuperName, SuperLoc, SuperTypeArgs,
|
|
|
|
SuperTypeArgsRange);
|
2007-12-12 15:09:47 +08:00
|
|
|
} else { // we have a root class.
|
2011-12-16 06:34:59 +08:00
|
|
|
IDecl->setEndOfDefinitionLoc(ClassLoc);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-13 08:28:03 +08:00
|
|
|
// Check then save referenced protocols.
|
2008-07-26 12:13:19 +08:00
|
|
|
if (NumProtoRefs) {
|
2015-04-20 04:15:55 +08:00
|
|
|
diagnoseUseOfProtocols(*this, IDecl, (ObjCProtocolDecl*const*)ProtoRefs,
|
|
|
|
NumProtoRefs, ProtoLocs);
|
2012-09-06 23:59:27 +08:00
|
|
|
IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
|
2010-01-16 23:02:53 +08:00
|
|
|
ProtoLocs, Context);
|
2011-12-16 06:34:59 +08:00
|
|
|
IDecl->setEndOfDefinitionLoc(EndProtoLoc);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
CheckObjCDeclScope(IDecl);
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(IDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2013-09-26 03:36:32 +08:00
|
|
|
/// 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,
|
|
|
|
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>())
|
2015-02-18 00:48:30 +08:00
|
|
|
ProtocolRefs.append(OPT->qual_begin(), OPT->qual_end());
|
2013-09-26 03:36:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-09 07:32:13 +08:00
|
|
|
/// ActOnCompatibilityAlias - this action is called after complete parsing of
|
2012-06-15 05:40:34 +08:00
|
|
|
/// a \@compatibility_alias declaration. It sets up the alias relationships.
|
2012-08-09 07:32:13 +08:00
|
|
|
Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
|
|
|
|
IdentifierInfo *AliasName,
|
|
|
|
SourceLocation AliasLocation,
|
|
|
|
IdentifierInfo *ClassName,
|
|
|
|
SourceLocation ClassLocation) {
|
2007-12-12 15:09:47 +08:00
|
|
|
// Look for previous declaration of alias name
|
2010-04-16 06:33:43 +08:00
|
|
|
NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
|
2010-04-16 07:40:53 +08:00
|
|
|
LookupOrdinaryName, ForRedeclaration);
|
2007-12-12 15:09:47 +08:00
|
|
|
if (ADecl) {
|
2013-06-21 09:49:53 +08:00
|
|
|
Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
|
2008-11-24 07:20:13 +08:00
|
|
|
Diag(ADecl->getLocation(), diag::note_previous_declaration);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
// Check for class declaration
|
2010-04-16 06:33:43 +08:00
|
|
|
NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
|
2010-04-16 07:40:53 +08:00
|
|
|
LookupOrdinaryName, ForRedeclaration);
|
2011-04-15 22:24:37 +08:00
|
|
|
if (const TypedefNameDecl *TDecl =
|
|
|
|
dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
|
2009-01-08 09:10:55 +08:00
|
|
|
QualType T = TDecl->getUnderlyingType();
|
2010-05-15 19:32:37 +08:00
|
|
|
if (T->isObjCObjectType()) {
|
|
|
|
if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
|
2009-01-08 09:10:55 +08:00
|
|
|
ClassName = IDecl->getIdentifier();
|
2010-04-16 06:33:43 +08:00
|
|
|
CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
|
2010-04-16 07:40:53 +08:00
|
|
|
LookupOrdinaryName, ForRedeclaration);
|
2009-01-08 09:10:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-03-17 05:17:37 +08:00
|
|
|
ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
|
2014-05-26 14:22:03 +08:00
|
|
|
if (!CDecl) {
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
|
2008-03-17 05:17:37 +08:00
|
|
|
if (CDeclU)
|
2008-11-24 07:20:13 +08:00
|
|
|
Diag(CDeclU->getLocation(), diag::note_previous_declaration);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-03-17 05:17:37 +08:00
|
|
|
// Everything checked out, instantiate a new alias declaration AST.
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCCompatibleAliasDecl *AliasDecl =
|
2009-01-09 08:49:46 +08:00
|
|
|
ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
if (!CheckObjCDeclScope(AliasDecl))
|
2009-04-24 10:57:34 +08:00
|
|
|
PushOnScopeChains(AliasDecl, TUScope);
|
2009-01-09 08:49:46 +08:00
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
return AliasDecl;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2011-05-14 02:02:08 +08:00
|
|
|
bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
|
2009-03-05 23:22:01 +08:00
|
|
|
IdentifierInfo *PName,
|
|
|
|
SourceLocation &Ploc, SourceLocation PrevLoc,
|
2011-05-14 02:02:08 +08:00
|
|
|
const ObjCList<ObjCProtocolDecl> &PList) {
|
|
|
|
|
|
|
|
bool res = false;
|
2009-03-05 23:22:01 +08:00
|
|
|
for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
|
|
|
|
E = PList.end(); I != E; ++I) {
|
2010-04-16 06:33:43 +08:00
|
|
|
if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
|
|
|
|
Ploc)) {
|
2009-03-05 23:22:01 +08:00
|
|
|
if (PDecl->getIdentifier() == PName) {
|
|
|
|
Diag(Ploc, diag::err_protocol_has_circular_dependency);
|
|
|
|
Diag(PrevLoc, diag::note_previous_definition);
|
2011-05-14 02:02:08 +08:00
|
|
|
res = true;
|
2009-03-05 23:22:01 +08:00
|
|
|
}
|
2012-01-02 03:29:29 +08:00
|
|
|
|
|
|
|
if (!PDecl->hasDefinition())
|
|
|
|
continue;
|
|
|
|
|
2011-05-14 02:02:08 +08:00
|
|
|
if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
|
|
|
|
PDecl->getLocation(), PDecl->getReferencedProtocols()))
|
|
|
|
res = true;
|
2009-03-05 23:22:01 +08:00
|
|
|
}
|
|
|
|
}
|
2011-05-14 02:02:08 +08:00
|
|
|
return res;
|
2009-03-05 23:22:01 +08:00
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *
|
2008-07-26 12:03:38 +08:00
|
|
|
Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
|
|
|
|
IdentifierInfo *ProtocolName,
|
|
|
|
SourceLocation ProtocolLoc,
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl * const *ProtoRefs,
|
2008-07-26 12:03:38 +08:00
|
|
|
unsigned NumProtoRefs,
|
2010-01-16 23:02:53 +08:00
|
|
|
const SourceLocation *ProtoLocs,
|
2008-09-26 12:48:09 +08:00
|
|
|
SourceLocation EndProtoLoc,
|
|
|
|
AttributeList *AttrList) {
|
2011-05-13 06:04:39 +08:00
|
|
|
bool err = false;
|
2008-09-26 12:48:09 +08:00
|
|
|
// FIXME: Deal with AttrList.
|
2007-12-12 15:09:47 +08:00
|
|
|
assert(ProtocolName && "Missing protocol identifier");
|
2012-01-02 04:30:41 +08:00
|
|
|
ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
|
|
|
|
ForRedeclaration);
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCProtocolDecl *PDecl = nullptr;
|
|
|
|
if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) {
|
2012-01-02 04:30:41 +08:00
|
|
|
// 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,
|
2014-05-26 14:22:03 +08:00
|
|
|
/*PrevDecl=*/nullptr);
|
2012-01-02 04:30:41 +08:00
|
|
|
PDecl->startDefinition();
|
|
|
|
} else {
|
|
|
|
if (PrevDecl) {
|
|
|
|
// Check for circular dependencies among protocol declarations. This can
|
|
|
|
// only happen if this protocol was forward-declared.
|
2011-11-14 06:08:30 +08:00
|
|
|
ObjCList<ObjCProtocolDecl> PList;
|
|
|
|
PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
|
|
|
|
err = CheckForwardProtocolDeclarationForCircularDependency(
|
2012-01-02 04:30:41 +08:00
|
|
|
ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2012-01-02 04:30:41 +08:00
|
|
|
|
|
|
|
// Create the new declaration.
|
2011-10-04 12:48:02 +08:00
|
|
|
PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
|
2011-10-18 03:48:06 +08:00
|
|
|
ProtocolLoc, AtProtoInterfaceLoc,
|
2012-01-02 06:06:18 +08:00
|
|
|
/*PrevDecl=*/PrevDecl);
|
2012-01-02 04:30:41 +08:00
|
|
|
|
2009-04-24 07:18:26 +08:00
|
|
|
PushOnScopeChains(PDecl, TUScope);
|
2012-01-02 03:29:29 +08:00
|
|
|
PDecl->startDefinition();
|
2008-03-16 09:23:04 +08:00
|
|
|
}
|
2012-01-02 03:29:29 +08:00
|
|
|
|
2008-12-17 09:07:27 +08:00
|
|
|
if (AttrList)
|
2009-06-18 05:51:59 +08:00
|
|
|
ProcessDeclAttributeList(TUScope, PDecl, AttrList);
|
2012-01-02 04:30:41 +08:00
|
|
|
|
|
|
|
// Merge attributes from previous declarations.
|
|
|
|
if (PrevDecl)
|
|
|
|
mergeDeclAttributes(PDecl, PrevDecl);
|
|
|
|
|
2011-05-13 06:04:39 +08:00
|
|
|
if (!err && NumProtoRefs ) {
|
2008-03-17 04:19:15 +08:00
|
|
|
/// Check then save referenced protocols.
|
2015-04-20 04:15:55 +08:00
|
|
|
diagnoseUseOfProtocols(*this, PDecl, (ObjCProtocolDecl*const*)ProtoRefs,
|
|
|
|
NumProtoRefs, ProtoLocs);
|
2012-09-06 23:59:27 +08:00
|
|
|
PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
|
2010-01-16 23:02:53 +08:00
|
|
|
ProtoLocs, Context);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
CheckObjCDeclScope(PDecl);
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(PDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2014-03-12 01:10:51 +08:00
|
|
|
static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl,
|
|
|
|
ObjCProtocolDecl *&UndefinedProtocol) {
|
|
|
|
if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) {
|
|
|
|
UndefinedProtocol = PDecl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-03-14 06:58:06 +08:00
|
|
|
for (auto *PI : PDecl->protocols())
|
|
|
|
if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) {
|
|
|
|
UndefinedProtocol = PI;
|
2014-03-12 01:10:51 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
/// FindProtocolDeclaration - This routine looks up protocols and
|
2008-09-05 04:01:15 +08:00
|
|
|
/// issues an error if they are not declared. It returns list of
|
|
|
|
/// protocol declarations in its 'Protocols' argument.
|
2007-12-12 15:09:47 +08:00
|
|
|
void
|
2015-04-20 04:15:55 +08:00
|
|
|
Sema::FindProtocolDeclaration(bool WarnOnDeclarations, bool ForObjCContainer,
|
2015-10-22 12:59:56 +08:00
|
|
|
ArrayRef<IdentifierLocPair> ProtocolId,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<Decl *> &Protocols) {
|
2015-10-22 12:59:56 +08:00
|
|
|
for (const IdentifierLocPair &Pair : ProtocolId) {
|
|
|
|
ObjCProtocolDecl *PDecl = LookupProtocol(Pair.first, Pair.second);
|
2010-01-04 02:01:57 +08:00
|
|
|
if (!PDecl) {
|
2011-06-29 00:20:02 +08:00
|
|
|
TypoCorrection Corrected = CorrectTypo(
|
2015-10-22 12:59:56 +08:00
|
|
|
DeclarationNameInfo(Pair.first, Pair.second),
|
2014-10-28 02:07:29 +08:00
|
|
|
LookupObjCProtocolName, TUScope, nullptr,
|
|
|
|
llvm::make_unique<DeclFilterCCC<ObjCProtocolDecl>>(),
|
2014-05-26 14:22:03 +08:00
|
|
|
CTK_ErrorRecovery);
|
2013-08-17 08:46:16 +08:00
|
|
|
if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>()))
|
|
|
|
diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest)
|
2015-10-22 12:59:56 +08:00
|
|
|
<< Pair.first);
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
|
|
|
|
2008-07-26 11:47:43 +08:00
|
|
|
if (!PDecl) {
|
2015-10-22 12:59:56 +08:00
|
|
|
Diag(Pair.second, diag::err_undeclared_protocol) << Pair.first;
|
2008-07-26 11:47:43 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-04-10 01:52:29 +08:00
|
|
|
// If this is a forward protocol declaration, get its definition.
|
|
|
|
if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
|
|
|
|
PDecl = PDecl->getDefinition();
|
2015-04-20 04:15:55 +08:00
|
|
|
|
|
|
|
// 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) {
|
2015-10-22 12:59:56 +08:00
|
|
|
(void)DiagnoseUseOfDecl(PDecl, Pair.second);
|
2015-04-20 04:15:55 +08:00
|
|
|
}
|
2008-07-26 11:47:43 +08:00
|
|
|
|
|
|
|
// If this is a forward declaration and we are supposed to warn in this
|
|
|
|
// case, do it.
|
2013-01-17 08:38:46 +08:00
|
|
|
// FIXME: Recover nicely in the hidden case.
|
2014-03-12 01:10:51 +08:00
|
|
|
ObjCProtocolDecl *UndefinedProtocol;
|
|
|
|
|
2013-01-17 08:38:46 +08:00
|
|
|
if (WarnOnDeclarations &&
|
2014-03-12 01:10:51 +08:00
|
|
|
NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) {
|
2015-10-22 12:59:56 +08:00
|
|
|
Diag(Pair.second, diag::warn_undef_protocolref) << Pair.first;
|
2014-03-12 01:10:51 +08:00
|
|
|
Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined)
|
|
|
|
<< UndefinedProtocol;
|
|
|
|
}
|
2010-08-21 17:40:31 +08:00
|
|
|
Protocols.push_back(PDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-14 04:42:13 +08:00
|
|
|
namespace {
|
2015-07-07 11:57:35 +08:00
|
|
|
// Callback to only accept typo corrections that are either
|
|
|
|
// Objective-C protocols or valid Objective-C type arguments.
|
|
|
|
class ObjCTypeArgOrProtocolValidatorCCC : 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;
|
|
|
|
}
|
|
|
|
};
|
2015-07-14 04:42:13 +08:00
|
|
|
} // end anonymous namespace
|
2015-07-07 11:57:35 +08:00
|
|
|
|
2016-04-14 04:59:07 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-07-07 11:57:35 +08:00
|
|
|
void Sema::actOnObjCTypeArgsOrProtocolQualifiers(
|
|
|
|
Scope *S,
|
Warn when an intended Objective-C specialization was actually a useless protocol qualification.
Warn in cases where one has provided redundant protocol qualification
that might be a typo for a specialization, e.g., NSArray<NSObject>,
which is pointless (NSArray declares that it conforms to NSObject) and
is likely to be a typo for NSArray<NSObject *>, i.e., an array of
NSObject pointers. This warning is very narrow, only applying when the
base type being qualified is parameterized, has the same number of
parameters as their are protocols listed, all of the names can also
refer to types (including Objective-C class types, of course), and at
least one of those types is an Objective-C class (making this a typo
for a missing '*'). The limitations are partly for performance reasons
(we don't want to do redundant name lookup unless we really need to),
and because we want the warning to apply in very limited cases to
limit false positives.
Part of rdar://problem/6294649.
llvm-svn: 241547
2015-07-07 11:58:28 +08:00
|
|
|
ParsedType baseType,
|
2015-07-07 11:57:35 +08:00
|
|
|
SourceLocation lAngleLoc,
|
|
|
|
ArrayRef<IdentifierInfo *> identifiers,
|
|
|
|
ArrayRef<SourceLocation> identifierLocs,
|
|
|
|
SourceLocation rAngleLoc,
|
2015-07-07 11:58:14 +08:00
|
|
|
SourceLocation &typeArgsLAngleLoc,
|
|
|
|
SmallVectorImpl<ParsedType> &typeArgs,
|
|
|
|
SourceLocation &typeArgsRAngleLoc,
|
|
|
|
SourceLocation &protocolLAngleLoc,
|
|
|
|
SmallVectorImpl<Decl *> &protocols,
|
|
|
|
SourceLocation &protocolRAngleLoc,
|
2015-07-07 11:57:35 +08:00
|
|
|
bool warnOnIncompleteProtocols) {
|
|
|
|
// Local function that updates the declaration specifiers with
|
|
|
|
// protocol information.
|
|
|
|
unsigned numProtocolsResolved = 0;
|
|
|
|
auto resolvedAsProtocols = [&] {
|
|
|
|
assert(numProtocolsResolved == identifiers.size() && "Unresolved protocols");
|
|
|
|
|
Warn when an intended Objective-C specialization was actually a useless protocol qualification.
Warn in cases where one has provided redundant protocol qualification
that might be a typo for a specialization, e.g., NSArray<NSObject>,
which is pointless (NSArray declares that it conforms to NSObject) and
is likely to be a typo for NSArray<NSObject *>, i.e., an array of
NSObject pointers. This warning is very narrow, only applying when the
base type being qualified is parameterized, has the same number of
parameters as their are protocols listed, all of the names can also
refer to types (including Objective-C class types, of course), and at
least one of those types is an Objective-C class (making this a typo
for a missing '*'). The limitations are partly for performance reasons
(we don't want to do redundant name lookup unless we really need to),
and because we want the warning to apply in very limited cases to
limit false positives.
Part of rdar://problem/6294649.
llvm-svn: 241547
2015-07-07 11:58:28 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 11:57:35 +08:00
|
|
|
for (unsigned i = 0, n = protocols.size(); i != n; ++i) {
|
2015-07-07 11:58:14 +08:00
|
|
|
ObjCProtocolDecl *&proto
|
|
|
|
= reinterpret_cast<ObjCProtocolDecl *&>(protocols[i]);
|
2015-07-07 11:57:35 +08:00
|
|
|
// 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;
|
|
|
|
}
|
Warn when an intended Objective-C specialization was actually a useless protocol qualification.
Warn in cases where one has provided redundant protocol qualification
that might be a typo for a specialization, e.g., NSArray<NSObject>,
which is pointless (NSArray declares that it conforms to NSObject) and
is likely to be a typo for NSArray<NSObject *>, i.e., an array of
NSObject pointers. This warning is very narrow, only applying when the
base type being qualified is parameterized, has the same number of
parameters as their are protocols listed, all of the names can also
refer to types (including Objective-C class types, of course), and at
least one of those types is an Objective-C class (making this a typo
for a missing '*'). The limitations are partly for performance reasons
(we don't want to do redundant name lookup unless we really need to),
and because we want the warning to apply in very limited cases to
limit false positives.
Part of rdar://problem/6294649.
llvm-svn: 241547
2015-07-07 11:58:28 +08:00
|
|
|
|
|
|
|
// 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)
|
2015-11-15 10:31:46 +08:00
|
|
|
<< FixItHint::CreateInsertion(getLocForEndOfToken(firstClassNameLoc),
|
|
|
|
" *");
|
Warn when an intended Objective-C specialization was actually a useless protocol qualification.
Warn in cases where one has provided redundant protocol qualification
that might be a typo for a specialization, e.g., NSArray<NSObject>,
which is pointless (NSArray declares that it conforms to NSObject) and
is likely to be a typo for NSArray<NSObject *>, i.e., an array of
NSObject pointers. This warning is very narrow, only applying when the
base type being qualified is parameterized, has the same number of
parameters as their are protocols listed, all of the names can also
refer to types (including Objective-C class types, of course), and at
least one of those types is an Objective-C class (making this a typo
for a missing '*'). The limitations are partly for performance reasons
(we don't want to do redundant name lookup unless we really need to),
and because we want the warning to apply in very limited cases to
limit false positives.
Part of rdar://problem/6294649.
llvm-svn: 241547
2015-07-07 11:58:28 +08:00
|
|
|
}
|
2015-07-07 11:57:35 +08:00
|
|
|
}
|
|
|
|
|
2015-07-07 11:58:14 +08:00
|
|
|
protocolLAngleLoc = lAngleLoc;
|
|
|
|
protocolRAngleLoc = rAngleLoc;
|
|
|
|
assert(protocols.size() == identifierLocs.size());
|
2015-07-07 11:57:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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, Declarator::TypeNameContext);
|
|
|
|
|
|
|
|
// If we have a typedef of an Objective-C class type that is missing a '*',
|
|
|
|
// add the '*'.
|
|
|
|
if (type->getAs<ObjCInterfaceType>()) {
|
2015-11-15 10:31:46 +08:00
|
|
|
SourceLocation starLoc = getLocForEndOfToken(loc);
|
2015-07-07 11:57:35 +08:00
|
|
|
ParsedAttributes parsedAttrs(attrFactory);
|
|
|
|
D.AddTypeInfo(DeclaratorChunk::getPointer(/*typeQuals=*/0, starLoc,
|
|
|
|
SourceLocation(),
|
|
|
|
SourceLocation(),
|
|
|
|
SourceLocation(),
|
2016-05-12 02:38:21 +08:00
|
|
|
SourceLocation(),
|
2015-07-07 11:57:35 +08:00
|
|
|
SourceLocation()),
|
2015-10-07 07:40:43 +08:00
|
|
|
parsedAttrs,
|
|
|
|
starLoc);
|
2015-07-07 11:57:35 +08:00
|
|
|
|
|
|
|
// 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 = [&] {
|
2015-07-07 11:58:14 +08:00
|
|
|
// We did not resolve these as protocols.
|
|
|
|
protocols.clear();
|
|
|
|
|
2015-07-07 11:57:35 +08:00
|
|
|
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]);
|
2015-07-07 11:58:14 +08:00
|
|
|
if (!type.isUsable()) {
|
|
|
|
typeArgs.clear();
|
2015-07-07 11:57:35 +08:00
|
|
|
return;
|
2015-07-07 11:58:14 +08:00
|
|
|
}
|
2015-07-07 11:57:35 +08:00
|
|
|
|
|
|
|
typeArgs.push_back(type.get());
|
|
|
|
}
|
|
|
|
|
2015-07-07 11:58:14 +08:00
|
|
|
typeArgsLAngleLoc = lAngleLoc;
|
|
|
|
typeArgsRAngleLoc = rAngleLoc;
|
2015-07-07 11:57:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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.
|
2016-04-14 04:59:07 +08:00
|
|
|
DiagnoseTypeArgsAndProtocols(identifiers[0], identifierLocs[0],
|
|
|
|
identifiers[i], identifierLocs[i],
|
|
|
|
protocols[i] != nullptr);
|
2015-07-07 11:57:35 +08:00
|
|
|
|
2015-07-07 11:58:14 +08:00
|
|
|
protocols.clear();
|
|
|
|
typeArgs.clear();
|
2015-07-07 11:57:35 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform typo correction on the name.
|
|
|
|
TypoCorrection corrected = CorrectTypo(
|
|
|
|
DeclarationNameInfo(identifiers[i], identifierLocs[i]), lookupKind, S,
|
|
|
|
nullptr,
|
|
|
|
llvm::make_unique<ObjCTypeArgOrProtocolValidatorCCC>(Context,
|
|
|
|
lookupKind),
|
|
|
|
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];
|
2015-07-07 11:58:14 +08:00
|
|
|
protocols.clear();
|
|
|
|
typeArgs.clear();
|
2015-07-07 11:57:35 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2009-03-03 03:06:08 +08:00
|
|
|
/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
|
2009-03-03 03:05:07 +08:00
|
|
|
/// a class method in its extension.
|
|
|
|
///
|
2009-09-09 23:08:12 +08:00
|
|
|
void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
|
2009-03-03 03:05:07 +08:00
|
|
|
ObjCInterfaceDecl *ID) {
|
|
|
|
if (!ID)
|
|
|
|
return; // Possibly due to previous error
|
|
|
|
|
|
|
|
llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
|
2014-03-14 03:03:34 +08:00
|
|
|
for (auto *MD : ID->methods())
|
2009-03-03 03:05:07 +08:00
|
|
|
MethodMap[MD->getSelector()] = MD;
|
|
|
|
|
|
|
|
if (MethodMap.empty())
|
|
|
|
return;
|
2014-03-14 03:03:34 +08:00
|
|
|
for (const auto *Method : CAT->methods()) {
|
2009-03-03 03:05:07 +08:00
|
|
|
const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
|
2014-03-18 01:46:10 +08:00
|
|
|
if (PrevMethod &&
|
|
|
|
(PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) &&
|
|
|
|
!MatchTwoMethodDeclarations(Method, PrevMethod)) {
|
2009-03-03 03:05:07 +08:00
|
|
|
Diag(Method->getLocation(), diag::err_duplicate_method_decl)
|
|
|
|
<< Method->getDeclName();
|
|
|
|
Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-15 05:40:34 +08:00
|
|
|
/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
|
2012-01-02 05:23:57 +08:00
|
|
|
Sema::DeclGroupPtrTy
|
2007-12-12 15:09:47 +08:00
|
|
|
Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
|
2015-10-22 13:00:01 +08:00
|
|
|
ArrayRef<IdentifierLocPair> IdentList,
|
2008-12-17 09:07:27 +08:00
|
|
|
AttributeList *attrList) {
|
2012-01-02 05:23:57 +08:00
|
|
|
SmallVector<Decl *, 8> DeclsInGroup;
|
2015-10-22 13:00:01 +08:00
|
|
|
for (const IdentifierLocPair &IdentPair : IdentList) {
|
|
|
|
IdentifierInfo *Ident = IdentPair.first;
|
|
|
|
ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentPair.second,
|
2012-01-02 04:30:41 +08:00
|
|
|
ForRedeclaration);
|
|
|
|
ObjCProtocolDecl *PDecl
|
|
|
|
= ObjCProtocolDecl::Create(Context, CurContext, Ident,
|
2015-10-22 13:00:01 +08:00
|
|
|
IdentPair.second, AtProtocolLoc,
|
2012-01-02 06:06:18 +08:00
|
|
|
PrevDecl);
|
2012-01-02 04:30:41 +08:00
|
|
|
|
|
|
|
PushOnScopeChains(PDecl, TUScope);
|
2012-01-02 05:23:57 +08:00
|
|
|
CheckObjCDeclScope(PDecl);
|
2012-01-02 04:30:41 +08:00
|
|
|
|
2012-01-02 04:33:24 +08:00
|
|
|
if (attrList)
|
2009-06-18 05:51:59 +08:00
|
|
|
ProcessDeclAttributeList(TUScope, PDecl, attrList);
|
2012-01-02 04:30:41 +08:00
|
|
|
|
|
|
|
if (PrevDecl)
|
|
|
|
mergeDeclAttributes(PDecl, PrevDecl);
|
|
|
|
|
2012-01-02 05:23:57 +08:00
|
|
|
DeclsInGroup.push_back(PDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-07-09 20:05:01 +08:00
|
|
|
return BuildDeclaratorGroup(DeclsInGroup, false);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::
|
2008-07-22 06:17:28 +08:00
|
|
|
ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
|
|
|
|
IdentifierInfo *ClassName, SourceLocation ClassLoc,
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
ObjCTypeParamList *typeParamList,
|
2008-07-22 06:17:28 +08:00
|
|
|
IdentifierInfo *CategoryName,
|
|
|
|
SourceLocation CategoryLoc,
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl * const *ProtoRefs,
|
2008-07-22 06:17:28 +08:00
|
|
|
unsigned NumProtoRefs,
|
2010-01-16 23:02:53 +08:00
|
|
|
const SourceLocation *ProtoLocs,
|
2008-07-22 06:17:28 +08:00
|
|
|
SourceLocation EndProtoLoc) {
|
2010-06-23 07:20:40 +08:00
|
|
|
ObjCCategoryDecl *CDecl;
|
2010-04-16 06:33:43 +08:00
|
|
|
ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
|
2010-02-24 03:39:46 +08:00
|
|
|
|
|
|
|
/// Check that class of this category is already completely declared.
|
2011-11-15 06:10:01 +08:00
|
|
|
|
|
|
|
if (!IDecl
|
|
|
|
|| RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
|
2012-05-05 00:32:21 +08:00
|
|
|
diag::err_category_forward_interface,
|
2014-05-26 14:22:03 +08:00
|
|
|
CategoryName == nullptr)) {
|
2010-02-24 03:39:46 +08:00
|
|
|
// 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,
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
ClassLoc, CategoryLoc, CategoryName,
|
|
|
|
IDecl, typeParamList);
|
2010-02-24 03:39:46 +08:00
|
|
|
CDecl->setInvalidDecl();
|
2012-03-13 02:34:26 +08:00
|
|
|
CurContext->addDecl(CDecl);
|
2011-11-15 06:10:01 +08:00
|
|
|
|
|
|
|
if (!IDecl)
|
|
|
|
Diag(ClassLoc, diag::err_undef_interface) << ClassName;
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(CDecl);
|
2010-02-24 03:39:46 +08:00
|
|
|
}
|
|
|
|
|
2010-06-23 07:20:40 +08:00
|
|
|
if (!CategoryName && IDecl->getImplementation()) {
|
|
|
|
Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
|
|
|
|
Diag(IDecl->getImplementation()->getLocation(),
|
|
|
|
diag::note_implementation_declared);
|
2010-02-24 03:39:46 +08:00
|
|
|
}
|
|
|
|
|
2010-02-16 05:55:26 +08:00
|
|
|
if (CategoryName) {
|
|
|
|
/// Check for duplicate interface declaration for this category
|
2013-01-17 07:00:23 +08:00
|
|
|
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);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2008-01-18 04:33:24 +08:00
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-31 03:43:26 +08:00
|
|
|
CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
ClassLoc, CategoryLoc, CategoryName, IDecl,
|
|
|
|
typeParamList);
|
2011-08-31 03:43:26 +08:00
|
|
|
// FIXME: PushOnScopeChains?
|
|
|
|
CurContext->addDecl(CDecl);
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
if (NumProtoRefs) {
|
2015-04-20 04:15:55 +08:00
|
|
|
diagnoseUseOfProtocols(*this, CDecl, (ObjCProtocolDecl*const*)ProtoRefs,
|
|
|
|
NumProtoRefs, ProtoLocs);
|
|
|
|
CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
|
2010-01-16 23:02:53 +08:00
|
|
|
ProtoLocs, Context);
|
2009-10-06 04:41:32 +08:00
|
|
|
// Protocols in the class extension belong to the class.
|
2010-02-16 05:55:26 +08:00
|
|
|
if (CDecl->IsClassExtension())
|
2012-09-06 23:59:27 +08:00
|
|
|
IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
|
2010-09-01 09:21:15 +08:00
|
|
|
NumProtoRefs, Context);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
CheckObjCDeclScope(CDecl);
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(CDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ActOnStartCategoryImplementation - Perform semantic checks on the
|
2008-01-08 03:49:32 +08:00
|
|
|
/// category implementation declaration and build an ObjCCategoryImplDecl
|
2007-12-12 15:09:47 +08:00
|
|
|
/// object.
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::ActOnStartCategoryImplementation(
|
2007-12-12 15:09:47 +08:00
|
|
|
SourceLocation AtCatImplLoc,
|
|
|
|
IdentifierInfo *ClassName, SourceLocation ClassLoc,
|
|
|
|
IdentifierInfo *CatName, SourceLocation CatLoc) {
|
2010-04-16 06:33:43 +08:00
|
|
|
ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCCategoryDecl *CatIDecl = nullptr;
|
2012-03-03 03:14:29 +08:00
|
|
|
if (IDecl && IDecl->hasDefinition()) {
|
2009-07-21 08:05:53 +08:00
|
|
|
CatIDecl = IDecl->FindCategoryDeclaration(CatName);
|
|
|
|
if (!CatIDecl) {
|
|
|
|
// Category @implementation with no corresponding @interface.
|
|
|
|
// Create and install one.
|
2011-11-24 04:27:26 +08:00
|
|
|
CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
|
|
|
|
ClassLoc, CatLoc,
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
CatName, IDecl,
|
|
|
|
/*typeParamList=*/nullptr);
|
2011-11-24 04:27:26 +08:00
|
|
|
CatIDecl->setImplicit();
|
2009-07-21 08:05:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCCategoryImplDecl *CDecl =
|
2011-10-04 12:48:02 +08:00
|
|
|
ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
|
2011-12-09 08:31:40 +08:00
|
|
|
ClassLoc, AtCatImplLoc, CatLoc);
|
2007-12-12 15:09:47 +08:00
|
|
|
/// Check that class of this category is already completely declared.
|
2011-11-15 06:10:01 +08:00
|
|
|
if (!IDecl) {
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(ClassLoc, diag::err_undef_interface) << ClassName;
|
2011-07-22 10:45:48 +08:00
|
|
|
CDecl->setInvalidDecl();
|
2011-11-15 06:10:01 +08:00
|
|
|
} else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
|
|
|
|
diag::err_undef_interface)) {
|
|
|
|
CDecl->setInvalidDecl();
|
2011-07-22 10:45:48 +08:00
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
|
2009-01-09 08:49:46 +08:00
|
|
|
// FIXME: PushOnScopeChains?
|
2009-06-30 10:36:12 +08:00
|
|
|
CurContext->addDecl(CDecl);
|
2009-01-09 08:49:46 +08:00
|
|
|
|
2011-10-07 07:23:27 +08:00
|
|
|
// If the interface is deprecated/unavailable, warn/error about it.
|
|
|
|
if (IDecl)
|
|
|
|
DiagnoseUseOfDecl(IDecl, ClassLoc);
|
|
|
|
|
2016-04-02 07:23:52 +08:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2009-07-21 08:05:53 +08:00
|
|
|
/// 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);
|
2013-05-31 02:53:21 +08:00
|
|
|
CDecl->setInvalidDecl();
|
2011-02-15 08:59:30 +08:00
|
|
|
} else {
|
2009-07-21 08:05:53 +08:00
|
|
|
CatIDecl->setImplementation(CDecl);
|
2011-02-15 08:59:30 +08:00
|
|
|
// Warn on implementating category of deprecated class under
|
|
|
|
// -Wdeprecated-implementations flag.
|
2011-02-16 01:49:58 +08:00
|
|
|
DiagnoseObjCImplementedDeprecations(*this,
|
|
|
|
dyn_cast<NamedDecl>(IDecl),
|
|
|
|
CDecl->getLocation(), 2);
|
2011-02-15 08:59:30 +08:00
|
|
|
}
|
2009-07-21 08:05:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
CheckObjCDeclScope(CDecl);
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(CDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::ActOnStartClassImplementation(
|
2007-12-12 15:09:47 +08:00
|
|
|
SourceLocation AtClassImplLoc,
|
|
|
|
IdentifierInfo *ClassName, SourceLocation ClassLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
IdentifierInfo *SuperClassname,
|
2007-12-12 15:09:47 +08:00
|
|
|
SourceLocation SuperClassLoc) {
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCInterfaceDecl *IDecl = nullptr;
|
2007-12-12 15:09:47 +08:00
|
|
|
// Check for another declaration kind with the same name.
|
2009-10-10 05:13:30 +08:00
|
|
|
NamedDecl *PrevDecl
|
2010-04-16 07:40:53 +08:00
|
|
|
= LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
|
|
|
|
ForRedeclaration);
|
2008-01-08 03:49:32 +08:00
|
|
|
if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_definition);
|
2010-08-11 20:19:30 +08:00
|
|
|
} else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
|
2015-12-19 06:40:25 +08:00
|
|
|
// FIXME: This will produce an error if the definition of the interface has
|
|
|
|
// been imported from a module but is not visible.
|
2011-12-16 11:12:41 +08:00
|
|
|
RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
|
|
|
|
diag::warn_undef_interface);
|
2010-01-05 01:27:12 +08:00
|
|
|
} else {
|
2013-08-17 08:46:16 +08:00
|
|
|
// We did not find anything with the name ClassName; try to correct for
|
2010-01-05 01:27:12 +08:00
|
|
|
// typos in the class name.
|
2014-10-28 02:07:29 +08:00
|
|
|
TypoCorrection Corrected = CorrectTypo(
|
|
|
|
DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
|
|
|
|
nullptr, llvm::make_unique<ObjCInterfaceValidatorCCC>(), CTK_NonError);
|
2013-08-17 08:46:16 +08:00
|
|
|
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);
|
2010-01-05 01:27:12 +08:00
|
|
|
} else {
|
|
|
|
Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// Check that super class name is valid class name
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCInterfaceDecl *SDecl = nullptr;
|
2007-12-12 15:09:47 +08:00
|
|
|
if (SuperClassname) {
|
|
|
|
// Check if a different kind of symbol declared in this scope.
|
2010-04-16 06:33:43 +08:00
|
|
|
PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
|
|
|
|
LookupOrdinaryName);
|
2008-01-08 03:49:32 +08:00
|
|
|
if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(SuperClassLoc, diag::err_redefinition_different_kind)
|
|
|
|
<< SuperClassname;
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_definition);
|
2008-11-19 16:23:25 +08:00
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
|
2012-03-13 09:09:36 +08:00
|
|
|
if (SDecl && !SDecl->hasDefinition())
|
2014-05-26 14:22:03 +08:00
|
|
|
SDecl = nullptr;
|
2007-12-12 15:09:47 +08:00
|
|
|
if (!SDecl)
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(SuperClassLoc, diag::err_undef_superclass)
|
|
|
|
<< SuperClassname << ClassName;
|
2011-12-15 08:29:59 +08:00
|
|
|
else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
|
2007-12-12 15:09:47 +08:00
|
|
|
// This implementation and its interface do not have the same
|
|
|
|
// super class.
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(SuperClassLoc, diag::err_conflicting_super_class)
|
2008-11-24 05:45:46 +08:00
|
|
|
<< SDecl->getDeclName();
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(SDecl->getLocation(), diag::note_previous_definition);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
if (!IDecl) {
|
|
|
|
// Legacy case of @implementation with no corresponding @interface.
|
|
|
|
// Build, chain & install the interface decl into the identifier.
|
2008-08-21 02:02:42 +08:00
|
|
|
|
2009-05-16 15:39:55 +08:00
|
|
|
// FIXME: Do we support attributes on the @implementation? If so we should
|
|
|
|
// copy them over.
|
2009-09-09 23:08:12 +08:00
|
|
|
IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
ClassName, /*typeParamList=*/nullptr,
|
|
|
|
/*PrevDecl=*/nullptr, ClassLoc,
|
2011-12-16 11:12:41 +08:00
|
|
|
true);
|
2011-12-15 13:27:12 +08:00
|
|
|
IDecl->startDefinition();
|
2011-12-16 06:34:59 +08:00
|
|
|
if (SDecl) {
|
2015-07-07 11:57:35 +08:00
|
|
|
IDecl->setSuperClass(Context.getTrivialTypeSourceInfo(
|
|
|
|
Context.getObjCInterfaceType(SDecl),
|
|
|
|
SuperClassLoc));
|
2011-12-16 06:34:59 +08:00
|
|
|
IDecl->setEndOfDefinitionLoc(SuperClassLoc);
|
|
|
|
} else {
|
|
|
|
IDecl->setEndOfDefinitionLoc(ClassLoc);
|
|
|
|
}
|
|
|
|
|
2009-04-24 08:16:12 +08:00
|
|
|
PushOnScopeChains(IDecl, TUScope);
|
2010-08-11 20:19:30 +08:00
|
|
|
} else {
|
|
|
|
// Mark the interface as being completed, even if it was just as
|
|
|
|
// @class ....;
|
|
|
|
// declaration; the user cannot reopen it.
|
2011-12-15 13:27:12 +08:00
|
|
|
if (!IDecl->hasDefinition())
|
|
|
|
IDecl->startDefinition();
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ObjCImplementationDecl* IMPDecl =
|
2011-10-04 12:48:02 +08:00
|
|
|
ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
|
2013-05-04 02:05:44 +08:00
|
|
|
ClassLoc, AtClassImplLoc, SuperClassLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
if (CheckObjCDeclScope(IMPDecl))
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(IMPDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// Check that there is no duplicate implementation of this class.
|
2010-08-11 20:19:30 +08:00
|
|
|
if (IDecl->getImplementation()) {
|
|
|
|
// FIXME: Don't leak everything!
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
|
2009-07-21 08:06:04 +08:00
|
|
|
Diag(IDecl->getImplementation()->getLocation(),
|
|
|
|
diag::note_previous_definition);
|
2013-05-31 02:53:21 +08:00
|
|
|
IMPDecl->setInvalidDecl();
|
2010-08-11 20:19:30 +08:00
|
|
|
} else { // add it to the list.
|
2009-07-21 08:05:53 +08:00
|
|
|
IDecl->setImplementation(IMPDecl);
|
2009-04-24 08:11:27 +08:00
|
|
|
PushOnScopeChains(IMPDecl, TUScope);
|
2011-02-15 08:59:30 +08:00
|
|
|
// Warn on implementating deprecated class under
|
|
|
|
// -Wdeprecated-implementations flag.
|
2011-02-16 01:49:58 +08:00
|
|
|
DiagnoseObjCImplementedDeprecations(*this,
|
|
|
|
dyn_cast<NamedDecl>(IDecl),
|
|
|
|
IMPDecl->getLocation(), 1);
|
2009-07-21 08:05:53 +08:00
|
|
|
}
|
2016-04-02 07:23:52 +08:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(IMPDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2012-02-24 05:11:20 +08:00
|
|
|
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);
|
|
|
|
|
2013-07-09 20:05:01 +08:00
|
|
|
return BuildDeclaratorGroup(DeclsInGroup, false);
|
2012-02-24 05:11:20 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
|
|
|
|
ObjCIvarDecl **ivars, unsigned numIvars,
|
2007-12-12 15:09:47 +08:00
|
|
|
SourceLocation RBrace) {
|
|
|
|
assert(ImpDecl && "missing implementation decl");
|
2009-01-20 09:17:11 +08:00
|
|
|
ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
|
2007-12-12 15:09:47 +08:00
|
|
|
if (!IDecl)
|
|
|
|
return;
|
2012-06-15 05:40:34 +08:00
|
|
|
/// Check case of non-existing \@interface decl.
|
|
|
|
/// (legacy objective-c \@implementation decl without an \@interface decl).
|
2007-12-12 15:09:47 +08:00
|
|
|
/// Add implementations's ivar to the synthesize class's ivar list.
|
2009-04-21 04:09:33 +08:00
|
|
|
if (IDecl->isImplicitInterfaceDecl()) {
|
2011-12-16 06:34:59 +08:00
|
|
|
IDecl->setEndOfDefinitionLoc(RBrace);
|
2010-02-18 01:00:07 +08:00
|
|
|
// Add ivar's to class's DeclContext.
|
|
|
|
for (unsigned i = 0, e = numIvars; i != e; ++i) {
|
2010-02-18 02:10:54 +08:00
|
|
|
ivars[i]->setLexicalDeclContext(ImpDecl);
|
2012-03-13 11:12:56 +08:00
|
|
|
IDecl->makeDeclVisibleInContext(ivars[i]);
|
2010-02-19 08:31:17 +08:00
|
|
|
ImpDecl->addDecl(ivars[i]);
|
2010-02-18 01:00:07 +08:00
|
|
|
}
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If implementation has empty ivar list, just return.
|
|
|
|
if (numIvars == 0)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
assert(ivars && "missing @implementation ivars");
|
2012-06-20 14:18:46 +08:00
|
|
|
if (LangOpts.ObjCRuntime.isNonFragile()) {
|
2010-02-20 04:58:54 +08:00
|
|
|
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;
|
|
|
|
}
|
2013-06-27 06:10:27 +08:00
|
|
|
// Check class extensions (unnamed categories) for duplicate ivars.
|
2014-03-14 05:47:07 +08:00
|
|
|
for (const auto *CDecl : IDecl->visible_extensions()) {
|
2013-06-27 06:10:27 +08:00
|
|
|
if (const ObjCIvarDecl *ClsExtIvar =
|
|
|
|
CDecl->getIvarDecl(ImplIvar->getIdentifier())) {
|
|
|
|
Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
|
|
|
|
Diag(ClsExtIvar->getLocation(), diag::note_previous_definition);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2010-02-20 04:58:54 +08:00
|
|
|
// Instance ivar to Implementation's DeclContext.
|
|
|
|
ImplIvar->setLexicalDeclContext(ImpDecl);
|
2012-03-13 11:12:56 +08:00
|
|
|
IDecl->makeDeclVisibleInContext(ImplIvar);
|
2010-02-20 04:58:54 +08:00
|
|
|
ImpDecl->addDecl(ImplIvar);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
// Check interface's Ivar list against those in the implementation.
|
|
|
|
// names and types must match.
|
|
|
|
//
|
|
|
|
unsigned j = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCInterfaceDecl::ivar_iterator
|
2007-12-13 01:58:05 +08:00
|
|
|
IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
|
|
|
|
for (; numIvars > 0 && IVI != IVE; ++IVI) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCIvarDecl* ImplIvar = ivars[j++];
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCIvarDecl* ClsIvar = *IVI;
|
2007-12-12 15:09:47 +08:00
|
|
|
assert (ImplIvar && "missing implementation ivar");
|
|
|
|
assert (ClsIvar && "missing class ivar");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-03 22:49:36 +08:00
|
|
|
// First, make sure the types match.
|
2011-10-11 02:28:20 +08:00
|
|
|
if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
|
2008-11-19 13:08:23 +08:00
|
|
|
Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
|
2008-11-24 05:45:46 +08:00
|
|
|
<< ImplIvar->getIdentifier()
|
|
|
|
<< ImplIvar->getType() << ClsIvar->getType();
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(ClsIvar->getLocation(), diag::note_previous_definition);
|
2011-10-11 02:28:20 +08:00
|
|
|
} else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
|
|
|
|
ImplIvar->getBitWidthValue(Context) !=
|
|
|
|
ClsIvar->getBitWidthValue(Context)) {
|
|
|
|
Diag(ImplIvar->getBitWidth()->getLocStart(),
|
|
|
|
diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
|
|
|
|
Diag(ClsIvar->getBitWidth()->getLocStart(),
|
|
|
|
diag::note_previous_definition);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-03-03 22:49:36 +08:00
|
|
|
// Make sure the names are identical.
|
|
|
|
if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
|
2008-11-19 13:08:23 +08:00
|
|
|
Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
|
2008-11-24 05:45:46 +08:00
|
|
|
<< ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(ClsIvar->getLocation(), diag::note_previous_definition);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
--numIvars;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-13 02:11:49 +08:00
|
|
|
if (numIvars > 0)
|
2013-12-02 11:50:21 +08:00
|
|
|
Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count);
|
2007-12-13 02:11:49 +08:00
|
|
|
else if (IVI != IVE)
|
2013-12-02 11:50:21 +08:00
|
|
|
Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2013-12-13 13:58:44 +08:00
|
|
|
static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc,
|
|
|
|
ObjCMethodDecl *method,
|
|
|
|
bool &IncompleteImpl,
|
2013-12-13 13:58:51 +08:00
|
|
|
unsigned DiagID,
|
2014-05-26 14:22:03 +08:00
|
|
|
NamedDecl *NeededFor = nullptr) {
|
2011-06-25 04:31:37 +08:00
|
|
|
// No point warning no definition of method which is 'unavailable'.
|
2012-12-12 02:53:07 +08:00
|
|
|
switch (method->getAvailability()) {
|
|
|
|
case AR_Available:
|
|
|
|
case AR_Deprecated:
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Don't warn about unavailable or not-yet-introduced methods.
|
|
|
|
case AR_NotYetIntroduced:
|
|
|
|
case AR_Unavailable:
|
2011-06-25 04:31:37 +08:00
|
|
|
return;
|
2012-12-12 02:53:07 +08:00
|
|
|
}
|
|
|
|
|
2013-03-27 08:02:21 +08:00
|
|
|
// 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.
|
2013-12-13 13:58:51 +08:00
|
|
|
{
|
|
|
|
const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID);
|
|
|
|
B << method;
|
|
|
|
if (NeededFor)
|
|
|
|
B << NeededFor;
|
|
|
|
}
|
2013-03-27 08:02:21 +08:00
|
|
|
|
|
|
|
// Issue a note to the original declaration.
|
|
|
|
SourceLocation MethodLoc = method->getLocStart();
|
|
|
|
if (MethodLoc.isValid())
|
2013-12-13 13:58:44 +08:00
|
|
|
S.Diag(MethodLoc, diag::note_method_declared_at) << method;
|
2008-02-11 05:38:56 +08:00
|
|
|
}
|
|
|
|
|
2010-10-26 01:23:52 +08:00
|
|
|
/// 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.
|
2010-10-28 10:34:38 +08:00
|
|
|
static bool isObjCTypeSubstitutable(ASTContext &Context,
|
|
|
|
const ObjCObjectPointerType *A,
|
|
|
|
const ObjCObjectPointerType *B,
|
|
|
|
bool rejectId) {
|
|
|
|
// Reject a protocol-unqualified id.
|
|
|
|
if (rejectId && B->isObjCIdType()) return false;
|
2010-10-26 01:23:52 +08:00
|
|
|
|
|
|
|
// 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() &&
|
2010-10-28 10:34:38 +08:00
|
|
|
Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
|
|
|
|
QualType(B,0),
|
|
|
|
false);
|
2010-10-26 01:23:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// 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.
|
2010-10-28 10:34:38 +08:00
|
|
|
return Context.canAssignObjCInterfaces(A, B);
|
2010-10-26 01:23:52 +08:00
|
|
|
}
|
|
|
|
|
2010-10-28 10:34:38 +08:00
|
|
|
static SourceRange getTypeRange(TypeSourceInfo *TSI) {
|
|
|
|
return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
|
|
|
|
}
|
|
|
|
|
2015-06-20 02:14:38 +08:00
|
|
|
/// 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);
|
|
|
|
}
|
|
|
|
|
2011-07-29 07:19:50 +08:00
|
|
|
static bool CheckMethodOverrideReturn(Sema &S,
|
2010-10-28 10:34:38 +08:00
|
|
|
ObjCMethodDecl *MethodImpl,
|
2011-02-22 07:49:15 +08:00
|
|
|
ObjCMethodDecl *MethodDecl,
|
2011-07-25 04:53:26 +08:00
|
|
|
bool IsProtocolMethodDecl,
|
2011-08-11 01:16:30 +08:00
|
|
|
bool IsOverridingMode,
|
2011-07-29 07:19:50 +08:00
|
|
|
bool Warn) {
|
2011-02-22 07:49:15 +08:00
|
|
|
if (IsProtocolMethodDecl &&
|
2015-06-20 02:14:38 +08:00
|
|
|
objcModifiersConflict(MethodDecl->getObjCDeclQualifier(),
|
|
|
|
MethodImpl->getObjCDeclQualifier())) {
|
2011-07-29 07:19:50 +08:00
|
|
|
if (Warn) {
|
2014-01-26 00:55:45 +08:00
|
|
|
S.Diag(MethodImpl->getLocation(),
|
|
|
|
(IsOverridingMode
|
|
|
|
? diag::warn_conflicting_overriding_ret_type_modifiers
|
|
|
|
: diag::warn_conflicting_ret_type_modifiers))
|
2011-08-11 01:16:30 +08:00
|
|
|
<< MethodImpl->getDeclName()
|
2014-08-01 21:20:09 +08:00
|
|
|
<< MethodImpl->getReturnTypeSourceRange();
|
2014-01-26 00:55:45 +08:00
|
|
|
S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
|
2014-08-01 21:20:09 +08:00
|
|
|
<< MethodDecl->getReturnTypeSourceRange();
|
2011-07-29 07:19:50 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2011-02-22 07:49:15 +08:00
|
|
|
}
|
2015-06-20 02:14:38 +08:00
|
|
|
if (Warn && IsOverridingMode &&
|
|
|
|
!isa<ObjCImplementationDecl>(MethodImpl->getDeclContext()) &&
|
|
|
|
!S.Context.hasSameNullabilityTypeQualifier(MethodImpl->getReturnType(),
|
|
|
|
MethodDecl->getReturnType(),
|
|
|
|
false)) {
|
2015-06-25 06:02:08 +08:00
|
|
|
auto nullabilityMethodImpl =
|
|
|
|
*MethodImpl->getReturnType()->getNullability(S.Context);
|
|
|
|
auto nullabilityMethodDecl =
|
|
|
|
*MethodDecl->getReturnType()->getNullability(S.Context);
|
2015-06-20 02:14:38 +08:00
|
|
|
S.Diag(MethodImpl->getLocation(),
|
|
|
|
diag::warn_conflicting_nullability_attr_overriding_ret_types)
|
2015-06-25 06:02:08 +08:00
|
|
|
<< DiagNullabilityKind(
|
|
|
|
nullabilityMethodImpl,
|
|
|
|
((MethodImpl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability)
|
|
|
|
!= 0))
|
|
|
|
<< DiagNullabilityKind(
|
|
|
|
nullabilityMethodDecl,
|
|
|
|
((MethodDecl->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability)
|
|
|
|
!= 0));
|
2015-06-20 02:14:38 +08:00
|
|
|
S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
|
|
|
|
}
|
|
|
|
|
2014-01-26 00:55:45 +08:00
|
|
|
if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(),
|
|
|
|
MethodDecl->getReturnType()))
|
2011-07-29 07:19:50 +08:00
|
|
|
return true;
|
|
|
|
if (!Warn)
|
|
|
|
return false;
|
2010-10-28 10:34:38 +08:00
|
|
|
|
2011-08-11 01:16:30 +08:00
|
|
|
unsigned DiagID =
|
|
|
|
IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
|
|
|
|
: diag::warn_conflicting_ret_types;
|
2010-10-28 10:34:38 +08:00
|
|
|
|
|
|
|
// Mismatches between ObjC pointers go into a different warning
|
|
|
|
// category, and sometimes they're even completely whitelisted.
|
|
|
|
if (const ObjCObjectPointerType *ImplPtrTy =
|
2014-01-26 00:55:45 +08:00
|
|
|
MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) {
|
2010-10-28 10:34:38 +08:00
|
|
|
if (const ObjCObjectPointerType *IfacePtrTy =
|
2014-01-26 00:55:45 +08:00
|
|
|
MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) {
|
2010-10-28 10:34:38 +08:00
|
|
|
// 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))
|
2011-07-29 07:19:50 +08:00
|
|
|
return false;
|
2010-10-28 10:34:38 +08:00
|
|
|
|
2011-08-11 01:16:30 +08:00
|
|
|
DiagID =
|
|
|
|
IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
|
2015-11-17 13:40:05 +08:00
|
|
|
: diag::warn_non_covariant_ret_types;
|
2010-10-28 10:34:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
S.Diag(MethodImpl->getLocation(), DiagID)
|
2014-01-26 00:55:45 +08:00
|
|
|
<< MethodImpl->getDeclName() << MethodDecl->getReturnType()
|
|
|
|
<< MethodImpl->getReturnType()
|
2014-08-01 21:20:09 +08:00
|
|
|
<< MethodImpl->getReturnTypeSourceRange();
|
2014-01-26 00:55:45 +08:00
|
|
|
S.Diag(MethodDecl->getLocation(), IsOverridingMode
|
|
|
|
? diag::note_previous_declaration
|
|
|
|
: diag::note_previous_definition)
|
2014-08-01 21:20:09 +08:00
|
|
|
<< MethodDecl->getReturnTypeSourceRange();
|
2011-07-29 07:19:50 +08:00
|
|
|
return false;
|
2010-10-28 10:34:38 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 07:19:50 +08:00
|
|
|
static bool CheckMethodOverrideParam(Sema &S,
|
2010-10-28 10:34:38 +08:00
|
|
|
ObjCMethodDecl *MethodImpl,
|
2011-02-22 07:49:15 +08:00
|
|
|
ObjCMethodDecl *MethodDecl,
|
2010-10-28 10:34:38 +08:00
|
|
|
ParmVarDecl *ImplVar,
|
2011-02-22 07:49:15 +08:00
|
|
|
ParmVarDecl *IfaceVar,
|
2011-07-25 04:53:26 +08:00
|
|
|
bool IsProtocolMethodDecl,
|
2011-08-11 01:16:30 +08:00
|
|
|
bool IsOverridingMode,
|
2011-07-29 07:19:50 +08:00
|
|
|
bool Warn) {
|
2011-02-22 07:49:15 +08:00
|
|
|
if (IsProtocolMethodDecl &&
|
2015-06-20 02:14:38 +08:00
|
|
|
objcModifiersConflict(ImplVar->getObjCDeclQualifier(),
|
|
|
|
IfaceVar->getObjCDeclQualifier())) {
|
2011-07-29 07:19:50 +08:00
|
|
|
if (Warn) {
|
2011-08-11 01:16:30 +08:00
|
|
|
if (IsOverridingMode)
|
|
|
|
S.Diag(ImplVar->getLocation(),
|
|
|
|
diag::warn_conflicting_overriding_param_modifiers)
|
|
|
|
<< getTypeRange(ImplVar->getTypeSourceInfo())
|
|
|
|
<< MethodImpl->getDeclName();
|
|
|
|
else S.Diag(ImplVar->getLocation(),
|
2011-07-29 07:19:50 +08:00
|
|
|
diag::warn_conflicting_param_modifiers)
|
|
|
|
<< getTypeRange(ImplVar->getTypeSourceInfo())
|
2011-08-11 01:16:30 +08:00
|
|
|
<< MethodImpl->getDeclName();
|
2011-07-29 07:19:50 +08:00
|
|
|
S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
|
|
|
|
<< getTypeRange(IfaceVar->getTypeSourceInfo());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2011-02-22 07:49:15 +08:00
|
|
|
}
|
|
|
|
|
2010-10-28 10:34:38 +08:00
|
|
|
QualType ImplTy = ImplVar->getType();
|
|
|
|
QualType IfaceTy = IfaceVar->getType();
|
2015-06-20 02:14:38 +08:00
|
|
|
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)
|
2015-06-25 06:02:08 +08:00
|
|
|
<< 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);
|
2015-06-20 02:14:38 +08:00
|
|
|
}
|
2010-10-28 10:34:38 +08:00
|
|
|
if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
|
2011-07-29 07:19:50 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!Warn)
|
|
|
|
return false;
|
2011-08-11 01:16:30 +08:00
|
|
|
unsigned DiagID =
|
|
|
|
IsOverridingMode ? diag::warn_conflicting_overriding_param_types
|
|
|
|
: diag::warn_conflicting_param_types;
|
2010-10-28 10:34:38 +08:00
|
|
|
|
|
|
|
// 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))
|
2011-07-29 07:19:50 +08:00
|
|
|
return false;
|
2010-10-28 10:34:38 +08:00
|
|
|
|
2011-08-11 01:16:30 +08:00
|
|
|
DiagID =
|
|
|
|
IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
|
2015-11-17 13:40:05 +08:00
|
|
|
: diag::warn_non_contravariant_param_types;
|
2010-10-28 10:34:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
S.Diag(ImplVar->getLocation(), DiagID)
|
|
|
|
<< getTypeRange(ImplVar->getTypeSourceInfo())
|
2011-08-11 01:16:30 +08:00
|
|
|
<< MethodImpl->getDeclName() << IfaceTy << ImplTy;
|
|
|
|
S.Diag(IfaceVar->getLocation(),
|
|
|
|
(IsOverridingMode ? diag::note_previous_declaration
|
2015-11-17 13:40:05 +08:00
|
|
|
: diag::note_previous_definition))
|
2010-10-28 10:34:38 +08:00
|
|
|
<< getTypeRange(IfaceVar->getTypeSourceInfo());
|
2011-07-29 07:19:50 +08:00
|
|
|
return false;
|
2010-10-28 10:34:38 +08:00
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
/// 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:
|
2011-08-29 06:35:17 +08:00
|
|
|
case OMF_finalize:
|
2011-06-16 07:02:42 +08:00
|
|
|
case OMF_retainCount:
|
|
|
|
case OMF_self:
|
2014-08-23 00:57:26 +08:00
|
|
|
case OMF_initialize:
|
2011-07-06 06:38:59 +08:00
|
|
|
case OMF_performSelector:
|
2011-06-16 07:02:42 +08:00
|
|
|
// 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.
|
2014-01-26 00:55:45 +08:00
|
|
|
if (unmatched->getReturnType()->isObjCObjectPointerType()) {
|
2011-06-16 07:02:42 +08:00
|
|
|
reasonSelector = R_UnrelatedReturn;
|
|
|
|
} else {
|
|
|
|
reasonSelector = R_NonObjectReturn;
|
|
|
|
}
|
|
|
|
|
2013-06-27 05:31:47 +08:00
|
|
|
S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
|
|
|
|
S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-28 10:34:38 +08:00
|
|
|
|
2008-12-06 02:18:52 +08:00
|
|
|
void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
|
2011-02-22 07:49:15 +08:00
|
|
|
ObjCMethodDecl *MethodDecl,
|
2011-10-11 01:53:29 +08:00
|
|
|
bool IsProtocolMethodDecl) {
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
2011-06-16 07:02:42 +08:00
|
|
|
checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
|
|
|
|
return;
|
|
|
|
|
2011-02-22 07:49:15 +08:00
|
|
|
CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
|
2011-10-11 01:53:29 +08:00
|
|
|
IsProtocolMethodDecl, false,
|
2011-08-11 01:16:30 +08:00
|
|
|
true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 03:58:42 +08:00
|
|
|
for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
|
2012-05-18 07:13:29 +08:00
|
|
|
IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
|
|
|
|
EF = MethodDecl->param_end();
|
|
|
|
IM != EM && IF != EF; ++IM, ++IF) {
|
2011-02-22 07:49:15 +08:00
|
|
|
CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
|
2011-10-11 01:53:29 +08:00
|
|
|
IsProtocolMethodDecl, false, true);
|
2010-05-22 07:28:58 +08:00
|
|
|
}
|
2008-12-06 02:18:52 +08:00
|
|
|
|
2011-08-09 02:03:17 +08:00
|
|
|
if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
|
2011-10-11 01:53:29 +08:00
|
|
|
Diag(ImpMethodDecl->getLocation(),
|
|
|
|
diag::warn_conflicting_variadic);
|
2011-08-09 02:03:17 +08:00
|
|
|
Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-11 01:53:29 +08:00
|
|
|
void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
|
|
|
|
ObjCMethodDecl *Overridden,
|
|
|
|
bool IsProtocolMethodDecl) {
|
|
|
|
|
|
|
|
CheckMethodOverrideReturn(*this, Method, Overridden,
|
|
|
|
IsProtocolMethodDecl, true,
|
|
|
|
true);
|
|
|
|
|
|
|
|
for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
|
2012-05-18 07:13:29 +08:00
|
|
|
IF = Overridden->param_begin(), EM = Method->param_end(),
|
|
|
|
EF = Overridden->param_end();
|
|
|
|
IM != EM && IF != EF; ++IM, ++IF) {
|
2011-10-11 01:53:29 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 07:19:50 +08:00
|
|
|
/// 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(),
|
2012-05-18 07:13:29 +08:00
|
|
|
IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
|
|
|
|
EF = MethodDecl->param_end();
|
|
|
|
IM != EM && IF != EF; ++IM, ++IF) {
|
2011-07-29 07:19:50 +08:00
|
|
|
match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
|
|
|
|
*IM, *IF,
|
|
|
|
IsProtocolMethodDecl, false, false);
|
|
|
|
if (!match)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (match)
|
|
|
|
match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
|
2011-08-09 01:32:19 +08:00
|
|
|
if (match)
|
|
|
|
match = !(MethodDecl->isClassMethod() &&
|
|
|
|
MethodDecl->getSelector() == GetNullarySelector("load", Context));
|
2011-07-29 07:19:50 +08:00
|
|
|
|
|
|
|
if (match) {
|
|
|
|
Diag(ImpMethodDecl->getLocation(),
|
|
|
|
diag::warn_category_method_impl_match);
|
2012-02-28 06:55:11 +08:00
|
|
|
Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
|
|
|
|
<< MethodDecl->getDeclName();
|
2011-07-29 07:19:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-16 15:39:55 +08:00
|
|
|
/// 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.
|
2008-08-27 13:40:03 +08:00
|
|
|
|
2014-03-05 16:13:08 +08:00
|
|
|
typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet;
|
2014-03-09 19:34:25 +08:00
|
|
|
typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet;
|
2014-03-06 07:18:22 +08:00
|
|
|
|
|
|
|
static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl,
|
|
|
|
ProtocolNameSet &PNS) {
|
|
|
|
if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
|
|
|
|
PNS.insert(PDecl->getIdentifier());
|
2014-03-14 06:58:06 +08:00
|
|
|
for (const auto *PI : PDecl->protocols())
|
|
|
|
findProtocolsWithExplicitImpls(PI, PNS);
|
2014-03-06 07:18:22 +08:00
|
|
|
}
|
|
|
|
|
2014-03-05 16:13:08 +08:00
|
|
|
/// 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;
|
|
|
|
|
2014-03-14 04:55:22 +08:00
|
|
|
for (const auto *I : Super->all_referenced_protocols())
|
|
|
|
findProtocolsWithExplicitImpls(I, PNS);
|
2014-03-06 07:18:22 +08:00
|
|
|
|
|
|
|
findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS);
|
2014-03-05 16:13:08 +08:00
|
|
|
}
|
|
|
|
|
2008-02-09 06:06:17 +08:00
|
|
|
/// CheckProtocolMethodDefs - This routine checks unimplemented methods
|
2007-12-12 15:09:47 +08:00
|
|
|
/// Declared in protocol, and those referenced by it.
|
2013-12-13 14:26:10 +08:00
|
|
|
static void CheckProtocolMethodDefs(Sema &S,
|
|
|
|
SourceLocation ImpLoc,
|
|
|
|
ObjCProtocolDecl *PDecl,
|
|
|
|
bool& IncompleteImpl,
|
|
|
|
const Sema::SelectorSet &InsMap,
|
|
|
|
const Sema::SelectorSet &ClsMap,
|
2013-12-13 14:26:14 +08:00
|
|
|
ObjCContainerDecl *CDecl,
|
2014-03-05 16:13:08 +08:00
|
|
|
LazyProtocolNameSet &ProtocolsExplictImpl) {
|
2012-02-10 05:30:24 +08:00
|
|
|
ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
|
|
|
|
ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
|
|
|
|
: dyn_cast<ObjCInterfaceDecl>(CDecl);
|
2010-03-28 05:10:05 +08:00
|
|
|
assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
|
|
|
|
|
2008-09-05 04:01:15 +08:00
|
|
|
ObjCInterfaceDecl *Super = IDecl->getSuperClass();
|
2014-05-26 14:22:03 +08:00
|
|
|
ObjCInterfaceDecl *NSIDecl = nullptr;
|
2014-03-05 16:13:08 +08:00
|
|
|
|
|
|
|
// 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>()) {
|
2014-03-09 19:34:25 +08:00
|
|
|
if (!ProtocolsExplictImpl) {
|
2014-03-05 16:13:08 +08:00
|
|
|
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.
|
2014-05-26 14:22:03 +08:00
|
|
|
Super = nullptr;
|
2014-03-05 16:13:08 +08:00
|
|
|
}
|
|
|
|
|
2013-12-13 14:26:10 +08:00
|
|
|
if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// check to see if class implements forwardInvocation method and objects
|
|
|
|
// of this class are derived from 'NSProxy' so that to forward requests
|
2009-05-23 01:12:32 +08:00
|
|
|
// from one object to another.
|
2009-09-09 23:08:12 +08:00
|
|
|
// Under such conditions, which means that every method possible is
|
|
|
|
// implemented in the class, we should not issue "Method definition not
|
2009-05-23 01:12:32 +08:00
|
|
|
// found" warnings.
|
|
|
|
// FIXME: Use a general GetUnarySelector method for this.
|
2013-12-13 14:26:10 +08:00
|
|
|
IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation");
|
|
|
|
Selector fISelector = S.Context.Selectors.getSelector(1, &II);
|
2009-05-23 01:12:32 +08:00
|
|
|
if (InsMap.count(fISelector))
|
|
|
|
// Is IDecl derived from 'NSProxy'? If so, no instance methods
|
|
|
|
// need be implemented in the implementation.
|
2013-12-13 14:26:10 +08:00
|
|
|
NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy"));
|
2009-05-23 01:12:32 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-01-08 03:21:03 +08:00
|
|
|
// If this is a forward protocol declaration, get its definition.
|
|
|
|
if (!PDecl->isThisDeclarationADefinition() &&
|
|
|
|
PDecl->getDefinition())
|
|
|
|
PDecl = PDecl->getDefinition();
|
|
|
|
|
2008-09-05 04:01:15 +08:00
|
|
|
// 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.
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// check unimplemented instance methods.
|
2009-05-23 01:12:32 +08:00
|
|
|
if (!NSIDecl)
|
2014-03-14 03:50:17 +08:00
|
|
|
for (auto *method : PDecl->instance_methods()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
|
2012-10-11 00:42:25 +08:00
|
|
|
!method->isPropertyAccessor() &&
|
|
|
|
!InsMap.count(method->getSelector()) &&
|
2013-11-23 09:01:29 +08:00
|
|
|
(!Super || !Super->lookupMethod(method->getSelector(),
|
|
|
|
true /* instance */,
|
|
|
|
false /* shallowCategory */,
|
2013-11-23 09:01:34 +08:00
|
|
|
true /* followsSuper */,
|
2014-05-26 14:22:03 +08:00
|
|
|
nullptr /* category */))) {
|
2012-02-10 05:30:24 +08:00
|
|
|
// 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.
|
|
|
|
|
2009-05-23 01:12:32 +08:00
|
|
|
// Ugly, but necessary. Method declared in protcol might have
|
|
|
|
// have been synthesized due to a property declared in the class which
|
|
|
|
// uses the protocol.
|
2012-02-10 05:30:24 +08:00
|
|
|
if (ObjCMethodDecl *MethodInClass =
|
2013-11-23 09:01:29 +08:00
|
|
|
IDecl->lookupMethod(method->getSelector(),
|
|
|
|
true /* instance */,
|
|
|
|
true /* shallowCategoryLookup */,
|
|
|
|
false /* followSuper */))
|
2012-10-11 00:42:25 +08:00
|
|
|
if (C || MethodInClass->isPropertyAccessor())
|
2012-02-10 05:30:24 +08:00
|
|
|
continue;
|
|
|
|
unsigned DIAG = diag::warn_unimplemented_protocol_method;
|
2014-06-16 07:30:39 +08:00
|
|
|
if (!S.Diags.isIgnored(DIAG, ImpLoc)) {
|
2013-12-13 14:26:10 +08:00
|
|
|
WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG,
|
2013-12-13 13:58:51 +08:00
|
|
|
PDecl);
|
2010-03-28 03:02:17 +08:00
|
|
|
}
|
2009-05-23 01:12:32 +08:00
|
|
|
}
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
// check unimplemented class methods
|
2014-03-14 04:11:06 +08:00
|
|
|
for (auto *method : PDecl->class_methods()) {
|
2008-09-05 04:01:15 +08:00
|
|
|
if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
|
|
|
|
!ClsMap.count(method->getSelector()) &&
|
2013-11-23 09:01:29 +08:00
|
|
|
(!Super || !Super->lookupMethod(method->getSelector(),
|
|
|
|
false /* class method */,
|
|
|
|
false /* shallowCategoryLookup */,
|
2013-11-23 09:01:34 +08:00
|
|
|
true /* followSuper */,
|
2014-05-26 14:22:03 +08:00
|
|
|
nullptr /* category */))) {
|
2012-02-10 05:30:24 +08:00
|
|
|
// See above comment for instance method lookups.
|
2013-11-23 09:01:29 +08:00
|
|
|
if (C && IDecl->lookupMethod(method->getSelector(),
|
|
|
|
false /* class */,
|
|
|
|
true /* shallowCategoryLookup */,
|
|
|
|
false /* followSuper */))
|
2012-02-10 05:30:24 +08:00
|
|
|
continue;
|
2013-11-23 09:01:29 +08:00
|
|
|
|
2010-04-01 02:23:33 +08:00
|
|
|
unsigned DIAG = diag::warn_unimplemented_protocol_method;
|
2014-06-16 07:30:39 +08:00
|
|
|
if (!S.Diags.isIgnored(DIAG, ImpLoc)) {
|
2013-12-13 14:26:10 +08:00
|
|
|
WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl);
|
2010-04-01 02:23:33 +08:00
|
|
|
}
|
2010-03-28 03:02:17 +08:00
|
|
|
}
|
2007-12-15 07:37:57 +08:00
|
|
|
}
|
2008-07-22 05:32:27 +08:00
|
|
|
// Check on this protocols's referenced protocols, recursively.
|
2014-03-14 06:58:06 +08:00
|
|
|
for (auto *PI : PDecl->protocols())
|
|
|
|
CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap,
|
2014-03-05 16:13:08 +08:00
|
|
|
CDecl, ProtocolsExplictImpl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2011-07-16 08:08:33 +08:00
|
|
|
/// MatchAllMethodDeclarations - Check methods declared in interface
|
2009-05-02 04:07:12 +08:00
|
|
|
/// or protocol against those declared in their implementations.
|
|
|
|
///
|
2012-05-27 21:28:52 +08:00
|
|
|
void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
|
|
|
|
const SelectorSet &ClsMap,
|
|
|
|
SelectorSet &InsMapSeen,
|
|
|
|
SelectorSet &ClsMapSeen,
|
2009-05-02 04:07:12 +08:00
|
|
|
ObjCImplDecl* IMPDecl,
|
|
|
|
ObjCContainerDecl* CDecl,
|
|
|
|
bool &IncompleteImpl,
|
2011-07-29 07:19:50 +08:00
|
|
|
bool ImmediateClass,
|
2012-02-10 05:30:24 +08:00
|
|
|
bool WarnCategoryMethodImpl) {
|
2009-05-02 04:07:12 +08:00
|
|
|
// Check and see if instance methods in class interface have been
|
|
|
|
// implemented in the implementation class. If so, their types match.
|
2014-03-14 03:50:17 +08:00
|
|
|
for (auto *I : CDecl->instance_methods()) {
|
2014-11-19 15:49:47 +08:00
|
|
|
if (!InsMapSeen.insert(I->getSelector()).second)
|
2013-10-14 23:16:10 +08:00
|
|
|
continue;
|
2014-03-14 03:50:17 +08:00
|
|
|
if (!I->isPropertyAccessor() &&
|
|
|
|
!InsMap.count(I->getSelector())) {
|
2009-05-02 04:07:12 +08:00
|
|
|
if (ImmediateClass)
|
2014-03-14 03:50:17 +08:00
|
|
|
WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
|
2013-03-27 08:02:21 +08:00
|
|
|
diag::warn_undef_method_impl);
|
2009-05-02 04:07:12 +08:00
|
|
|
continue;
|
2009-08-05 05:02:39 +08:00
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCMethodDecl *ImpMethodDecl =
|
2014-03-14 03:50:17 +08:00
|
|
|
IMPDecl->getInstanceMethod(I->getSelector());
|
|
|
|
assert(CDecl->getInstanceMethod(I->getSelector()) &&
|
2011-08-31 03:43:21 +08:00
|
|
|
"Expected to find the method through lookup as well");
|
2009-05-02 04:07:12 +08:00
|
|
|
// ImpMethodDecl may be null as in a @dynamic property.
|
2011-07-29 07:19:50 +08:00
|
|
|
if (ImpMethodDecl) {
|
2012-02-10 05:30:24 +08:00
|
|
|
if (!WarnCategoryMethodImpl)
|
2014-03-14 03:50:17 +08:00
|
|
|
WarnConflictingTypedMethods(ImpMethodDecl, I,
|
2011-07-29 07:19:50 +08:00
|
|
|
isa<ObjCProtocolDecl>(CDecl));
|
2014-03-14 03:50:17 +08:00
|
|
|
else if (!I->isPropertyAccessor())
|
|
|
|
WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl));
|
2011-07-29 07:19:50 +08:00
|
|
|
}
|
2009-05-02 04:07:12 +08:00
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-02 04:07:12 +08:00
|
|
|
// Check and see if class methods in class interface have been
|
|
|
|
// implemented in the implementation class. If so, their types match.
|
2014-03-14 04:11:06 +08:00
|
|
|
for (auto *I : CDecl->class_methods()) {
|
2014-11-19 15:49:47 +08:00
|
|
|
if (!ClsMapSeen.insert(I->getSelector()).second)
|
2013-10-14 23:16:10 +08:00
|
|
|
continue;
|
2016-01-28 04:10:32 +08:00
|
|
|
if (!I->isPropertyAccessor() &&
|
|
|
|
!ClsMap.count(I->getSelector())) {
|
2009-05-02 04:07:12 +08:00
|
|
|
if (ImmediateClass)
|
2014-03-14 04:11:06 +08:00
|
|
|
WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
|
2013-03-27 08:02:21 +08:00
|
|
|
diag::warn_undef_method_impl);
|
2009-08-05 05:02:39 +08:00
|
|
|
} else {
|
2009-06-30 10:36:12 +08:00
|
|
|
ObjCMethodDecl *ImpMethodDecl =
|
2014-03-14 04:11:06 +08:00
|
|
|
IMPDecl->getClassMethod(I->getSelector());
|
|
|
|
assert(CDecl->getClassMethod(I->getSelector()) &&
|
2011-08-31 03:43:21 +08:00
|
|
|
"Expected to find the method through lookup as well");
|
2016-01-28 04:10:32 +08:00
|
|
|
// ImpMethodDecl may be null as in a @dynamic property.
|
|
|
|
if (ImpMethodDecl) {
|
|
|
|
if (!WarnCategoryMethodImpl)
|
|
|
|
WarnConflictingTypedMethods(ImpMethodDecl, I,
|
|
|
|
isa<ObjCProtocolDecl>(CDecl));
|
|
|
|
else if (!I->isPropertyAccessor())
|
|
|
|
WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl));
|
|
|
|
}
|
2009-05-02 04:07:12 +08:00
|
|
|
}
|
|
|
|
}
|
2010-10-09 06:59:25 +08:00
|
|
|
|
2013-08-15 07:58:55 +08:00
|
|
|
if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) {
|
|
|
|
// Also, check for methods declared in protocols inherited by
|
|
|
|
// this protocol.
|
2014-03-14 06:58:06 +08:00
|
|
|
for (auto *PI : PD->protocols())
|
2013-08-15 07:58:55 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
2014-03-14 06:58:06 +08:00
|
|
|
IMPDecl, PI, IncompleteImpl, false,
|
2013-08-15 07:58:55 +08:00
|
|
|
WarnCategoryMethodImpl);
|
|
|
|
}
|
|
|
|
|
2009-05-02 04:07:12 +08:00
|
|
|
if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
|
2012-10-24 07:06:22 +08:00
|
|
|
// when checking that methods in implementation match their declaration,
|
|
|
|
// i.e. when WarnCategoryMethodImpl is false, check declarations in class
|
|
|
|
// extension; as well as those in categories.
|
2013-01-17 07:00:23 +08:00
|
|
|
if (!WarnCategoryMethodImpl) {
|
2014-03-14 05:47:07 +08:00
|
|
|
for (auto *Cat : I->visible_categories())
|
2012-10-24 07:06:22 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
2015-10-14 07:27:34 +08:00
|
|
|
IMPDecl, Cat, IncompleteImpl,
|
|
|
|
ImmediateClass && Cat->IsClassExtension(),
|
2012-10-24 07:06:22 +08:00
|
|
|
WarnCategoryMethodImpl);
|
2013-01-17 07:00:23 +08:00
|
|
|
} else {
|
2012-10-24 07:06:22 +08:00
|
|
|
// Also methods in class extensions need be looked at next.
|
2014-03-14 05:47:07 +08:00
|
|
|
for (auto *Ext : I->visible_extensions())
|
2012-10-24 07:06:22 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
2014-03-14 05:47:07 +08:00
|
|
|
IMPDecl, Ext, IncompleteImpl, false,
|
2012-10-24 07:06:22 +08:00
|
|
|
WarnCategoryMethodImpl);
|
2013-01-17 07:00:23 +08:00
|
|
|
}
|
|
|
|
|
2009-05-02 04:07:12 +08:00
|
|
|
// Check for any implementation of a methods declared in protocol.
|
2014-03-14 04:55:22 +08:00
|
|
|
for (auto *PI : I->all_referenced_protocols())
|
2009-09-09 23:08:12 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
2014-03-14 04:55:22 +08:00
|
|
|
IMPDecl, PI, IncompleteImpl, false,
|
2012-02-10 05:30:24 +08:00
|
|
|
WarnCategoryMethodImpl);
|
2014-03-14 04:55:22 +08:00
|
|
|
|
2011-07-29 07:19:50 +08:00
|
|
|
// FIXME. For now, we are not checking for extact match of methods
|
|
|
|
// in category implementation and its primary class's super class.
|
2012-02-10 05:30:24 +08:00
|
|
|
if (!WarnCategoryMethodImpl && I->getSuperClass())
|
2009-05-02 04:07:12 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
2009-09-09 23:08:12 +08:00
|
|
|
IMPDecl,
|
2009-05-02 04:07:12 +08:00
|
|
|
I->getSuperClass(), IncompleteImpl, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 07:19:50 +08:00
|
|
|
/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
|
|
|
|
/// category matches with those implemented in its primary class and
|
|
|
|
/// warns each time an exact match is found.
|
|
|
|
void Sema::CheckCategoryVsClassMethodMatches(
|
|
|
|
ObjCCategoryImplDecl *CatIMPDecl) {
|
2013-12-06 04:52:31 +08:00
|
|
|
// Get category's primary class.
|
|
|
|
ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
|
|
|
|
if (!CatDecl)
|
|
|
|
return;
|
|
|
|
ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
|
|
|
|
if (!IDecl)
|
|
|
|
return;
|
|
|
|
ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass();
|
2012-05-27 21:28:52 +08:00
|
|
|
SelectorSet InsMap, ClsMap;
|
2011-07-29 07:19:50 +08:00
|
|
|
|
2014-03-14 03:50:17 +08:00
|
|
|
for (const auto *I : CatIMPDecl->instance_methods()) {
|
|
|
|
Selector Sel = I->getSelector();
|
2013-12-06 04:52:31 +08:00
|
|
|
// When checking for methods implemented in the category, skip over
|
|
|
|
// those declared in category class's super class. This is because
|
|
|
|
// the super class must implement the method.
|
|
|
|
if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true))
|
|
|
|
continue;
|
|
|
|
InsMap.insert(Sel);
|
|
|
|
}
|
2011-07-29 07:19:50 +08:00
|
|
|
|
2014-03-14 04:11:06 +08:00
|
|
|
for (const auto *I : CatIMPDecl->class_methods()) {
|
|
|
|
Selector Sel = I->getSelector();
|
2013-12-06 04:52:31 +08:00
|
|
|
if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false))
|
|
|
|
continue;
|
|
|
|
ClsMap.insert(Sel);
|
|
|
|
}
|
2011-07-29 07:19:50 +08:00
|
|
|
if (InsMap.empty() && ClsMap.empty())
|
|
|
|
return;
|
|
|
|
|
2012-05-27 21:28:52 +08:00
|
|
|
SelectorSet InsMapSeen, ClsMapSeen;
|
2011-07-29 07:19:50 +08:00
|
|
|
bool IncompleteImpl = false;
|
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
|
|
|
CatIMPDecl, IDecl,
|
2012-02-10 05:30:24 +08:00
|
|
|
IncompleteImpl, false,
|
|
|
|
true /*WarnCategoryMethodImpl*/);
|
2011-07-29 07:19:50 +08:00
|
|
|
}
|
2011-07-25 04:53:26 +08:00
|
|
|
|
2010-05-06 05:52:17 +08:00
|
|
|
void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCContainerDecl* CDecl,
|
2009-03-01 08:56:52 +08:00
|
|
|
bool IncompleteImpl) {
|
2012-05-27 21:28:52 +08:00
|
|
|
SelectorSet InsMap;
|
2007-12-12 15:09:47 +08:00
|
|
|
// Check and see if instance methods in class interface have been
|
|
|
|
// implemented in the implementation class.
|
2014-03-14 03:50:17 +08:00
|
|
|
for (const auto *I : IMPDecl->instance_methods())
|
|
|
|
InsMap.insert(I->getSelector());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Stop back-patching 'readonly' Objective-C properties with 'readwrite' ones.
A 'readonly' Objective-C property declared in the primary class can
effectively be shadowed by a 'readwrite' property declared within an
extension of that class, so long as the types and attributes of the
two property declarations are compatible.
Previously, this functionality was implemented by back-patching the
original 'readonly' property to make it 'readwrite', destroying source
information and causing some hideously redundant, incorrect
code. Simplify the implementation to express how this should actually
be modeled: as a separate property declaration in the extension that
shadows (via the name lookup rules) the declaration in the primary
class. While here, correct some broken Fix-Its, eliminate a pile of
redundant code, clean up the ARC migrator's handling of properties
declared in extensions, and fix debug info's naming of methods that
come from categories.
A wonderous side effect of doing this write is that it eliminates the
"AddedObjCPropertyInClassExtension" method from the AST mutation
listener, which in turn eliminates the last place where we rewrite
entire declarations in a chained PCH file or a module file. This
change (which fixes rdar://problem/18475765) will allow us to
eliminate the rewritten-decls logic from the serialization library,
and fixes a crash (rdar://problem/23247794) illustrated by the
test/PCH/chain-categories.m example.
llvm-svn: 251874
2015-11-03 09:15:46 +08:00
|
|
|
// Add the selectors for getters/setters of @dynamic properties.
|
|
|
|
for (const auto *PImpl : IMPDecl->property_impls()) {
|
|
|
|
// We only care about @dynamic implementations.
|
|
|
|
if (PImpl->getPropertyImplementation() != ObjCPropertyImplDecl::Dynamic)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const auto *P = PImpl->getPropertyDecl();
|
|
|
|
if (!P) continue;
|
|
|
|
|
|
|
|
InsMap.insert(P->getGetterName());
|
|
|
|
if (!P->getSetterName().isNull())
|
|
|
|
InsMap.insert(P->getSetterName());
|
|
|
|
}
|
|
|
|
|
2009-04-15 07:15:21 +08:00
|
|
|
// Check and see if properties declared in the interface have either 1)
|
|
|
|
// an implementation or 2) there is a @synthesize/@dynamic implementation
|
|
|
|
// of the property in the @implementation.
|
2014-02-22 03:41:34 +08:00
|
|
|
if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
|
|
|
|
bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties &&
|
|
|
|
LangOpts.ObjCRuntime.isNonFragile() &&
|
|
|
|
!IDecl->isObjCRequiresPropertyDefs();
|
|
|
|
DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties);
|
|
|
|
}
|
|
|
|
|
2015-06-20 02:14:46 +08:00
|
|
|
// Diagnose null-resettable synthesized setters.
|
|
|
|
diagnoseNullResettableSynthesizedSetters(IMPDecl);
|
|
|
|
|
2012-05-27 21:28:52 +08:00
|
|
|
SelectorSet ClsMap;
|
2014-03-14 04:11:06 +08:00
|
|
|
for (const auto *I : IMPDecl->class_methods())
|
|
|
|
ClsMap.insert(I->getSelector());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-02 04:07:12 +08:00
|
|
|
// Check for type conflict of methods declared in a class/protocol and
|
|
|
|
// its implementation; if any.
|
2012-05-27 21:28:52 +08:00
|
|
|
SelectorSet InsMapSeen, ClsMapSeen;
|
2009-09-09 23:08:12 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
|
|
|
IMPDecl, CDecl,
|
2009-05-02 04:07:12 +08:00
|
|
|
IncompleteImpl, true);
|
2011-08-04 02:21:12 +08:00
|
|
|
|
2011-07-29 07:19:50 +08:00
|
|
|
// check all methods implemented in category against those declared
|
|
|
|
// in its primary class.
|
|
|
|
if (ObjCCategoryImplDecl *CatDecl =
|
|
|
|
dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
|
|
|
|
CheckCategoryVsClassMethodMatches(CatDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// Check the protocol list for unimplemented methods in the @implementation
|
|
|
|
// class.
|
2009-05-02 04:07:12 +08:00
|
|
|
// Check and see if class methods in class interface have been
|
|
|
|
// implemented in the implementation class.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-03-05 16:13:08 +08:00
|
|
|
LazyProtocolNameSet ExplicitImplProtocols;
|
|
|
|
|
2009-03-01 08:56:52 +08:00
|
|
|
if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
|
2014-03-14 04:55:22 +08:00
|
|
|
for (auto *PI : I->all_referenced_protocols())
|
|
|
|
CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl,
|
|
|
|
InsMap, ClsMap, I, ExplicitImplProtocols);
|
2009-03-01 08:56:52 +08:00
|
|
|
} else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
|
2009-10-06 05:32:49 +08:00
|
|
|
// For extended class, unimplemented methods in its protocols will
|
|
|
|
// be reported in the primary class.
|
2010-02-16 05:55:26 +08:00
|
|
|
if (!C->IsClassExtension()) {
|
2014-03-14 20:55:57 +08:00
|
|
|
for (auto *P : C->protocols())
|
|
|
|
CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P,
|
2014-03-05 16:13:08 +08:00
|
|
|
IncompleteImpl, InsMap, ClsMap, CDecl,
|
|
|
|
ExplicitImplProtocols);
|
2014-02-22 03:41:34 +08:00
|
|
|
DiagnoseUnimplementedProperties(S, IMPDecl, CDecl,
|
2014-12-27 11:58:08 +08:00
|
|
|
/*SynthesizeProperties=*/false);
|
2010-01-21 03:36:21 +08:00
|
|
|
}
|
2009-03-01 08:56:52 +08:00
|
|
|
} else
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("invalid ObjCContainerDecl type.");
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2011-08-28 04:50:59 +08:00
|
|
|
Sema::DeclGroupPtrTy
|
2007-12-12 15:09:47 +08:00
|
|
|
Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
|
2009-02-17 03:25:52 +08:00
|
|
|
IdentifierInfo **IdentList,
|
2009-11-18 07:12:20 +08:00
|
|
|
SourceLocation *IdentLocs,
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
ArrayRef<ObjCTypeParamList *> TypeParamLists,
|
2009-02-17 03:25:52 +08:00
|
|
|
unsigned NumElts) {
|
2011-08-28 04:50:59 +08:00
|
|
|
SmallVector<Decl *, 8> DeclsInGroup;
|
2007-12-12 15:09:47 +08:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
// Check for another declaration kind with the same name.
|
2009-10-10 05:13:30 +08:00
|
|
|
NamedDecl *PrevDecl
|
2010-04-16 06:33:43 +08:00
|
|
|
= LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
|
2010-04-16 07:40:53 +08:00
|
|
|
LookupOrdinaryName, ForRedeclaration);
|
2008-01-08 03:49:32 +08:00
|
|
|
if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
|
2008-06-06 06:57:10 +08:00
|
|
|
// GCC apparently allows the following idiom:
|
|
|
|
//
|
|
|
|
// typedef NSObject < XCElementTogglerP > XCElementToggler;
|
|
|
|
// @class XCElementToggler;
|
|
|
|
//
|
2012-01-24 08:40:15 +08:00
|
|
|
// Here we have chosen to ignore the forward class declaration
|
|
|
|
// with a warning. Since this is the implied behavior.
|
2011-04-15 22:24:37 +08:00
|
|
|
TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
|
2010-05-15 19:32:37 +08:00
|
|
|
if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_definition);
|
2010-05-15 19:32:37 +08:00
|
|
|
} else {
|
2009-08-05 05:02:39 +08:00
|
|
|
// a forward class declaration matching a typedef name of a class refers
|
2012-01-24 08:40:15 +08:00
|
|
|
// to the underlying class. Just ignore the forward class with a warning
|
2014-12-27 11:58:08 +08:00
|
|
|
// as this will force the intended behavior which is to lookup the
|
|
|
|
// typedef name.
|
2012-01-24 08:40:15 +08:00
|
|
|
if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
|
2014-12-27 11:58:08 +08:00
|
|
|
Diag(AtClassLoc, diag::warn_forward_class_redefinition)
|
|
|
|
<< IdentList[i];
|
2012-01-24 08:40:15 +08:00
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_definition);
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-08 05:49:26 +08:00
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2011-12-16 04:29:51 +08:00
|
|
|
|
|
|
|
// Create a declaration to describe this forward declaration.
|
2011-12-16 11:12:41 +08:00
|
|
|
ObjCInterfaceDecl *PrevIDecl
|
|
|
|
= dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
|
2013-06-19 05:26:33 +08:00
|
|
|
|
|
|
|
IdentifierInfo *ClassName = IdentList[i];
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
// If this forward declaration has type parameters, compare them with the
|
|
|
|
// type parameters of the previous declaration.
|
|
|
|
ObjCTypeParamList *TypeParams = TypeParamLists[i];
|
|
|
|
if (PrevIDecl && TypeParams) {
|
|
|
|
if (ObjCTypeParamList *PrevTypeParams = PrevIDecl->getTypeParamList()) {
|
|
|
|
// Check for consistency with the previous declaration.
|
|
|
|
if (checkTypeParamListConsistency(
|
|
|
|
*this, PrevTypeParams, TypeParams,
|
|
|
|
TypeParamListContext::ForwardDeclaration)) {
|
|
|
|
TypeParams = nullptr;
|
|
|
|
}
|
|
|
|
} else if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
|
|
|
|
// The @interface does not have type parameters. Complain.
|
|
|
|
Diag(IdentLocs[i], diag::err_objc_parameterized_forward_class)
|
|
|
|
<< ClassName
|
|
|
|
<< TypeParams->getSourceRange();
|
|
|
|
Diag(Def->getLocation(), diag::note_defined_here)
|
|
|
|
<< ClassName;
|
|
|
|
|
|
|
|
TypeParams = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-16 04:29:51 +08:00
|
|
|
ObjCInterfaceDecl *IDecl
|
|
|
|
= ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
|
Parsing, semantic analysis, and AST for Objective-C type parameters.
Produce type parameter declarations for Objective-C type parameters,
and attach lists of type parameters to Objective-C classes,
categories, forward declarations, and extensions as
appropriate. Perform semantic analysis of type bounds for type
parameters, both in isolation and across classes/categories/extensions
to ensure consistency.
Also handle (de-)serialization of Objective-C type parameter lists,
along with sundry other things one must do to add a new declaration to
Clang.
Note that Objective-C type parameters are typedef name declarations,
like typedefs and C++11 type aliases, in support of type erasure.
Part of rdar://problem/6294649.
llvm-svn: 241541
2015-07-07 11:57:15 +08:00
|
|
|
ClassName, TypeParams, PrevIDecl,
|
|
|
|
IdentLocs[i]);
|
2011-12-16 04:29:51 +08:00
|
|
|
IDecl->setAtEndRange(IdentLocs[i]);
|
|
|
|
|
|
|
|
PushOnScopeChains(IDecl, TUScope);
|
2011-12-28 06:43:10 +08:00
|
|
|
CheckObjCDeclScope(IDecl);
|
|
|
|
DeclsInGroup.push_back(IDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2013-07-09 20:05:01 +08:00
|
|
|
|
|
|
|
return BuildDeclaratorGroup(DeclsInGroup, false);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 09:15:19 +08:00
|
|
|
static bool tryMatchRecordTypes(ASTContext &Context,
|
|
|
|
Sema::MethodMatchStrategy strategy,
|
|
|
|
const Type *left, const Type *right);
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
|
|
|
|
QualType leftQT, QualType rightQT) {
|
|
|
|
const Type *left =
|
|
|
|
Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
|
|
|
|
const Type *right =
|
|
|
|
Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
|
|
|
|
|
|
|
|
if (left == right) return true;
|
|
|
|
|
|
|
|
// If we're doing a strict match, the types have to match exactly.
|
|
|
|
if (strategy == Sema::MMS_strict) return false;
|
|
|
|
|
|
|
|
if (left->isIncompleteType() || right->isIncompleteType()) return false;
|
|
|
|
|
|
|
|
// Otherwise, use this absurdly complicated algorithm to try to
|
|
|
|
// validate the basic, low-level compatibility of the two types.
|
|
|
|
|
|
|
|
// As a minimum, require the sizes and alignments to match.
|
2014-07-30 09:30:47 +08:00
|
|
|
TypeInfo LeftTI = Context.getTypeInfo(left);
|
|
|
|
TypeInfo RightTI = Context.getTypeInfo(right);
|
|
|
|
if (LeftTI.Width != RightTI.Width)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (LeftTI.Align != RightTI.Align)
|
2011-06-16 07:02:42 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Consider all the kinds of non-dependent canonical types:
|
|
|
|
// - functions and arrays aren't possible as return and parameter types
|
|
|
|
|
|
|
|
// - vector types of equal size can be arbitrarily mixed
|
|
|
|
if (isa<VectorType>(left)) return isa<VectorType>(right);
|
|
|
|
if (isa<VectorType>(right)) return false;
|
|
|
|
|
|
|
|
// - references should only match references of identical type
|
2011-06-16 09:15:19 +08:00
|
|
|
// - structs, unions, and Objective-C objects must match more-or-less
|
|
|
|
// exactly
|
2011-06-16 07:02:42 +08:00
|
|
|
// - everything else should be a scalar
|
|
|
|
if (!left->isScalarType() || !right->isScalarType())
|
2011-06-16 09:15:19 +08:00
|
|
|
return tryMatchRecordTypes(Context, strategy, left, right);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-09-09 13:25:32 +08:00
|
|
|
// Make scalars agree in kind, except count bools as chars, and group
|
|
|
|
// all non-member pointers together.
|
2011-06-16 07:02:42 +08:00
|
|
|
Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
|
|
|
|
Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
|
|
|
|
if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
|
|
|
|
if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
|
2011-09-09 13:25:32 +08:00
|
|
|
if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
|
|
|
|
leftSK = Type::STK_ObjCObjectPointer;
|
|
|
|
if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
|
|
|
|
rightSK = Type::STK_ObjCObjectPointer;
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
// Note that data member pointers and function member pointers don't
|
|
|
|
// intermix because of the size differences.
|
|
|
|
|
|
|
|
return (leftSK == rightSK);
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
|
2011-06-16 09:15:19 +08:00
|
|
|
static bool tryMatchRecordTypes(ASTContext &Context,
|
|
|
|
Sema::MethodMatchStrategy strategy,
|
|
|
|
const Type *lt, const Type *rt) {
|
|
|
|
assert(lt && rt && lt != rt);
|
|
|
|
|
|
|
|
if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
|
|
|
|
RecordDecl *left = cast<RecordType>(lt)->getDecl();
|
|
|
|
RecordDecl *right = cast<RecordType>(rt)->getDecl();
|
|
|
|
|
|
|
|
// Require union-hood to match.
|
|
|
|
if (left->isUnion() != right->isUnion()) return false;
|
|
|
|
|
|
|
|
// Require an exact match if either is non-POD.
|
|
|
|
if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
|
|
|
|
(isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Require size and alignment to match.
|
2014-07-30 09:30:47 +08:00
|
|
|
TypeInfo LeftTI = Context.getTypeInfo(lt);
|
|
|
|
TypeInfo RightTI = Context.getTypeInfo(rt);
|
|
|
|
if (LeftTI.Width != RightTI.Width)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (LeftTI.Align != RightTI.Align)
|
|
|
|
return false;
|
2011-06-16 09:15:19 +08:00
|
|
|
|
|
|
|
// Require fields to match.
|
|
|
|
RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
|
|
|
|
RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
|
|
|
|
for (; li != le && ri != re; ++li, ++ri) {
|
|
|
|
if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (li == le && ri == re);
|
|
|
|
}
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
|
|
|
|
/// returns true, or false, accordingly.
|
|
|
|
/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
|
2011-06-16 07:02:42 +08:00
|
|
|
bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
|
|
|
|
const ObjCMethodDecl *right,
|
|
|
|
MethodMatchStrategy strategy) {
|
2014-01-26 00:55:45 +08:00
|
|
|
if (!matchTypes(Context, strategy, left->getReturnType(),
|
|
|
|
right->getReturnType()))
|
2011-06-16 07:02:42 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-02-08 03:13:24 +08:00
|
|
|
// If either is hidden, it is not considered to match.
|
|
|
|
if (left->isHidden() || right->isHidden())
|
|
|
|
return false;
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
2011-06-16 07:02:42 +08:00
|
|
|
(left->hasAttr<NSReturnsRetainedAttr>()
|
|
|
|
!= right->hasAttr<NSReturnsRetainedAttr>() ||
|
|
|
|
left->hasAttr<NSConsumesSelfAttr>()
|
|
|
|
!= right->hasAttr<NSConsumesSelfAttr>()))
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-10-03 14:37:04 +08:00
|
|
|
ObjCMethodDecl::param_const_iterator
|
2012-05-18 07:13:29 +08:00
|
|
|
li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
|
|
|
|
re = right->param_end();
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2012-05-18 07:13:29 +08:00
|
|
|
for (; li != le && ri != re; ++li, ++ri) {
|
2011-06-16 07:02:42 +08:00
|
|
|
assert(ri != right->param_end() && "Param mismatch");
|
2011-10-03 14:37:04 +08:00
|
|
|
const ParmVarDecl *lparm = *li, *rparm = *ri;
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
|
|
|
|
return false;
|
|
|
|
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount &&
|
2011-06-16 07:02:42 +08:00
|
|
|
lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
|
|
|
|
return false;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-10 02:59:48 +08:00
|
|
|
static bool isMethodContextSameForKindofLookup(ObjCMethodDecl *Method,
|
|
|
|
ObjCMethodDecl *MethodInList) {
|
|
|
|
auto *MethodProtocol = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext());
|
|
|
|
auto *MethodInListProtocol =
|
|
|
|
dyn_cast<ObjCProtocolDecl>(MethodInList->getDeclContext());
|
|
|
|
// If this method belongs to a protocol but the method in list does not, or
|
|
|
|
// vice versa, we say the context is not the same.
|
|
|
|
if ((MethodProtocol && !MethodInListProtocol) ||
|
|
|
|
(!MethodProtocol && MethodInListProtocol))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (MethodProtocol && MethodInListProtocol)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
ObjCInterfaceDecl *MethodInterface = Method->getClassInterface();
|
|
|
|
ObjCInterfaceDecl *MethodInListInterface =
|
|
|
|
MethodInList->getClassInterface();
|
|
|
|
return MethodInterface == MethodInListInterface;
|
|
|
|
}
|
|
|
|
|
2014-12-27 11:58:08 +08:00
|
|
|
void Sema::addMethodToGlobalList(ObjCMethodList *List,
|
|
|
|
ObjCMethodDecl *Method) {
|
2013-04-17 08:08:58 +08:00
|
|
|
// Record at the head of the list whether there were 0, 1, or >= 2 methods
|
|
|
|
// inside categories.
|
2014-12-27 11:58:08 +08:00
|
|
|
if (ObjCCategoryDecl *CD =
|
|
|
|
dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
|
2013-04-27 08:10:12 +08:00
|
|
|
if (!CD->IsClassExtension() && List->getBits() < 2)
|
2014-12-27 11:58:08 +08:00
|
|
|
List->setBits(List->getBits() + 1);
|
2013-04-17 08:08:58 +08:00
|
|
|
|
2012-01-25 08:19:56 +08:00
|
|
|
// If the list is empty, make it a singleton list.
|
2014-12-27 11:58:08 +08:00
|
|
|
if (List->getMethod() == nullptr) {
|
|
|
|
List->setMethod(Method);
|
2014-05-26 14:22:03 +08:00
|
|
|
List->setNext(nullptr);
|
2012-05-02 07:37:00 +08:00
|
|
|
return;
|
2012-01-25 08:19:56 +08:00
|
|
|
}
|
2014-12-27 11:58:08 +08:00
|
|
|
|
2012-01-25 08:19:56 +08:00
|
|
|
// We've seen a method with this name, see if we have already seen this type
|
|
|
|
// signature.
|
|
|
|
ObjCMethodList *Previous = List;
|
2016-04-14 07:43:56 +08:00
|
|
|
ObjCMethodList *ListWithSameDeclaration = nullptr;
|
2013-04-17 08:08:58 +08:00
|
|
|
for (; List; Previous = List, List = List->getNext()) {
|
2013-06-21 08:20:25 +08:00
|
|
|
// If we are building a module, keep all of the methods.
|
2016-02-20 06:25:36 +08:00
|
|
|
if (getLangOpts().CompilingModule)
|
2013-06-21 08:20:25 +08:00
|
|
|
continue;
|
|
|
|
|
2016-04-14 07:43:56 +08:00
|
|
|
bool SameDeclaration = MatchTwoMethodDeclarations(Method,
|
|
|
|
List->getMethod());
|
2016-04-10 02:59:48 +08:00
|
|
|
// Looking for method with a type bound requires the correct context exists.
|
2016-04-14 07:43:56 +08:00
|
|
|
// We need to insert a method into the list if the context is different.
|
|
|
|
// If the method's declaration matches the list
|
|
|
|
// a> the method belongs to a different context: we need to insert it, in
|
|
|
|
// order to emit the availability message, we need to prioritize over
|
|
|
|
// availability among the methods with the same declaration.
|
|
|
|
// b> the method belongs to the same context: there is no need to insert a
|
|
|
|
// new entry.
|
|
|
|
// If the method's declaration does not match the list, we insert it to the
|
|
|
|
// end.
|
|
|
|
if (!SameDeclaration ||
|
2016-04-10 02:59:48 +08:00
|
|
|
!isMethodContextSameForKindofLookup(Method, List->getMethod())) {
|
2015-04-08 00:56:27 +08:00
|
|
|
// Even if two method types do not match, we would like to say
|
|
|
|
// there is more than one declaration so unavailability/deprecated
|
|
|
|
// warning is not too noisy.
|
|
|
|
if (!Method->isDefined())
|
|
|
|
List->setHasMoreThanOneDecl(true);
|
2016-04-14 07:43:56 +08:00
|
|
|
|
|
|
|
// For methods with the same declaration, the one that is deprecated
|
|
|
|
// should be put in the front for better diagnostics.
|
|
|
|
if (Method->isDeprecated() && SameDeclaration &&
|
|
|
|
!ListWithSameDeclaration && !List->getMethod()->isDeprecated())
|
|
|
|
ListWithSameDeclaration = List;
|
|
|
|
|
|
|
|
if (Method->isUnavailable() && SameDeclaration &&
|
|
|
|
!ListWithSameDeclaration &&
|
|
|
|
List->getMethod()->getAvailability() < AR_Deprecated)
|
|
|
|
ListWithSameDeclaration = List;
|
2012-01-25 08:19:56 +08:00
|
|
|
continue;
|
2015-04-08 00:56:27 +08:00
|
|
|
}
|
2014-12-27 11:58:08 +08:00
|
|
|
|
|
|
|
ObjCMethodDecl *PrevObjCMethod = List->getMethod();
|
2012-01-25 08:19:56 +08:00
|
|
|
|
|
|
|
// Propagate the 'defined' bit.
|
|
|
|
if (Method->isDefined())
|
|
|
|
PrevObjCMethod->setDefined(true);
|
2014-12-27 15:09:37 +08:00
|
|
|
else {
|
2014-12-27 11:58:08 +08:00
|
|
|
// Objective-C doesn't allow an @interface for a class after its
|
|
|
|
// @implementation. So if Method is not defined and there already is
|
|
|
|
// an entry for this type signature, Method has to be for a different
|
|
|
|
// class than PrevObjCMethod.
|
|
|
|
List->setHasMoreThanOneDecl(true);
|
|
|
|
}
|
|
|
|
|
2012-01-25 08:19:56 +08:00
|
|
|
// If a method is deprecated, push it in the global pool.
|
|
|
|
// This is used for better diagnostics.
|
|
|
|
if (Method->isDeprecated()) {
|
|
|
|
if (!PrevObjCMethod->isDeprecated())
|
2014-12-27 11:58:08 +08:00
|
|
|
List->setMethod(Method);
|
2012-01-25 08:19:56 +08:00
|
|
|
}
|
2014-12-27 11:58:08 +08:00
|
|
|
// If the new method is unavailable, push it into global pool
|
2012-01-25 08:19:56 +08:00
|
|
|
// unless previous one is deprecated.
|
|
|
|
if (Method->isUnavailable()) {
|
|
|
|
if (PrevObjCMethod->getAvailability() < AR_Deprecated)
|
2014-12-27 11:58:08 +08:00
|
|
|
List->setMethod(Method);
|
2012-01-25 08:19:56 +08:00
|
|
|
}
|
2014-12-27 11:58:08 +08:00
|
|
|
|
2012-05-02 07:37:00 +08:00
|
|
|
return;
|
2012-01-25 08:19:56 +08:00
|
|
|
}
|
2014-12-27 11:58:08 +08:00
|
|
|
|
2012-01-25 08:19:56 +08:00
|
|
|
// We have a new signature for an existing method - add it.
|
|
|
|
// This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
|
2012-01-25 08:49:42 +08:00
|
|
|
ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
|
2016-04-10 02:59:48 +08:00
|
|
|
|
2016-04-14 07:43:56 +08:00
|
|
|
// We insert it right before ListWithSameDeclaration.
|
|
|
|
if (ListWithSameDeclaration) {
|
|
|
|
auto *List = new (Mem) ObjCMethodList(*ListWithSameDeclaration);
|
|
|
|
// FIXME: should we clear the other bits in ListWithSameDeclaration?
|
|
|
|
ListWithSameDeclaration->setMethod(Method);
|
|
|
|
ListWithSameDeclaration->setNext(List);
|
2016-04-10 02:59:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-27 11:58:08 +08:00
|
|
|
Previous->setNext(new (Mem) ObjCMethodList(Method));
|
2012-01-25 08:19:56 +08:00
|
|
|
}
|
|
|
|
|
2010-08-03 07:18:59 +08:00
|
|
|
/// \brief Read the contents of the method pool for a given selector from
|
|
|
|
/// external storage.
|
2012-01-25 08:49:42 +08:00
|
|
|
void Sema::ReadMethodPool(Selector Sel) {
|
2009-04-25 05:10:55 +08:00
|
|
|
assert(ExternalSource && "We need an external AST source");
|
2012-01-25 08:49:42 +08:00
|
|
|
ExternalSource->ReadMethodPool(Sel);
|
2009-04-25 05:10:55 +08:00
|
|
|
}
|
|
|
|
|
2016-04-30 03:04:05 +08:00
|
|
|
void Sema::updateOutOfDateSelector(Selector Sel) {
|
|
|
|
if (!ExternalSource)
|
|
|
|
return;
|
|
|
|
ExternalSource->updateOutOfDateSelector(Sel);
|
|
|
|
}
|
|
|
|
|
2012-05-02 07:37:00 +08:00
|
|
|
void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
|
2010-08-03 07:18:59 +08:00
|
|
|
bool instance) {
|
2012-03-13 02:34:26 +08:00
|
|
|
// Ignore methods of invalid containers.
|
|
|
|
if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
|
2012-05-02 07:37:00 +08:00
|
|
|
return;
|
2012-03-13 02:34:26 +08:00
|
|
|
|
2012-01-25 08:59:09 +08:00
|
|
|
if (ExternalSource)
|
|
|
|
ReadMethodPool(Method->getSelector());
|
|
|
|
|
2010-08-03 07:18:59 +08:00
|
|
|
GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
|
2012-01-25 08:59:09 +08:00
|
|
|
if (Pos == MethodPool.end())
|
|
|
|
Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
|
|
|
|
GlobalMethods())).first;
|
2014-12-27 11:58:08 +08:00
|
|
|
|
2010-07-23 02:24:20 +08:00
|
|
|
Method->setDefined(impl);
|
2012-01-25 08:19:56 +08:00
|
|
|
|
2010-08-03 07:18:59 +08:00
|
|
|
ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
|
2012-05-02 07:37:00 +08:00
|
|
|
addMethodToGlobalList(&Entry, Method);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
/// Determines if this is an "acceptable" loose mismatch in the global
|
|
|
|
/// method pool. This exists mostly as a hack to get around certain
|
|
|
|
/// global mismatches which we can't afford to make warnings / errors.
|
|
|
|
/// Really, what we want is a way to take a method out of the global
|
|
|
|
/// method pool.
|
|
|
|
static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
|
|
|
|
ObjCMethodDecl *other) {
|
|
|
|
if (!chosen->isInstanceMethod())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Selector sel = chosen->getSelector();
|
|
|
|
if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Don't complain about mismatches for -length if the method we
|
|
|
|
// chose has an integral result type.
|
2014-01-26 00:55:45 +08:00
|
|
|
return (chosen->getReturnType()->isIntegerType());
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
|
2016-04-08 03:32:24 +08:00
|
|
|
/// Return true if the given method is wthin the type bound.
|
|
|
|
static bool FilterMethodsByTypeBound(ObjCMethodDecl *Method,
|
|
|
|
const ObjCObjectType *TypeBound) {
|
|
|
|
if (!TypeBound)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (TypeBound->isObjCId())
|
|
|
|
// FIXME: should we handle the case of bounding to id<A, B> differently?
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto *BoundInterface = TypeBound->getInterface();
|
|
|
|
assert(BoundInterface && "unexpected object type!");
|
|
|
|
|
|
|
|
// Check if the Method belongs to a protocol. We should allow any method
|
|
|
|
// defined in any protocol, because any subclass could adopt the protocol.
|
|
|
|
auto *MethodProtocol = dyn_cast<ObjCProtocolDecl>(Method->getDeclContext());
|
|
|
|
if (MethodProtocol) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the Method belongs to a class, check if it belongs to the class
|
|
|
|
// hierarchy of the class bound.
|
|
|
|
if (ObjCInterfaceDecl *MethodInterface = Method->getClassInterface()) {
|
|
|
|
// We allow methods declared within classes that are part of the hierarchy
|
|
|
|
// of the class bound (superclass of, subclass of, or the same as the class
|
|
|
|
// bound).
|
|
|
|
return MethodInterface == BoundInterface ||
|
|
|
|
MethodInterface->isSuperClassOf(BoundInterface) ||
|
|
|
|
BoundInterface->isSuperClassOf(MethodInterface);
|
|
|
|
}
|
|
|
|
llvm_unreachable("unknow method context");
|
|
|
|
}
|
|
|
|
|
2016-04-08 03:30:20 +08:00
|
|
|
/// We first select the type of the method: Instance or Factory, then collect
|
|
|
|
/// all methods with that type.
|
2014-12-27 11:58:08 +08:00
|
|
|
bool Sema::CollectMultipleMethodsInGlobalPool(
|
2016-04-08 03:30:20 +08:00
|
|
|
Selector Sel, SmallVectorImpl<ObjCMethodDecl *> &Methods,
|
2016-04-08 03:32:24 +08:00
|
|
|
bool InstanceFirst, bool CheckTheOther,
|
|
|
|
const ObjCObjectType *TypeBound) {
|
2014-08-14 05:07:35 +08:00
|
|
|
if (ExternalSource)
|
|
|
|
ReadMethodPool(Sel);
|
|
|
|
|
|
|
|
GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
|
|
|
|
if (Pos == MethodPool.end())
|
|
|
|
return false;
|
2016-04-08 03:30:20 +08:00
|
|
|
|
2014-08-14 05:07:35 +08:00
|
|
|
// Gather the non-hidden methods.
|
2016-04-08 03:30:20 +08:00
|
|
|
ObjCMethodList &MethList = InstanceFirst ? Pos->second.first :
|
|
|
|
Pos->second.second;
|
2014-08-14 05:07:35 +08:00
|
|
|
for (ObjCMethodList *M = &MethList; M; M = M->getNext())
|
2016-04-08 03:32:24 +08:00
|
|
|
if (M->getMethod() && !M->getMethod()->isHidden()) {
|
|
|
|
if (FilterMethodsByTypeBound(M->getMethod(), TypeBound))
|
|
|
|
Methods.push_back(M->getMethod());
|
|
|
|
}
|
2016-04-08 03:30:20 +08:00
|
|
|
|
|
|
|
// Return if we find any method with the desired kind.
|
|
|
|
if (!Methods.empty())
|
|
|
|
return Methods.size() > 1;
|
|
|
|
|
|
|
|
if (!CheckTheOther)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Gather the other kind.
|
|
|
|
ObjCMethodList &MethList2 = InstanceFirst ? Pos->second.second :
|
|
|
|
Pos->second.first;
|
|
|
|
for (ObjCMethodList *M = &MethList2; M; M = M->getNext())
|
2016-04-08 03:32:24 +08:00
|
|
|
if (M->getMethod() && !M->getMethod()->isHidden()) {
|
|
|
|
if (FilterMethodsByTypeBound(M->getMethod(), TypeBound))
|
|
|
|
Methods.push_back(M->getMethod());
|
|
|
|
}
|
2016-04-08 03:30:20 +08:00
|
|
|
|
2014-12-27 11:58:08 +08:00
|
|
|
return Methods.size() > 1;
|
2014-08-14 05:07:35 +08:00
|
|
|
}
|
|
|
|
|
2016-04-08 03:30:20 +08:00
|
|
|
bool Sema::AreMultipleMethodsInGlobalPool(
|
|
|
|
Selector Sel, ObjCMethodDecl *BestMethod, SourceRange R,
|
|
|
|
bool receiverIdOrClass, SmallVectorImpl<ObjCMethodDecl *> &Methods) {
|
|
|
|
// Diagnose finding more than one method in global pool.
|
|
|
|
SmallVector<ObjCMethodDecl *, 4> FilteredMethods;
|
|
|
|
FilteredMethods.push_back(BestMethod);
|
|
|
|
|
|
|
|
for (auto *M : Methods)
|
|
|
|
if (M != BestMethod && !M->hasAttr<UnavailableAttr>())
|
|
|
|
FilteredMethods.push_back(M);
|
|
|
|
|
|
|
|
if (FilteredMethods.size() > 1)
|
|
|
|
DiagnoseMultipleMethodInGlobalPool(FilteredMethods, Sel, R,
|
|
|
|
receiverIdOrClass);
|
|
|
|
|
2014-11-14 06:27:05 +08:00
|
|
|
GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
|
2014-12-27 11:58:08 +08:00
|
|
|
// Test for no method in the pool which should not trigger any warning by
|
|
|
|
// caller.
|
2014-11-14 06:27:05 +08:00
|
|
|
if (Pos == MethodPool.end())
|
|
|
|
return true;
|
2015-04-16 01:26:21 +08:00
|
|
|
ObjCMethodList &MethList =
|
|
|
|
BestMethod->isInstanceMethod() ? Pos->second.first : Pos->second.second;
|
2014-12-27 11:58:08 +08:00
|
|
|
return MethList.hasMoreThanOneDecl();
|
2014-11-14 06:27:05 +08:00
|
|
|
}
|
|
|
|
|
2010-08-03 07:18:59 +08:00
|
|
|
ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
|
2010-08-10 07:27:58 +08:00
|
|
|
bool receiverIdOrClass,
|
2015-04-16 01:26:21 +08:00
|
|
|
bool instance) {
|
2012-01-25 08:59:09 +08:00
|
|
|
if (ExternalSource)
|
|
|
|
ReadMethodPool(Sel);
|
|
|
|
|
2010-08-03 07:18:59 +08:00
|
|
|
GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
|
2012-01-25 08:59:09 +08:00
|
|
|
if (Pos == MethodPool.end())
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2009-04-25 05:10:55 +08:00
|
|
|
|
2013-01-17 02:47:38 +08:00
|
|
|
// Gather the non-hidden methods.
|
2010-08-03 07:18:59 +08:00
|
|
|
ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
|
2013-08-10 20:33:24 +08:00
|
|
|
SmallVector<ObjCMethodDecl *, 4> Methods;
|
2013-04-17 08:08:58 +08:00
|
|
|
for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
|
2015-04-16 01:26:21 +08:00
|
|
|
if (M->getMethod() && !M->getMethod()->isHidden())
|
|
|
|
return M->getMethod();
|
2013-01-17 02:47:38 +08:00
|
|
|
}
|
2015-04-16 01:26:21 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2015-04-16 01:26:21 +08:00
|
|
|
void Sema::DiagnoseMultipleMethodInGlobalPool(SmallVectorImpl<ObjCMethodDecl*> &Methods,
|
|
|
|
Selector Sel, SourceRange R,
|
|
|
|
bool receiverIdOrClass) {
|
2013-01-17 02:47:38 +08:00
|
|
|
// We found multiple methods, so we may have to complain.
|
|
|
|
bool issueDiagnostic = false, issueError = false;
|
2015-04-29 02:04:44 +08:00
|
|
|
|
2013-01-17 02:47:38 +08:00
|
|
|
// We support a warning which complains about *any* difference in
|
|
|
|
// method signature.
|
|
|
|
bool strictSelectorMatch =
|
2015-04-16 01:26:21 +08:00
|
|
|
receiverIdOrClass &&
|
|
|
|
!Diags.isIgnored(diag::warn_strict_multiple_method_decl, R.getBegin());
|
2013-01-17 02:47:38 +08:00
|
|
|
if (strictSelectorMatch) {
|
|
|
|
for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
|
|
|
|
if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
|
|
|
|
issueDiagnostic = true;
|
|
|
|
break;
|
2010-08-10 07:27:58 +08:00
|
|
|
}
|
2013-01-17 02:47:38 +08:00
|
|
|
}
|
|
|
|
}
|
2015-04-29 02:04:44 +08:00
|
|
|
|
2013-01-17 02:47:38 +08:00
|
|
|
// If we didn't see any strict differences, we won't see any loose
|
|
|
|
// differences. In ARC, however, we also need to check for loose
|
|
|
|
// mismatches, because most of them are errors.
|
|
|
|
if (!strictSelectorMatch ||
|
|
|
|
(issueDiagnostic && getLangOpts().ObjCAutoRefCount))
|
|
|
|
for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
|
|
|
|
// This checks if the methods differ in type mismatch.
|
|
|
|
if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
|
|
|
|
!isAcceptableMethodMismatch(Methods[0], Methods[I])) {
|
|
|
|
issueDiagnostic = true;
|
|
|
|
if (getLangOpts().ObjCAutoRefCount)
|
|
|
|
issueError = true;
|
|
|
|
break;
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2015-04-16 01:26:21 +08:00
|
|
|
|
2013-01-17 02:47:38 +08:00
|
|
|
if (issueDiagnostic) {
|
|
|
|
if (issueError)
|
|
|
|
Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
|
|
|
|
else if (strictSelectorMatch)
|
|
|
|
Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
|
|
|
|
else
|
|
|
|
Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
|
2015-04-16 01:26:21 +08:00
|
|
|
|
2013-01-17 02:47:38 +08:00
|
|
|
Diag(Methods[0]->getLocStart(),
|
|
|
|
issueError ? diag::note_possibility : diag::note_using)
|
2015-04-16 01:26:21 +08:00
|
|
|
<< Methods[0]->getSourceRange();
|
2013-01-17 02:47:38 +08:00
|
|
|
for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
|
|
|
|
Diag(Methods[I]->getLocStart(), diag::note_also_found)
|
2015-04-16 01:26:21 +08:00
|
|
|
<< Methods[I]->getSourceRange();
|
|
|
|
}
|
2009-04-25 05:10:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-23 02:24:20 +08:00
|
|
|
ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
|
2010-08-03 07:18:59 +08:00
|
|
|
GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
|
|
|
|
if (Pos == MethodPool.end())
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-08-03 07:18:59 +08:00
|
|
|
|
|
|
|
GlobalMethods &Methods = Pos->second;
|
2014-03-27 04:59:26 +08:00
|
|
|
for (const ObjCMethodList *Method = &Methods.first; Method;
|
|
|
|
Method = Method->getNext())
|
2015-02-20 05:52:41 +08:00
|
|
|
if (Method->getMethod() &&
|
|
|
|
(Method->getMethod()->isDefined() ||
|
|
|
|
Method->getMethod()->isPropertyAccessor()))
|
2014-12-27 11:58:08 +08:00
|
|
|
return Method->getMethod();
|
2014-03-27 04:59:26 +08:00
|
|
|
|
|
|
|
for (const ObjCMethodList *Method = &Methods.second; Method;
|
|
|
|
Method = Method->getNext())
|
2015-02-20 05:52:41 +08:00
|
|
|
if (Method->getMethod() &&
|
|
|
|
(Method->getMethod()->isDefined() ||
|
|
|
|
Method->getMethod()->isPropertyAccessor()))
|
2014-12-27 11:58:08 +08:00
|
|
|
return Method->getMethod();
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2010-07-23 02:24:20 +08:00
|
|
|
}
|
|
|
|
|
2013-06-06 02:46:14 +08:00
|
|
|
static void
|
|
|
|
HelperSelectorsForTypoCorrection(
|
|
|
|
SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
|
|
|
|
StringRef Typo, const ObjCMethodDecl * Method) {
|
|
|
|
const unsigned MaxEditDistance = 1;
|
|
|
|
unsigned BestEditDistance = MaxEditDistance + 1;
|
2013-06-06 10:22:29 +08:00
|
|
|
std::string MethodName = Method->getSelector().getAsString();
|
2013-06-06 02:46:14 +08:00
|
|
|
|
|
|
|
unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
|
|
|
|
if (MinPossibleEditDistance > 0 &&
|
|
|
|
Typo.size() / MinPossibleEditDistance < 1)
|
|
|
|
return;
|
|
|
|
unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
|
|
|
|
if (EditDistance > MaxEditDistance)
|
|
|
|
return;
|
|
|
|
if (EditDistance == BestEditDistance)
|
|
|
|
BestMethod.push_back(Method);
|
|
|
|
else if (EditDistance < BestEditDistance) {
|
|
|
|
BestMethod.clear();
|
|
|
|
BestMethod.push_back(Method);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 01:10:54 +08:00
|
|
|
static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
|
|
|
|
QualType ObjectType) {
|
|
|
|
if (ObjectType.isNull())
|
|
|
|
return true;
|
|
|
|
if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
|
|
|
|
return true;
|
2014-05-26 14:22:03 +08:00
|
|
|
return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) !=
|
|
|
|
nullptr;
|
2013-06-18 01:10:54 +08:00
|
|
|
}
|
|
|
|
|
2013-06-06 02:46:14 +08:00
|
|
|
const ObjCMethodDecl *
|
2013-06-18 01:10:54 +08:00
|
|
|
Sema::SelectorsForTypoCorrection(Selector Sel,
|
|
|
|
QualType ObjectType) {
|
2013-06-06 02:46:14 +08:00
|
|
|
unsigned NumArgs = Sel.getNumArgs();
|
|
|
|
SmallVector<const ObjCMethodDecl *, 8> Methods;
|
2013-06-18 23:31:36 +08:00
|
|
|
bool ObjectIsId = true, ObjectIsClass = true;
|
|
|
|
if (ObjectType.isNull())
|
|
|
|
ObjectIsId = ObjectIsClass = false;
|
|
|
|
else if (!ObjectType->isObjCObjectPointerType())
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2013-06-18 23:31:36 +08:00
|
|
|
else if (const ObjCObjectPointerType *ObjCPtr =
|
|
|
|
ObjectType->getAsObjCInterfacePointerType()) {
|
|
|
|
ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
|
|
|
|
ObjectIsId = ObjectIsClass = false;
|
|
|
|
}
|
|
|
|
else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
|
|
|
|
ObjectIsClass = false;
|
|
|
|
else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
|
|
|
|
ObjectIsId = false;
|
|
|
|
else
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2013-06-06 02:46:14 +08:00
|
|
|
for (GlobalMethodPool::iterator b = MethodPool.begin(),
|
|
|
|
e = MethodPool.end(); b != e; b++) {
|
|
|
|
// instance methods
|
|
|
|
for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
|
2014-12-27 11:58:08 +08:00
|
|
|
if (M->getMethod() &&
|
|
|
|
(M->getMethod()->getSelector().getNumArgs() == NumArgs) &&
|
|
|
|
(M->getMethod()->getSelector() != Sel)) {
|
2013-06-18 23:31:36 +08:00
|
|
|
if (ObjectIsId)
|
2014-12-27 11:58:08 +08:00
|
|
|
Methods.push_back(M->getMethod());
|
2013-06-18 23:31:36 +08:00
|
|
|
else if (!ObjectIsClass &&
|
2014-12-27 11:58:08 +08:00
|
|
|
HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(),
|
|
|
|
ObjectType))
|
|
|
|
Methods.push_back(M->getMethod());
|
2013-06-18 23:31:36 +08:00
|
|
|
}
|
2013-06-06 02:46:14 +08:00
|
|
|
// class methods
|
|
|
|
for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
|
2014-12-27 11:58:08 +08:00
|
|
|
if (M->getMethod() &&
|
|
|
|
(M->getMethod()->getSelector().getNumArgs() == NumArgs) &&
|
|
|
|
(M->getMethod()->getSelector() != Sel)) {
|
2013-06-18 23:31:36 +08:00
|
|
|
if (ObjectIsClass)
|
2014-12-27 11:58:08 +08:00
|
|
|
Methods.push_back(M->getMethod());
|
2013-06-18 23:31:36 +08:00
|
|
|
else if (!ObjectIsId &&
|
2014-12-27 11:58:08 +08:00
|
|
|
HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(),
|
|
|
|
ObjectType))
|
|
|
|
Methods.push_back(M->getMethod());
|
2013-06-18 23:31:36 +08:00
|
|
|
}
|
2013-06-06 02:46:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
|
|
|
|
for (unsigned i = 0, e = Methods.size(); i < e; i++) {
|
|
|
|
HelperSelectorsForTypoCorrection(SelectedMethods,
|
|
|
|
Sel.getAsString(), Methods[i]);
|
|
|
|
}
|
2014-05-26 14:22:03 +08:00
|
|
|
return (SelectedMethods.size() == 1) ? SelectedMethods[0] : nullptr;
|
2013-06-06 02:46:14 +08:00
|
|
|
}
|
|
|
|
|
2013-05-31 05:48:58 +08:00
|
|
|
/// DiagnoseDuplicateIvars -
|
2010-02-24 07:41:11 +08:00
|
|
|
/// Check for duplicate ivars in the entire class at the start of
|
2012-06-15 05:40:34 +08:00
|
|
|
/// \@implementation. This becomes necesssary because class extension can
|
2010-02-24 07:41:11 +08:00
|
|
|
/// add ivars to a class in random order which will not be known until
|
2012-06-15 05:40:34 +08:00
|
|
|
/// class's \@implementation is seen.
|
2010-02-24 07:41:11 +08:00
|
|
|
void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
|
|
|
|
ObjCInterfaceDecl *SID) {
|
2014-03-14 05:09:43 +08:00
|
|
|
for (auto *Ivar : ID->ivars()) {
|
2010-02-24 07:41:11 +08:00
|
|
|
if (Ivar->isInvalidDecl())
|
|
|
|
continue;
|
|
|
|
if (IdentifierInfo *II = Ivar->getIdentifier()) {
|
|
|
|
ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
|
|
|
|
if (prevIvar) {
|
|
|
|
Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
|
|
|
|
Diag(prevIvar->getLocation(), diag::note_previous_declaration);
|
|
|
|
Ivar->setInvalidDecl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-27 12:54:50 +08:00
|
|
|
/// Diagnose attempts to define ARC-__weak ivars when __weak is disabled.
|
|
|
|
static void DiagnoseWeakIvars(Sema &S, ObjCImplementationDecl *ID) {
|
|
|
|
if (S.getLangOpts().ObjCWeak) return;
|
|
|
|
|
|
|
|
for (auto ivar = ID->getClassInterface()->all_declared_ivar_begin();
|
|
|
|
ivar; ivar = ivar->getNextIvar()) {
|
|
|
|
if (ivar->isInvalidDecl()) continue;
|
|
|
|
if (ivar->getType().getObjCLifetime() == Qualifiers::OCL_Weak) {
|
|
|
|
if (S.getLangOpts().ObjCWeakRuntime) {
|
|
|
|
S.Diag(ivar->getLocation(), diag::err_arc_weak_disabled);
|
|
|
|
} else {
|
|
|
|
S.Diag(ivar->getLocation(), diag::err_arc_weak_no_runtime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-06 17:25:23 +08:00
|
|
|
Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
|
|
|
|
switch (CurContext->getDeclKind()) {
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
return Sema::OCK_Interface;
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
return Sema::OCK_Protocol;
|
|
|
|
case Decl::ObjCCategory:
|
2015-04-10 19:37:55 +08:00
|
|
|
if (cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
|
2011-12-06 17:25:23 +08:00
|
|
|
return Sema::OCK_ClassExtension;
|
2015-04-10 19:37:55 +08:00
|
|
|
return Sema::OCK_Category;
|
2011-12-06 17:25:23 +08:00
|
|
|
case Decl::ObjCImplementation:
|
|
|
|
return Sema::OCK_Implementation;
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
return Sema::OCK_CategoryImplementation;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return Sema::OCK_None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-16 23:33:19 +08:00
|
|
|
// Note: For class/category implementations, allMethods is always null.
|
2013-07-18 05:14:35 +08:00
|
|
|
Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods,
|
2013-07-17 08:05:08 +08:00
|
|
|
ArrayRef<DeclGroupPtrTy> allTUVars) {
|
2011-12-06 17:25:23 +08:00
|
|
|
if (getObjCContainerKind() == Sema::OCK_None)
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2011-12-06 17:25:23 +08:00
|
|
|
|
|
|
|
assert(AtEnd.isValid() && "Invalid location for '@end'");
|
2011-08-22 23:54:49 +08:00
|
|
|
|
|
|
|
ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
|
|
|
|
Decl *ClassDecl = cast<Decl>(OCD);
|
2009-11-17 02:57:01 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool isInterfaceDeclKind =
|
2008-03-17 05:17:37 +08:00
|
|
|
isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
|
|
|
|
|| isa<ObjCProtocolDecl>(ClassDecl);
|
2008-01-08 03:49:32 +08:00
|
|
|
bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
|
2009-01-09 23:36:25 +08:00
|
|
|
|
2009-01-09 01:28:14 +08:00
|
|
|
// FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
|
|
|
|
llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
|
|
|
|
llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
|
|
|
|
|
2013-07-16 23:33:19 +08:00
|
|
|
for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCMethodDecl *Method =
|
2010-08-21 17:40:31 +08:00
|
|
|
cast_or_null<ObjCMethodDecl>(allMethods[i]);
|
2007-12-12 15:09:47 +08:00
|
|
|
|
|
|
|
if (!Method) continue; // Already issued a diagnostic.
|
2009-01-10 01:18:27 +08:00
|
|
|
if (Method->isInstanceMethod()) {
|
2007-12-12 15:09:47 +08:00
|
|
|
/// Check for instance method of the same name with incompatible types
|
2008-01-08 03:49:32 +08:00
|
|
|
const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
|
2009-09-09 23:08:12 +08:00
|
|
|
bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
|
2007-12-12 15:09:47 +08:00
|
|
|
: false;
|
2009-09-09 23:08:12 +08:00
|
|
|
if ((isInterfaceDeclKind && PrevMethod && !match)
|
2008-12-17 04:15:50 +08:00
|
|
|
|| (checkIdenticalMethods && match)) {
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(Method->getLocation(), diag::err_duplicate_method_decl)
|
2008-11-24 11:33:13 +08:00
|
|
|
<< Method->getDeclName();
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
|
Fix a major inconsistency in the representation of Objective-C
classes, categories, protocols, and class extensions, where the
methods and properties of these entities would be inserted into the
DeclContext in an ordering that doesn't necessarily reflect source
order. The culprits were Sema::ActOnMethodDeclaration(), which did not
perform the insertion of the just-created method declaration into
the DeclContext for these Objective-C entities, and
Sema::ActOnAtEnd(), which inserted all method declarations at the
*end* of the DeclContext.
With this fix in hand, clean up the code-completion actions for
property setters/getters that worked around this brokenness in the AST.
Fixes <rdar://problem/8062781>, where this problem manifested as poor
token-annotation information, but this would have struck again in many
other places.
llvm-svn: 122347
2010-12-22 01:34:17 +08:00
|
|
|
Method->setInvalidDecl();
|
2007-12-12 15:09:47 +08:00
|
|
|
} else {
|
2011-12-14 03:40:34 +08:00
|
|
|
if (PrevMethod) {
|
2011-10-14 16:02:31 +08:00
|
|
|
Method->setAsRedeclaration(PrevMethod);
|
2011-12-14 03:40:34 +08:00
|
|
|
if (!Context.getSourceManager().isInSystemHeader(
|
|
|
|
Method->getLocation()))
|
|
|
|
Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
|
|
|
|
<< Method->getDeclName();
|
|
|
|
Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
InsMap[Method->getSelector()] = Method;
|
|
|
|
/// The following allows us to typecheck messages to "id".
|
2012-05-02 07:37:00 +08:00
|
|
|
AddInstanceMethodToGlobalPool(Method);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-08-05 05:02:39 +08:00
|
|
|
} else {
|
2007-12-12 15:09:47 +08:00
|
|
|
/// Check for class method of the same name with incompatible types
|
2008-01-08 03:49:32 +08:00
|
|
|
const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
|
2009-09-09 23:08:12 +08:00
|
|
|
bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
|
2007-12-12 15:09:47 +08:00
|
|
|
: false;
|
2009-09-09 23:08:12 +08:00
|
|
|
if ((isInterfaceDeclKind && PrevMethod && !match)
|
2008-12-17 04:15:50 +08:00
|
|
|
|| (checkIdenticalMethods && match)) {
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(Method->getLocation(), diag::err_duplicate_method_decl)
|
2008-11-24 11:33:13 +08:00
|
|
|
<< Method->getDeclName();
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
|
Fix a major inconsistency in the representation of Objective-C
classes, categories, protocols, and class extensions, where the
methods and properties of these entities would be inserted into the
DeclContext in an ordering that doesn't necessarily reflect source
order. The culprits were Sema::ActOnMethodDeclaration(), which did not
perform the insertion of the just-created method declaration into
the DeclContext for these Objective-C entities, and
Sema::ActOnAtEnd(), which inserted all method declarations at the
*end* of the DeclContext.
With this fix in hand, clean up the code-completion actions for
property setters/getters that worked around this brokenness in the AST.
Fixes <rdar://problem/8062781>, where this problem manifested as poor
token-annotation information, but this would have struck again in many
other places.
llvm-svn: 122347
2010-12-22 01:34:17 +08:00
|
|
|
Method->setInvalidDecl();
|
2007-12-12 15:09:47 +08:00
|
|
|
} else {
|
2011-12-14 03:40:34 +08:00
|
|
|
if (PrevMethod) {
|
2011-10-14 16:02:31 +08:00
|
|
|
Method->setAsRedeclaration(PrevMethod);
|
2011-12-14 03:40:34 +08:00
|
|
|
if (!Context.getSourceManager().isInSystemHeader(
|
|
|
|
Method->getLocation()))
|
|
|
|
Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
|
|
|
|
<< Method->getDeclName();
|
|
|
|
Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
ClsMap[Method->getSelector()] = Method;
|
2012-05-02 07:37:00 +08:00
|
|
|
AddFactoryMethodToGlobalPool(Method);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-22 03:42:21 +08:00
|
|
|
if (isa<ObjCInterfaceDecl>(ClassDecl)) {
|
|
|
|
// Nothing to do here.
|
2009-01-09 23:36:25 +08:00
|
|
|
} else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
|
2008-12-07 03:59:02 +08:00
|
|
|
// Categories are used to extend the class by declaring new methods.
|
2009-09-09 23:08:12 +08:00
|
|
|
// By the same token, they are also used to add new properties. No
|
2008-12-07 03:59:02 +08:00
|
|
|
// need to compare the added property to those in the class.
|
2008-08-27 13:40:03 +08:00
|
|
|
|
2010-12-11 07:36:33 +08:00
|
|
|
if (C->IsClassExtension()) {
|
|
|
|
ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
|
|
|
|
DiagnoseClassExtensionDupMethods(C, CCPrimary);
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-01-09 23:36:25 +08:00
|
|
|
if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
|
2010-02-16 05:55:26 +08:00
|
|
|
if (CDecl->getIdentifier())
|
|
|
|
// ProcessPropertyDecl is responsible for diagnosing conflicts with any
|
|
|
|
// user-defined setter/getter. It also synthesizes setter/getter methods
|
|
|
|
// and adds them to the DeclContext and global method pools.
|
2016-01-28 04:00:32 +08:00
|
|
|
for (auto *I : CDecl->properties())
|
2015-11-04 01:02:34 +08:00
|
|
|
ProcessPropertyDecl(I);
|
2010-01-07 09:20:12 +08:00
|
|
|
CDecl->setAtEndRange(AtEnd);
|
2009-01-09 23:36:25 +08:00
|
|
|
}
|
|
|
|
if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
|
2010-01-07 09:20:12 +08:00
|
|
|
IC->setAtEndRange(AtEnd);
|
2009-11-12 06:40:11 +08:00
|
|
|
if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
|
2010-12-12 02:39:37 +08:00
|
|
|
// Any property declared in a class extension might have user
|
|
|
|
// declared setter or getter in current class extension or one
|
|
|
|
// of the other class extensions. Mark them as synthesized as
|
|
|
|
// property will be synthesized when property with same name is
|
|
|
|
// seen in the @implementation.
|
2014-03-14 05:47:07 +08:00
|
|
|
for (const auto *Ext : IDecl->visible_extensions()) {
|
2016-01-27 02:05:23 +08:00
|
|
|
for (const auto *Property : Ext->instance_properties()) {
|
2010-12-12 02:39:37 +08:00
|
|
|
// Skip over properties declared @dynamic
|
|
|
|
if (const ObjCPropertyImplDecl *PIDecl
|
2016-01-29 02:49:28 +08:00
|
|
|
= IC->FindPropertyImplDecl(Property->getIdentifier(),
|
|
|
|
Property->getQueryKind()))
|
2010-12-12 02:39:37 +08:00
|
|
|
if (PIDecl->getPropertyImplementation()
|
|
|
|
== ObjCPropertyImplDecl::Dynamic)
|
|
|
|
continue;
|
2013-01-17 07:00:23 +08:00
|
|
|
|
2014-03-14 05:47:07 +08:00
|
|
|
for (const auto *Ext : IDecl->visible_extensions()) {
|
2013-01-17 07:00:23 +08:00
|
|
|
if (ObjCMethodDecl *GetterMethod
|
|
|
|
= Ext->getInstanceMethod(Property->getGetterName()))
|
2012-10-11 00:42:25 +08:00
|
|
|
GetterMethod->setPropertyAccessor(true);
|
2010-12-12 02:39:37 +08:00
|
|
|
if (!Property->isReadOnly())
|
2013-01-17 07:00:23 +08:00
|
|
|
if (ObjCMethodDecl *SetterMethod
|
|
|
|
= Ext->getInstanceMethod(Property->getSetterName()))
|
2012-10-11 00:42:25 +08:00
|
|
|
SetterMethod->setPropertyAccessor(true);
|
2013-01-17 07:00:23 +08:00
|
|
|
}
|
2010-12-12 02:39:37 +08:00
|
|
|
}
|
|
|
|
}
|
2010-05-06 05:52:17 +08:00
|
|
|
ImplMethodsVsClassMethods(S, IC, IDecl);
|
2009-11-12 06:40:11 +08:00
|
|
|
AtomicPropertySetterGetterRules(IC, IDecl);
|
2011-06-16 07:02:42 +08:00
|
|
|
DiagnoseOwningPropertyGetterSynthesis(IC);
|
2014-01-04 02:32:18 +08:00
|
|
|
DiagnoseUnusedBackingIvarInAccessor(S, IC);
|
2015-03-12 00:59:48 +08:00
|
|
|
if (IDecl->hasDesignatedInitializers())
|
|
|
|
DiagnoseMissingDesignatedInitOverrides(IC, IDecl);
|
2015-10-27 12:54:50 +08:00
|
|
|
DiagnoseWeakIvars(*this, IC);
|
2013-12-04 05:11:54 +08:00
|
|
|
|
2012-04-07 02:12:22 +08:00
|
|
|
bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
|
2014-05-26 14:22:03 +08:00
|
|
|
if (IDecl->getSuperClass() == nullptr) {
|
2012-04-07 02:12:22 +08:00
|
|
|
// This class has no superclass, so check that it has been marked with
|
|
|
|
// __attribute((objc_root_class)).
|
|
|
|
if (!HasRootClassAttr) {
|
|
|
|
SourceLocation DeclLoc(IDecl->getLocation());
|
2014-05-03 11:45:55 +08:00
|
|
|
SourceLocation SuperClassLoc(getLocForEndOfToken(DeclLoc));
|
2012-04-07 02:12:22 +08:00
|
|
|
Diag(DeclLoc, diag::warn_objc_root_class_missing)
|
|
|
|
<< IDecl->getIdentifier();
|
|
|
|
// See if NSObject is in the current scope, and if it is, suggest
|
|
|
|
// adding " : NSObject " to the class declaration.
|
|
|
|
NamedDecl *IF = LookupSingleName(TUScope,
|
|
|
|
NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
|
|
|
|
DeclLoc, LookupOrdinaryName);
|
|
|
|
ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
|
|
|
|
if (NSObjectDecl && NSObjectDecl->getDefinition()) {
|
|
|
|
Diag(SuperClassLoc, diag::note_objc_needs_superclass)
|
|
|
|
<< FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
|
|
|
|
} else {
|
|
|
|
Diag(SuperClassLoc, diag::note_objc_needs_superclass);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (HasRootClassAttr) {
|
|
|
|
// Complain that only root classes may have this attribute.
|
|
|
|
Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
|
|
|
|
}
|
|
|
|
|
2012-06-20 14:18:46 +08:00
|
|
|
if (LangOpts.ObjCRuntime.isNonFragile()) {
|
2010-02-24 07:41:11 +08:00
|
|
|
while (IDecl->getSuperClass()) {
|
|
|
|
DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
|
|
|
|
IDecl = IDecl->getSuperClass();
|
|
|
|
}
|
2012-04-07 02:12:22 +08:00
|
|
|
}
|
2009-11-12 06:40:11 +08:00
|
|
|
}
|
2010-04-29 00:11:27 +08:00
|
|
|
SetIvarInitializers(IC);
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (ObjCCategoryImplDecl* CatImplClass =
|
2009-01-09 23:36:25 +08:00
|
|
|
dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
|
2010-01-07 09:20:12 +08:00
|
|
|
CatImplClass->setAtEndRange(AtEnd);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// Find category interface decl and then check that all methods declared
|
2008-08-27 13:40:03 +08:00
|
|
|
// in this interface are implemented in the category @implementation.
|
2009-02-17 02:32:47 +08:00
|
|
|
if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
|
2013-01-17 07:00:23 +08:00
|
|
|
if (ObjCCategoryDecl *Cat
|
|
|
|
= IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
|
|
|
|
ImplMethodsVsClassMethods(S, CatImplClass, Cat);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-30 00:50:03 +08:00
|
|
|
if (isInterfaceDeclKind) {
|
|
|
|
// Reject invalid vardecls.
|
2013-07-16 23:33:19 +08:00
|
|
|
for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
|
2013-08-27 21:15:56 +08:00
|
|
|
DeclGroupRef DG = allTUVars[i].get();
|
2009-03-30 00:50:03 +08:00
|
|
|
for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
|
|
|
|
if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
|
2009-04-14 10:25:56 +08:00
|
|
|
if (!VDecl->hasExternalStorage())
|
2009-04-14 01:58:46 +08:00
|
|
|
Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
|
2009-03-22 02:06:45 +08:00
|
|
|
}
|
2009-03-30 00:50:03 +08:00
|
|
|
}
|
2009-03-19 06:33:24 +08:00
|
|
|
}
|
2011-08-30 01:33:12 +08:00
|
|
|
ActOnObjCContainerFinishDefinition();
|
2011-10-18 03:48:13 +08:00
|
|
|
|
2013-07-16 23:33:19 +08:00
|
|
|
for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
|
2013-08-27 21:15:56 +08:00
|
|
|
DeclGroupRef DG = allTUVars[i].get();
|
2011-11-24 04:27:36 +08:00
|
|
|
for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
|
|
|
|
(*I)->setTopLevelDeclInObjCContainer();
|
2011-10-18 03:48:13 +08:00
|
|
|
Consumer.HandleTopLevelDeclInObjCContainer(DG);
|
|
|
|
}
|
2011-12-06 17:25:23 +08:00
|
|
|
|
2012-07-13 09:06:46 +08:00
|
|
|
ActOnDocumentableDecl(ClassDecl);
|
2011-12-06 17:25:23 +08:00
|
|
|
return ClassDecl;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
|
|
|
|
/// objective-c's type qualifier from the parser version of the same info.
|
2009-09-09 23:08:12 +08:00
|
|
|
static Decl::ObjCDeclQualifier
|
2008-01-08 03:49:32 +08:00
|
|
|
CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
|
2011-05-01 11:04:29 +08:00
|
|
|
return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2011-06-11 09:09:30 +08:00
|
|
|
/// \brief Check whether the declared result type of the given Objective-C
|
|
|
|
/// method declaration is compatible with the method's class.
|
|
|
|
///
|
2012-05-10 00:12:57 +08:00
|
|
|
static Sema::ResultTypeCompatibilityKind
|
2011-06-11 09:09:30 +08:00
|
|
|
CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
|
|
|
|
ObjCInterfaceDecl *CurrentClass) {
|
2014-01-26 00:55:45 +08:00
|
|
|
QualType ResultType = Method->getReturnType();
|
|
|
|
|
2011-06-11 09:09:30 +08:00
|
|
|
// If an Objective-C method inherits its related result type, then its
|
|
|
|
// declared result type must be compatible with its own class type. The
|
|
|
|
// declared result type is compatible if:
|
|
|
|
if (const ObjCObjectPointerType *ResultObjectType
|
|
|
|
= ResultType->getAs<ObjCObjectPointerType>()) {
|
|
|
|
// - it is id or qualified id, or
|
|
|
|
if (ResultObjectType->isObjCIdType() ||
|
|
|
|
ResultObjectType->isObjCQualifiedIdType())
|
2012-05-10 00:12:57 +08:00
|
|
|
return Sema::RTC_Compatible;
|
2011-06-11 09:09:30 +08:00
|
|
|
|
|
|
|
if (CurrentClass) {
|
|
|
|
if (ObjCInterfaceDecl *ResultClass
|
|
|
|
= ResultObjectType->getInterfaceDecl()) {
|
|
|
|
// - it is the same as the method's class type, or
|
2011-12-15 08:29:59 +08:00
|
|
|
if (declaresSameEntity(CurrentClass, ResultClass))
|
2012-05-10 00:12:57 +08:00
|
|
|
return Sema::RTC_Compatible;
|
2011-06-11 09:09:30 +08:00
|
|
|
|
|
|
|
// - it is a superclass of the method's class type
|
|
|
|
if (ResultClass->isSuperClassOf(CurrentClass))
|
2012-05-10 00:12:57 +08:00
|
|
|
return Sema::RTC_Compatible;
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
2011-09-08 09:46:34 +08:00
|
|
|
} else {
|
|
|
|
// Any Objective-C pointer type might be acceptable for a protocol
|
|
|
|
// method; we just don't know.
|
2012-05-10 00:12:57 +08:00
|
|
|
return Sema::RTC_Unknown;
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-10 00:12:57 +08:00
|
|
|
return Sema::RTC_Incompatible;
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
|
|
|
|
2011-07-22 10:45:48 +08:00
|
|
|
namespace {
|
|
|
|
/// A helper class for searching for methods which a particular method
|
|
|
|
/// overrides.
|
|
|
|
class OverrideSearch {
|
2012-02-29 11:04:05 +08:00
|
|
|
public:
|
2011-07-22 10:45:48 +08:00
|
|
|
Sema &S;
|
|
|
|
ObjCMethodDecl *Method;
|
2012-02-29 11:04:05 +08:00
|
|
|
llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
|
2011-07-22 10:45:48 +08:00
|
|
|
bool Recursive;
|
|
|
|
|
|
|
|
public:
|
|
|
|
OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
|
|
|
|
Selector selector = method->getSelector();
|
|
|
|
|
|
|
|
// Bypass this search if we've never seen an instance/class method
|
|
|
|
// with this selector before.
|
|
|
|
Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
|
|
|
|
if (it == S.MethodPool.end()) {
|
2012-10-19 03:05:02 +08:00
|
|
|
if (!S.getExternalSource()) return;
|
2012-01-25 08:49:42 +08:00
|
|
|
S.ReadMethodPool(selector);
|
|
|
|
|
|
|
|
it = S.MethodPool.find(selector);
|
|
|
|
if (it == S.MethodPool.end())
|
|
|
|
return;
|
2011-07-22 10:45:48 +08:00
|
|
|
}
|
|
|
|
ObjCMethodList &list =
|
|
|
|
method->isInstanceMethod() ? it->second.first : it->second.second;
|
2014-12-27 11:58:08 +08:00
|
|
|
if (!list.getMethod()) return;
|
2011-07-22 10:45:48 +08:00
|
|
|
|
|
|
|
ObjCContainerDecl *container
|
|
|
|
= cast<ObjCContainerDecl>(method->getDeclContext());
|
|
|
|
|
|
|
|
// Prevent the search from reaching this container again. This is
|
|
|
|
// important with categories, which override methods from the
|
|
|
|
// interface and each other.
|
2012-05-04 05:25:24 +08:00
|
|
|
if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
|
|
|
|
searchFromContainer(container);
|
2012-05-18 06:39:14 +08:00
|
|
|
if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
|
|
|
|
searchFromContainer(Interface);
|
2012-05-04 05:25:24 +08:00
|
|
|
} else {
|
|
|
|
searchFromContainer(container);
|
|
|
|
}
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
2011-07-22 10:45:48 +08:00
|
|
|
|
2016-01-30 09:27:06 +08:00
|
|
|
typedef llvm::SmallPtrSetImpl<ObjCMethodDecl*>::iterator iterator;
|
2011-07-22 10:45:48 +08:00
|
|
|
iterator begin() const { return Overridden.begin(); }
|
|
|
|
iterator end() const { return Overridden.end(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void searchFromContainer(ObjCContainerDecl *container) {
|
|
|
|
if (container->isInvalidDecl()) return;
|
|
|
|
|
|
|
|
switch (container->getDeclKind()) {
|
|
|
|
#define OBJCCONTAINER(type, base) \
|
|
|
|
case Decl::type: \
|
|
|
|
searchFrom(cast<type##Decl>(container)); \
|
|
|
|
break;
|
|
|
|
#define ABSTRACT_DECL(expansion)
|
|
|
|
#define DECL(type, base) \
|
|
|
|
case Decl::type:
|
|
|
|
#include "clang/AST/DeclNodes.inc"
|
|
|
|
llvm_unreachable("not an ObjC container!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void searchFrom(ObjCProtocolDecl *protocol) {
|
2012-01-02 03:29:29 +08:00
|
|
|
if (!protocol->hasDefinition())
|
|
|
|
return;
|
|
|
|
|
2011-07-22 10:45:48 +08:00
|
|
|
// A method in a protocol declaration overrides declarations from
|
|
|
|
// referenced ("parent") protocols.
|
|
|
|
search(protocol->getReferencedProtocols());
|
|
|
|
}
|
|
|
|
|
|
|
|
void searchFrom(ObjCCategoryDecl *category) {
|
|
|
|
// A method in a category declaration overrides declarations from
|
|
|
|
// the main class and from protocols the category references.
|
2012-05-04 05:25:24 +08:00
|
|
|
// The main class is handled in the constructor.
|
2011-07-22 10:45:48 +08:00
|
|
|
search(category->getReferencedProtocols());
|
|
|
|
}
|
|
|
|
|
|
|
|
void searchFrom(ObjCCategoryImplDecl *impl) {
|
|
|
|
// A method in a category definition that has a category
|
|
|
|
// declaration overrides declarations from the category
|
|
|
|
// declaration.
|
|
|
|
if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
|
|
|
|
search(category);
|
2012-05-18 06:39:14 +08:00
|
|
|
if (ObjCInterfaceDecl *Interface = category->getClassInterface())
|
|
|
|
search(Interface);
|
2011-07-22 10:45:48 +08:00
|
|
|
|
|
|
|
// Otherwise it overrides declarations from the class.
|
2012-05-18 06:39:14 +08:00
|
|
|
} else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
|
|
|
|
search(Interface);
|
2011-07-22 10:45:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void searchFrom(ObjCInterfaceDecl *iface) {
|
|
|
|
// A method in a class declaration overrides declarations from
|
2011-12-15 13:27:12 +08:00
|
|
|
if (!iface->hasDefinition())
|
|
|
|
return;
|
|
|
|
|
2011-07-22 10:45:48 +08:00
|
|
|
// - categories,
|
2014-03-14 05:35:02 +08:00
|
|
|
for (auto *Cat : iface->known_categories())
|
|
|
|
search(Cat);
|
2011-07-22 10:45:48 +08:00
|
|
|
|
|
|
|
// - the super class, and
|
|
|
|
if (ObjCInterfaceDecl *super = iface->getSuperClass())
|
|
|
|
search(super);
|
|
|
|
|
|
|
|
// - any referenced protocols.
|
|
|
|
search(iface->getReferencedProtocols());
|
|
|
|
}
|
|
|
|
|
|
|
|
void searchFrom(ObjCImplementationDecl *impl) {
|
|
|
|
// A method in a class implementation overrides declarations from
|
|
|
|
// the class interface.
|
2012-05-18 06:39:14 +08:00
|
|
|
if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
|
|
|
|
search(Interface);
|
2011-07-22 10:45:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void search(const ObjCProtocolList &protocols) {
|
|
|
|
for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
|
|
|
|
i != e; ++i)
|
|
|
|
search(*i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void search(ObjCContainerDecl *container) {
|
|
|
|
// Check for a method in this container which matches this selector.
|
|
|
|
ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
|
2013-03-30 05:51:48 +08:00
|
|
|
Method->isInstanceMethod(),
|
|
|
|
/*AllowHidden=*/true);
|
2011-07-22 10:45:48 +08:00
|
|
|
|
|
|
|
// If we find one, record it and bail out.
|
|
|
|
if (meth) {
|
|
|
|
Overridden.insert(meth);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, search for methods that a hypothetical method here
|
|
|
|
// would have overridden.
|
|
|
|
|
|
|
|
// Note that we're now in a recursive case.
|
|
|
|
Recursive = true;
|
|
|
|
|
|
|
|
searchFromContainer(container);
|
|
|
|
}
|
|
|
|
};
|
2015-10-07 07:40:43 +08:00
|
|
|
} // end anonymous namespace
|
2011-06-11 09:09:30 +08:00
|
|
|
|
2012-05-10 00:12:57 +08:00
|
|
|
void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
|
|
|
|
ObjCInterfaceDecl *CurrentClass,
|
|
|
|
ResultTypeCompatibilityKind RTC) {
|
|
|
|
// Search for overridden methods and merge information down from them.
|
|
|
|
OverrideSearch overrides(*this, ObjCMethod);
|
|
|
|
// Keep track if the method overrides any method in the class's base classes,
|
|
|
|
// its protocols, or its categories' protocols; we will keep that info
|
|
|
|
// in the ObjCMethodDecl.
|
|
|
|
// For this info, a method in an implementation is not considered as
|
|
|
|
// overriding the same method in the interface or its categories.
|
|
|
|
bool hasOverriddenMethodsInBaseOrProtocol = false;
|
|
|
|
for (OverrideSearch::iterator
|
|
|
|
i = overrides.begin(), e = overrides.end(); i != e; ++i) {
|
|
|
|
ObjCMethodDecl *overridden = *i;
|
|
|
|
|
2013-04-17 08:09:08 +08:00
|
|
|
if (!hasOverriddenMethodsInBaseOrProtocol) {
|
|
|
|
if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
|
|
|
|
CurrentClass != overridden->getClassInterface() ||
|
|
|
|
overridden->isOverriding()) {
|
|
|
|
hasOverriddenMethodsInBaseOrProtocol = true;
|
|
|
|
|
|
|
|
} else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
|
|
|
|
// OverrideSearch will return as "overridden" the same method in the
|
|
|
|
// interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
|
|
|
|
// check whether a category of a base class introduced a method with the
|
|
|
|
// same selector, after the interface method declaration.
|
|
|
|
// To avoid unnecessary lookups in the majority of cases, we use the
|
|
|
|
// extra info bits in GlobalMethodPool to check whether there were any
|
|
|
|
// category methods with this selector.
|
|
|
|
GlobalMethodPool::iterator It =
|
|
|
|
MethodPool.find(ObjCMethod->getSelector());
|
|
|
|
if (It != MethodPool.end()) {
|
|
|
|
ObjCMethodList &List =
|
|
|
|
ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
|
|
|
|
unsigned CategCount = List.getBits();
|
|
|
|
if (CategCount > 0) {
|
|
|
|
// If the method is in a category we'll do lookup if there were at
|
|
|
|
// least 2 category methods recorded, otherwise only one will do.
|
|
|
|
if (CategCount > 1 ||
|
|
|
|
!isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
|
|
|
|
OverrideSearch overrides(*this, overridden);
|
|
|
|
for (OverrideSearch::iterator
|
|
|
|
OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
|
|
|
|
ObjCMethodDecl *SuperOverridden = *OI;
|
2013-04-27 08:10:12 +08:00
|
|
|
if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
|
|
|
|
CurrentClass != SuperOverridden->getClassInterface()) {
|
2013-04-17 08:09:08 +08:00
|
|
|
hasOverriddenMethodsInBaseOrProtocol = true;
|
|
|
|
overridden->setOverriding(true);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-05-10 00:12:57 +08:00
|
|
|
|
|
|
|
// Propagate down the 'related result type' bit from overridden methods.
|
|
|
|
if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
|
|
|
|
ObjCMethod->SetRelatedResultType();
|
|
|
|
|
|
|
|
// Then merge the declarations.
|
|
|
|
mergeObjCMethodDecls(ObjCMethod, overridden);
|
|
|
|
|
|
|
|
if (ObjCMethod->isImplicit() && overridden->isImplicit())
|
|
|
|
continue; // Conflicting properties are detected elsewhere.
|
|
|
|
|
|
|
|
// Check for overriding methods
|
|
|
|
if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
|
|
|
|
isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
|
|
|
|
CheckConflictingOverridingMethod(ObjCMethod, overridden,
|
|
|
|
isa<ObjCProtocolDecl>(overridden->getDeclContext()));
|
|
|
|
|
|
|
|
if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
|
2012-07-06 06:26:07 +08:00
|
|
|
isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
|
|
|
|
!overridden->isImplicit() /* not meant for properties */) {
|
2012-05-10 00:12:57 +08:00
|
|
|
ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
|
|
|
|
E = ObjCMethod->param_end();
|
2012-05-18 07:13:29 +08:00
|
|
|
ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
|
|
|
|
PrevE = overridden->param_end();
|
|
|
|
for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
|
2012-05-10 00:12:57 +08:00
|
|
|
assert(PrevI != overridden->param_end() && "Param mismatch");
|
|
|
|
QualType T1 = Context.getCanonicalType((*ParamI)->getType());
|
|
|
|
QualType T2 = Context.getCanonicalType((*PrevI)->getType());
|
|
|
|
// If type of argument of method in this class does not match its
|
|
|
|
// respective argument type in the super class method, issue warning;
|
|
|
|
if (!Context.typesAreCompatible(T1, T2)) {
|
|
|
|
Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
|
|
|
|
<< T1 << T2;
|
|
|
|
Diag(overridden->getLocation(), diag::note_previous_declaration);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
|
|
|
|
}
|
|
|
|
|
2015-06-20 02:14:38 +08:00
|
|
|
/// Merge type nullability from for a redeclaration of the same entity,
|
|
|
|
/// producing the updated type of the redeclared entity.
|
|
|
|
static QualType mergeTypeNullabilityForRedecl(Sema &S, SourceLocation loc,
|
|
|
|
QualType type,
|
|
|
|
bool usesCSKeyword,
|
|
|
|
SourceLocation prevLoc,
|
|
|
|
QualType prevType,
|
|
|
|
bool prevUsesCSKeyword) {
|
|
|
|
// Determine the nullability of both types.
|
|
|
|
auto nullability = type->getNullability(S.Context);
|
|
|
|
auto prevNullability = prevType->getNullability(S.Context);
|
|
|
|
|
|
|
|
// Easy case: both have nullability.
|
|
|
|
if (nullability.hasValue() == prevNullability.hasValue()) {
|
|
|
|
// Neither has nullability; continue.
|
|
|
|
if (!nullability)
|
|
|
|
return type;
|
|
|
|
|
|
|
|
// The nullabilities are equivalent; do nothing.
|
|
|
|
if (*nullability == *prevNullability)
|
|
|
|
return type;
|
|
|
|
|
|
|
|
// Complain about mismatched nullability.
|
|
|
|
S.Diag(loc, diag::err_nullability_conflicting)
|
2015-06-25 06:02:08 +08:00
|
|
|
<< DiagNullabilityKind(*nullability, usesCSKeyword)
|
|
|
|
<< DiagNullabilityKind(*prevNullability, prevUsesCSKeyword);
|
2015-06-20 02:14:38 +08:00
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's the redeclaration that has nullability, don't change anything.
|
|
|
|
if (nullability)
|
|
|
|
return type;
|
|
|
|
|
|
|
|
// Otherwise, provide the result with the same nullability.
|
|
|
|
return S.Context.getAttributedType(
|
|
|
|
AttributedType::getNullabilityAttrKind(*prevNullability),
|
|
|
|
type, type);
|
|
|
|
}
|
|
|
|
|
2015-06-20 11:52:52 +08:00
|
|
|
/// Merge information from the declaration of a method in the \@interface
|
2015-06-20 02:14:38 +08:00
|
|
|
/// (or a category/extension) into the corresponding method in the
|
|
|
|
/// @implementation (for a class or category).
|
|
|
|
static void mergeInterfaceMethodToImpl(Sema &S,
|
|
|
|
ObjCMethodDecl *method,
|
|
|
|
ObjCMethodDecl *prevMethod) {
|
|
|
|
// Merge the objc_requires_super attribute.
|
|
|
|
if (prevMethod->hasAttr<ObjCRequiresSuperAttr>() &&
|
|
|
|
!method->hasAttr<ObjCRequiresSuperAttr>()) {
|
|
|
|
// merge the attribute into implementation.
|
|
|
|
method->addAttr(
|
|
|
|
ObjCRequiresSuperAttr::CreateImplicit(S.Context,
|
|
|
|
method->getLocation()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge nullability of the result type.
|
|
|
|
QualType newReturnType
|
|
|
|
= mergeTypeNullabilityForRedecl(
|
|
|
|
S, method->getReturnTypeSourceRange().getBegin(),
|
|
|
|
method->getReturnType(),
|
|
|
|
method->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability,
|
|
|
|
prevMethod->getReturnTypeSourceRange().getBegin(),
|
|
|
|
prevMethod->getReturnType(),
|
|
|
|
prevMethod->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability);
|
|
|
|
method->setReturnType(newReturnType);
|
|
|
|
|
|
|
|
// Handle each of the parameters.
|
|
|
|
unsigned numParams = method->param_size();
|
|
|
|
unsigned numPrevParams = prevMethod->param_size();
|
|
|
|
for (unsigned i = 0, n = std::min(numParams, numPrevParams); i != n; ++i) {
|
|
|
|
ParmVarDecl *param = method->param_begin()[i];
|
|
|
|
ParmVarDecl *prevParam = prevMethod->param_begin()[i];
|
|
|
|
|
|
|
|
// Merge nullability.
|
|
|
|
QualType newParamType
|
|
|
|
= mergeTypeNullabilityForRedecl(
|
|
|
|
S, param->getLocation(), param->getType(),
|
|
|
|
param->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability,
|
|
|
|
prevParam->getLocation(), prevParam->getType(),
|
|
|
|
prevParam->getObjCDeclQualifier() & Decl::OBJC_TQ_CSNullability);
|
|
|
|
param->setType(newParamType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::ActOnMethodDeclaration(
|
2011-02-10 06:20:01 +08:00
|
|
|
Scope *S,
|
2007-12-12 15:09:47 +08:00
|
|
|
SourceLocation MethodLoc, SourceLocation EndLoc,
|
2011-08-22 23:54:49 +08:00
|
|
|
tok::TokenKind MethodType,
|
2010-08-24 13:47:05 +08:00
|
|
|
ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
|
2011-10-03 14:36:36 +08:00
|
|
|
ArrayRef<SourceLocation> SelectorLocs,
|
2007-12-12 15:09:47 +08:00
|
|
|
Selector Sel,
|
|
|
|
// optional arguments. The number of types/arguments is obtained
|
|
|
|
// from the Sel.getNumArgs().
|
2009-04-12 02:57:04 +08:00
|
|
|
ObjCArgInfo *ArgInfo,
|
2010-04-08 08:30:06 +08:00
|
|
|
DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
|
2007-12-12 15:09:47 +08:00
|
|
|
AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
|
2011-03-13 02:54:30 +08:00
|
|
|
bool isVariadic, bool MethodDefinition) {
|
2008-03-01 05:48:07 +08:00
|
|
|
// Make sure we can establish a context for the method.
|
2011-08-22 23:54:49 +08:00
|
|
|
if (!CurContext->isObjCContainer()) {
|
2008-03-01 05:48:07 +08:00
|
|
|
Diag(MethodLoc, diag::error_missing_method_context);
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2008-03-01 05:48:07 +08:00
|
|
|
}
|
2011-08-22 23:54:49 +08:00
|
|
|
ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
|
|
|
|
Decl *ClassDecl = cast<Decl>(OCD);
|
2008-04-04 14:12:32 +08:00
|
|
|
QualType resultDeclType;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-08 09:46:34 +08:00
|
|
|
bool HasRelatedResultType = false;
|
2014-05-26 14:22:03 +08:00
|
|
|
TypeSourceInfo *ReturnTInfo = nullptr;
|
2009-02-21 06:59:16 +08:00
|
|
|
if (ReturnType) {
|
2014-01-26 00:55:45 +08:00
|
|
|
resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-06-15 05:14:10 +08:00
|
|
|
if (CheckFunctionReturnType(resultDeclType, MethodLoc))
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2013-06-15 05:14:10 +08:00
|
|
|
|
2015-06-20 02:14:38 +08:00
|
|
|
QualType bareResultType = resultDeclType;
|
|
|
|
(void)AttributedType::stripOuterNullability(bareResultType);
|
|
|
|
HasRelatedResultType = (bareResultType == Context.getObjCInstanceType());
|
2011-07-22 01:00:47 +08:00
|
|
|
} else { // get the type for "id".
|
2008-04-04 14:12:32 +08:00
|
|
|
resultDeclType = Context.getObjCIdType();
|
2011-07-22 01:38:14 +08:00
|
|
|
Diag(MethodLoc, diag::warn_missing_method_return_type)
|
2011-10-03 14:36:36 +08:00
|
|
|
<< FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
|
2011-07-22 01:00:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2014-01-26 00:55:45 +08:00
|
|
|
ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create(
|
|
|
|
Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext,
|
|
|
|
MethodType == tok::minus, isVariadic,
|
|
|
|
/*isPropertyAccessor=*/false,
|
|
|
|
/*isImplicitlyDeclared=*/false, /*isDefined=*/false,
|
|
|
|
MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional
|
|
|
|
: ObjCMethodDecl::Required,
|
|
|
|
HasRelatedResultType);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<ParmVarDecl*, 16> Params;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 03:42:43 +08:00
|
|
|
for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
|
2009-10-24 05:48:59 +08:00
|
|
|
QualType ArgType;
|
2009-12-07 10:54:59 +08:00
|
|
|
TypeSourceInfo *DI;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-05-15 15:37:26 +08:00
|
|
|
if (!ArgInfo[i].Type) {
|
2009-10-24 05:48:59 +08:00
|
|
|
ArgType = Context.getObjCIdType();
|
2014-05-26 14:22:03 +08:00
|
|
|
DI = nullptr;
|
2009-04-12 02:57:04 +08:00
|
|
|
} else {
|
2009-10-24 05:48:59 +08:00
|
|
|
ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
|
2009-04-12 02:57:04 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-02-10 06:20:01 +08:00
|
|
|
LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
|
|
|
|
LookupOrdinaryName, ForRedeclaration);
|
|
|
|
LookupName(R, S);
|
|
|
|
if (R.isSingleResult()) {
|
|
|
|
NamedDecl *PrevDecl = R.getFoundDecl();
|
|
|
|
if (S->isDeclScope(PrevDecl)) {
|
2011-03-13 02:54:30 +08:00
|
|
|
Diag(ArgInfo[i].NameLoc,
|
|
|
|
(MethodDefinition ? diag::warn_method_param_redefinition
|
|
|
|
: diag::warn_method_param_declaration))
|
2011-02-10 06:20:01 +08:00
|
|
|
<< ArgInfo[i].Name;
|
|
|
|
Diag(PrevDecl->getLocation(),
|
|
|
|
diag::note_previous_declaration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-08 16:55:46 +08:00
|
|
|
SourceLocation StartLoc = DI
|
|
|
|
? DI->getTypeLoc().getBeginLoc()
|
|
|
|
: ArgInfo[i].NameLoc;
|
|
|
|
|
2011-04-23 10:46:06 +08:00
|
|
|
ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
|
|
|
|
ArgInfo[i].NameLoc, ArgInfo[i].Name,
|
2013-04-04 03:27:57 +08:00
|
|
|
ArgType, DI, SC_None);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-05-02 08:30:12 +08:00
|
|
|
Param->setObjCMethodScopeInfo(i);
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
Param->setObjCDeclQualifier(
|
2009-04-12 02:57:04 +08:00
|
|
|
CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 03:34:56 +08:00
|
|
|
// Apply the attributes to the parameter.
|
2009-06-18 05:51:59 +08:00
|
|
|
ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2012-01-15 02:44:35 +08:00
|
|
|
if (Param->hasAttr<BlocksAttr>()) {
|
|
|
|
Diag(Param->getLocation(), diag::err_block_on_nonlocal);
|
|
|
|
Param->setInvalidDecl();
|
|
|
|
}
|
2011-02-10 06:20:01 +08:00
|
|
|
S->AddDecl(Param);
|
|
|
|
IdResolver.AddDecl(Param);
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
Params.push_back(Param);
|
|
|
|
}
|
2011-02-10 06:20:01 +08:00
|
|
|
|
2010-04-08 08:30:06 +08:00
|
|
|
for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
|
2010-08-21 17:40:31 +08:00
|
|
|
ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
|
2010-04-08 08:30:06 +08:00
|
|
|
QualType ArgType = Param->getType();
|
|
|
|
if (ArgType.isNull())
|
|
|
|
ArgType = Context.getObjCIdType();
|
|
|
|
else
|
|
|
|
// Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
|
2011-07-12 12:42:08 +08:00
|
|
|
ArgType = Context.getAdjustedParameterType(ArgType);
|
2013-06-15 05:14:10 +08:00
|
|
|
|
2010-04-08 08:30:06 +08:00
|
|
|
Param->setDeclContext(ObjCMethod);
|
|
|
|
Params.push_back(Param);
|
|
|
|
}
|
|
|
|
|
2011-10-03 14:37:04 +08:00
|
|
|
ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCMethod->setObjCDeclQualifier(
|
|
|
|
CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
|
2008-09-26 12:12:28 +08:00
|
|
|
|
|
|
|
if (AttrList)
|
2009-06-18 05:51:59 +08:00
|
|
|
ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Fix a major inconsistency in the representation of Objective-C
classes, categories, protocols, and class extensions, where the
methods and properties of these entities would be inserted into the
DeclContext in an ordering that doesn't necessarily reflect source
order. The culprits were Sema::ActOnMethodDeclaration(), which did not
perform the insertion of the just-created method declaration into
the DeclContext for these Objective-C entities, and
Sema::ActOnAtEnd(), which inserted all method declarations at the
*end* of the DeclContext.
With this fix in hand, clean up the code-completion actions for
property setters/getters that worked around this brokenness in the AST.
Fixes <rdar://problem/8062781>, where this problem manifested as poor
token-annotation information, but this would have struck again in many
other places.
llvm-svn: 122347
2010-12-22 01:34:17 +08:00
|
|
|
// Add the method now.
|
2014-05-26 14:22:03 +08:00
|
|
|
const ObjCMethodDecl *PrevMethod = nullptr;
|
2011-07-22 10:45:48 +08:00
|
|
|
if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
|
2007-12-12 15:09:47 +08:00
|
|
|
if (MethodType == tok::minus) {
|
2009-06-30 10:36:12 +08:00
|
|
|
PrevMethod = ImpDecl->getInstanceMethod(Sel);
|
|
|
|
ImpDecl->addInstanceMethod(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
} else {
|
2009-06-30 10:36:12 +08:00
|
|
|
PrevMethod = ImpDecl->getClassMethod(Sel);
|
|
|
|
ImpDecl->addClassMethod(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2011-06-11 09:09:30 +08:00
|
|
|
|
2015-06-20 02:14:38 +08:00
|
|
|
// Merge information from the @interface declaration into the
|
|
|
|
// @implementation.
|
|
|
|
if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface()) {
|
|
|
|
if (auto *IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
|
|
|
|
ObjCMethod->isInstanceMethod())) {
|
|
|
|
mergeInterfaceMethodToImpl(*this, ObjCMethod, IMD);
|
|
|
|
|
|
|
|
// Warn about defining -dealloc in a category.
|
|
|
|
if (isa<ObjCCategoryImplDecl>(ImpDecl) && IMD->isOverriding() &&
|
|
|
|
ObjCMethod->getSelector().getMethodFamily() == OMF_dealloc) {
|
|
|
|
Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category)
|
|
|
|
<< ObjCMethod->getDeclName();
|
|
|
|
}
|
|
|
|
}
|
2013-12-18 06:44:28 +08:00
|
|
|
}
|
Fix a major inconsistency in the representation of Objective-C
classes, categories, protocols, and class extensions, where the
methods and properties of these entities would be inserted into the
DeclContext in an ordering that doesn't necessarily reflect source
order. The culprits were Sema::ActOnMethodDeclaration(), which did not
perform the insertion of the just-created method declaration into
the DeclContext for these Objective-C entities, and
Sema::ActOnAtEnd(), which inserted all method declarations at the
*end* of the DeclContext.
With this fix in hand, clean up the code-completion actions for
property setters/getters that worked around this brokenness in the AST.
Fixes <rdar://problem/8062781>, where this problem manifested as poor
token-annotation information, but this would have struck again in many
other places.
llvm-svn: 122347
2010-12-22 01:34:17 +08:00
|
|
|
} else {
|
|
|
|
cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2011-07-22 10:45:48 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
if (PrevMethod) {
|
|
|
|
// You can never have two method definitions with the same name.
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
|
2008-11-24 11:33:13 +08:00
|
|
|
<< ObjCMethod->getDeclName();
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
|
2013-05-14 01:27:00 +08:00
|
|
|
ObjCMethod->setInvalidDecl();
|
|
|
|
return ObjCMethod;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-11-04 10:18:39 +08:00
|
|
|
|
2011-06-11 09:09:30 +08:00
|
|
|
// If this Objective-C method does not have a related result type, but we
|
|
|
|
// are allowed to infer related result types, try to do so based on the
|
|
|
|
// method family.
|
|
|
|
ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
|
|
|
|
if (!CurrentClass) {
|
|
|
|
if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
|
|
|
|
CurrentClass = Cat->getClassInterface();
|
|
|
|
else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
|
|
|
|
CurrentClass = Impl->getClassInterface();
|
|
|
|
else if (ObjCCategoryImplDecl *CatImpl
|
|
|
|
= dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
|
|
|
|
CurrentClass = CatImpl->getClassInterface();
|
|
|
|
}
|
2011-07-22 10:45:48 +08:00
|
|
|
|
2011-09-08 09:46:34 +08:00
|
|
|
ResultTypeCompatibilityKind RTC
|
|
|
|
= CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
|
2011-07-22 10:45:48 +08:00
|
|
|
|
2012-05-10 00:12:57 +08:00
|
|
|
CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
|
2011-07-22 10:45:48 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
bool ARCError = false;
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount)
|
2013-04-04 09:38:37 +08:00
|
|
|
ARCError = CheckARCMethodDecl(ObjCMethod);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-09-08 09:46:34 +08:00
|
|
|
// Infer the related result type when possible.
|
2012-05-10 00:12:57 +08:00
|
|
|
if (!ARCError && RTC == Sema::RTC_Compatible &&
|
2011-09-08 09:46:34 +08:00
|
|
|
!ObjCMethod->hasRelatedResultType() &&
|
|
|
|
LangOpts.ObjCInferRelatedResultType) {
|
2011-06-11 09:09:30 +08:00
|
|
|
bool InferRelatedResultType = false;
|
|
|
|
switch (ObjCMethod->getMethodFamily()) {
|
|
|
|
case OMF_None:
|
|
|
|
case OMF_copy:
|
|
|
|
case OMF_dealloc:
|
2011-08-29 06:35:17 +08:00
|
|
|
case OMF_finalize:
|
2011-06-11 09:09:30 +08:00
|
|
|
case OMF_mutableCopy:
|
|
|
|
case OMF_release:
|
|
|
|
case OMF_retainCount:
|
2014-08-23 00:57:26 +08:00
|
|
|
case OMF_initialize:
|
2011-07-06 06:38:59 +08:00
|
|
|
case OMF_performSelector:
|
2011-06-11 09:09:30 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OMF_alloc:
|
|
|
|
case OMF_new:
|
2015-04-17 02:38:44 +08:00
|
|
|
InferRelatedResultType = ObjCMethod->isClassMethod();
|
2011-06-11 09:09:30 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OMF_init:
|
|
|
|
case OMF_autorelease:
|
|
|
|
case OMF_retain:
|
|
|
|
case OMF_self:
|
|
|
|
InferRelatedResultType = ObjCMethod->isInstanceMethod();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-04-17 02:38:44 +08:00
|
|
|
if (InferRelatedResultType &&
|
|
|
|
!ObjCMethod->getReturnType()->isObjCIndependentClassType())
|
2011-06-11 09:09:30 +08:00
|
|
|
ObjCMethod->SetRelatedResultType();
|
|
|
|
}
|
2012-07-12 05:38:39 +08:00
|
|
|
|
|
|
|
ActOnDocumentableDecl(ObjCMethod);
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
return ObjCMethod;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2008-12-17 15:13:27 +08:00
|
|
|
bool Sema::CheckObjCDeclScope(Decl *D) {
|
2011-08-23 02:34:22 +08:00
|
|
|
// Following is also an error. But it is caused by a missing @end
|
|
|
|
// and diagnostic is issued elsewhere.
|
2012-03-24 07:24:23 +08:00
|
|
|
if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If we switched context to translation unit while we are still lexically in
|
|
|
|
// an objc container, it means the parser missed emitting an error.
|
|
|
|
if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
|
2011-08-22 23:54:49 +08:00
|
|
|
return false;
|
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
|
|
|
|
D->setInvalidDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-12-17 15:13:27 +08:00
|
|
|
|
2012-06-15 05:40:34 +08:00
|
|
|
/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
|
2008-12-17 15:13:27 +08:00
|
|
|
/// instance variables of ClassName into Decls.
|
2010-08-21 17:40:31 +08:00
|
|
|
void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
|
2008-12-17 15:13:27 +08:00
|
|
|
IdentifierInfo *ClassName,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<Decl*> &Decls) {
|
2008-12-17 15:13:27 +08:00
|
|
|
// Check that ClassName is a valid class
|
2010-04-16 06:33:43 +08:00
|
|
|
ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
|
2008-12-17 15:13:27 +08:00
|
|
|
if (!Class) {
|
|
|
|
Diag(DeclStart, diag::err_undef_interface) << ClassName;
|
|
|
|
return;
|
|
|
|
}
|
2012-06-20 14:18:46 +08:00
|
|
|
if (LangOpts.ObjCRuntime.isNonFragile()) {
|
2009-04-22 04:28:41 +08:00
|
|
|
Diag(DeclStart, diag::err_atdef_nonfragile_interface);
|
|
|
|
return;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 15:13:27 +08:00
|
|
|
// Collect the instance variables
|
2011-07-22 10:08:32 +08:00
|
|
|
SmallVector<const ObjCIvarDecl*, 32> Ivars;
|
2010-08-21 05:21:08 +08:00
|
|
|
Context.DeepCollectObjCIvars(Class, true, Ivars);
|
2009-06-05 01:08:55 +08:00
|
|
|
// For each ivar, create a fresh ObjCAtDefsFieldDecl.
|
2010-08-21 05:21:08 +08:00
|
|
|
for (unsigned i = 0; i < Ivars.size(); i++) {
|
2011-07-22 10:08:32 +08:00
|
|
|
const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
|
2010-08-21 17:40:31 +08:00
|
|
|
RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
|
2011-03-08 16:55:46 +08:00
|
|
|
Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
|
|
|
|
/*FIXME: StartL=*/ID->getLocation(),
|
|
|
|
ID->getLocation(),
|
2009-06-05 01:08:55 +08:00
|
|
|
ID->getIdentifier(), ID->getType(),
|
|
|
|
ID->getBitWidth());
|
2010-08-21 17:40:31 +08:00
|
|
|
Decls.push_back(FD);
|
2009-06-05 01:08:55 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 15:13:27 +08:00
|
|
|
// Introduce all of these fields into the appropriate scope.
|
2011-07-23 18:55:15 +08:00
|
|
|
for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
|
2008-12-17 15:13:27 +08:00
|
|
|
D != Decls.end(); ++D) {
|
2010-08-21 17:40:31 +08:00
|
|
|
FieldDecl *FD = cast<FieldDecl>(*D);
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus)
|
2008-12-17 15:13:27 +08:00
|
|
|
PushOnScopeChains(cast<FieldDecl>(FD), S);
|
2010-08-21 17:40:31 +08:00
|
|
|
else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
|
2009-06-30 10:36:12 +08:00
|
|
|
Record->addDecl(FD);
|
2008-12-17 15:13:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 01:32:49 +08:00
|
|
|
/// \brief Build a type-check a new Objective-C exception variable declaration.
|
2011-03-08 16:55:46 +08:00
|
|
|
VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
|
|
|
|
SourceLocation StartLoc,
|
|
|
|
SourceLocation IdLoc,
|
|
|
|
IdentifierInfo *Id,
|
2010-04-27 01:32:49 +08:00
|
|
|
bool Invalid) {
|
|
|
|
// ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
|
|
|
|
// duration shall not be qualified by an address-space qualifier."
|
|
|
|
// Since all parameters have automatic store duration, they can not have
|
|
|
|
// an address space.
|
|
|
|
if (T.getAddressSpace() != 0) {
|
2011-03-08 16:55:46 +08:00
|
|
|
Diag(IdLoc, diag::err_arg_with_address_space);
|
2010-04-27 01:32:49 +08:00
|
|
|
Invalid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// An @catch parameter must be an unqualified object pointer type;
|
|
|
|
// FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
|
|
|
|
if (Invalid) {
|
|
|
|
// Don't do any further checking.
|
2010-04-27 01:57:08 +08:00
|
|
|
} else if (T->isDependentType()) {
|
|
|
|
// Okay: we don't know what this type will instantiate to.
|
2010-04-27 01:32:49 +08:00
|
|
|
} else if (!T->isObjCObjectPointerType()) {
|
|
|
|
Invalid = true;
|
2011-03-08 16:55:46 +08:00
|
|
|
Diag(IdLoc ,diag::err_catch_param_not_objc_type);
|
2010-04-27 01:32:49 +08:00
|
|
|
} else if (T->isObjCQualifiedIdType()) {
|
|
|
|
Invalid = true;
|
2011-03-08 16:55:46 +08:00
|
|
|
Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
|
2010-04-27 01:32:49 +08:00
|
|
|
}
|
|
|
|
|
2011-03-08 16:55:46 +08:00
|
|
|
VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
|
2013-04-04 03:27:57 +08:00
|
|
|
T, TInfo, SC_None);
|
2010-05-04 02:51:14 +08:00
|
|
|
New->setExceptionVariable(true);
|
|
|
|
|
2011-12-10 09:22:52 +08:00
|
|
|
// In ARC, infer 'retaining' for variables of retainable type.
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
|
2011-12-10 09:22:52 +08:00
|
|
|
Invalid = true;
|
|
|
|
|
2010-04-27 01:32:49 +08:00
|
|
|
if (Invalid)
|
|
|
|
New->setInvalidDecl();
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
|
2010-04-27 01:32:49 +08:00
|
|
|
const DeclSpec &DS = D.getDeclSpec();
|
|
|
|
|
|
|
|
// We allow the "register" storage class on exception variables because
|
|
|
|
// GCC did, but we drop it completely. Any other storage class is an error.
|
|
|
|
if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
|
|
|
|
Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
|
|
|
|
<< FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
|
2013-04-13 06:46:28 +08:00
|
|
|
} else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
|
2010-04-27 01:32:49 +08:00
|
|
|
Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
|
2013-04-13 06:46:28 +08:00
|
|
|
<< DeclSpec::getSpecifierName(SCS);
|
|
|
|
}
|
2016-06-25 08:15:56 +08:00
|
|
|
if (DS.isInlineSpecified())
|
|
|
|
Diag(DS.getInlineSpecLoc(), diag::err_inline_non_function)
|
|
|
|
<< getLangOpts().CPlusPlus1z;
|
2013-04-13 06:46:28 +08:00
|
|
|
if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
|
|
|
|
Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
|
|
|
|
diag::err_invalid_thread)
|
|
|
|
<< DeclSpec::getSpecifierName(TSCS);
|
2010-04-27 01:32:49 +08:00
|
|
|
D.getMutableDeclSpec().ClearStorageClassSpecs();
|
|
|
|
|
2013-03-19 06:52:47 +08:00
|
|
|
DiagnoseFunctionSpecifiers(D.getDeclSpec());
|
2010-04-27 01:32:49 +08:00
|
|
|
|
|
|
|
// Check that there are no default arguments inside the type of this
|
|
|
|
// exception object (C++ only).
|
2012-03-11 15:00:24 +08:00
|
|
|
if (getLangOpts().CPlusPlus)
|
2010-04-27 01:32:49 +08:00
|
|
|
CheckExtraCXXDefaultArguments(D);
|
|
|
|
|
2011-06-28 11:01:15 +08:00
|
|
|
TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
|
2010-06-05 07:28:52 +08:00
|
|
|
QualType ExceptionType = TInfo->getType();
|
2010-04-27 01:32:49 +08:00
|
|
|
|
2011-03-08 16:55:46 +08:00
|
|
|
VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
|
|
|
|
D.getSourceRange().getBegin(),
|
|
|
|
D.getIdentifierLoc(),
|
|
|
|
D.getIdentifier(),
|
2010-04-27 01:32:49 +08:00
|
|
|
D.isInvalidType());
|
|
|
|
|
|
|
|
// Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
|
|
|
|
if (D.getCXXScopeSpec().isSet()) {
|
|
|
|
Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
|
|
|
|
<< D.getCXXScopeSpec().getRange();
|
|
|
|
New->setInvalidDecl();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the parameter declaration into this scope.
|
2010-08-21 17:40:31 +08:00
|
|
|
S->AddDecl(New);
|
2010-04-27 01:32:49 +08:00
|
|
|
if (D.getIdentifier())
|
|
|
|
IdResolver.AddDecl(New);
|
|
|
|
|
|
|
|
ProcessDeclAttributes(S, New, D);
|
|
|
|
|
|
|
|
if (New->hasAttr<BlocksAttr>())
|
|
|
|
Diag(New->getLocation(), diag::err_block_on_nonlocal);
|
2010-08-21 17:40:31 +08:00
|
|
|
return New;
|
2010-04-24 07:01:43 +08:00
|
|
|
}
|
2010-04-28 01:18:58 +08:00
|
|
|
|
|
|
|
/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
|
2010-04-29 00:11:27 +08:00
|
|
|
/// initialization.
|
2010-08-21 05:21:08 +08:00
|
|
|
void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
|
2010-08-21 05:21:08 +08:00
|
|
|
for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
|
|
|
|
Iv= Iv->getNextIvar()) {
|
2010-04-28 01:18:58 +08:00
|
|
|
QualType QT = Context.getBaseElementType(Iv->getType());
|
2010-05-20 10:24:22 +08:00
|
|
|
if (QT->isRecordType())
|
2010-08-21 05:21:08 +08:00
|
|
|
Ivars.push_back(Iv);
|
2010-04-28 01:18:58 +08:00
|
|
|
}
|
|
|
|
}
|
2010-04-29 00:11:27 +08:00
|
|
|
|
2010-07-23 02:24:20 +08:00
|
|
|
void Sema::DiagnoseUseOfUnimplementedSelectors() {
|
2011-07-28 22:54:22 +08:00
|
|
|
// Load referenced selectors from the external source.
|
|
|
|
if (ExternalSource) {
|
|
|
|
SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
|
|
|
|
ExternalSource->ReadReferencedSelectors(Sels);
|
|
|
|
for (unsigned I = 0, N = Sels.size(); I != N; ++I)
|
|
|
|
ReferencedSelectors[Sels[I].first] = Sels[I].second;
|
|
|
|
}
|
|
|
|
|
2011-02-05 07:19:27 +08:00
|
|
|
// Warning will be issued only when selector table is
|
|
|
|
// generated (which means there is at lease one implementation
|
|
|
|
// in the TU). This is to match gcc's behavior.
|
|
|
|
if (ReferencedSelectors.empty() ||
|
|
|
|
!Context.AnyObjCImplementation())
|
2010-07-23 02:24:20 +08:00
|
|
|
return;
|
2015-03-27 08:55:05 +08:00
|
|
|
for (auto &SelectorAndLocation : ReferencedSelectors) {
|
|
|
|
Selector Sel = SelectorAndLocation.first;
|
|
|
|
SourceLocation Loc = SelectorAndLocation.second;
|
2010-07-23 02:24:20 +08:00
|
|
|
if (!LookupImplementedMethodInGlobalPool(Sel))
|
2015-03-27 08:55:05 +08:00
|
|
|
Diag(Loc, diag::warn_unimplemented_selector) << Sel;
|
2010-07-23 02:24:20 +08:00
|
|
|
}
|
|
|
|
}
|
2013-10-26 05:44:50 +08:00
|
|
|
|
|
|
|
ObjCIvarDecl *
|
|
|
|
Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method,
|
|
|
|
const ObjCPropertyDecl *&PDecl) const {
|
2014-01-03 01:24:32 +08:00
|
|
|
if (Method->isClassMethod())
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2013-10-26 05:44:50 +08:00
|
|
|
const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
|
|
|
|
if (!IDecl)
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2014-01-04 02:32:18 +08:00
|
|
|
Method = IDecl->lookupMethod(Method->getSelector(), /*isInstance=*/true,
|
|
|
|
/*shallowCategoryLookup=*/false,
|
|
|
|
/*followSuper=*/false);
|
2013-10-26 05:44:50 +08:00
|
|
|
if (!Method || !Method->isPropertyAccessor())
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2014-01-04 02:32:18 +08:00
|
|
|
if ((PDecl = Method->findPropertyDecl()))
|
2014-01-28 06:27:43 +08:00
|
|
|
if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) {
|
|
|
|
// property backing ivar must belong to property's class
|
|
|
|
// or be a private ivar in class's implementation.
|
|
|
|
// FIXME. fix the const-ness issue.
|
|
|
|
IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable(
|
|
|
|
IV->getIdentifier());
|
|
|
|
return IV;
|
|
|
|
}
|
2014-05-26 14:22:03 +08:00
|
|
|
return nullptr;
|
2013-10-26 05:44:50 +08:00
|
|
|
}
|
|
|
|
|
2014-01-04 02:32:18 +08:00
|
|
|
namespace {
|
|
|
|
/// Used by Sema::DiagnoseUnusedBackingIvarInAccessor to check if a property
|
|
|
|
/// accessor references the backing ivar.
|
2014-01-04 03:53:09 +08:00
|
|
|
class UnusedBackingIvarChecker :
|
2015-11-24 11:55:01 +08:00
|
|
|
public RecursiveASTVisitor<UnusedBackingIvarChecker> {
|
2014-01-04 02:32:18 +08:00
|
|
|
public:
|
|
|
|
Sema &S;
|
|
|
|
const ObjCMethodDecl *Method;
|
|
|
|
const ObjCIvarDecl *IvarD;
|
|
|
|
bool AccessedIvar;
|
|
|
|
bool InvokedSelfMethod;
|
|
|
|
|
|
|
|
UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method,
|
|
|
|
const ObjCIvarDecl *IvarD)
|
|
|
|
: S(S), Method(Method), IvarD(IvarD),
|
|
|
|
AccessedIvar(false), InvokedSelfMethod(false) {
|
|
|
|
assert(IvarD);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
|
|
|
|
if (E->getDecl() == IvarD) {
|
|
|
|
AccessedIvar = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
|
|
|
|
if (E->getReceiverKind() == ObjCMessageExpr::Instance &&
|
|
|
|
S.isSelfExpr(E->getInstanceReceiver(), Method)) {
|
|
|
|
InvokedSelfMethod = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
2015-10-07 07:40:43 +08:00
|
|
|
} // end anonymous namespace
|
2014-01-04 02:32:18 +08:00
|
|
|
|
|
|
|
void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S,
|
|
|
|
const ObjCImplementationDecl *ImplD) {
|
|
|
|
if (S->hasUnrecoverableErrorOccurred())
|
2013-10-26 05:44:50 +08:00
|
|
|
return;
|
2014-01-04 02:32:18 +08:00
|
|
|
|
2014-03-14 03:50:17 +08:00
|
|
|
for (const auto *CurMethod : ImplD->instance_methods()) {
|
2014-01-04 02:32:18 +08:00
|
|
|
unsigned DIAG = diag::warn_unused_property_backing_ivar;
|
|
|
|
SourceLocation Loc = CurMethod->getLocation();
|
2014-06-16 07:30:39 +08:00
|
|
|
if (Diags.isIgnored(DIAG, Loc))
|
2014-01-04 02:32:18 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
const ObjCPropertyDecl *PDecl;
|
|
|
|
const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl);
|
|
|
|
if (!IV)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
UnusedBackingIvarChecker Checker(*this, CurMethod, IV);
|
|
|
|
Checker.TraverseStmt(CurMethod->getBody());
|
|
|
|
if (Checker.AccessedIvar)
|
|
|
|
continue;
|
|
|
|
|
2014-01-03 06:42:09 +08:00
|
|
|
// Do not issue this warning if backing ivar is used somewhere and accessor
|
2014-01-04 02:32:18 +08:00
|
|
|
// implementation makes a self call. This is to prevent false positive in
|
|
|
|
// cases where the ivar is accessed by another method that the accessor
|
|
|
|
// delegates to.
|
|
|
|
if (!IV->isReferenced() || !Checker.InvokedSelfMethod) {
|
2014-01-04 03:39:23 +08:00
|
|
|
Diag(Loc, DIAG) << IV;
|
2014-01-03 06:42:09 +08:00
|
|
|
Diag(PDecl->getLocation(), diag::note_property_declare);
|
|
|
|
}
|
2013-10-26 05:44:50 +08:00
|
|
|
}
|
|
|
|
}
|