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"
|
2010-08-13 04:07:10 +08:00
|
|
|
#include "clang/Sema/Lookup.h"
|
2009-04-25 05:10:55 +08:00
|
|
|
#include "clang/Sema/ExternalSemaSource.h"
|
2010-08-24 16:50:51 +08:00
|
|
|
#include "clang/Sema/Scope.h"
|
2010-08-25 16:40:02 +08:00
|
|
|
#include "clang/Sema/ScopeInfo.h"
|
2011-06-16 07:02:42 +08:00
|
|
|
#include "clang/AST/ASTConsumer.h"
|
2009-03-03 22:49:36 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2011-06-16 07:02:42 +08:00
|
|
|
#include "clang/AST/ExprObjC.h"
|
2007-12-12 15:09:47 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
2011-06-16 07:02:42 +08:00
|
|
|
#include "clang/Basic/SourceManager.h"
|
2010-08-21 02:27:03 +08:00
|
|
|
#include "clang/Sema/DeclSpec.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.
|
|
|
|
if (resultClass->isForwardDecl()) {
|
|
|
|
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)) {
|
|
|
|
method->addAttr(new (Context) UnavailableAttr(loc, Context,
|
|
|
|
"init method returns a type unrelated to its receiver type"));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, it's an error.
|
|
|
|
Diag(loc, diag::err_arc_init_method_unrelated_result_type);
|
|
|
|
method->setInvalidDecl();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-06-11 09:09:30 +08:00
|
|
|
bool Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
|
|
|
|
const ObjCMethodDecl *Overridden,
|
|
|
|
bool IsImplementation) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
Diag(Overridden->getLocation(), diag::note_related_result_type_overridden)
|
|
|
|
<< Overridden->getMethodFamily();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-06-14 00:07:18 +08:00
|
|
|
/// \brief Check for consistency between a given method declaration and the
|
|
|
|
/// methods it overrides within the class hierarchy.
|
|
|
|
///
|
|
|
|
/// This method walks the inheritance hierarchy starting at the given
|
|
|
|
/// declaration context (\p DC), invoking Sema::CheckObjCMethodOverride() with
|
|
|
|
/// the given new method (\p NewMethod) and any method it directly overrides
|
|
|
|
/// in the hierarchy. Sema::CheckObjCMethodOverride() is responsible for
|
|
|
|
/// checking consistency, e.g., among return types for methods that return a
|
|
|
|
/// related result type.
|
2011-06-11 09:09:30 +08:00
|
|
|
static bool CheckObjCMethodOverrides(Sema &S, ObjCMethodDecl *NewMethod,
|
|
|
|
DeclContext *DC,
|
|
|
|
bool SkipCurrent = true) {
|
|
|
|
if (!DC)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!SkipCurrent) {
|
|
|
|
// Look for this method. If we find it, we're done.
|
|
|
|
Selector Sel = NewMethod->getSelector();
|
|
|
|
bool IsInstance = NewMethod->isInstanceMethod();
|
|
|
|
DeclContext::lookup_const_iterator Meth, MethEnd;
|
|
|
|
for (llvm::tie(Meth, MethEnd) = DC->lookup(Sel); Meth != MethEnd; ++Meth) {
|
|
|
|
ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(*Meth);
|
|
|
|
if (MD && MD->isInstanceMethod() == IsInstance)
|
|
|
|
return S.CheckObjCMethodOverride(NewMethod, MD, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ObjCInterfaceDecl *Class = llvm::dyn_cast<ObjCInterfaceDecl>(DC)) {
|
|
|
|
// Look through categories.
|
|
|
|
for (ObjCCategoryDecl *Category = Class->getCategoryList();
|
|
|
|
Category; Category = Category->getNextClassCategory()) {
|
|
|
|
if (CheckObjCMethodOverrides(S, NewMethod, Category, false))
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-06-11 09:09:30 +08:00
|
|
|
// Look through protocols.
|
|
|
|
for (ObjCList<ObjCProtocolDecl>::iterator I = Class->protocol_begin(),
|
2011-06-16 07:02:42 +08:00
|
|
|
IEnd = Class->protocol_end();
|
2011-06-11 09:09:30 +08:00
|
|
|
I != IEnd; ++I)
|
|
|
|
if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Look in our superclass.
|
|
|
|
return CheckObjCMethodOverrides(S, NewMethod, Class->getSuperClass(),
|
|
|
|
false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(DC)) {
|
|
|
|
// Look through protocols.
|
|
|
|
for (ObjCList<ObjCProtocolDecl>::iterator I = Category->protocol_begin(),
|
2011-06-16 07:02:42 +08:00
|
|
|
IEnd = Category->protocol_end();
|
2011-06-11 09:09:30 +08:00
|
|
|
I != IEnd; ++I)
|
|
|
|
if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(DC)) {
|
|
|
|
// Look through protocols.
|
|
|
|
for (ObjCList<ObjCProtocolDecl>::iterator I = Protocol->protocol_begin(),
|
2011-06-16 07:02:42 +08:00
|
|
|
IEnd = Protocol->protocol_end();
|
2011-06-11 09:09:30 +08:00
|
|
|
I != IEnd; ++I)
|
|
|
|
if (CheckObjCMethodOverrides(S, NewMethod, *I, false))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sema::CheckObjCMethodOverrides(ObjCMethodDecl *NewMethod,
|
|
|
|
DeclContext *DC) {
|
|
|
|
if (ObjCInterfaceDecl *Class = dyn_cast<ObjCInterfaceDecl>(DC))
|
|
|
|
return ::CheckObjCMethodOverrides(*this, NewMethod, Class);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-06-11 09:09:30 +08:00
|
|
|
if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(DC))
|
|
|
|
return ::CheckObjCMethodOverrides(*this, NewMethod, Category);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-06-11 09:09:30 +08:00
|
|
|
if (ObjCProtocolDecl *Protocol = dyn_cast<ObjCProtocolDecl>(DC))
|
|
|
|
return ::CheckObjCMethodOverrides(*this, NewMethod, Protocol);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-06-11 09:09:30 +08:00
|
|
|
if (ObjCImplementationDecl *Impl = dyn_cast<ObjCImplementationDecl>(DC))
|
|
|
|
return ::CheckObjCMethodOverrides(*this, NewMethod,
|
|
|
|
Impl->getClassInterface());
|
|
|
|
|
|
|
|
if (ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(DC))
|
|
|
|
return ::CheckObjCMethodOverrides(*this, NewMethod,
|
|
|
|
CatImpl->getClassInterface());
|
|
|
|
|
|
|
|
return ::CheckObjCMethodOverrides(*this, NewMethod, CurContext);
|
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
/// \brief Check a method declaration for compatibility with the Objective-C
|
|
|
|
/// ARC conventions.
|
|
|
|
static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
|
|
|
|
ObjCMethodFamily family = method->getMethodFamily();
|
|
|
|
switch (family) {
|
|
|
|
case OMF_None:
|
|
|
|
case OMF_dealloc:
|
|
|
|
case OMF_retain:
|
|
|
|
case OMF_release:
|
|
|
|
case OMF_autorelease:
|
|
|
|
case OMF_retainCount:
|
|
|
|
case OMF_self:
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case OMF_init:
|
|
|
|
// If the method doesn't obey the init rules, don't bother annotating it.
|
|
|
|
if (S.checkInitMethod(method, QualType()))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
|
|
|
|
S.Context));
|
|
|
|
|
|
|
|
// Don't add a second copy of this attribute, but otherwise don't
|
|
|
|
// let it be suppressed.
|
|
|
|
if (method->hasAttr<NSReturnsRetainedAttr>())
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OMF_alloc:
|
|
|
|
case OMF_copy:
|
|
|
|
case OMF_mutableCopy:
|
|
|
|
case OMF_new:
|
|
|
|
if (method->hasAttr<NSReturnsRetainedAttr>() ||
|
|
|
|
method->hasAttr<NSReturnsNotRetainedAttr>() ||
|
|
|
|
method->hasAttr<NSReturnsAutoreleasedAttr>())
|
|
|
|
return false;
|
|
|
|
break;
|
2011-07-06 06:38:59 +08:00
|
|
|
|
|
|
|
case OMF_performSelector:
|
|
|
|
// we don't annotate performSelector's
|
|
|
|
return true;
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
|
|
|
|
S.Context));
|
|
|
|
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)
|
|
|
|
S.Diag(ND->getLocation(), diag::note_method_declared_at);
|
|
|
|
else
|
|
|
|
S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-01 00:59:13 +08:00
|
|
|
/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
|
2007-12-12 15:09:47 +08:00
|
|
|
/// and user declared, in the method definition's AST.
|
2010-08-21 17:40:31 +08:00
|
|
|
void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
|
2008-06-28 14:07:14 +08:00
|
|
|
assert(getCurMethodDecl() == 0 && "Method parsing confused");
|
2010-08-21 17:40:31 +08:00
|
|
|
ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
|
2009-09-09 23:08:12 +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
|
|
|
|
|
|
|
// Allow the rest of sema to find private method decl implementations.
|
2009-01-10 01:18:27 +08:00
|
|
|
if (MDecl->isInstanceMethod())
|
2010-07-23 02:24:20 +08:00
|
|
|
AddInstanceMethodToGlobalPool(MDecl, true);
|
2007-12-18 09:30:32 +08:00
|
|
|
else
|
2010-07-23 02:24:20 +08:00
|
|
|
AddFactoryMethodToGlobalPool(MDecl, true);
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
if (!Param->isInvalidDecl() &&
|
|
|
|
RequireCompleteType(Param->getLocation(), Param->getType(),
|
|
|
|
diag::err_typecheck_decl_incomplete_type))
|
|
|
|
Param->setInvalidDecl();
|
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
|
|
|
|
if (getLangOptions().ObjCAutoRefCount) {
|
|
|
|
switch (MDecl->getMethodFamily()) {
|
|
|
|
case OMF_retain:
|
|
|
|
case OMF_retainCount:
|
|
|
|
case OMF_release:
|
|
|
|
case OMF_autorelease:
|
|
|
|
Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
|
|
|
|
<< MDecl->getSelector();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OMF_None:
|
|
|
|
case OMF_dealloc:
|
|
|
|
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-02-15 08:59:30 +08:00
|
|
|
// Warn on implementating deprecated methods under
|
|
|
|
// -Wdeprecated-implementations flag.
|
2011-02-16 01:49:58 +08:00
|
|
|
if (ObjCInterfaceDecl *IC = MDecl->getClassInterface())
|
|
|
|
if (ObjCMethodDecl *IMD =
|
2011-02-15 08:59:30 +08:00
|
|
|
IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
|
2011-02-16 01:49:58 +08:00
|
|
|
DiagnoseObjCImplementedDeprecations(*this,
|
|
|
|
dyn_cast<NamedDecl>(IMD),
|
|
|
|
MDecl->getLocation(), 0);
|
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
|
|
|
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
|
|
|
|
2010-08-11 20:19:30 +08:00
|
|
|
ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
|
|
|
|
if (IDecl) {
|
2007-12-12 15:09:47 +08:00
|
|
|
// Class already seen. Is it a forward declaration?
|
2010-08-11 20:19:30 +08:00
|
|
|
if (!IDecl->isForwardDecl()) {
|
|
|
|
IDecl->setInvalidDecl();
|
|
|
|
Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
|
|
|
|
Diag(IDecl->getLocation(), diag::note_previous_definition);
|
2008-11-24 06:46:27 +08:00
|
|
|
|
2010-08-11 20:19:30 +08:00
|
|
|
// Return the previous class interface.
|
|
|
|
// FIXME: don't leak the objects passed in!
|
2010-08-21 17:40:31 +08:00
|
|
|
return IDecl;
|
2010-08-11 20:19:30 +08:00
|
|
|
} else {
|
|
|
|
IDecl->setLocation(AtInterfaceLoc);
|
|
|
|
IDecl->setForwardDecl(false);
|
|
|
|
IDecl->setClassLoc(ClassLoc);
|
2010-08-13 08:28:03 +08:00
|
|
|
// If the forward decl was in a PCH, we need to write it again in a
|
2010-08-19 07:57:06 +08:00
|
|
|
// dependent AST file.
|
2010-08-13 08:28:03 +08:00
|
|
|
IDecl->setChangedSinceDeserialization(true);
|
2010-08-11 20:19:30 +08:00
|
|
|
|
|
|
|
// Since this ObjCInterfaceDecl was created by a forward declaration,
|
|
|
|
// we now add it to the DeclContext since it wasn't added before
|
|
|
|
// (see ActOnForwardClassDeclaration).
|
|
|
|
IDecl->setLexicalDeclContext(CurContext);
|
|
|
|
CurContext->addDecl(IDecl);
|
|
|
|
|
|
|
|
if (AttrList)
|
|
|
|
ProcessDeclAttributeList(TUScope, IDecl, AttrList);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2010-08-11 20:19:30 +08:00
|
|
|
} else {
|
|
|
|
IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
|
|
|
|
ClassName, ClassLoc);
|
|
|
|
if (AttrList)
|
|
|
|
ProcessDeclAttributeList(TUScope, IDecl, AttrList);
|
2010-08-10 05:55:28 +08:00
|
|
|
|
2010-08-11 20:19:30 +08:00
|
|
|
PushOnScopeChains(IDecl, TUScope);
|
|
|
|
}
|
2010-08-10 05:55:28 +08:00
|
|
|
|
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) {
|
|
|
|
// Try to correct for a typo in the superclass name.
|
2011-06-29 00:20:02 +08:00
|
|
|
TypoCorrection Corrected = CorrectTypo(
|
|
|
|
DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
|
|
|
|
NULL, NULL, false, CTC_NoKeywords);
|
|
|
|
if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
|
2010-01-04 02:01:57 +08:00
|
|
|
Diag(SuperLoc, diag::err_undef_superclass_suggest)
|
|
|
|
<< SuperName << ClassName << PrevDecl->getDeclName();
|
2010-01-07 08:17:44 +08:00
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_decl)
|
|
|
|
<< PrevDecl->getDeclName();
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-10 06:08:26 +08:00
|
|
|
if (PrevDecl == IDecl) {
|
|
|
|
Diag(SuperLoc, diag::err_recursive_superclass)
|
|
|
|
<< SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
|
|
|
|
IDecl->setLocEnd(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()) {
|
2010-08-11 20:19:30 +08:00
|
|
|
if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
|
|
|
|
SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
|
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-06-24 07:16:19 +08:00
|
|
|
else if (SuperClassDecl->isForwardDecl()) {
|
|
|
|
Diag(SuperLoc, diag::err_forward_superclass)
|
2009-07-10 06:08:26 +08:00
|
|
|
<< SuperClassDecl->getDeclName() << ClassName
|
|
|
|
<< SourceRange(AtInterfaceLoc, ClassLoc);
|
2011-06-24 07:16:19 +08:00
|
|
|
Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
|
|
|
|
SuperClassDecl = 0;
|
|
|
|
}
|
2009-07-10 06:08:26 +08:00
|
|
|
}
|
|
|
|
IDecl->setSuperClass(SuperClassDecl);
|
|
|
|
IDecl->setSuperClassLoc(SuperLoc);
|
|
|
|
IDecl->setLocEnd(SuperLoc);
|
2009-02-05 01:14:05 +08:00
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
} else { // we have a root class.
|
|
|
|
IDecl->setLocEnd(ClassLoc);
|
|
|
|
}
|
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) {
|
2009-02-21 05:35:13 +08:00
|
|
|
IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
|
2010-01-16 23:02:53 +08:00
|
|
|
ProtoLocs, Context);
|
2007-12-12 15:09:47 +08:00
|
|
|
IDecl->setLocEnd(EndProtoLoc);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
CheckObjCDeclScope(IDecl);
|
2010-08-21 17:40:31 +08:00
|
|
|
return IDecl;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ActOnCompatiblityAlias - this action is called after complete parsing of
|
2008-09-05 04:01:15 +08:00
|
|
|
/// @compatibility_alias declaration. It sets up the alias relationships.
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::ActOnCompatiblityAlias(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) {
|
2008-11-24 07:20:13 +08:00
|
|
|
if (isa<ObjCCompatibleAliasDecl>(ADecl))
|
2007-12-12 15:09:47 +08:00
|
|
|
Diag(AliasLocation, diag::warn_previous_alias_decl);
|
2008-11-24 07:20:13 +08:00
|
|
|
else
|
2008-11-19 16:23:25 +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
|
|
|
}
|
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");
|
2010-04-16 06:33:43 +08:00
|
|
|
ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
|
2007-12-12 15:09:47 +08:00
|
|
|
if (PDecl) {
|
|
|
|
// Protocol already seen. Better be a forward protocol declaration
|
2008-03-16 09:25:17 +08:00
|
|
|
if (!PDecl->isForwardDecl()) {
|
2009-04-07 07:43:32 +08:00
|
|
|
Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
|
2008-11-24 06:46:27 +08:00
|
|
|
Diag(PDecl->getLocation(), diag::note_previous_definition);
|
2008-03-16 09:25:17 +08:00
|
|
|
// Just return the protocol we already had.
|
|
|
|
// FIXME: don't leak the objects passed in!
|
2010-08-21 17:40:31 +08:00
|
|
|
return PDecl;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-03-05 23:22:01 +08:00
|
|
|
ObjCList<ObjCProtocolDecl> PList;
|
2009-09-09 23:08:12 +08:00
|
|
|
PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
|
2011-05-14 02:02:08 +08:00
|
|
|
err = CheckForwardProtocolDeclarationForCircularDependency(
|
|
|
|
ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-14 00:39:22 +08:00
|
|
|
// Make sure the cached decl gets a valid start location.
|
|
|
|
PDecl->setLocation(AtProtoInterfaceLoc);
|
2008-03-16 09:25:17 +08:00
|
|
|
PDecl->setForwardDecl(false);
|
2010-08-13 08:28:03 +08:00
|
|
|
CurContext->addDecl(PDecl);
|
2010-08-19 07:57:06 +08:00
|
|
|
// Repeat in dependent AST files.
|
2010-08-13 08:28:03 +08:00
|
|
|
PDecl->setChangedSinceDeserialization(true);
|
2008-03-16 09:25:17 +08:00
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
PDecl = ObjCProtocolDecl::Create(Context, CurContext,
|
2009-01-09 08:49:46 +08:00
|
|
|
AtProtoInterfaceLoc,ProtocolName);
|
2009-04-24 07:18:26 +08:00
|
|
|
PushOnScopeChains(PDecl, TUScope);
|
2008-03-17 04:19:15 +08:00
|
|
|
PDecl->setForwardDecl(false);
|
2008-03-16 09:23:04 +08:00
|
|
|
}
|
2008-12-17 09:07:27 +08:00
|
|
|
if (AttrList)
|
2009-06-18 05:51:59 +08:00
|
|
|
ProcessDeclAttributeList(TUScope, PDecl, AttrList);
|
2011-05-13 06:04:39 +08:00
|
|
|
if (!err && NumProtoRefs ) {
|
2008-03-17 04:19:15 +08:00
|
|
|
/// Check then save referenced protocols.
|
2010-01-16 23:02:53 +08:00
|
|
|
PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
|
|
|
|
ProtoLocs, Context);
|
2007-12-12 15:09:47 +08:00
|
|
|
PDecl->setLocEnd(EndProtoLoc);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
CheckObjCDeclScope(PDecl);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 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,
|
2010-08-21 17:40:31 +08:00
|
|
|
llvm::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) {
|
2011-06-29 00:20:02 +08:00
|
|
|
TypoCorrection Corrected = CorrectTypo(
|
|
|
|
DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
|
|
|
|
LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
|
|
|
|
if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
|
2010-01-04 02:01:57 +08:00
|
|
|
Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
|
2011-06-29 00:20:02 +08:00
|
|
|
<< ProtocolId[i].first << Corrected.getCorrection();
|
2010-01-07 08:17:44 +08:00
|
|
|
Diag(PDecl->getLocation(), diag::note_previous_decl)
|
|
|
|
<< PDecl->getDeclName();
|
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;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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.
|
|
|
|
if (WarnOnDeclarations && PDecl->isForwardDecl())
|
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) {
|
2009-03-03 03:05:07 +08:00
|
|
|
ObjCMethodDecl *MD = *i;
|
|
|
|
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) {
|
2009-03-03 03:05:07 +08:00
|
|
|
ObjCMethodDecl *Method = *i;
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-12 16:43:13 +08:00
|
|
|
/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *
|
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) {
|
2008-01-08 03:49:32 +08:00
|
|
|
llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
|
2010-01-16 23:02:53 +08:00
|
|
|
llvm::SmallVector<SourceLocation, 8> ProtoLocs;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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;
|
2010-04-16 06:33:43 +08:00
|
|
|
ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
|
2010-08-13 08:28:03 +08:00
|
|
|
bool isNew = false;
|
2009-01-09 08:49:46 +08:00
|
|
|
if (PDecl == 0) { // Not already seen?
|
2009-09-09 23:08:12 +08:00
|
|
|
PDecl = ObjCProtocolDecl::Create(Context, CurContext,
|
2009-01-09 08:49:46 +08:00
|
|
|
IdentList[i].second, Ident);
|
2010-08-13 08:28:03 +08:00
|
|
|
PushOnScopeChains(PDecl, TUScope, false);
|
|
|
|
isNew = true;
|
2009-01-09 08:49:46 +08:00
|
|
|
}
|
2010-08-13 08:28:03 +08:00
|
|
|
if (attrList) {
|
2009-06-18 05:51:59 +08:00
|
|
|
ProcessDeclAttributeList(TUScope, PDecl, attrList);
|
2010-08-13 08:28:03 +08:00
|
|
|
if (!isNew)
|
|
|
|
PDecl->setChangedSinceDeserialization(true);
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
Protocols.push_back(PDecl);
|
2010-01-16 23:02:53 +08:00
|
|
|
ProtoLocs.push_back(IdentList[i].second);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ObjCForwardProtocolDecl *PDecl =
|
2009-01-09 08:49:46 +08:00
|
|
|
ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
|
2010-01-16 23:02:53 +08:00
|
|
|
Protocols.data(), Protocols.size(),
|
|
|
|
ProtoLocs.data());
|
2009-06-30 10:36:12 +08:00
|
|
|
CurContext->addDecl(PDecl);
|
2008-11-05 00:57:32 +08:00
|
|
|
CheckObjCDeclScope(PDecl);
|
2010-08-21 17:40:31 +08:00
|
|
|
return PDecl;
|
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.
|
|
|
|
if (!IDecl || IDecl->isForwardDecl()) {
|
|
|
|
// Create an invalid ObjCCategoryDecl to serve as context for
|
|
|
|
// the enclosing method declarations. We mark the decl invalid
|
|
|
|
// to make it clear that this isn't a valid AST.
|
|
|
|
CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
|
|
|
|
ClassLoc, CategoryLoc, CategoryName);
|
|
|
|
CDecl->setInvalidDecl();
|
|
|
|
Diag(ClassLoc, diag::err_undef_interface) << ClassName;
|
2010-08-21 17:40:31 +08:00
|
|
|
return 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-06-23 07:20:40 +08:00
|
|
|
CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
|
|
|
|
ClassLoc, CategoryLoc, CategoryName);
|
|
|
|
// FIXME: PushOnScopeChains?
|
|
|
|
CurContext->addDecl(CDecl);
|
2010-02-16 05:55:26 +08:00
|
|
|
|
2010-06-23 07:20:40 +08:00
|
|
|
CDecl->setClassInterface(IDecl);
|
|
|
|
// Insert class extension to the list of class's categories.
|
|
|
|
if (!CategoryName)
|
|
|
|
CDecl->insertNextClassCategory();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-17 05:30:01 +08:00
|
|
|
// If the interface is deprecated, warn about it.
|
2009-02-19 05:56:37 +08:00
|
|
|
(void)DiagnoseUseOfDecl(IDecl, ClassLoc);
|
2009-02-17 05:26:43 +08:00
|
|
|
|
2010-02-16 05:55:26 +08:00
|
|
|
if (CategoryName) {
|
|
|
|
/// Check for duplicate interface declaration for this category
|
|
|
|
ObjCCategoryDecl *CDeclChain;
|
|
|
|
for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
|
|
|
|
CDeclChain = CDeclChain->getNextClassCategory()) {
|
|
|
|
if (CDeclChain->getIdentifier() == CategoryName) {
|
|
|
|
// Class extensions can be declared multiple times.
|
|
|
|
Diag(CategoryLoc, diag::warn_dup_category_def)
|
|
|
|
<< ClassName << CategoryName;
|
|
|
|
Diag(CDeclChain->getLocation(), diag::note_previous_definition);
|
|
|
|
break;
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2010-02-16 05:55:26 +08:00
|
|
|
if (!CDeclChain)
|
|
|
|
CDecl->insertNextClassCategory();
|
2008-01-18 04:33:24 +08:00
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
|
|
|
|
if (NumProtoRefs) {
|
2009-10-06 05:32:49 +08:00
|
|
|
CDecl->setProtocolList((ObjCProtocolDecl**)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())
|
2009-10-06 04:41:32 +08:00
|
|
|
IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)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);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 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;
|
|
|
|
if (IDecl) {
|
|
|
|
CatIDecl = IDecl->FindCategoryDeclaration(CatName);
|
|
|
|
if (!CatIDecl) {
|
|
|
|
// Category @implementation with no corresponding @interface.
|
|
|
|
// Create and install one.
|
|
|
|
CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
|
2010-01-17 00:38:58 +08:00
|
|
|
SourceLocation(), SourceLocation(),
|
2009-07-21 08:05:53 +08:00
|
|
|
CatName);
|
|
|
|
CatIDecl->setClassInterface(IDecl);
|
|
|
|
CatIDecl->insertNextClassCategory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCCategoryImplDecl *CDecl =
|
2009-01-09 08:49:46 +08:00
|
|
|
ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
|
|
|
|
IDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
/// Check that class of this category is already completely declared.
|
|
|
|
if (!IDecl || IDecl->isForwardDecl())
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(ClassLoc, diag::err_undef_interface) << ClassName;
|
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
|
|
|
|
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);
|
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);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 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) {
|
2010-08-11 20:19:30 +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))) {
|
|
|
|
// If this is a forward declaration of an interface, warn.
|
|
|
|
if (IDecl->isForwardDecl()) {
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
|
2010-08-11 20:19:30 +08:00
|
|
|
IDecl = 0;
|
2009-04-24 05:49:04 +08:00
|
|
|
}
|
2010-01-05 01:27:12 +08:00
|
|
|
} else {
|
|
|
|
// We did not find anything with the name ClassName; try to correct for
|
|
|
|
// typos in the class name.
|
2011-06-29 00:20:02 +08:00
|
|
|
TypoCorrection Corrected = CorrectTypo(
|
|
|
|
DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
|
|
|
|
NULL, NULL, false, CTC_NoKeywords);
|
|
|
|
if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
|
2010-01-07 07:44:25 +08:00
|
|
|
// Suggest the (potentially) correct interface name. However, put the
|
|
|
|
// fix-it hint itself in a separate note, since changing the name in
|
2010-08-11 20:19:30 +08:00
|
|
|
// the warning would make the fix-it change semantics.However, don't
|
2010-01-05 01:27:12 +08:00
|
|
|
// provide a code-modification hint or use the typo name for recovery,
|
|
|
|
// because this is just a warning. The program may actually be correct.
|
2011-06-29 00:20:02 +08:00
|
|
|
DeclarationName CorrectedName = Corrected.getCorrection();
|
2010-01-05 01:27:12 +08:00
|
|
|
Diag(ClassLoc, diag::warn_undef_interface_suggest)
|
2011-06-29 00:20:02 +08:00
|
|
|
<< ClassName << CorrectedName;
|
|
|
|
Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
|
|
|
|
<< FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
|
2010-01-05 01:27:12 +08:00
|
|
|
IDecl = 0;
|
|
|
|
} 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);
|
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;
|
2007-12-12 15:09:47 +08:00
|
|
|
else if (IDecl && IDecl->getSuperClass() != SDecl) {
|
|
|
|
// 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,
|
2010-08-11 20:19:30 +08:00
|
|
|
ClassName, ClassLoc, false, true);
|
2007-12-12 15:09:47 +08:00
|
|
|
IDecl->setSuperClass(SDecl);
|
|
|
|
IDecl->setLocEnd(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.
|
|
|
|
IDecl->setForwardDecl(false);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ObjCImplementationDecl* IMPDecl =
|
|
|
|
ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
|
2009-01-20 09:17:11 +08:00
|
|
|
IDecl, SDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
if (CheckObjCDeclScope(IMPDecl))
|
2010-08-21 17:40:31 +08:00
|
|
|
return 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);
|
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
|
|
|
}
|
2010-08-21 17:40:31 +08:00
|
|
|
return IMPDecl;
|
2007-12-12 15:09:47 +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;
|
|
|
|
/// Check case of non-existing @interface decl.
|
|
|
|
/// (legacy objective-c @implementation decl without an @interface decl).
|
|
|
|
/// Add implementations's ivar to the synthesize class's ivar list.
|
2009-04-21 04:09:33 +08:00
|
|
|
if (IDecl->isImplicitInterfaceDecl()) {
|
2009-02-21 05:35:13 +08:00
|
|
|
IDecl->setLocEnd(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);
|
|
|
|
IDecl->makeDeclVisibleInContext(ivars[i], false);
|
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");
|
2010-02-20 04:58:54 +08:00
|
|
|
if (LangOpts.ObjCNonFragileABI2) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
// Instance ivar to Implementation's DeclContext.
|
|
|
|
ImplIvar->setLexicalDeclContext(ImpDecl);
|
|
|
|
IDecl->makeDeclVisibleInContext(ImplIvar, false);
|
|
|
|
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++];
|
|
|
|
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.
|
2008-07-27 08:05:05 +08:00
|
|
|
if (Context.getCanonicalType(ImplIvar->getType()) !=
|
|
|
|
Context.getCanonicalType(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);
|
2009-03-03 22:49:36 +08:00
|
|
|
} else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
|
|
|
|
Expr *ImplBitWidth = ImplIvar->getBitWidth();
|
|
|
|
Expr *ClsBitWidth = ClsIvar->getBitWidth();
|
2009-04-27 03:19:15 +08:00
|
|
|
if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
|
|
|
|
ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
|
2009-03-03 22:49:36 +08:00
|
|
|
Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
|
|
|
|
<< ImplIvar->getIdentifier();
|
|
|
|
Diag(ClsBitWidth->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)
|
2007-12-13 02:19:52 +08:00
|
|
|
Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
|
2007-12-13 02:11:49 +08:00
|
|
|
else if (IVI != IVE)
|
2007-12-13 02:19:52 +08:00
|
|
|
Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2008-02-11 05:38:56 +08:00
|
|
|
void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
|
2010-04-01 02:23:33 +08:00
|
|
|
bool &IncompleteImpl, unsigned DiagID) {
|
2011-06-25 04:31:37 +08:00
|
|
|
// No point warning no definition of method which is 'unavailable'.
|
|
|
|
if (method->hasAttr<UnavailableAttr>())
|
|
|
|
return;
|
2008-02-11 05:38:56 +08:00
|
|
|
if (!IncompleteImpl) {
|
|
|
|
Diag(ImpLoc, diag::warn_incomplete_impl);
|
|
|
|
IncompleteImpl = true;
|
|
|
|
}
|
2010-10-30 07:20:05 +08:00
|
|
|
if (DiagID == diag::warn_unimplemented_protocol_method)
|
|
|
|
Diag(ImpLoc, DiagID) << method->getDeclName();
|
|
|
|
else
|
|
|
|
Diag(method->getLocation(), DiagID) << method->getDeclName();
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckMethodOverrideReturn(Sema &S,
|
|
|
|
ObjCMethodDecl *MethodImpl,
|
2011-02-22 07:49:15 +08:00
|
|
|
ObjCMethodDecl *MethodDecl,
|
|
|
|
bool IsProtocolMethodDecl) {
|
|
|
|
if (IsProtocolMethodDecl &&
|
|
|
|
(MethodDecl->getObjCDeclQualifier() !=
|
|
|
|
MethodImpl->getObjCDeclQualifier())) {
|
|
|
|
S.Diag(MethodImpl->getLocation(),
|
|
|
|
diag::warn_conflicting_ret_type_modifiers)
|
|
|
|
<< MethodImpl->getDeclName()
|
|
|
|
<< getTypeRange(MethodImpl->getResultTypeSourceInfo());
|
|
|
|
S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
|
|
|
|
<< getTypeRange(MethodDecl->getResultTypeSourceInfo());
|
|
|
|
}
|
|
|
|
|
2010-10-28 10:34:38 +08:00
|
|
|
if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
|
2011-02-22 07:49:15 +08:00
|
|
|
MethodDecl->getResultType()))
|
2010-10-28 10:34:38 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned DiagID = diag::warn_conflicting_ret_types;
|
|
|
|
|
|
|
|
// Mismatches between ObjC pointers go into a different warning
|
|
|
|
// category, and sometimes they're even completely whitelisted.
|
|
|
|
if (const ObjCObjectPointerType *ImplPtrTy =
|
|
|
|
MethodImpl->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))
|
|
|
|
return;
|
|
|
|
|
|
|
|
DiagID = diag::warn_non_covariant_ret_types;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-02-22 07:49:15 +08:00
|
|
|
S.Diag(MethodDecl->getLocation(), diag::note_previous_definition)
|
|
|
|
<< getTypeRange(MethodDecl->getResultTypeSourceInfo());
|
2010-10-28 10:34:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void CheckMethodOverrideParam(Sema &S,
|
|
|
|
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,
|
|
|
|
bool IsProtocolMethodDecl) {
|
|
|
|
if (IsProtocolMethodDecl &&
|
|
|
|
(ImplVar->getObjCDeclQualifier() !=
|
|
|
|
IfaceVar->getObjCDeclQualifier())) {
|
|
|
|
S.Diag(ImplVar->getLocation(),
|
|
|
|
diag::warn_conflicting_param_modifiers)
|
|
|
|
<< getTypeRange(ImplVar->getTypeSourceInfo())
|
|
|
|
<< MethodImpl->getDeclName();
|
|
|
|
S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
|
|
|
|
<< getTypeRange(IfaceVar->getTypeSourceInfo());
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned DiagID = diag::warn_conflicting_param_types;
|
|
|
|
|
|
|
|
// Mismatches between ObjC pointers go into a different warning
|
|
|
|
// category, and sometimes they're even completely whitelisted.
|
|
|
|
if (const ObjCObjectPointerType *ImplPtrTy =
|
|
|
|
ImplTy->getAs<ObjCObjectPointerType>()) {
|
|
|
|
if (const ObjCObjectPointerType *IfacePtrTy =
|
|
|
|
IfaceTy->getAs<ObjCObjectPointerType>()) {
|
|
|
|
// Allow non-matching argument types as long as they don't
|
|
|
|
// violate the principle of substitutability. Specifically, the
|
|
|
|
// implementation must accept any objects that the superclass
|
|
|
|
// accepts, however it may also accept others.
|
|
|
|
if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
|
|
|
|
return;
|
|
|
|
|
|
|
|
DiagID = diag::warn_non_contravariant_param_types;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
S.Diag(ImplVar->getLocation(), DiagID)
|
|
|
|
<< getTypeRange(ImplVar->getTypeSourceInfo())
|
|
|
|
<< MethodImpl->getDeclName() << IfaceTy << ImplTy;
|
|
|
|
S.Diag(IfaceVar->getLocation(), diag::note_previous_definition)
|
|
|
|
<< getTypeRange(IfaceVar->getTypeSourceInfo());
|
|
|
|
}
|
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:
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
|
|
|
|
S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
|
|
|
|
|
|
|
|
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,
|
|
|
|
bool IsProtocolMethodDecl) {
|
2011-06-16 07:02:42 +08:00
|
|
|
if (getLangOptions().ObjCAutoRefCount &&
|
|
|
|
checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
|
|
|
|
return;
|
|
|
|
|
2011-02-22 07:49:15 +08:00
|
|
|
CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
|
|
|
|
IsProtocolMethodDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 03:58:42 +08:00
|
|
|
for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
|
2011-02-22 07:49:15 +08:00
|
|
|
IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
|
2010-10-28 10:34:38 +08:00
|
|
|
IM != EM; ++IM, ++IF)
|
2011-02-22 07:49:15 +08:00
|
|
|
CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
|
|
|
|
IsProtocolMethodDecl);
|
2010-10-26 01:23:52 +08:00
|
|
|
|
2011-02-22 07:49:15 +08:00
|
|
|
if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
|
2010-05-22 07:28:58 +08:00
|
|
|
Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
|
2011-02-22 07:49:15 +08:00
|
|
|
Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
|
2010-05-22 07:28:58 +08:00
|
|
|
}
|
2008-12-06 02:18:52 +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.
|
2008-02-09 06:06:17 +08:00
|
|
|
void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
|
|
|
|
ObjCProtocolDecl *PDecl,
|
2007-12-12 15:09:47 +08:00
|
|
|
bool& IncompleteImpl,
|
2008-02-09 06:06:17 +08:00
|
|
|
const llvm::DenseSet<Selector> &InsMap,
|
2008-09-05 04:01:15 +08:00
|
|
|
const llvm::DenseSet<Selector> &ClsMap,
|
2010-03-28 05:10:05 +08:00
|
|
|
ObjCContainerDecl *CDecl) {
|
|
|
|
ObjCInterfaceDecl *IDecl;
|
|
|
|
if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
|
|
|
|
IDecl = C->getClassInterface();
|
|
|
|
else
|
|
|
|
IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
|
|
|
|
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;
|
|
|
|
if (getLangOptions().NeXTRuntime) {
|
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.
|
|
|
|
IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
|
|
|
|
Selector fISelector = Context.Selectors.getSelector(1, &II);
|
|
|
|
if (InsMap.count(fISelector))
|
|
|
|
// Is IDecl derived from 'NSProxy'? If so, no instance methods
|
|
|
|
// need be implemented in the implementation.
|
|
|
|
NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-05 04:01:15 +08:00
|
|
|
// If a method lookup fails locally we still need to look and see if
|
|
|
|
// the method was implemented by a base class or an inherited
|
|
|
|
// protocol. This lookup is slow, but occurs rarely in correct code
|
|
|
|
// and otherwise would terminate in a warning.
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// check unimplemented instance methods.
|
2009-05-23 01:12:32 +08:00
|
|
|
if (!NSIDecl)
|
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 &&
|
2009-05-23 01:12:32 +08:00
|
|
|
!method->isSynthesized() && !InsMap.count(method->getSelector()) &&
|
2009-09-09 23:08:12 +08:00
|
|
|
(!Super ||
|
2009-06-30 10:36:12 +08:00
|
|
|
!Super->lookupInstanceMethod(method->getSelector()))) {
|
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.
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCMethodDecl *MethodInClass =
|
2009-06-30 10:36:12 +08:00
|
|
|
IDecl->lookupInstanceMethod(method->getSelector());
|
2010-03-28 03:02:17 +08:00
|
|
|
if (!MethodInClass || !MethodInClass->isSynthesized()) {
|
2010-04-01 02:23:33 +08:00
|
|
|
unsigned DIAG = diag::warn_unimplemented_protocol_method;
|
2010-12-16 02:44:22 +08:00
|
|
|
if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
|
|
|
|
!= Diagnostic::Ignored) {
|
2010-04-01 02:23:33 +08:00
|
|
|
WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
|
2010-10-30 07:20:05 +08:00
|
|
|
Diag(method->getLocation(), diag::note_method_declared_at);
|
2010-04-01 02:23:33 +08:00
|
|
|
Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
|
|
|
|
<< PDecl->getDeclName();
|
|
|
|
}
|
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()) &&
|
2010-03-28 03:02:17 +08:00
|
|
|
(!Super || !Super->lookupClassMethod(method->getSelector()))) {
|
2010-04-01 02:23:33 +08:00
|
|
|
unsigned DIAG = diag::warn_unimplemented_protocol_method;
|
2010-12-16 02:44:22 +08:00
|
|
|
if (Diags.getDiagnosticLevel(DIAG, ImpLoc) != Diagnostic::Ignored) {
|
2010-04-01 02:23:33 +08:00
|
|
|
WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
|
2010-10-30 07:20:05 +08:00
|
|
|
Diag(method->getLocation(), diag::note_method_declared_at);
|
2010-04-01 02:23:33 +08:00
|
|
|
Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
|
|
|
|
PDecl->getDeclName();
|
|
|
|
}
|
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)
|
2008-09-05 04:01:15 +08:00
|
|
|
CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
|
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.
|
|
|
|
///
|
|
|
|
void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
|
|
|
|
const llvm::DenseSet<Selector> &ClsMap,
|
|
|
|
llvm::DenseSet<Selector> &InsMapSeen,
|
|
|
|
llvm::DenseSet<Selector> &ClsMapSeen,
|
|
|
|
ObjCImplDecl* IMPDecl,
|
|
|
|
ObjCContainerDecl* CDecl,
|
|
|
|
bool &IncompleteImpl,
|
2009-09-09 23:08:12 +08:00
|
|
|
bool ImmediateClass) {
|
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) {
|
2009-05-02 04:07:12 +08:00
|
|
|
if (InsMapSeen.count((*I)->getSelector()))
|
|
|
|
continue;
|
|
|
|
InsMapSeen.insert((*I)->getSelector());
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!(*I)->isSynthesized() &&
|
2009-05-02 04:07:12 +08:00
|
|
|
!InsMap.count((*I)->getSelector())) {
|
|
|
|
if (ImmediateClass)
|
2010-04-01 02:23:33 +08:00
|
|
|
WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
|
|
|
|
diag::note_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 =
|
2009-06-30 10:36:12 +08:00
|
|
|
IMPDecl->getInstanceMethod((*I)->getSelector());
|
2011-02-22 07:49:15 +08:00
|
|
|
ObjCMethodDecl *MethodDecl =
|
2009-06-30 10:36:12 +08:00
|
|
|
CDecl->getInstanceMethod((*I)->getSelector());
|
2011-02-22 07:49:15 +08:00
|
|
|
assert(MethodDecl &&
|
|
|
|
"MethodDecl is null in ImplMethodsVsClassMethods");
|
2009-05-02 04:07:12 +08:00
|
|
|
// ImpMethodDecl may be null as in a @dynamic property.
|
|
|
|
if (ImpMethodDecl)
|
2011-02-22 07:49:15 +08:00
|
|
|
WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
|
|
|
|
isa<ObjCProtocolDecl>(CDecl));
|
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.
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCInterfaceDecl::classmeth_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
|
2009-05-02 04:07:12 +08:00
|
|
|
if (ClsMapSeen.count((*I)->getSelector()))
|
|
|
|
continue;
|
|
|
|
ClsMapSeen.insert((*I)->getSelector());
|
|
|
|
if (!ClsMap.count((*I)->getSelector())) {
|
|
|
|
if (ImmediateClass)
|
2010-04-01 02:23:33 +08:00
|
|
|
WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
|
|
|
|
diag::note_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-02-22 07:49:15 +08:00
|
|
|
ObjCMethodDecl *MethodDecl =
|
2009-06-30 10:36:12 +08:00
|
|
|
CDecl->getClassMethod((*I)->getSelector());
|
2011-02-22 07:49:15 +08:00
|
|
|
WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
|
|
|
|
isa<ObjCProtocolDecl>(CDecl));
|
2009-05-02 04:07:12 +08:00
|
|
|
}
|
|
|
|
}
|
2010-10-09 06:59:25 +08:00
|
|
|
|
2009-05-02 04:07:12 +08:00
|
|
|
if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
|
2010-10-09 06:59:25 +08:00
|
|
|
// Also methods in class extensions need be looked at next.
|
|
|
|
for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
|
|
|
|
ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
|
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
|
|
|
IMPDecl,
|
|
|
|
const_cast<ObjCCategoryDecl *>(ClsExtDecl),
|
|
|
|
IncompleteImpl, false);
|
|
|
|
|
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,
|
2009-05-02 04:07:12 +08:00
|
|
|
(*PI), IncompleteImpl, false);
|
|
|
|
if (I->getSuperClass())
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2007-12-12 15:09:47 +08:00
|
|
|
llvm::DenseSet<Selector> InsMap;
|
|
|
|
// 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.
|
2010-12-24 05:35:43 +08:00
|
|
|
if (isa<ObjCInterfaceDecl>(CDecl) &&
|
|
|
|
!(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
|
2010-05-06 05:52:17 +08:00
|
|
|
DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
|
2010-01-20 09:51:55 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
llvm::DenseSet<Selector> 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.
|
|
|
|
llvm::DenseSet<Selector> 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);
|
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)
|
2009-09-09 23:08:12 +08:00
|
|
|
CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
|
2009-03-01 08:56:52 +08:00
|
|
|
InsMap, ClsMap, I);
|
|
|
|
// Check class extensions (unnamed categories)
|
2010-06-23 07:20:40 +08:00
|
|
|
for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
|
|
|
|
Categories; Categories = Categories->getNextClassExtension())
|
|
|
|
ImplMethodsVsClassMethods(S, IMPDecl,
|
|
|
|
const_cast<ObjCCategoryDecl*>(Categories),
|
|
|
|
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)
|
|
|
|
CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
|
2010-03-28 05:10:05 +08:00
|
|
|
InsMap, ClsMap, CDecl);
|
2010-01-21 03:36:21 +08:00
|
|
|
// Report unimplemented properties in the category as well.
|
|
|
|
// When reporting on missing setter/getters, do not report when
|
|
|
|
// setter/getter is implemented in category's primary class
|
|
|
|
// implementation.
|
|
|
|
if (ObjCInterfaceDecl *ID = C->getClassInterface())
|
|
|
|
if (ObjCImplDecl *IMP = ID->getImplementation()) {
|
|
|
|
for (ObjCImplementationDecl::instmeth_iterator
|
|
|
|
I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
|
|
|
|
InsMap.insert((*I)->getSelector());
|
|
|
|
}
|
2010-05-06 05:52:17 +08:00
|
|
|
DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
|
2010-01-21 03:36:21 +08:00
|
|
|
}
|
2009-03-01 08:56:52 +08:00
|
|
|
} else
|
|
|
|
assert(false && "invalid ObjCContainerDecl type.");
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// ActOnForwardClassDeclaration -
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *
|
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) {
|
2008-01-08 03:49:32 +08:00
|
|
|
llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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-12-09 02:40:42 +08:00
|
|
|
if (PrevDecl && PrevDecl->isTemplateParameter()) {
|
2008-12-06 02:15:24 +08:00
|
|
|
// Maybe we will complain about the shadowed template parameter.
|
|
|
|
DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
|
|
|
|
// Just pretend that we didn't see the previous declaration.
|
|
|
|
PrevDecl = 0;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
//
|
2009-09-09 23:08:12 +08:00
|
|
|
// FIXME: Make an extension?
|
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
|
|
|
|
// to the underlying class.
|
2010-05-15 19:32:37 +08:00
|
|
|
if (const ObjCObjectType *OI =
|
|
|
|
TDD->getUnderlyingType()->getAs<ObjCObjectType>())
|
|
|
|
PrevDecl = OI->getInterface();
|
2009-05-08 05:49:26 +08:00
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2010-08-11 20:19:30 +08:00
|
|
|
ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
|
|
|
|
if (!IDecl) { // Not already seen? Make a forward decl.
|
|
|
|
IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
|
|
|
|
IdentList[i], IdentLocs[i], true);
|
|
|
|
|
|
|
|
// Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
|
|
|
|
// the current DeclContext. This prevents clients that walk DeclContext
|
|
|
|
// from seeing the imaginary ObjCInterfaceDecl until it is actually
|
|
|
|
// declared later (if at all). We also take care to explicitly make
|
|
|
|
// sure this declaration is visible for name lookup.
|
|
|
|
PushOnScopeChains(IDecl, TUScope, false);
|
|
|
|
CurContext->makeDeclVisibleInContext(IDecl, true);
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
|
|
|
|
Interfaces.push_back(IDecl);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-18 08:28:11 +08:00
|
|
|
assert(Interfaces.size() == NumElts);
|
2009-01-09 08:49:46 +08:00
|
|
|
ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
|
2009-11-18 08:28:11 +08:00
|
|
|
Interfaces.data(), IdentLocs,
|
2008-11-05 00:57:32 +08:00
|
|
|
Interfaces.size());
|
2009-06-30 10:36:12 +08:00
|
|
|
CurContext->addDecl(CDecl);
|
2008-11-05 00:57:32 +08:00
|
|
|
CheckObjCDeclScope(CDecl);
|
2010-08-21 17:40:31 +08:00
|
|
|
return CDecl;
|
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
|
|
|
|
|
|
|
// Make scalars agree in kind, except count bools as chars.
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
if (getLangOptions().ObjCAutoRefCount &&
|
|
|
|
(left->hasAttr<NSReturnsRetainedAttr>()
|
|
|
|
!= right->hasAttr<NSReturnsRetainedAttr>() ||
|
|
|
|
left->hasAttr<NSConsumesSelfAttr>()
|
|
|
|
!= right->hasAttr<NSConsumesSelfAttr>()))
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
ObjCMethodDecl::param_iterator
|
|
|
|
li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
|
|
|
|
|
|
|
|
for (; li != le; ++li, ++ri) {
|
|
|
|
assert(ri != right->param_end() && "Param mismatch");
|
|
|
|
ParmVarDecl *lparm = *li, *rparm = *ri;
|
|
|
|
|
|
|
|
if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (getLangOptions().ObjCAutoRefCount &&
|
|
|
|
lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
|
|
|
|
return false;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-08-03 07:18:59 +08:00
|
|
|
/// \brief Read the contents of the method pool for a given selector from
|
|
|
|
/// external storage.
|
2009-04-25 05:10:55 +08:00
|
|
|
///
|
2010-08-03 07:18:59 +08:00
|
|
|
/// This routine should only be called once, when the method pool has no entry
|
|
|
|
/// for this selector.
|
|
|
|
Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
|
2009-04-25 05:10:55 +08:00
|
|
|
assert(ExternalSource && "We need an external AST source");
|
2010-08-03 07:18:59 +08:00
|
|
|
assert(MethodPool.find(Sel) == MethodPool.end() &&
|
|
|
|
"Selector data already loaded into the method pool");
|
2009-04-25 05:10:55 +08:00
|
|
|
|
|
|
|
// Read the method list from the external source.
|
2010-08-03 07:18:59 +08:00
|
|
|
GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-03 07:18:59 +08:00
|
|
|
return MethodPool.insert(std::make_pair(Sel, Methods)).first;
|
2009-04-25 05:10:55 +08:00
|
|
|
}
|
|
|
|
|
2010-08-03 07:18:59 +08:00
|
|
|
void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
|
|
|
|
bool instance) {
|
|
|
|
GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
|
|
|
|
if (Pos == MethodPool.end()) {
|
|
|
|
if (ExternalSource)
|
|
|
|
Pos = ReadMethodPool(Method->getSelector());
|
2009-04-25 05:10:55 +08:00
|
|
|
else
|
2010-08-03 07:18:59 +08:00
|
|
|
Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
|
|
|
|
GlobalMethods())).first;
|
2009-04-25 05:10:55 +08:00
|
|
|
}
|
2010-07-23 02:24:20 +08:00
|
|
|
Method->setDefined(impl);
|
2010-08-03 07:18:59 +08:00
|
|
|
ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
|
2009-03-04 13:16:45 +08:00
|
|
|
if (Entry.Method == 0) {
|
2007-12-12 15:09:47 +08:00
|
|
|
// Haven't seen a method with this selector name yet - add it.
|
2009-03-04 13:16:45 +08:00
|
|
|
Entry.Method = Method;
|
|
|
|
Entry.Next = 0;
|
|
|
|
return;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 13:16:45 +08:00
|
|
|
// We've seen a method with this name, see if we have already seen this type
|
|
|
|
// signature.
|
2011-06-16 07:02:42 +08:00
|
|
|
for (ObjCMethodList *List = &Entry; List; List = List->Next) {
|
|
|
|
bool match = MatchTwoMethodDeclarations(Method, List->Method);
|
|
|
|
|
|
|
|
if (match) {
|
2010-12-21 08:44:01 +08:00
|
|
|
ObjCMethodDecl *PrevObjCMethod = List->Method;
|
|
|
|
PrevObjCMethod->setDefined(impl);
|
|
|
|
// If a method is deprecated, push it in the global pool.
|
|
|
|
// This is used for better diagnostics.
|
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 (Method->isDeprecated()) {
|
|
|
|
if (!PrevObjCMethod->isDeprecated())
|
2010-12-21 08:44:01 +08:00
|
|
|
List->Method = Method;
|
|
|
|
}
|
|
|
|
// If new method is unavailable, push it into global pool
|
|
|
|
// unless previous one is deprecated.
|
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 (Method->isUnavailable()) {
|
|
|
|
if (PrevObjCMethod->getAvailability() < AR_Deprecated)
|
2010-12-21 08:44:01 +08:00
|
|
|
List->Method = Method;
|
|
|
|
}
|
2009-03-04 13:16:45 +08:00
|
|
|
return;
|
2010-07-23 02:24:20 +08:00
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-04 13:16:45 +08:00
|
|
|
// We have a new signature for an existing method - add it.
|
|
|
|
// This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
|
2010-02-11 08:53:01 +08:00
|
|
|
ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
|
|
|
|
Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
|
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) {
|
|
|
|
GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
|
|
|
|
if (Pos == MethodPool.end()) {
|
|
|
|
if (ExternalSource)
|
|
|
|
Pos = ReadMethodPool(Sel);
|
2009-04-25 05:10:55 +08:00
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-03 07:18:59 +08:00
|
|
|
ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-08-03 07:18:59 +08:00
|
|
|
if (warn && MethList.Method && MethList.Next) {
|
2011-06-16 07:02:42 +08:00
|
|
|
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()) !=
|
|
|
|
Diagnostic::Ignored));
|
2010-08-10 07:27:58 +08:00
|
|
|
if (strictSelectorMatch)
|
|
|
|
for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
|
2011-06-16 07:02:42 +08:00
|
|
|
if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
|
|
|
|
MMS_strict)) {
|
|
|
|
issueDiagnostic = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-08-10 07:27:58 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +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 && getLangOptions().ObjCAutoRefCount))
|
2010-08-10 07:27:58 +08:00
|
|
|
for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
|
2011-06-16 07:02:42 +08:00
|
|
|
// This checks if the methods differ in type mismatch.
|
|
|
|
if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
|
|
|
|
MMS_loose) &&
|
|
|
|
!isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
|
|
|
|
issueDiagnostic = true;
|
|
|
|
if (getLangOptions().ObjCAutoRefCount)
|
|
|
|
issueError = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-08-10 07:27:58 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
if (issueDiagnostic) {
|
|
|
|
if (issueError)
|
|
|
|
Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
|
|
|
|
else if (strictSelectorMatch)
|
2010-08-10 07:27:58 +08:00
|
|
|
Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
|
|
|
|
else
|
|
|
|
Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
Diag(MethList.Method->getLocStart(),
|
|
|
|
issueError ? diag::note_possibility : diag::note_using)
|
2010-08-03 07:18:59 +08:00
|
|
|
<< MethList.Method->getSourceRange();
|
|
|
|
for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
|
|
|
|
Diag(Next->Method->getLocStart(), diag::note_also_found)
|
|
|
|
<< Next->Method->getSourceRange();
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-04-25 05:10:55 +08:00
|
|
|
}
|
|
|
|
return MethList.Method;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-08-05 01:01:09 +08:00
|
|
|
/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
|
|
|
|
/// identical selector names in current and its super classes and issues
|
|
|
|
/// a warning if any of their argument types are incompatible.
|
2009-08-04 09:07:16 +08:00
|
|
|
void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
|
|
|
|
ObjCMethodDecl *Method,
|
|
|
|
bool IsInstance) {
|
2009-08-05 01:01:09 +08:00
|
|
|
ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
|
|
|
|
if (ID == 0) return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-08-05 01:01:09 +08:00
|
|
|
while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCMethodDecl *SuperMethodDecl =
|
2009-08-05 01:01:09 +08:00
|
|
|
SD->lookupMethod(Method->getSelector(), IsInstance);
|
|
|
|
if (SuperMethodDecl == 0) {
|
2009-08-04 09:07:16 +08:00
|
|
|
ID = SD;
|
2009-08-05 01:01:09 +08:00
|
|
|
continue;
|
2009-08-04 09:07:16 +08:00
|
|
|
}
|
2009-08-05 01:01:09 +08:00
|
|
|
ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
|
|
|
|
E = Method->param_end();
|
|
|
|
ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
|
|
|
|
for (; ParamI != E; ++ParamI, ++PrevI) {
|
|
|
|
// Number of parameters are the same and is guaranteed by selector match.
|
|
|
|
assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
|
|
|
|
QualType T1 = Context.getCanonicalType((*ParamI)->getType());
|
|
|
|
QualType T2 = Context.getCanonicalType((*PrevI)->getType());
|
2011-04-15 13:22:18 +08:00
|
|
|
// If type of argument of method in this class does not match its
|
2009-08-05 01:01:09 +08:00
|
|
|
// respective argument type in the super class method, issue warning;
|
|
|
|
if (!Context.typesAreCompatible(T1, T2)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
|
2009-08-05 01:01:09 +08:00
|
|
|
<< T1 << T2;
|
|
|
|
Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ID = SD;
|
|
|
|
}
|
2009-08-04 09:07:16 +08:00
|
|
|
}
|
|
|
|
|
2010-02-24 07:41:11 +08:00
|
|
|
/// DiagnoseDuplicateIvars -
|
|
|
|
/// Check for duplicate ivars in the entire class at the start of
|
|
|
|
/// @implementation. This becomes necesssary because class extension can
|
|
|
|
/// add ivars to a class in random order which will not be known until
|
|
|
|
/// class's @implementation is seen.
|
|
|
|
void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
|
|
|
|
ObjCInterfaceDecl *SID) {
|
|
|
|
for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
|
|
|
|
IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
|
|
|
|
ObjCIvarDecl* Ivar = (*IVI);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-18 09:30:32 +08:00
|
|
|
// Note: For class/category implemenations, allMethods/allProperties is
|
|
|
|
// always null.
|
2010-05-06 05:52:17 +08:00
|
|
|
void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *ClassDecl,
|
|
|
|
Decl **allMethods, unsigned allNum,
|
|
|
|
Decl **allProperties, unsigned pNum,
|
2009-03-30 00:50:03 +08:00
|
|
|
DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
|
2007-12-18 09:30:32 +08:00
|
|
|
// FIXME: If we don't have a ClassDecl, we have an error. We should consider
|
|
|
|
// always passing in a decl. If the decl has an error, isInvalidDecl()
|
2007-12-12 15:09:47 +08:00
|
|
|
// should be true.
|
|
|
|
if (!ClassDecl)
|
|
|
|
return;
|
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
|
|
|
|
2010-01-07 09:20:12 +08:00
|
|
|
if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
|
|
|
|
// FIXME: This is wrong. We shouldn't be pretending that there is
|
|
|
|
// an '@end' in the declaration.
|
|
|
|
SourceLocation L = ClassDecl->getLocation();
|
|
|
|
AtEnd.setBegin(L);
|
|
|
|
AtEnd.setEnd(L);
|
2011-04-23 06:02:28 +08:00
|
|
|
Diag(L, diag::err_missing_atend);
|
2009-11-17 02:57:01 +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;
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
for (unsigned i = 0; i < allNum; 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 {
|
|
|
|
InsMap[Method->getSelector()] = Method;
|
|
|
|
/// The following allows us to typecheck messages to "id".
|
|
|
|
AddInstanceMethodToGlobalPool(Method);
|
2009-09-09 23:08:12 +08:00
|
|
|
// verify that the instance method conforms to the same definition of
|
2009-08-05 01:01:09 +08:00
|
|
|
// parent methods if it shadows one.
|
2009-08-04 09:07:16 +08:00
|
|
|
CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
|
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 {
|
|
|
|
ClsMap[Method->getSelector()] = Method;
|
2007-12-18 09:30:32 +08:00
|
|
|
/// The following allows us to typecheck messages to "Class".
|
|
|
|
AddFactoryMethodToGlobalPool(Method);
|
2009-09-09 23:08:12 +08:00
|
|
|
// verify that the class method conforms to the same definition of
|
2009-08-05 01:01:09 +08:00
|
|
|
// parent methods if it shadows one.
|
2009-08-04 09:07:16 +08:00
|
|
|
CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-01-08 03:49:32 +08:00
|
|
|
if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// Compares properties declared in this class to those of its
|
2008-05-01 08:03:38 +08:00
|
|
|
// super class.
|
2008-05-03 03:17:30 +08:00
|
|
|
ComparePropertiesInBaseAndSuper(I);
|
2010-08-21 17:40:31 +08:00
|
|
|
CompareProperties(I, I);
|
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-01-19 02:41:16 +08:00
|
|
|
// Compare protocol properties with those in category
|
2010-08-21 17:40:31 +08:00
|
|
|
CompareProperties(C, C);
|
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)
|
|
|
|
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.
|
|
|
|
for (const ObjCCategoryDecl *ClsExtDecl =
|
|
|
|
IDecl->getFirstClassExtension();
|
|
|
|
ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
|
|
|
|
for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
|
|
|
|
E = ClsExtDecl->prop_end(); I != E; ++I) {
|
|
|
|
ObjCPropertyDecl *Property = (*I);
|
|
|
|
// Skip over properties declared @dynamic
|
|
|
|
if (const ObjCPropertyImplDecl *PIDecl
|
|
|
|
= IC->FindPropertyImplDecl(Property->getIdentifier()))
|
|
|
|
if (PIDecl->getPropertyImplementation()
|
|
|
|
== ObjCPropertyImplDecl::Dynamic)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (const ObjCCategoryDecl *CExtDecl =
|
|
|
|
IDecl->getFirstClassExtension();
|
|
|
|
CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
|
|
|
|
if (ObjCMethodDecl *GetterMethod =
|
|
|
|
CExtDecl->getInstanceMethod(Property->getGetterName()))
|
|
|
|
GetterMethod->setSynthesized(true);
|
|
|
|
if (!Property->isReadOnly())
|
|
|
|
if (ObjCMethodDecl *SetterMethod =
|
|
|
|
CExtDecl->getInstanceMethod(Property->getSetterName()))
|
|
|
|
SetterMethod->setSynthesized(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-24 05:35:43 +08:00
|
|
|
if (LangOpts.ObjCDefaultSynthProperties &&
|
|
|
|
LangOpts.ObjCNonFragileABI2)
|
2010-05-15 02:35:57 +08:00
|
|
|
DefaultSynthesizeProperties(S, IC, IDecl);
|
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);
|
2010-08-21 05:21:08 +08:00
|
|
|
|
2010-02-24 07:41:11 +08:00
|
|
|
if (LangOpts.ObjCNonFragileABI2)
|
|
|
|
while (IDecl->getSuperClass()) {
|
|
|
|
DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
|
|
|
|
IDecl = IDecl->getSuperClass();
|
|
|
|
}
|
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()) {
|
2008-01-08 03:49:32 +08:00
|
|
|
for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
|
2007-12-12 15:09:47 +08:00
|
|
|
Categories; Categories = Categories->getNextClassCategory()) {
|
|
|
|
if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
|
2010-05-06 05:52:17 +08:00
|
|
|
ImplMethodsVsClassMethods(S, CatImplClass, Categories);
|
2007-12-12 15:09:47 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-30 00:50:03 +08:00
|
|
|
if (isInterfaceDeclKind) {
|
|
|
|
// Reject invalid vardecls.
|
|
|
|
for (unsigned i = 0; i != tuvNum; i++) {
|
|
|
|
DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
|
|
|
|
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
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2010-04-18 12:59:38 +08:00
|
|
|
static inline
|
2010-08-19 07:23:40 +08:00
|
|
|
bool containsInvalidMethodImplAttribute(const AttrVec &A) {
|
2010-04-18 12:59:38 +08:00
|
|
|
// The 'ibaction' attribute is allowed on method definitions because of
|
|
|
|
// how the IBAction macro is used on both method declarations and definitions.
|
|
|
|
// If the method definitions contains any other attributes, return true.
|
2010-08-19 07:23:40 +08:00
|
|
|
for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
|
|
|
|
if ((*i)->getKind() != attr::IBAction)
|
|
|
|
return true;
|
|
|
|
return false;
|
2010-04-18 12:59:38 +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.
|
|
|
|
///
|
|
|
|
static bool
|
|
|
|
CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
|
|
|
|
ObjCInterfaceDecl *CurrentClass) {
|
|
|
|
QualType ResultType = Method->getResultType();
|
|
|
|
SourceRange ResultTypeRange;
|
|
|
|
if (const TypeSourceInfo *ResultTypeInfo = Method->getResultTypeSourceInfo())
|
|
|
|
ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
|
|
|
|
|
|
|
|
// 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())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (CurrentClass) {
|
|
|
|
if (ObjCInterfaceDecl *ResultClass
|
|
|
|
= ResultObjectType->getInterfaceDecl()) {
|
|
|
|
// - it is the same as the method's class type, or
|
|
|
|
if (CurrentClass == ResultClass)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// - it is a superclass of the method's class type
|
|
|
|
if (ResultClass->isSuperClassOf(CurrentClass))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Determine if any method in the global method pool has an inferred
|
|
|
|
/// result type.
|
|
|
|
static bool
|
|
|
|
anyMethodInfersRelatedResultType(Sema &S, Selector Sel, bool IsInstance) {
|
|
|
|
Sema::GlobalMethodPool::iterator Pos = S.MethodPool.find(Sel);
|
|
|
|
if (Pos == S.MethodPool.end()) {
|
|
|
|
if (S.ExternalSource)
|
|
|
|
Pos = S.ReadMethodPool(Sel);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCMethodList &List = IsInstance ? Pos->second.first : Pos->second.second;
|
|
|
|
for (ObjCMethodList *M = &List; M; M = M->Next) {
|
|
|
|
if (M->Method && M->Method->hasRelatedResultType())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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,
|
2010-08-21 17:40:31 +08:00
|
|
|
tok::TokenKind MethodType, Decl *ClassDecl,
|
2010-08-24 13:47:05 +08:00
|
|
|
ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
|
2011-06-11 09:09:30 +08:00
|
|
|
SourceLocation SelectorStartLoc,
|
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.
|
|
|
|
if (!ClassDecl) {
|
|
|
|
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
|
|
|
}
|
2008-04-04 14:12:32 +08:00
|
|
|
QualType resultDeclType;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
|
2009-02-21 06:59:16 +08:00
|
|
|
// Methods cannot return interface types. All ObjC objects are
|
|
|
|
// passed by reference.
|
2010-05-15 19:32:37 +08:00
|
|
|
if (resultDeclType->isObjCObjectType()) {
|
2009-04-12 03:08:56 +08:00
|
|
|
Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
|
|
|
|
<< 0 << resultDeclType;
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
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:00:47 +08:00
|
|
|
Diag(MethodLoc, diag::warn_missing_method_return_type);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ObjCMethodDecl* ObjCMethod =
|
2008-04-04 14:12:32 +08:00
|
|
|
ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
|
2010-03-08 22:59:44 +08:00
|
|
|
ResultTInfo,
|
2009-09-09 23:08:12 +08:00
|
|
|
cast<DeclContext>(ClassDecl),
|
2008-04-04 14:12:32 +08:00
|
|
|
MethodType == tok::minus, isVariadic,
|
2010-07-23 02:24:20 +08:00
|
|
|
false, false,
|
2011-06-11 09:09:30 +08:00
|
|
|
MethodDeclKind == tok::objc_optional
|
|
|
|
? ObjCMethodDecl::Optional
|
|
|
|
: ObjCMethodDecl::Required,
|
|
|
|
false);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
llvm::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
|
|
|
|
2009-04-12 02:57:04 +08:00
|
|
|
if (ArgInfo[i].Type == 0) {
|
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);
|
2008-12-10 03:36:17 +08:00
|
|
|
// 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);
|
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,
|
|
|
|
ArgType, DI, SC_None, 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
|
|
|
|
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);
|
2010-05-15 19:32:37 +08:00
|
|
|
if (ArgType->isObjCObjectType()) {
|
2010-04-08 08:30:06 +08:00
|
|
|
Diag(Param->getLocation(),
|
|
|
|
diag::err_object_cannot_be_passed_returned_by_value)
|
|
|
|
<< 1 << ArgType;
|
|
|
|
Param->setInvalidDecl();
|
|
|
|
}
|
|
|
|
Param->setDeclContext(ObjCMethod);
|
2011-02-10 06:20:01 +08:00
|
|
|
|
2010-04-08 08:30:06 +08:00
|
|
|
Params.push_back(Param);
|
|
|
|
}
|
|
|
|
|
2010-04-09 23:40:42 +08:00
|
|
|
ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
|
|
|
|
Sel.getNumArgs());
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCMethod->setObjCDeclQualifier(
|
|
|
|
CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
|
|
|
|
const ObjCMethodDecl *PrevMethod = 0;
|
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
|
|
|
|
2009-11-04 10:18:39 +08:00
|
|
|
const ObjCMethodDecl *InterfaceMD = 0;
|
|
|
|
|
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.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (ObjCImplementationDecl *ImpDecl =
|
2008-03-16 08:49:28 +08:00
|
|
|
dyn_cast<ObjCImplementationDecl>(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
|
|
|
}
|
2009-11-04 10:18:39 +08:00
|
|
|
InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
|
|
|
|
MethodType == tok::minus);
|
2011-06-11 09:09:30 +08:00
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
if (ObjCMethod->hasAttrs() &&
|
|
|
|
containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
|
2009-05-13 05:36:23 +08:00
|
|
|
Diag(EndLoc, diag::warn_attribute_method_def);
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (ObjCCategoryImplDecl *CatImpDecl =
|
2009-08-05 05:02:39 +08:00
|
|
|
dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
|
2007-12-12 15:09:47 +08:00
|
|
|
if (MethodType == tok::minus) {
|
2009-06-30 10:36:12 +08:00
|
|
|
PrevMethod = CatImpDecl->getInstanceMethod(Sel);
|
|
|
|
CatImpDecl->addInstanceMethod(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
} else {
|
2009-06-30 10:36:12 +08:00
|
|
|
PrevMethod = CatImpDecl->getClassMethod(Sel);
|
|
|
|
CatImpDecl->addClassMethod(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2011-06-11 09:09:30 +08:00
|
|
|
|
|
|
|
if (ObjCCategoryDecl *Cat = CatImpDecl->getCategoryDecl())
|
|
|
|
InterfaceMD = Cat->getMethod(Sel, MethodType == tok::minus);
|
|
|
|
|
2010-08-19 07:23:40 +08:00
|
|
|
if (ObjCMethod->hasAttrs() &&
|
|
|
|
containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
|
2009-05-13 05:36:23 +08:00
|
|
|
Diag(EndLoc, diag::warn_attribute_method_def);
|
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
|
|
|
}
|
|
|
|
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);
|
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-03-02 12:00:57 +08:00
|
|
|
// Merge information down from the interface declaration if we have one.
|
2011-06-11 09:09:30 +08:00
|
|
|
if (InterfaceMD) {
|
|
|
|
// Inherit the related result type, if we can.
|
|
|
|
if (InterfaceMD->hasRelatedResultType() &&
|
|
|
|
!CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
|
|
|
|
ObjCMethod->SetRelatedResultType();
|
|
|
|
|
2011-03-02 12:00:57 +08:00
|
|
|
mergeObjCMethodDecls(ObjCMethod, InterfaceMD);
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
bool ARCError = false;
|
|
|
|
if (getLangOptions().ObjCAutoRefCount)
|
|
|
|
ARCError = CheckARCMethodDecl(*this, ObjCMethod);
|
|
|
|
|
|
|
|
if (!ObjCMethod->hasRelatedResultType() && !ARCError &&
|
2011-06-15 07:20:43 +08:00
|
|
|
getLangOptions().ObjCInferRelatedResultType) {
|
2011-06-11 09:09:30 +08:00
|
|
|
bool InferRelatedResultType = false;
|
|
|
|
switch (ObjCMethod->getMethodFamily()) {
|
|
|
|
case OMF_None:
|
|
|
|
case OMF_copy:
|
|
|
|
case OMF_dealloc:
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (InferRelatedResultType &&
|
|
|
|
!CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass))
|
|
|
|
ObjCMethod->SetRelatedResultType();
|
|
|
|
|
|
|
|
if (!InterfaceMD &&
|
|
|
|
anyMethodInfersRelatedResultType(*this, ObjCMethod->getSelector(),
|
|
|
|
ObjCMethod->isInstanceMethod()))
|
|
|
|
CheckObjCMethodOverrides(ObjCMethod, cast<DeclContext>(ClassDecl));
|
|
|
|
}
|
|
|
|
|
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) {
|
2010-08-31 08:36:30 +08:00
|
|
|
if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
|
2008-11-05 00:57:32 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
|
|
|
|
/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
|
|
|
|
/// 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,
|
2010-08-21 17:40:31 +08:00
|
|
|
llvm::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;
|
|
|
|
}
|
2009-04-22 04:28:41 +08:00
|
|
|
if (LangOpts.ObjCNonFragileABI) {
|
|
|
|
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
|
2010-08-21 05:21:08 +08:00
|
|
|
llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
|
|
|
|
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++) {
|
|
|
|
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.
|
2010-08-21 17:40:31 +08:00
|
|
|
for (llvm::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);
|
2008-12-17 15:13:27 +08:00
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
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,
|
|
|
|
T, TInfo, SC_None, SC_None);
|
2010-05-04 02:51:14 +08:00
|
|
|
New->setExceptionVariable(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()));
|
|
|
|
} else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
|
|
|
|
Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
|
|
|
|
<< DS.getStorageClassSpec();
|
|
|
|
}
|
|
|
|
if (D.getDeclSpec().isThreadSpecified())
|
|
|
|
Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
|
|
|
|
D.getMutableDeclSpec().ClearStorageClassSpecs();
|
|
|
|
|
|
|
|
DiagnoseFunctionSpecifiers(D);
|
|
|
|
|
|
|
|
// Check that there are no default arguments inside the type of this
|
|
|
|
// exception object (C++ only).
|
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
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,
|
2010-04-29 00:11:27 +08:00
|
|
|
llvm::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
|
|
|
|
|
|
|
void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
|
2011-01-09 04:30:50 +08:00
|
|
|
CXXCtorInitializer ** initializers,
|
2010-04-29 00:11:27 +08:00
|
|
|
unsigned numInitializers) {
|
|
|
|
if (numInitializers > 0) {
|
|
|
|
NumIvarInitializers = numInitializers;
|
2011-01-09 04:30:50 +08:00
|
|
|
CXXCtorInitializer **ivarInitializers =
|
|
|
|
new (C) CXXCtorInitializer*[NumIvarInitializers];
|
2010-04-29 00:11:27 +08:00
|
|
|
memcpy(ivarInitializers, initializers,
|
2011-01-09 04:30:50 +08:00
|
|
|
numInitializers * sizeof(CXXCtorInitializer*));
|
2010-04-29 00:11:27 +08:00
|
|
|
IvarInitializers = ivarInitializers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-23 02:24:20 +08:00
|
|
|
void Sema::DiagnoseUseOfUnimplementedSelectors() {
|
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;
|
|
|
|
}
|