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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-08-26 06:03:47 +08:00
|
|
|
#include "clang/Sema/SemaInternal.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"
|
2014-01-04 02:32:18 +08:00
|
|
|
#include "clang/AST/DataRecursiveASTVisitor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/AST/Expr.h"
|
|
|
|
#include "clang/AST/ExprObjC.h"
|
2011-06-16 07:02:42 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2012-04-07 02:12:22 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Sema/DeclSpec.h"
|
|
|
|
#include "clang/Sema/ExternalSemaSource.h"
|
|
|
|
#include "clang/Sema/Lookup.h"
|
|
|
|
#include "clang/Sema/Scope.h"
|
|
|
|
#include "clang/Sema/ScopeInfo.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?
|
|
|
|
|
|
|
|
const ObjCObjectType *result = method->getResultType()
|
|
|
|
->castAs<ObjCObjectPointerType>()->getObjectType();
|
|
|
|
|
|
|
|
if (result->isObjCId()) {
|
|
|
|
return false;
|
|
|
|
} else if (result->isObjCClass()) {
|
|
|
|
// fall through: always an error
|
|
|
|
} else {
|
|
|
|
ObjCInterfaceDecl *resultClass = result->getInterface();
|
|
|
|
assert(resultClass && "unexpected object type!");
|
|
|
|
|
|
|
|
// It's okay for the result type to still be a forward declaration
|
|
|
|
// if we're checking an interface declaration.
|
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.
|
|
|
|
const ObjCInterfaceDecl *receiverClass = 0;
|
|
|
|
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)) {
|
2014-01-16 21:03:14 +08:00
|
|
|
method->addAttr(UnavailableAttr::CreateImplicit(Context,
|
|
|
|
"init method returns a type unrelated to its receiver type",
|
|
|
|
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.
|
|
|
|
QualType ResultType = NewMethod->getResultType();
|
|
|
|
SourceRange ResultTypeRange;
|
|
|
|
if (const TypeSourceInfo *ResultTypeInfo
|
2011-06-16 07:02:42 +08:00
|
|
|
= NewMethod->getResultTypeSourceInfo())
|
2011-06-11 09:09:30 +08:00
|
|
|
ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
|
|
|
|
|
|
|
|
// 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:
|
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:
|
2013-04-04 09:38:37 +08:00
|
|
|
if (!Context.hasSameType(method->getResultType(), Context.VoidTy)) {
|
2012-07-31 04:52:48 +08:00
|
|
|
SourceRange ResultTypeRange;
|
|
|
|
if (const TypeSourceInfo *ResultTypeInfo
|
|
|
|
= method->getResultTypeSourceInfo())
|
|
|
|
ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
|
|
|
|
if (ResultTypeRange.isInvalid())
|
2013-04-04 09:38:37 +08:00
|
|
|
Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
|
2012-07-31 04:52:48 +08:00
|
|
|
<< method->getResultType()
|
|
|
|
<< FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
|
|
|
|
else
|
2013-04-04 09:38:37 +08:00
|
|
|
Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
|
2012-07-31 04:52:48 +08:00
|
|
|
<< method->getResultType()
|
|
|
|
<< FixItHint::CreateReplacement(ResultTypeRange, "void");
|
|
|
|
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) {
|
|
|
|
assert((getCurMethodDecl() == 0) && "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.
|
|
|
|
CheckParmsForFunctionDef(MDecl->param_begin(), MDecl->param_end(),
|
|
|
|
/*CheckParameterNames=*/false);
|
|
|
|
|
2008-04-10 10:22:51 +08:00
|
|
|
// Introduce all of the other parameters into this scope.
|
2009-02-21 02:43:26 +08:00
|
|
|
for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
|
2010-09-18 06:07:07 +08:00
|
|
|
E = MDecl->param_end(); PI != E; ++PI) {
|
|
|
|
ParmVarDecl *Param = (*PI);
|
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
|
|
|
|
2009-02-21 02:43:26 +08:00
|
|
|
if ((*PI)->getIdentifier())
|
|
|
|
PushOnScopeChains(*PI, 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:
|
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());
|
|
|
|
ObjCImplDecl *ImplDeclOfMethodDecl = 0;
|
|
|
|
if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
|
|
|
|
ImplDeclOfMethodDecl = OID->getImplementation();
|
|
|
|
else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl))
|
|
|
|
ImplDeclOfMethodDecl = CD->getImplementation();
|
|
|
|
// No need to issue deprecated warning if deprecated mehod in class/category
|
|
|
|
// is being implemented in its own implementation (no overriding is involved).
|
|
|
|
if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef)
|
|
|
|
DiagnoseObjCImplementedDeprecations(*this,
|
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;
|
|
|
|
getCurFunction()->ObjCWarnForNoDesignatedInitChain =
|
|
|
|
IC->getSuperClass() != 0;
|
|
|
|
} else if (IC->hasDesignatedInitializers()) {
|
|
|
|
getCurFunction()->ObjCIsSecondaryInit = true;
|
|
|
|
getCurFunction()->ObjCWarnForNoInitDelegation = true;
|
|
|
|
}
|
|
|
|
}
|
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:
|
|
|
|
ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
|
|
|
|
explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
|
|
|
|
: CurrentIDecl(IDecl) {}
|
|
|
|
|
|
|
|
virtual bool ValidateCandidate(const TypoCorrection &candidate) {
|
|
|
|
ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
|
|
|
|
return ID && !declaresSameEntity(ID, CurrentIDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ObjCInterfaceDecl *CurrentIDecl;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::
|
2008-07-22 06:17:28 +08:00
|
|
|
ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
|
|
|
|
IdentifierInfo *ClassName, SourceLocation ClassLoc,
|
|
|
|
IdentifierInfo *SuperName, SourceLocation SuperLoc,
|
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();
|
|
|
|
}
|
|
|
|
|
2011-12-16 04:29:51 +08:00
|
|
|
ObjCInterfaceDecl *IDecl
|
|
|
|
= ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
|
2011-12-16 11:12:41 +08:00
|
|
|
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) {
|
|
|
|
// Check if a different kind of symbol declared in this scope.
|
2010-04-16 06:33:43 +08:00
|
|
|
PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
|
|
|
|
LookupOrdinaryName);
|
2010-01-04 02:01:57 +08:00
|
|
|
|
|
|
|
if (!PrevDecl) {
|
2012-01-13 09:32:50 +08:00
|
|
|
// Try to correct for a typo in the superclass name without correcting
|
|
|
|
// to the class we're defining.
|
|
|
|
ObjCInterfaceValidatorCCC Validator(IDecl);
|
|
|
|
if (TypoCorrection Corrected = CorrectTypo(
|
2011-06-29 00:20:02 +08:00
|
|
|
DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
|
2012-02-01 07:49:25 +08:00
|
|
|
NULL, Validator)) {
|
2013-08-17 08:46:16 +08:00
|
|
|
diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest)
|
|
|
|
<< SuperName << ClassName);
|
2012-01-13 09:32:50 +08:00
|
|
|
PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-15 08:29:59 +08:00
|
|
|
if (declaresSameEntity(PrevDecl, IDecl)) {
|
2009-07-10 06:08:26 +08:00
|
|
|
Diag(SuperLoc, diag::err_recursive_superclass)
|
|
|
|
<< SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
|
2011-12-16 06:34:59 +08:00
|
|
|
IDecl->setEndOfDefinitionLoc(ClassLoc);
|
2009-08-05 05:02:39 +08:00
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCInterfaceDecl *SuperClassDecl =
|
2009-07-10 06:08:26 +08:00
|
|
|
dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
|
2008-11-19 16:23:25 +08:00
|
|
|
|
2009-07-10 06:08:26 +08:00
|
|
|
// Diagnose classes that inherit from deprecated classes.
|
|
|
|
if (SuperClassDecl)
|
|
|
|
(void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-10 06:08:26 +08:00
|
|
|
if (PrevDecl && SuperClassDecl == 0) {
|
|
|
|
// The previous declaration was not a class decl. Check if we have a
|
|
|
|
// typedef. If we do, get the underlying class type.
|
2011-04-15 22:24:37 +08:00
|
|
|
if (const TypedefNameDecl *TDecl =
|
|
|
|
dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
|
2009-07-10 06:08:26 +08:00
|
|
|
QualType T = TDecl->getUnderlyingType();
|
2010-05-15 19:32:37 +08:00
|
|
|
if (T->isObjCObjectType()) {
|
2013-04-05 02:45:52 +08:00
|
|
|
if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
|
2010-08-11 20:19:30 +08:00
|
|
|
SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
|
2013-04-05 02:45:52 +08:00
|
|
|
// 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);
|
|
|
|
}
|
2009-07-10 06:08:26 +08:00
|
|
|
}
|
2009-02-05 01:14:05 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-10 06:08:26 +08:00
|
|
|
// 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);
|
|
|
|
}
|
2009-02-05 01:14:05 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-04-15 22:24:37 +08:00
|
|
|
if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
|
2009-07-10 06:08:26 +08:00
|
|
|
if (!SuperClassDecl)
|
|
|
|
Diag(SuperLoc, diag::err_undef_superclass)
|
|
|
|
<< SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
|
2011-11-15 06:10:01 +08:00
|
|
|
else if (RequireCompleteType(SuperLoc,
|
2012-05-05 00:32:21 +08:00
|
|
|
Context.getObjCInterfaceType(SuperClassDecl),
|
|
|
|
diag::err_forward_superclass,
|
|
|
|
SuperClassDecl->getDeclName(),
|
|
|
|
ClassName,
|
|
|
|
SourceRange(AtInterfaceLoc, ClassLoc))) {
|
2011-06-24 07:16:19 +08:00
|
|
|
SuperClassDecl = 0;
|
|
|
|
}
|
2009-07-10 06:08:26 +08:00
|
|
|
}
|
|
|
|
IDecl->setSuperClass(SuperClassDecl);
|
|
|
|
IDecl->setSuperClassLoc(SuperLoc);
|
2011-12-16 06:34:59 +08:00
|
|
|
IDecl->setEndOfDefinitionLoc(SuperLoc);
|
2009-02-05 01:14:05 +08:00
|
|
|
}
|
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) {
|
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>())
|
|
|
|
for (ObjCObjectType::qual_iterator I = OPT->qual_begin(),
|
|
|
|
E = OPT->qual_end(); I != E; ++I)
|
|
|
|
ProtocolRefs.push_back(*I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
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);
|
|
|
|
if (CDecl == 0) {
|
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);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
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);
|
|
|
|
ObjCProtocolDecl *PDecl = 0;
|
|
|
|
if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
|
|
|
|
// 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,
|
2012-01-02 06:06:18 +08:00
|
|
|
/*PrevDecl=*/0);
|
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.
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// 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
|
2008-07-26 12:03:38 +08:00
|
|
|
Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
|
2008-07-22 06:17:28 +08:00
|
|
|
const IdentifierLocPair *ProtocolId,
|
2007-12-12 15:09:47 +08:00
|
|
|
unsigned NumProtocols,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<Decl *> &Protocols) {
|
2007-12-12 15:09:47 +08:00
|
|
|
for (unsigned i = 0; i != NumProtocols; ++i) {
|
2010-04-16 06:33:43 +08:00
|
|
|
ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
|
|
|
|
ProtocolId[i].second);
|
2010-01-04 02:01:57 +08:00
|
|
|
if (!PDecl) {
|
2012-01-13 09:32:50 +08:00
|
|
|
DeclFilterCCC<ObjCProtocolDecl> Validator;
|
2011-06-29 00:20:02 +08:00
|
|
|
TypoCorrection Corrected = CorrectTypo(
|
|
|
|
DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
|
2012-02-01 07:49:25 +08:00
|
|
|
LookupObjCProtocolName, TUScope, NULL, Validator);
|
2013-08-17 08:46:16 +08:00
|
|
|
if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>()))
|
|
|
|
diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest)
|
|
|
|
<< ProtocolId[i].first);
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
|
|
|
|
2008-07-26 11:47:43 +08:00
|
|
|
if (!PDecl) {
|
2008-11-19 13:08:23 +08:00
|
|
|
Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< ProtocolId[i].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();
|
|
|
|
|
2009-02-19 05:56:37 +08:00
|
|
|
(void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
|
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.
|
|
|
|
if (WarnOnDeclarations &&
|
|
|
|
(!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()))
|
2008-11-19 13:08:23 +08:00
|
|
|
Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
|
2008-11-19 16:23:25 +08:00
|
|
|
<< ProtocolId[i].first;
|
2010-08-21 17:40:31 +08:00
|
|
|
Protocols.push_back(PDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2009-06-30 10:36:12 +08:00
|
|
|
for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
|
|
|
|
e = ID->meth_end(); i != e; ++i) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCMethodDecl *MD = *i;
|
2009-03-03 03:05:07 +08:00
|
|
|
MethodMap[MD->getSelector()] = MD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MethodMap.empty())
|
|
|
|
return;
|
2009-06-30 10:36:12 +08:00
|
|
|
for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
|
|
|
|
e = CAT->meth_end(); i != e; ++i) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCMethodDecl *Method = *i;
|
2009-03-03 03:05:07 +08:00
|
|
|
const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
|
|
|
|
if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
|
|
|
|
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,
|
2008-07-22 06:17:28 +08:00
|
|
|
const IdentifierLocPair *IdentList,
|
2008-12-17 09:07:27 +08:00
|
|
|
unsigned NumElts,
|
|
|
|
AttributeList *attrList) {
|
2012-01-02 05:23:57 +08:00
|
|
|
SmallVector<Decl *, 8> DeclsInGroup;
|
2007-12-12 15:09:47 +08:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
2008-07-22 06:17:28 +08:00
|
|
|
IdentifierInfo *Ident = IdentList[i].first;
|
2012-01-02 04:30:41 +08:00
|
|
|
ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
|
|
|
|
ForRedeclaration);
|
|
|
|
ObjCProtocolDecl *PDecl
|
|
|
|
= ObjCProtocolDecl::Create(Context, CurContext, Ident,
|
|
|
|
IdentList[i].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,
|
|
|
|
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,
|
|
|
|
CategoryName == 0)) {
|
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,
|
2011-08-31 03:43:26 +08:00
|
|
|
ClassLoc, CategoryLoc, CategoryName,IDecl);
|
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
|
|
|
|
2011-08-31 03:43:26 +08:00
|
|
|
CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
|
|
|
|
ClassLoc, CategoryLoc, CategoryName, IDecl);
|
|
|
|
// FIXME: PushOnScopeChains?
|
|
|
|
CurContext->addDecl(CDecl);
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
if (NumProtoRefs) {
|
2012-09-06 23:59:27 +08:00
|
|
|
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);
|
2009-07-21 08:05:53 +08:00
|
|
|
ObjCCategoryDecl *CatIDecl = 0;
|
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,
|
2011-08-31 03:43:26 +08:00
|
|
|
CatName, IDecl);
|
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);
|
|
|
|
|
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) {
|
2013-08-17 08:46:16 +08:00
|
|
|
ObjCInterfaceDecl *IDecl = 0;
|
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))) {
|
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.
|
2012-01-13 09:32:50 +08:00
|
|
|
ObjCInterfaceValidatorCCC Validator;
|
2013-08-17 08:46:16 +08:00
|
|
|
TypoCorrection Corrected =
|
|
|
|
CorrectTypo(DeclarationNameInfo(ClassName, ClassLoc),
|
|
|
|
LookupOrdinaryName, TUScope, NULL, Validator);
|
|
|
|
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
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl* SDecl = 0;
|
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())
|
|
|
|
SDecl = 0;
|
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,
|
2011-12-16 11:12:41 +08:00
|
|
|
ClassName, /*PrevDecl=*/0, ClassLoc,
|
|
|
|
true);
|
2011-12-15 13:27:12 +08:00
|
|
|
IDecl->startDefinition();
|
2011-12-16 06:34:59 +08:00
|
|
|
if (SDecl) {
|
|
|
|
IDecl->setSuperClass(SDecl);
|
|
|
|
IDecl->setSuperClassLoc(SuperClassLoc);
|
|
|
|
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
|
|
|
}
|
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.
|
|
|
|
for (ObjCInterfaceDecl::visible_extensions_iterator
|
|
|
|
Ext = IDecl->visible_extensions_begin(),
|
|
|
|
ExtEnd = IDecl->visible_extensions_end();
|
|
|
|
Ext != ExtEnd; ++Ext) {
|
|
|
|
ObjCCategoryDecl *CDecl = *Ext;
|
|
|
|
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,
|
|
|
|
NamedDecl *NeededFor = 0) {
|
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());
|
|
|
|
}
|
|
|
|
|
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 &&
|
|
|
|
(MethodDecl->getObjCDeclQualifier() !=
|
|
|
|
MethodImpl->getObjCDeclQualifier())) {
|
2011-07-29 07:19:50 +08:00
|
|
|
if (Warn) {
|
2011-08-11 01:16:30 +08:00
|
|
|
S.Diag(MethodImpl->getLocation(),
|
|
|
|
(IsOverridingMode ?
|
|
|
|
diag::warn_conflicting_overriding_ret_type_modifiers
|
|
|
|
: diag::warn_conflicting_ret_type_modifiers))
|
|
|
|
<< MethodImpl->getDeclName()
|
2011-07-29 07:19:50 +08:00
|
|
|
<< getTypeRange(MethodImpl->getResultTypeSourceInfo());
|
|
|
|
S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
|
|
|
|
<< getTypeRange(MethodDecl->getResultTypeSourceInfo());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2011-02-22 07:49:15 +08:00
|
|
|
}
|
|
|
|
|
2010-10-28 10:34:38 +08:00
|
|
|
if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
|
2011-02-22 07:49:15 +08:00
|
|
|
MethodDecl->getResultType()))
|
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 =
|
|
|
|
MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
|
|
|
|
if (const ObjCObjectPointerType *IfacePtrTy =
|
2011-02-22 07:49:15 +08:00
|
|
|
MethodDecl->getResultType()->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
|
|
|
|
: diag::warn_non_covariant_ret_types;
|
2010-10-28 10:34:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
S.Diag(MethodImpl->getLocation(), DiagID)
|
|
|
|
<< MethodImpl->getDeclName()
|
2011-02-22 07:49:15 +08:00
|
|
|
<< MethodDecl->getResultType()
|
2010-10-28 10:34:38 +08:00
|
|
|
<< MethodImpl->getResultType()
|
|
|
|
<< getTypeRange(MethodImpl->getResultTypeSourceInfo());
|
2011-08-11 01:16:30 +08:00
|
|
|
S.Diag(MethodDecl->getLocation(),
|
|
|
|
IsOverridingMode ? diag::note_previous_declaration
|
|
|
|
: diag::note_previous_definition)
|
2011-02-22 07:49:15 +08:00
|
|
|
<< getTypeRange(MethodDecl->getResultTypeSourceInfo());
|
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 &&
|
|
|
|
(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();
|
2011-02-22 07:49:15 +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
|
|
|
|
: 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
|
|
|
|
: 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:
|
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.
|
|
|
|
if (unmatched->getResultType()->isObjCObjectPointerType()) {
|
|
|
|
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
|
|
|
|
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,
|
|
|
|
bool isExplicitProtocol = true) {
|
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();
|
2009-05-23 01:12:32 +08:00
|
|
|
ObjCInterfaceDecl *NSIDecl = 0;
|
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.
|
2013-12-13 14:26:14 +08:00
|
|
|
if (isExplicitProtocol && PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
|
2013-12-11 03:43:48 +08:00
|
|
|
Super = NULL;
|
2008-09-05 04:01:15 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// check unimplemented instance methods.
|
2009-05-23 01:12:32 +08:00
|
|
|
if (!NSIDecl)
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
|
2009-06-30 10:36:12 +08:00
|
|
|
E = PDecl->instmeth_end(); I != E; ++I) {
|
2009-05-23 01:12:32 +08:00
|
|
|
ObjCMethodDecl *method = *I;
|
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 */,
|
2013-12-11 03:43:48 +08:00
|
|
|
NULL /* 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;
|
2013-12-13 14:26:10 +08:00
|
|
|
if (S.Diags.getDiagnosticLevel(DIAG, ImpLoc)
|
2012-02-10 05:30:24 +08:00
|
|
|
!= DiagnosticsEngine::Ignored) {
|
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
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCProtocolDecl::classmeth_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
|
2009-04-10 05:40:53 +08:00
|
|
|
I != E; ++I) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCMethodDecl *method = *I;
|
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 */,
|
2013-12-11 03:43:48 +08:00
|
|
|
NULL /* 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;
|
2013-12-13 14:26:10 +08:00
|
|
|
if (S.Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine::Ignored) {
|
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.
|
|
|
|
for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
|
|
|
|
E = PDecl->protocol_end(); PI != E; ++PI)
|
2013-12-13 14:26:10 +08:00
|
|
|
CheckProtocolMethodDefs(S, ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap,
|
2013-12-13 14:26:14 +08:00
|
|
|
CDecl, /* isExplicitProtocl */ false);
|
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.
|
2009-06-30 10:36:12 +08:00
|
|
|
for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
|
|
|
|
E = CDecl->instmeth_end(); I != E; ++I) {
|
2013-10-14 23:16:10 +08:00
|
|
|
if (!InsMapSeen.insert((*I)->getSelector()))
|
|
|
|
continue;
|
2012-10-11 00:42:25 +08:00
|
|
|
if (!(*I)->isPropertyAccessor() &&
|
2009-05-02 04:07:12 +08:00
|
|
|
!InsMap.count((*I)->getSelector())) {
|
|
|
|
if (ImmediateClass)
|
2013-12-13 13:58:44 +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 =
|
2011-08-31 03:43:21 +08:00
|
|
|
IMPDecl->getInstanceMethod((*I)->getSelector());
|
|
|
|
assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
|
|
|
|
"Expected to find the method through lookup as well");
|
|
|
|
ObjCMethodDecl *MethodDecl = *I;
|
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)
|
2011-07-29 07:19:50 +08:00
|
|
|
WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
|
|
|
|
isa<ObjCProtocolDecl>(CDecl));
|
2012-10-11 00:42:25 +08:00
|
|
|
else if (!MethodDecl->isPropertyAccessor())
|
2011-07-29 07:19:50 +08:00
|
|
|
WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
|
2012-02-10 05:30:24 +08:00
|
|
|
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.
|
2013-10-14 23:16:10 +08:00
|
|
|
for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
|
|
|
|
E = CDecl->classmeth_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
if (!ClsMapSeen.insert((*I)->getSelector()))
|
|
|
|
continue;
|
2009-05-02 04:07:12 +08:00
|
|
|
if (!ClsMap.count((*I)->getSelector())) {
|
|
|
|
if (ImmediateClass)
|
2013-12-13 13:58:44 +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 =
|
|
|
|
IMPDecl->getClassMethod((*I)->getSelector());
|
2011-08-31 03:43:21 +08:00
|
|
|
assert(CDecl->getClassMethod((*I)->getSelector()) &&
|
|
|
|
"Expected to find the method through lookup as well");
|
|
|
|
ObjCMethodDecl *MethodDecl = *I;
|
2012-02-10 05:30:24 +08:00
|
|
|
if (!WarnCategoryMethodImpl)
|
2011-07-29 07:19:50 +08:00
|
|
|
WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
|
|
|
|
isa<ObjCProtocolDecl>(CDecl));
|
|
|
|
else
|
|
|
|
WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
|
2012-02-10 05:30:24 +08:00
|
|
|
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.
|
|
|
|
for (ObjCProtocolDecl::protocol_iterator
|
|
|
|
PI = PD->protocol_begin(), E = PD->protocol_end(); PI != E; ++PI)
|
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
|
|
|
IMPDecl, (*PI), IncompleteImpl, false,
|
|
|
|
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) {
|
|
|
|
for (ObjCInterfaceDecl::visible_categories_iterator
|
|
|
|
Cat = I->visible_categories_begin(),
|
|
|
|
CatEnd = I->visible_categories_end();
|
|
|
|
Cat != CatEnd; ++Cat) {
|
2012-10-24 07:06:22 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
2013-01-17 07:00:23 +08:00
|
|
|
IMPDecl, *Cat, IncompleteImpl, false,
|
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.
|
2013-01-17 07:00:23 +08:00
|
|
|
for (ObjCInterfaceDecl::visible_extensions_iterator
|
|
|
|
Ext = I->visible_extensions_begin(),
|
|
|
|
ExtEnd = I->visible_extensions_end();
|
|
|
|
Ext != ExtEnd; ++Ext) {
|
2012-10-24 07:06:22 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
2013-01-17 07:00:23 +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.
|
2010-09-01 09:21:15 +08:00
|
|
|
for (ObjCInterfaceDecl::all_protocol_iterator
|
|
|
|
PI = I->all_referenced_protocol_begin(),
|
|
|
|
E = I->all_referenced_protocol_end(); PI != E; ++PI)
|
2009-09-09 23:08:12 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
|
|
|
IMPDecl,
|
2012-02-10 05:30:24 +08:00
|
|
|
(*PI), IncompleteImpl, false,
|
|
|
|
WarnCategoryMethodImpl);
|
2011-07-25 04:53:26 +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
|
|
|
|
|
|
|
for (ObjCImplementationDecl::instmeth_iterator
|
|
|
|
I = CatIMPDecl->instmeth_begin(),
|
2013-12-06 04:52:31 +08:00
|
|
|
E = CatIMPDecl->instmeth_end(); I!=E; ++I) {
|
|
|
|
Selector Sel = (*I)->getSelector();
|
|
|
|
// 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
|
|
|
|
|
|
|
for (ObjCImplementationDecl::classmeth_iterator
|
|
|
|
I = CatIMPDecl->classmeth_begin(),
|
2013-12-06 04:52:31 +08:00
|
|
|
E = CatIMPDecl->classmeth_end(); I != E; ++I) {
|
|
|
|
Selector Sel = (*I)->getSelector();
|
|
|
|
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.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCImplementationDecl::instmeth_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
|
2007-12-13 01:58:05 +08:00
|
|
|
InsMap.insert((*I)->getSelector());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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.
|
2012-01-04 03:46:00 +08:00
|
|
|
if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
|
2012-06-20 14:18:46 +08:00
|
|
|
if (!(LangOpts.ObjCDefaultSynthProperties &&
|
|
|
|
LangOpts.ObjCRuntime.isNonFragile()) ||
|
|
|
|
IDecl->isObjCRequiresPropertyDefs())
|
2013-04-25 01:06:38 +08:00
|
|
|
DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
|
2010-01-20 09:51:55 +08:00
|
|
|
|
2012-05-27 21:28:52 +08:00
|
|
|
SelectorSet ClsMap;
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCImplementationDecl::classmeth_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = IMPDecl->classmeth_begin(),
|
|
|
|
E = IMPDecl->classmeth_end(); I != E; ++I)
|
2007-12-13 01:58:05 +08:00
|
|
|
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
|
|
|
|
2009-03-01 08:56:52 +08:00
|
|
|
if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
|
2010-09-01 09:21:15 +08:00
|
|
|
for (ObjCInterfaceDecl::all_protocol_iterator
|
|
|
|
PI = I->all_referenced_protocol_begin(),
|
|
|
|
E = I->all_referenced_protocol_end(); PI != E; ++PI)
|
2013-12-13 14:26:10 +08:00
|
|
|
CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), *PI,
|
|
|
|
IncompleteImpl, InsMap, ClsMap, I);
|
2009-03-01 08:56:52 +08:00
|
|
|
// Check class extensions (unnamed categories)
|
2013-01-17 07:00:23 +08:00
|
|
|
for (ObjCInterfaceDecl::visible_extensions_iterator
|
|
|
|
Ext = I->visible_extensions_begin(),
|
|
|
|
ExtEnd = I->visible_extensions_end();
|
|
|
|
Ext != ExtEnd; ++Ext) {
|
|
|
|
ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
|
|
|
|
}
|
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()) {
|
2009-10-06 05:32:49 +08:00
|
|
|
for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
|
|
|
|
E = C->protocol_end(); PI != E; ++PI)
|
2013-12-13 14:26:10 +08:00
|
|
|
CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), *PI,
|
|
|
|
IncompleteImpl, InsMap, ClsMap, CDecl);
|
2013-04-25 01:06:38 +08:00
|
|
|
DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
|
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
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// ActOnForwardClassDeclaration -
|
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,
|
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
|
|
|
|
// as this will force the intended behavior which is to lookup the typedef
|
|
|
|
// name.
|
|
|
|
if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
|
|
|
|
Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2011-12-16 04:29:51 +08:00
|
|
|
ObjCInterfaceDecl *IDecl
|
|
|
|
= ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
|
2013-06-19 05:26:33 +08:00
|
|
|
ClassName, 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.
|
|
|
|
if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
|
|
|
|
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.
|
|
|
|
if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
if (!matchTypes(Context, strategy,
|
|
|
|
left->getResultType(), right->getResultType()))
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-02 07:37:00 +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.
|
2013-04-27 08:10:12 +08:00
|
|
|
if (ObjCCategoryDecl *
|
|
|
|
CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
|
|
|
|
if (!CD->IsClassExtension() && List->getBits() < 2)
|
|
|
|
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.
|
|
|
|
if (List->Method == 0) {
|
|
|
|
List->Method = Method;
|
2013-04-17 08:08:58 +08:00
|
|
|
List->setNext(0);
|
2012-05-02 07:37:00 +08:00
|
|
|
return;
|
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;
|
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.
|
|
|
|
if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
|
|
|
|
continue;
|
|
|
|
|
2012-01-25 08:49:42 +08:00
|
|
|
if (!MatchTwoMethodDeclarations(Method, List->Method))
|
2012-01-25 08:19:56 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
ObjCMethodDecl *PrevObjCMethod = List->Method;
|
|
|
|
|
|
|
|
// Propagate the 'defined' bit.
|
|
|
|
if (Method->isDefined())
|
|
|
|
PrevObjCMethod->setDefined(true);
|
|
|
|
|
|
|
|
// If a method is deprecated, push it in the global pool.
|
|
|
|
// This is used for better diagnostics.
|
|
|
|
if (Method->isDeprecated()) {
|
|
|
|
if (!PrevObjCMethod->isDeprecated())
|
|
|
|
List->Method = Method;
|
|
|
|
}
|
|
|
|
// If new method is unavailable, push it into global pool
|
|
|
|
// unless previous one is deprecated.
|
|
|
|
if (Method->isUnavailable()) {
|
|
|
|
if (PrevObjCMethod->getAvailability() < AR_Deprecated)
|
|
|
|
List->Method = Method;
|
|
|
|
}
|
|
|
|
|
2012-05-02 07:37:00 +08:00
|
|
|
return;
|
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>();
|
2013-04-17 08:08:58 +08:00
|
|
|
Previous->setNext(new (Mem) ObjCMethodList(Method, 0));
|
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
|
|
|
}
|
|
|
|
|
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;
|
2012-01-25 08:19:56 +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.
|
|
|
|
return (chosen->getResultType()->isIntegerType());
|
|
|
|
}
|
|
|
|
|
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,
|
2010-08-03 07:18:59 +08:00
|
|
|
bool warn, 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())
|
|
|
|
return 0;
|
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()) {
|
2013-01-17 02:47:38 +08:00
|
|
|
if (M->Method && !M->Method->isHidden()) {
|
|
|
|
// If we're not supposed to warn about mismatches, we're done.
|
|
|
|
if (!warn)
|
|
|
|
return M->Method;
|
|
|
|
|
|
|
|
Methods.push_back(M->Method);
|
|
|
|
}
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-01-17 02:47:38 +08:00
|
|
|
// If there aren't any visible methods, we're done.
|
|
|
|
// FIXME: Recover if there are any known-but-hidden methods?
|
|
|
|
if (Methods.empty())
|
|
|
|
return 0;
|
2010-08-10 07:27:58 +08:00
|
|
|
|
2013-01-17 02:47:38 +08:00
|
|
|
if (Methods.size() == 1)
|
|
|
|
return Methods[0];
|
|
|
|
|
|
|
|
// We found multiple methods, so we may have to complain.
|
|
|
|
bool issueDiagnostic = false, issueError = false;
|
|
|
|
|
|
|
|
// We support a warning which complains about *any* difference in
|
|
|
|
// method signature.
|
|
|
|
bool strictSelectorMatch =
|
|
|
|
(receiverIdOrClass && warn &&
|
|
|
|
(Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
|
|
|
|
R.getBegin())
|
|
|
|
!= DiagnosticsEngine::Ignored));
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2010-08-10 07:27:58 +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
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
Diag(Methods[0]->getLocStart(),
|
|
|
|
issueError ? diag::note_possibility : diag::note_using)
|
|
|
|
<< Methods[0]->getSourceRange();
|
|
|
|
for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
|
|
|
|
Diag(Methods[I]->getLocStart(), diag::note_also_found)
|
|
|
|
<< Methods[I]->getSourceRange();
|
|
|
|
}
|
2009-04-25 05:10:55 +08:00
|
|
|
}
|
2013-01-17 02:47:38 +08:00
|
|
|
return Methods[0];
|
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())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
GlobalMethods &Methods = Pos->second;
|
|
|
|
|
|
|
|
if (Methods.first.Method && Methods.first.Method->isDefined())
|
|
|
|
return Methods.first.Method;
|
|
|
|
if (Methods.second.Method && Methods.second.Method->isDefined())
|
|
|
|
return Methods.second.Method;
|
2010-07-23 02:24:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != 0;
|
|
|
|
}
|
|
|
|
|
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())
|
|
|
|
return 0;
|
|
|
|
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
|
|
|
|
return 0;
|
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())
|
|
|
|
if (M->Method &&
|
2013-06-19 01:10:58 +08:00
|
|
|
(M->Method->getSelector().getNumArgs() == NumArgs) &&
|
|
|
|
(M->Method->getSelector() != Sel)) {
|
2013-06-18 23:31:36 +08:00
|
|
|
if (ObjectIsId)
|
|
|
|
Methods.push_back(M->Method);
|
|
|
|
else if (!ObjectIsClass &&
|
|
|
|
HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
|
|
|
|
Methods.push_back(M->Method);
|
|
|
|
}
|
2013-06-06 02:46:14 +08:00
|
|
|
// class methods
|
|
|
|
for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
|
|
|
|
if (M->Method &&
|
2013-06-19 01:10:58 +08:00
|
|
|
(M->Method->getSelector().getNumArgs() == NumArgs) &&
|
|
|
|
(M->Method->getSelector() != Sel)) {
|
2013-06-18 23:31:36 +08:00
|
|
|
if (ObjectIsClass)
|
|
|
|
Methods.push_back(M->Method);
|
|
|
|
else if (!ObjectIsId &&
|
|
|
|
HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
|
|
|
|
Methods.push_back(M->Method);
|
|
|
|
}
|
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]);
|
|
|
|
}
|
|
|
|
return (SelectedMethods.size() == 1) ? SelectedMethods[0] : NULL;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
|
|
|
|
IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCIvarDecl* Ivar = *IVI;
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
|
|
|
|
return Sema::OCK_ClassExtension;
|
|
|
|
else
|
|
|
|
return Sema::OCK_Category;
|
|
|
|
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)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
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.
|
|
|
|
for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
|
|
|
|
E = CDecl->prop_end();
|
|
|
|
I != E; ++I)
|
2012-06-07 04:45:41 +08:00
|
|
|
ProcessPropertyDecl(*I, CDecl);
|
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.
|
2013-01-17 07:00:23 +08:00
|
|
|
for (ObjCInterfaceDecl::visible_extensions_iterator
|
|
|
|
Ext = IDecl->visible_extensions_begin(),
|
|
|
|
ExtEnd = IDecl->visible_extensions_end();
|
|
|
|
Ext != ExtEnd; ++Ext) {
|
|
|
|
for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
|
|
|
|
E = Ext->prop_end(); I != E; ++I) {
|
2012-06-07 04:45:41 +08:00
|
|
|
ObjCPropertyDecl *Property = *I;
|
2010-12-12 02:39:37 +08:00
|
|
|
// Skip over properties declared @dynamic
|
|
|
|
if (const ObjCPropertyImplDecl *PIDecl
|
|
|
|
= IC->FindPropertyImplDecl(Property->getIdentifier()))
|
|
|
|
if (PIDecl->getPropertyImplementation()
|
|
|
|
== ObjCPropertyImplDecl::Dynamic)
|
|
|
|
continue;
|
2013-01-17 07:00:23 +08:00
|
|
|
|
|
|
|
for (ObjCInterfaceDecl::visible_extensions_iterator
|
|
|
|
Ext = IDecl->visible_extensions_begin(),
|
|
|
|
ExtEnd = IDecl->visible_extensions_end();
|
|
|
|
Ext != ExtEnd; ++Ext) {
|
|
|
|
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);
|
2013-12-04 05:11:54 +08:00
|
|
|
if (IDecl->hasDesignatedInitializers())
|
|
|
|
DiagnoseMissingDesignatedInitOverrides(IC, IDecl);
|
|
|
|
|
2012-04-07 02:12:22 +08:00
|
|
|
bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
|
|
|
|
if (IDecl->getSuperClass() == NULL) {
|
|
|
|
// This class has no superclass, so check that it has been marked with
|
|
|
|
// __attribute((objc_root_class)).
|
|
|
|
if (!HasRootClassAttr) {
|
|
|
|
SourceLocation DeclLoc(IDecl->getLocation());
|
|
|
|
SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
|
|
|
|
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) {
|
|
|
|
QualType ResultType = Method->getResultType();
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
if (!list.Method) return;
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-02-29 11:04:05 +08:00
|
|
|
typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::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,
|
2013-03-30 05:51:48 +08:00
|
|
|
for (ObjCInterfaceDecl::known_categories_iterator
|
|
|
|
cat = iface->known_categories_begin(),
|
|
|
|
catEnd = iface->known_categories_end();
|
2013-01-17 07:00:23 +08:00
|
|
|
cat != catEnd; ++cat) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
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);
|
|
|
|
}
|
|
|
|
|
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);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
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;
|
2010-03-08 22:59:44 +08:00
|
|
|
TypeSourceInfo *ResultTInfo = 0;
|
2009-02-21 06:59:16 +08:00
|
|
|
if (ReturnType) {
|
2010-03-08 22:59:44 +08:00
|
|
|
resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2013-06-15 05:14:10 +08:00
|
|
|
if (CheckFunctionReturnType(resultDeclType, MethodLoc))
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2013-06-15 05:14:10 +08:00
|
|
|
|
2011-09-08 09:46:34 +08:00
|
|
|
HasRelatedResultType = (resultDeclType == 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
|
|
|
|
|
|
|
ObjCMethodDecl* ObjCMethod =
|
2011-10-03 14:37:04 +08:00
|
|
|
ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
|
2011-10-03 14:36:36 +08:00
|
|
|
resultDeclType,
|
2010-03-08 22:59:44 +08:00
|
|
|
ResultTInfo,
|
2011-08-22 23:54:49 +08:00
|
|
|
CurContext,
|
2008-04-04 14:12:32 +08:00
|
|
|
MethodType == tok::minus, isVariadic,
|
2012-10-11 00:42:25 +08:00
|
|
|
/*isPropertyAccessor=*/false,
|
2011-08-18 03:25:08 +08:00
|
|
|
/*isImplicitlyDeclared=*/false, /*isDefined=*/false,
|
2011-06-11 09:09:30 +08:00
|
|
|
MethodDeclKind == tok::objc_optional
|
|
|
|
? ObjCMethodDecl::Optional
|
|
|
|
: ObjCMethodDecl::Required,
|
2011-09-08 09:46:34 +08:00
|
|
|
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();
|
|
|
|
DI = 0;
|
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.
|
2011-07-22 10:45:48 +08:00
|
|
|
const ObjCMethodDecl *PrevMethod = 0;
|
|
|
|
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
|
|
|
|
2011-10-22 09:21:15 +08:00
|
|
|
ObjCMethodDecl *IMD = 0;
|
|
|
|
if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
|
|
|
|
IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
|
|
|
|
ObjCMethod->isInstanceMethod());
|
2013-07-10 06:02:20 +08:00
|
|
|
if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
|
|
|
|
!ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
|
|
|
|
// merge the attribute into implementation.
|
2014-01-16 21:03:14 +08:00
|
|
|
ObjCMethod->addAttr(ObjCRequiresSuperAttr::CreateImplicit(Context,
|
|
|
|
ObjCMethod->getLocation()));
|
2013-07-10 06:02:20 +08:00
|
|
|
}
|
2013-12-18 06:44:28 +08:00
|
|
|
if (isa<ObjCCategoryImplDecl>(ImpDecl)) {
|
|
|
|
ObjCMethodFamily family = ObjCMethod->getMethodFamily();
|
2013-12-18 08:52:54 +08:00
|
|
|
if (family == OMF_dealloc && IMD && IMD->isOverriding())
|
2013-12-18 06:44:28 +08:00
|
|
|
Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category)
|
|
|
|
<< ObjCMethod->getDeclName();
|
|
|
|
}
|
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:
|
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:
|
|
|
|
InferRelatedResultType = ObjCMethod->isClassMethod();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OMF_init:
|
|
|
|
case OMF_autorelease:
|
|
|
|
case OMF_retain:
|
|
|
|
case OMF_self:
|
|
|
|
InferRelatedResultType = ObjCMethod->isInstanceMethod();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-22 10:45:48 +08:00
|
|
|
if (InferRelatedResultType)
|
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);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
|
|
|
|
ReferencedSelectors.begin(),
|
|
|
|
E = ReferencedSelectors.end(); S != E; ++S) {
|
|
|
|
Selector Sel = (*S).first;
|
|
|
|
if (!LookupImplementedMethodInGlobalPool(Sel))
|
|
|
|
Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
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())
|
|
|
|
return 0;
|
2013-10-26 05:44:50 +08:00
|
|
|
const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
|
|
|
|
if (!IDecl)
|
|
|
|
return 0;
|
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())
|
|
|
|
return 0;
|
2014-01-04 02:32:18 +08:00
|
|
|
if ((PDecl = Method->findPropertyDecl()))
|
2013-10-26 05:44:50 +08:00
|
|
|
return PDecl->getPropertyIvarDecl();
|
2014-01-04 02:32:18 +08:00
|
|
|
|
2013-10-26 05:44:50 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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 :
|
|
|
|
public DataRecursiveASTVisitor<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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
for (ObjCImplementationDecl::instmeth_iterator
|
|
|
|
MI = ImplD->instmeth_begin(),
|
|
|
|
ME = ImplD->instmeth_end(); MI != ME; ++MI) {
|
|
|
|
const ObjCMethodDecl *CurMethod = *MI;
|
|
|
|
unsigned DIAG = diag::warn_unused_property_backing_ivar;
|
|
|
|
SourceLocation Loc = CurMethod->getLocation();
|
|
|
|
if (Diags.getDiagnosticLevel(DIAG, Loc) == DiagnosticsEngine::Ignored)
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|