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-11-13 05:07:52 +08:00
|
|
|
#include "clang/AST/ASTMutationListener.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-09-28 06:35:36 +08:00
|
|
|
void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
|
2011-06-11 09:09:30 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-08 09:46:34 +08:00
|
|
|
if (ObjCMethodFamily Family = Overridden->getMethodFamily())
|
|
|
|
Diag(Overridden->getLocation(),
|
|
|
|
diag::note_related_result_type_overridden_family)
|
|
|
|
<< Family;
|
|
|
|
else
|
|
|
|
Diag(Overridden->getLocation(),
|
|
|
|
diag::note_related_result_type_overridden);
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
2011-09-28 06:35:36 +08:00
|
|
|
if (getLangOptions().ObjCAutoRefCount) {
|
|
|
|
if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
|
|
|
|
Overridden->hasAttr<NSReturnsRetainedAttr>())) {
|
|
|
|
Diag(NewMethod->getLocation(),
|
|
|
|
diag::err_nsreturns_retained_attribute_mismatch) << 1;
|
|
|
|
Diag(Overridden->getLocation(), diag::note_previous_decl)
|
|
|
|
<< "method";
|
|
|
|
}
|
|
|
|
if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
|
|
|
|
Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
|
|
|
|
Diag(NewMethod->getLocation(),
|
|
|
|
diag::err_nsreturns_retained_attribute_mismatch) << 0;
|
|
|
|
Diag(Overridden->getLocation(), diag::note_previous_decl)
|
|
|
|
<< "method";
|
|
|
|
}
|
2011-10-03 14:37:04 +08:00
|
|
|
ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin();
|
|
|
|
for (ObjCMethodDecl::param_iterator
|
|
|
|
ni = NewMethod->param_begin(), ne = NewMethod->param_end();
|
2011-09-28 06:35:36 +08:00
|
|
|
ni != ne; ++ni, ++oi) {
|
2011-10-03 14:37:04 +08:00
|
|
|
const ParmVarDecl *oldDecl = (*oi);
|
2011-09-28 06:35:36 +08:00
|
|
|
ParmVarDecl *newDecl = (*ni);
|
|
|
|
if (newDecl->hasAttr<NSConsumedAttr>() !=
|
|
|
|
oldDecl->hasAttr<NSConsumedAttr>()) {
|
|
|
|
Diag(newDecl->getLocation(),
|
|
|
|
diag::err_nsconsumed_attribute_mismatch);
|
|
|
|
Diag(oldDecl->getLocation(), diag::note_previous_decl)
|
|
|
|
<< "parameter";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
/// \brief Check a method declaration for compatibility with the Objective-C
|
|
|
|
/// ARC conventions.
|
|
|
|
static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
|
|
|
|
ObjCMethodFamily family = method->getMethodFamily();
|
|
|
|
switch (family) {
|
|
|
|
case OMF_None:
|
|
|
|
case OMF_dealloc:
|
2011-08-29 06:35:17 +08:00
|
|
|
case OMF_finalize:
|
2011-06-16 07:02:42 +08:00
|
|
|
case OMF_retain:
|
|
|
|
case OMF_release:
|
|
|
|
case OMF_autorelease:
|
|
|
|
case OMF_retainCount:
|
|
|
|
case OMF_self:
|
2011-07-22 10:45:48 +08:00
|
|
|
case OMF_performSelector:
|
2011-06-16 07:02:42 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-01 01:37:55 +08:00
|
|
|
/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
|
|
|
|
/// pool.
|
|
|
|
void Sema::AddAnyMethodToGlobalPool(Decl *D) {
|
|
|
|
ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
|
|
|
|
|
|
|
|
// If we don't have a valid method decl, simply return.
|
|
|
|
if (!MDecl)
|
|
|
|
return;
|
|
|
|
if (MDecl->isInstanceMethod())
|
|
|
|
AddInstanceMethodToGlobalPool(MDecl, true);
|
|
|
|
else
|
|
|
|
AddFactoryMethodToGlobalPool(MDecl, true);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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:
|
2011-08-29 06:35:17 +08:00
|
|
|
case OMF_finalize:
|
2011-06-16 07:02:42 +08:00
|
|
|
case OMF_alloc:
|
|
|
|
case OMF_init:
|
|
|
|
case OMF_mutableCopy:
|
|
|
|
case OMF_copy:
|
|
|
|
case OMF_new:
|
|
|
|
case OMF_self:
|
2011-07-06 06:38:59 +08:00
|
|
|
case OMF_performSelector:
|
2011-06-16 07:02:42 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-23 01:25:57 +08:00
|
|
|
// Warn on deprecated methods under -Wdeprecated-implementations,
|
|
|
|
// and prepare for warning on missing super calls.
|
|
|
|
if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
|
2011-02-16 01:49:58 +08:00
|
|
|
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);
|
2011-08-23 01:25:57 +08:00
|
|
|
|
2011-08-29 06:35:17 +08:00
|
|
|
// If this is "dealloc" or "finalize", set some bit here.
|
2011-08-23 01:25:57 +08:00
|
|
|
// Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
|
|
|
|
// Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
|
|
|
|
// Only do this if the current class actually has a superclass.
|
2011-08-29 06:35:17 +08:00
|
|
|
if (IC->getSuperClass()) {
|
2011-08-23 03:07:43 +08:00
|
|
|
ObjCShouldCallSuperDealloc =
|
2011-09-29 03:32:29 +08:00
|
|
|
!(Context.getLangOptions().ObjCAutoRefCount ||
|
|
|
|
Context.getLangOptions().getGC() == LangOptions::GCOnly) &&
|
2011-08-23 03:07:43 +08:00
|
|
|
MDecl->getMethodFamily() == OMF_dealloc;
|
2011-08-30 06:59:14 +08:00
|
|
|
ObjCShouldCallSuperFinalize =
|
2011-09-29 03:32:29 +08:00
|
|
|
Context.getLangOptions().getGC() != LangOptions::NonGC &&
|
2011-08-30 06:59:14 +08:00
|
|
|
MDecl->getMethodFamily() == OMF_finalize;
|
2011-08-29 06:35:17 +08:00
|
|
|
}
|
2011-08-23 01:25:57 +08:00
|
|
|
}
|
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
|
|
|
|
2011-11-14 06:08:30 +08:00
|
|
|
// Create a new one; the other may be in a different DeclContex, (e.g.
|
|
|
|
// this one may be in a LinkageSpecDecl while the other is not) which
|
|
|
|
// will break invariants.
|
|
|
|
IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
|
|
|
|
ClassName, ClassLoc);
|
|
|
|
if (AttrList)
|
|
|
|
ProcessDeclAttributeList(TUScope, IDecl, AttrList);
|
|
|
|
PushOnScopeChains(IDecl, TUScope);
|
|
|
|
|
2010-08-11 20:19:30 +08:00
|
|
|
} else {
|
2011-10-04 12:48:02 +08:00
|
|
|
IDecl->setLocation(ClassLoc);
|
|
|
|
IDecl->setAtStartLoc(AtInterfaceLoc);
|
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);
|
2011-11-13 05:07:46 +08:00
|
|
|
|
|
|
|
IDecl->completedForwardDecl();
|
|
|
|
|
2010-08-11 20:19:30 +08:00
|
|
|
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>())) {
|
2011-12-01 23:37:53 +08:00
|
|
|
if (PrevDecl == IDecl) {
|
|
|
|
// Don't correct to the class we're defining.
|
|
|
|
PrevDecl = 0;
|
|
|
|
} else {
|
|
|
|
Diag(SuperLoc, diag::err_undef_superclass_suggest)
|
|
|
|
<< SuperName << ClassName << PrevDecl->getDeclName();
|
|
|
|
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-11-15 06:10:01 +08:00
|
|
|
else if (RequireCompleteType(SuperLoc,
|
|
|
|
Context.getObjCInterfaceType(SuperClassDecl),
|
|
|
|
PDiag(diag::err_forward_superclass)
|
|
|
|
<< SuperClassDecl->getDeclName()
|
|
|
|
<< ClassName
|
|
|
|
<< SourceRange(AtInterfaceLoc, ClassLoc))) {
|
2011-06-24 07:16:19 +08:00
|
|
|
SuperClassDecl = 0;
|
|
|
|
}
|
2009-07-10 06:08:26 +08:00
|
|
|
}
|
|
|
|
IDecl->setSuperClass(SuperClassDecl);
|
|
|
|
IDecl->setSuperClassLoc(SuperLoc);
|
|
|
|
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);
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(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);
|
2011-11-14 06:08:30 +08:00
|
|
|
|
|
|
|
// Create a new one; the other may be in a different DeclContex, (e.g.
|
|
|
|
// this one may be in a LinkageSpecDecl while the other is not) which
|
|
|
|
// will break invariants.
|
|
|
|
// We will not add it to scope chains to ignore it as the warning says.
|
|
|
|
PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
|
|
|
|
ProtocolLoc, AtProtoInterfaceLoc,
|
|
|
|
/*isForwardDecl=*/false);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ObjCList<ObjCProtocolDecl> PList;
|
|
|
|
PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
|
|
|
|
err = CheckForwardProtocolDeclarationForCircularDependency(
|
|
|
|
ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
|
|
|
|
|
|
|
|
// Make sure the cached decl gets a valid start location.
|
|
|
|
PDecl->setAtStartLoc(AtProtoInterfaceLoc);
|
|
|
|
PDecl->setLocation(ProtocolLoc);
|
|
|
|
// Since this ObjCProtocolDecl was created by a forward declaration,
|
|
|
|
// we now add it to the DeclContext since it wasn't added before
|
|
|
|
PDecl->setLexicalDeclContext(CurContext);
|
|
|
|
CurContext->addDecl(PDecl);
|
|
|
|
PDecl->completedForwardDecl();
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2008-03-16 09:25:17 +08:00
|
|
|
} else {
|
2011-10-04 12:48:02 +08:00
|
|
|
PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
|
2011-10-18 03:48:06 +08:00
|
|
|
ProtocolLoc, AtProtoInterfaceLoc,
|
|
|
|
/*isForwardDecl=*/false);
|
2009-04-24 07:18:26 +08:00
|
|
|
PushOnScopeChains(PDecl, TUScope);
|
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);
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(PDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// FindProtocolDeclaration - This routine looks up protocols and
|
2008-09-05 04:01:15 +08:00
|
|
|
/// issues an error if they are not declared. It returns list of
|
|
|
|
/// protocol declarations in its 'Protocols' argument.
|
2007-12-12 15:09:47 +08:00
|
|
|
void
|
2008-07-26 12:03:38 +08:00
|
|
|
Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
|
2008-07-22 06:17:28 +08:00
|
|
|
const IdentifierLocPair *ProtocolId,
|
2007-12-12 15:09:47 +08:00
|
|
|
unsigned NumProtocols,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<Decl *> &Protocols) {
|
2007-12-12 15:09:47 +08:00
|
|
|
for (unsigned i = 0; i != NumProtocols; ++i) {
|
2010-04-16 06:33:43 +08:00
|
|
|
ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
|
|
|
|
ProtocolId[i].second);
|
2010-01-04 02:01:57 +08:00
|
|
|
if (!PDecl) {
|
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) {
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<ObjCProtocolDecl*, 32> Protocols;
|
|
|
|
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?
|
2011-10-04 12:48:02 +08:00
|
|
|
PDecl = ObjCProtocolDecl::Create(Context, CurContext, Ident,
|
2011-10-18 03:48:06 +08:00
|
|
|
IdentList[i].second, AtProtocolLoc,
|
|
|
|
/*isForwardDecl=*/true);
|
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);
|
2011-11-13 05:07:52 +08:00
|
|
|
if (!isNew) {
|
|
|
|
if (ASTMutationListener *L = Context.getASTMutationListener())
|
|
|
|
L->UpdatedAttributeList(PDecl);
|
|
|
|
}
|
2010-08-13 08:28:03 +08:00
|
|
|
}
|
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.
|
2011-11-15 06:10:01 +08:00
|
|
|
|
|
|
|
if (!IDecl
|
|
|
|
|| RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
|
|
|
|
PDiag(diag::err_category_forward_interface)
|
|
|
|
<< (CategoryName == 0))) {
|
2010-02-24 03:39:46 +08:00
|
|
|
// Create an invalid ObjCCategoryDecl to serve as context for
|
|
|
|
// the enclosing method declarations. We mark the decl invalid
|
|
|
|
// to make it clear that this isn't a valid AST.
|
|
|
|
CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
|
2011-08-31 03:43:26 +08:00
|
|
|
ClassLoc, CategoryLoc, CategoryName,IDecl);
|
2010-02-24 03:39:46 +08:00
|
|
|
CDecl->setInvalidDecl();
|
2011-11-15 06:10:01 +08:00
|
|
|
|
|
|
|
if (!IDecl)
|
|
|
|
Diag(ClassLoc, diag::err_undef_interface) << ClassName;
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(CDecl);
|
2010-02-24 03:39:46 +08:00
|
|
|
}
|
|
|
|
|
2010-06-23 07:20:40 +08:00
|
|
|
if (!CategoryName && IDecl->getImplementation()) {
|
|
|
|
Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
|
|
|
|
Diag(IDecl->getImplementation()->getLocation(),
|
|
|
|
diag::note_implementation_declared);
|
2010-02-24 03:39:46 +08:00
|
|
|
}
|
|
|
|
|
2010-02-16 05:55:26 +08:00
|
|
|
if (CategoryName) {
|
|
|
|
/// Check for duplicate interface declaration for this category
|
|
|
|
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
|
|
|
}
|
2008-01-18 04:33:24 +08:00
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
|
2011-08-31 03:43:26 +08:00
|
|
|
CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
|
|
|
|
ClassLoc, CategoryLoc, CategoryName, IDecl);
|
|
|
|
// FIXME: PushOnScopeChains?
|
|
|
|
CurContext->addDecl(CDecl);
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
if (NumProtoRefs) {
|
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);
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(CDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// ActOnStartCategoryImplementation - Perform semantic checks on the
|
2008-01-08 03:49:32 +08:00
|
|
|
/// category implementation declaration and build an ObjCCategoryImplDecl
|
2007-12-12 15:09:47 +08:00
|
|
|
/// object.
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::ActOnStartCategoryImplementation(
|
2007-12-12 15:09:47 +08:00
|
|
|
SourceLocation AtCatImplLoc,
|
|
|
|
IdentifierInfo *ClassName, SourceLocation ClassLoc,
|
|
|
|
IdentifierInfo *CatName, SourceLocation CatLoc) {
|
2010-04-16 06:33:43 +08:00
|
|
|
ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
|
2009-07-21 08:05:53 +08:00
|
|
|
ObjCCategoryDecl *CatIDecl = 0;
|
|
|
|
if (IDecl) {
|
|
|
|
CatIDecl = IDecl->FindCategoryDeclaration(CatName);
|
|
|
|
if (!CatIDecl) {
|
|
|
|
// Category @implementation with no corresponding @interface.
|
|
|
|
// Create and install one.
|
2011-11-24 04:27:26 +08:00
|
|
|
CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
|
|
|
|
ClassLoc, CatLoc,
|
2011-08-31 03:43:26 +08:00
|
|
|
CatName, IDecl);
|
2011-11-24 04:27:26 +08:00
|
|
|
CatIDecl->setImplicit();
|
2009-07-21 08:05:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCCategoryImplDecl *CDecl =
|
2011-10-04 12:48:02 +08:00
|
|
|
ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
|
2011-12-09 08:31:40 +08:00
|
|
|
ClassLoc, AtCatImplLoc, CatLoc);
|
2007-12-12 15:09:47 +08:00
|
|
|
/// Check that class of this category is already completely declared.
|
2011-11-15 06:10:01 +08:00
|
|
|
if (!IDecl) {
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(ClassLoc, diag::err_undef_interface) << ClassName;
|
2011-07-22 10:45:48 +08:00
|
|
|
CDecl->setInvalidDecl();
|
2011-11-15 06:10:01 +08:00
|
|
|
} else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
|
|
|
|
diag::err_undef_interface)) {
|
|
|
|
CDecl->setInvalidDecl();
|
2011-07-22 10:45:48 +08:00
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
|
2009-01-09 08:49:46 +08:00
|
|
|
// FIXME: PushOnScopeChains?
|
2009-06-30 10:36:12 +08:00
|
|
|
CurContext->addDecl(CDecl);
|
2009-01-09 08:49:46 +08:00
|
|
|
|
2011-10-07 07:23:27 +08:00
|
|
|
// If the interface is deprecated/unavailable, warn/error about it.
|
|
|
|
if (IDecl)
|
|
|
|
DiagnoseUseOfDecl(IDecl, ClassLoc);
|
|
|
|
|
2009-07-21 08:05:53 +08:00
|
|
|
/// Check that CatName, category name, is not used in another implementation.
|
|
|
|
if (CatIDecl) {
|
|
|
|
if (CatIDecl->getImplementation()) {
|
|
|
|
Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
|
|
|
|
<< CatName;
|
|
|
|
Diag(CatIDecl->getImplementation()->getLocation(),
|
|
|
|
diag::note_previous_definition);
|
2011-02-15 08:59:30 +08:00
|
|
|
} else {
|
2009-07-21 08:05:53 +08:00
|
|
|
CatIDecl->setImplementation(CDecl);
|
2011-02-15 08:59:30 +08:00
|
|
|
// Warn on implementating category of deprecated class under
|
|
|
|
// -Wdeprecated-implementations flag.
|
2011-02-16 01:49:58 +08:00
|
|
|
DiagnoseObjCImplementedDeprecations(*this,
|
|
|
|
dyn_cast<NamedDecl>(IDecl),
|
|
|
|
CDecl->getLocation(), 2);
|
2011-02-15 08:59:30 +08:00
|
|
|
}
|
2009-07-21 08:05:53 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
CheckObjCDeclScope(CDecl);
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(CDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::ActOnStartClassImplementation(
|
2007-12-12 15:09:47 +08:00
|
|
|
SourceLocation AtClassImplLoc,
|
|
|
|
IdentifierInfo *ClassName, SourceLocation ClassLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
IdentifierInfo *SuperClassname,
|
2007-12-12 15:09:47 +08:00
|
|
|
SourceLocation SuperClassLoc) {
|
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))) {
|
2011-11-15 06:10:01 +08:00
|
|
|
if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
|
|
|
|
diag::warn_undef_interface))
|
2010-08-11 20:19:30 +08:00
|
|
|
IDecl = 0;
|
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.
|
2011-11-13 05:07:46 +08:00
|
|
|
if (IDecl->isForwardDecl())
|
|
|
|
IDecl->completedForwardDecl();
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ObjCImplementationDecl* IMPDecl =
|
2011-10-04 12:48:02 +08:00
|
|
|
ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
|
|
|
|
ClassLoc, AtClassImplLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
if (CheckObjCDeclScope(IMPDecl))
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(IMPDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// Check that there is no duplicate implementation of this class.
|
2010-08-11 20:19:30 +08:00
|
|
|
if (IDecl->getImplementation()) {
|
|
|
|
// FIXME: Don't leak everything!
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
|
2009-07-21 08:06:04 +08:00
|
|
|
Diag(IDecl->getImplementation()->getLocation(),
|
|
|
|
diag::note_previous_definition);
|
2010-08-11 20:19:30 +08:00
|
|
|
} else { // add it to the list.
|
2009-07-21 08:05:53 +08:00
|
|
|
IDecl->setImplementation(IMPDecl);
|
2009-04-24 08:11:27 +08:00
|
|
|
PushOnScopeChains(IMPDecl, TUScope);
|
2011-02-15 08:59:30 +08:00
|
|
|
// Warn on implementating deprecated class under
|
|
|
|
// -Wdeprecated-implementations flag.
|
2011-02-16 01:49:58 +08:00
|
|
|
DiagnoseObjCImplementedDeprecations(*this,
|
|
|
|
dyn_cast<NamedDecl>(IDecl),
|
|
|
|
IMPDecl->getLocation(), 1);
|
2009-07-21 08:05:53 +08:00
|
|
|
}
|
2011-10-07 07:23:20 +08:00
|
|
|
return ActOnObjCContainerStartDefinition(IMPDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
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.
|
2011-10-11 02:28:20 +08:00
|
|
|
if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
|
2008-11-19 13:08:23 +08:00
|
|
|
Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
|
2008-11-24 05:45:46 +08:00
|
|
|
<< ImplIvar->getIdentifier()
|
|
|
|
<< ImplIvar->getType() << ClsIvar->getType();
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(ClsIvar->getLocation(), diag::note_previous_definition);
|
2011-10-11 02:28:20 +08:00
|
|
|
} else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
|
|
|
|
ImplIvar->getBitWidthValue(Context) !=
|
|
|
|
ClsIvar->getBitWidthValue(Context)) {
|
|
|
|
Diag(ImplIvar->getBitWidth()->getLocStart(),
|
|
|
|
diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
|
|
|
|
Diag(ClsIvar->getBitWidth()->getLocStart(),
|
|
|
|
diag::note_previous_definition);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-03-03 22:49:36 +08:00
|
|
|
// Make sure the names are identical.
|
|
|
|
if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
|
2008-11-19 13:08:23 +08:00
|
|
|
Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
|
2008-11-24 05:45:46 +08:00
|
|
|
<< ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(ClsIvar->getLocation(), diag::note_previous_definition);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
--numIvars;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-13 02:11:49 +08:00
|
|
|
if (numIvars > 0)
|
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());
|
|
|
|
}
|
|
|
|
|
2011-07-29 07:19:50 +08:00
|
|
|
static bool CheckMethodOverrideReturn(Sema &S,
|
2010-10-28 10:34:38 +08:00
|
|
|
ObjCMethodDecl *MethodImpl,
|
2011-02-22 07:49:15 +08:00
|
|
|
ObjCMethodDecl *MethodDecl,
|
2011-07-25 04:53:26 +08:00
|
|
|
bool IsProtocolMethodDecl,
|
2011-08-11 01:16:30 +08:00
|
|
|
bool IsOverridingMode,
|
2011-07-29 07:19:50 +08:00
|
|
|
bool Warn) {
|
2011-02-22 07:49:15 +08:00
|
|
|
if (IsProtocolMethodDecl &&
|
|
|
|
(MethodDecl->getObjCDeclQualifier() !=
|
|
|
|
MethodImpl->getObjCDeclQualifier())) {
|
2011-07-29 07:19:50 +08:00
|
|
|
if (Warn) {
|
2011-08-11 01:16:30 +08:00
|
|
|
S.Diag(MethodImpl->getLocation(),
|
|
|
|
(IsOverridingMode ?
|
|
|
|
diag::warn_conflicting_overriding_ret_type_modifiers
|
|
|
|
: diag::warn_conflicting_ret_type_modifiers))
|
|
|
|
<< MethodImpl->getDeclName()
|
2011-07-29 07:19:50 +08:00
|
|
|
<< getTypeRange(MethodImpl->getResultTypeSourceInfo());
|
|
|
|
S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
|
|
|
|
<< getTypeRange(MethodDecl->getResultTypeSourceInfo());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2011-02-22 07:49:15 +08:00
|
|
|
}
|
|
|
|
|
2010-10-28 10:34:38 +08:00
|
|
|
if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
|
2011-02-22 07:49:15 +08:00
|
|
|
MethodDecl->getResultType()))
|
2011-07-29 07:19:50 +08:00
|
|
|
return true;
|
|
|
|
if (!Warn)
|
|
|
|
return false;
|
2010-10-28 10:34:38 +08:00
|
|
|
|
2011-08-11 01:16:30 +08:00
|
|
|
unsigned DiagID =
|
|
|
|
IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
|
|
|
|
: diag::warn_conflicting_ret_types;
|
2010-10-28 10:34:38 +08:00
|
|
|
|
|
|
|
// Mismatches between ObjC pointers go into a different warning
|
|
|
|
// category, and sometimes they're even completely whitelisted.
|
|
|
|
if (const ObjCObjectPointerType *ImplPtrTy =
|
|
|
|
MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
|
|
|
|
if (const ObjCObjectPointerType *IfacePtrTy =
|
2011-02-22 07:49:15 +08:00
|
|
|
MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
|
2010-10-28 10:34:38 +08:00
|
|
|
// Allow non-matching return types as long as they don't violate
|
|
|
|
// the principle of substitutability. Specifically, we permit
|
|
|
|
// return types that are subclasses of the declared return type,
|
|
|
|
// or that are more-qualified versions of the declared type.
|
|
|
|
if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
|
2011-07-29 07:19:50 +08:00
|
|
|
return false;
|
2010-10-28 10:34:38 +08:00
|
|
|
|
2011-08-11 01:16:30 +08:00
|
|
|
DiagID =
|
|
|
|
IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
|
|
|
|
: diag::warn_non_covariant_ret_types;
|
2010-10-28 10:34:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
S.Diag(MethodImpl->getLocation(), DiagID)
|
|
|
|
<< MethodImpl->getDeclName()
|
2011-02-22 07:49:15 +08:00
|
|
|
<< MethodDecl->getResultType()
|
2010-10-28 10:34:38 +08:00
|
|
|
<< MethodImpl->getResultType()
|
|
|
|
<< getTypeRange(MethodImpl->getResultTypeSourceInfo());
|
2011-08-11 01:16:30 +08:00
|
|
|
S.Diag(MethodDecl->getLocation(),
|
|
|
|
IsOverridingMode ? diag::note_previous_declaration
|
|
|
|
: diag::note_previous_definition)
|
2011-02-22 07:49:15 +08:00
|
|
|
<< getTypeRange(MethodDecl->getResultTypeSourceInfo());
|
2011-07-29 07:19:50 +08:00
|
|
|
return false;
|
2010-10-28 10:34:38 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 07:19:50 +08:00
|
|
|
static bool CheckMethodOverrideParam(Sema &S,
|
2010-10-28 10:34:38 +08:00
|
|
|
ObjCMethodDecl *MethodImpl,
|
2011-02-22 07:49:15 +08:00
|
|
|
ObjCMethodDecl *MethodDecl,
|
2010-10-28 10:34:38 +08:00
|
|
|
ParmVarDecl *ImplVar,
|
2011-02-22 07:49:15 +08:00
|
|
|
ParmVarDecl *IfaceVar,
|
2011-07-25 04:53:26 +08:00
|
|
|
bool IsProtocolMethodDecl,
|
2011-08-11 01:16:30 +08:00
|
|
|
bool IsOverridingMode,
|
2011-07-29 07:19:50 +08:00
|
|
|
bool Warn) {
|
2011-02-22 07:49:15 +08:00
|
|
|
if (IsProtocolMethodDecl &&
|
|
|
|
(ImplVar->getObjCDeclQualifier() !=
|
|
|
|
IfaceVar->getObjCDeclQualifier())) {
|
2011-07-29 07:19:50 +08:00
|
|
|
if (Warn) {
|
2011-08-11 01:16:30 +08:00
|
|
|
if (IsOverridingMode)
|
|
|
|
S.Diag(ImplVar->getLocation(),
|
|
|
|
diag::warn_conflicting_overriding_param_modifiers)
|
|
|
|
<< getTypeRange(ImplVar->getTypeSourceInfo())
|
|
|
|
<< MethodImpl->getDeclName();
|
|
|
|
else S.Diag(ImplVar->getLocation(),
|
2011-07-29 07:19:50 +08:00
|
|
|
diag::warn_conflicting_param_modifiers)
|
|
|
|
<< getTypeRange(ImplVar->getTypeSourceInfo())
|
2011-08-11 01:16:30 +08:00
|
|
|
<< MethodImpl->getDeclName();
|
2011-07-29 07:19:50 +08:00
|
|
|
S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
|
|
|
|
<< getTypeRange(IfaceVar->getTypeSourceInfo());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
2011-02-22 07:49:15 +08:00
|
|
|
}
|
|
|
|
|
2010-10-28 10:34:38 +08:00
|
|
|
QualType ImplTy = ImplVar->getType();
|
|
|
|
QualType IfaceTy = IfaceVar->getType();
|
2011-02-22 07:49:15 +08:00
|
|
|
|
2010-10-28 10:34:38 +08:00
|
|
|
if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
|
2011-07-29 07:19:50 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!Warn)
|
|
|
|
return false;
|
2011-08-11 01:16:30 +08:00
|
|
|
unsigned DiagID =
|
|
|
|
IsOverridingMode ? diag::warn_conflicting_overriding_param_types
|
|
|
|
: diag::warn_conflicting_param_types;
|
2010-10-28 10:34:38 +08:00
|
|
|
|
|
|
|
// Mismatches between ObjC pointers go into a different warning
|
|
|
|
// category, and sometimes they're even completely whitelisted.
|
|
|
|
if (const ObjCObjectPointerType *ImplPtrTy =
|
|
|
|
ImplTy->getAs<ObjCObjectPointerType>()) {
|
|
|
|
if (const ObjCObjectPointerType *IfacePtrTy =
|
|
|
|
IfaceTy->getAs<ObjCObjectPointerType>()) {
|
|
|
|
// Allow non-matching argument types as long as they don't
|
|
|
|
// violate the principle of substitutability. Specifically, the
|
|
|
|
// implementation must accept any objects that the superclass
|
|
|
|
// accepts, however it may also accept others.
|
|
|
|
if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
|
2011-07-29 07:19:50 +08:00
|
|
|
return false;
|
2010-10-28 10:34:38 +08:00
|
|
|
|
2011-08-11 01:16:30 +08:00
|
|
|
DiagID =
|
|
|
|
IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
|
|
|
|
: diag::warn_non_contravariant_param_types;
|
2010-10-28 10:34:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
S.Diag(ImplVar->getLocation(), DiagID)
|
|
|
|
<< getTypeRange(ImplVar->getTypeSourceInfo())
|
2011-08-11 01:16:30 +08:00
|
|
|
<< MethodImpl->getDeclName() << IfaceTy << ImplTy;
|
|
|
|
S.Diag(IfaceVar->getLocation(),
|
|
|
|
(IsOverridingMode ? diag::note_previous_declaration
|
|
|
|
: diag::note_previous_definition))
|
2010-10-28 10:34:38 +08:00
|
|
|
<< getTypeRange(IfaceVar->getTypeSourceInfo());
|
2011-07-29 07:19:50 +08:00
|
|
|
return false;
|
2010-10-28 10:34:38 +08:00
|
|
|
}
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
/// In ARC, check whether the conventional meanings of the two methods
|
|
|
|
/// match. If they don't, it's a hard error.
|
|
|
|
static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
|
|
|
|
ObjCMethodDecl *decl) {
|
|
|
|
ObjCMethodFamily implFamily = impl->getMethodFamily();
|
|
|
|
ObjCMethodFamily declFamily = decl->getMethodFamily();
|
|
|
|
if (implFamily == declFamily) return false;
|
|
|
|
|
|
|
|
// Since conventions are sorted by selector, the only possibility is
|
|
|
|
// that the types differ enough to cause one selector or the other
|
|
|
|
// to fall out of the family.
|
|
|
|
assert(implFamily == OMF_None || declFamily == OMF_None);
|
|
|
|
|
|
|
|
// No further diagnostics required on invalid declarations.
|
|
|
|
if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
|
|
|
|
|
|
|
|
const ObjCMethodDecl *unmatched = impl;
|
|
|
|
ObjCMethodFamily family = declFamily;
|
|
|
|
unsigned errorID = diag::err_arc_lost_method_convention;
|
|
|
|
unsigned noteID = diag::note_arc_lost_method_convention;
|
|
|
|
if (declFamily == OMF_None) {
|
|
|
|
unmatched = decl;
|
|
|
|
family = implFamily;
|
|
|
|
errorID = diag::err_arc_gained_method_convention;
|
|
|
|
noteID = diag::note_arc_gained_method_convention;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indexes into a %select clause in the diagnostic.
|
|
|
|
enum FamilySelector {
|
|
|
|
F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
|
|
|
|
};
|
|
|
|
FamilySelector familySelector = FamilySelector();
|
|
|
|
|
|
|
|
switch (family) {
|
|
|
|
case OMF_None: llvm_unreachable("logic error, no method convention");
|
|
|
|
case OMF_retain:
|
|
|
|
case OMF_release:
|
|
|
|
case OMF_autorelease:
|
|
|
|
case OMF_dealloc:
|
2011-08-29 06:35:17 +08:00
|
|
|
case OMF_finalize:
|
2011-06-16 07:02:42 +08:00
|
|
|
case OMF_retainCount:
|
|
|
|
case OMF_self:
|
2011-07-06 06:38:59 +08:00
|
|
|
case OMF_performSelector:
|
2011-06-16 07:02:42 +08:00
|
|
|
// Mismatches for these methods don't change ownership
|
|
|
|
// conventions, so we don't care.
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case OMF_init: familySelector = F_init; break;
|
|
|
|
case OMF_alloc: familySelector = F_alloc; break;
|
|
|
|
case OMF_copy: familySelector = F_copy; break;
|
|
|
|
case OMF_mutableCopy: familySelector = F_mutableCopy; break;
|
|
|
|
case OMF_new: familySelector = F_new; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
|
|
|
|
ReasonSelector reasonSelector;
|
|
|
|
|
|
|
|
// The only reason these methods don't fall within their families is
|
|
|
|
// due to unusual result types.
|
|
|
|
if (unmatched->getResultType()->isObjCObjectPointerType()) {
|
|
|
|
reasonSelector = R_UnrelatedReturn;
|
|
|
|
} else {
|
|
|
|
reasonSelector = R_NonObjectReturn;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2011-10-11 01:53:29 +08:00
|
|
|
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,
|
2011-10-11 01:53:29 +08:00
|
|
|
IsProtocolMethodDecl, false,
|
2011-08-11 01:16:30 +08:00
|
|
|
true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 03:58:42 +08:00
|
|
|
for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
|
2011-02-22 07:49:15 +08:00
|
|
|
IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
|
2011-08-11 01:16:30 +08:00
|
|
|
IM != EM; ++IM, ++IF) {
|
2011-02-22 07:49:15 +08:00
|
|
|
CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
|
2011-10-11 01:53:29 +08:00
|
|
|
IsProtocolMethodDecl, false, true);
|
2010-05-22 07:28:58 +08:00
|
|
|
}
|
2008-12-06 02:18:52 +08:00
|
|
|
|
2011-08-09 02:03:17 +08:00
|
|
|
if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
|
2011-10-11 01:53:29 +08:00
|
|
|
Diag(ImpMethodDecl->getLocation(),
|
|
|
|
diag::warn_conflicting_variadic);
|
2011-08-09 02:03:17 +08:00
|
|
|
Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-11 01:53:29 +08:00
|
|
|
void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
|
|
|
|
ObjCMethodDecl *Overridden,
|
|
|
|
bool IsProtocolMethodDecl) {
|
|
|
|
|
|
|
|
CheckMethodOverrideReturn(*this, Method, Overridden,
|
|
|
|
IsProtocolMethodDecl, true,
|
|
|
|
true);
|
|
|
|
|
|
|
|
for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
|
|
|
|
IF = Overridden->param_begin(), EM = Method->param_end();
|
|
|
|
IM != EM; ++IM, ++IF) {
|
|
|
|
CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
|
|
|
|
IsProtocolMethodDecl, true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Method->isVariadic() != Overridden->isVariadic()) {
|
|
|
|
Diag(Method->getLocation(),
|
|
|
|
diag::warn_conflicting_overriding_variadic);
|
|
|
|
Diag(Overridden->getLocation(), diag::note_previous_declaration);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 07:19:50 +08:00
|
|
|
/// WarnExactTypedMethods - This routine issues a warning if method
|
|
|
|
/// implementation declaration matches exactly that of its declaration.
|
|
|
|
void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
|
|
|
|
ObjCMethodDecl *MethodDecl,
|
|
|
|
bool IsProtocolMethodDecl) {
|
|
|
|
// don't issue warning when protocol method is optional because primary
|
|
|
|
// class is not required to implement it and it is safe for protocol
|
|
|
|
// to implement it.
|
|
|
|
if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
|
|
|
|
return;
|
|
|
|
// don't issue warning when primary class's method is
|
|
|
|
// depecated/unavailable.
|
|
|
|
if (MethodDecl->hasAttr<UnavailableAttr>() ||
|
|
|
|
MethodDecl->hasAttr<DeprecatedAttr>())
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
|
|
|
|
IsProtocolMethodDecl, false, false);
|
|
|
|
if (match)
|
|
|
|
for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
|
|
|
|
IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
|
|
|
|
IM != EM; ++IM, ++IF) {
|
|
|
|
match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
|
|
|
|
*IM, *IF,
|
|
|
|
IsProtocolMethodDecl, false, false);
|
|
|
|
if (!match)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (match)
|
|
|
|
match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
|
2011-08-09 01:32:19 +08:00
|
|
|
if (match)
|
|
|
|
match = !(MethodDecl->isClassMethod() &&
|
|
|
|
MethodDecl->getSelector() == GetNullarySelector("load", Context));
|
2011-07-29 07:19:50 +08:00
|
|
|
|
|
|
|
if (match) {
|
|
|
|
Diag(ImpMethodDecl->getLocation(),
|
|
|
|
diag::warn_category_method_impl_match);
|
|
|
|
Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2011-09-26 07:23:43 +08:00
|
|
|
!= DiagnosticsEngine::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;
|
2011-09-26 07:23:43 +08:00
|
|
|
if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
|
|
|
|
DiagnosticsEngine::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,
|
2011-07-29 07:19:50 +08:00
|
|
|
bool ImmediateClass,
|
|
|
|
bool WarnExactMatch) {
|
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 =
|
2011-08-31 03:43:21 +08:00
|
|
|
IMPDecl->getInstanceMethod((*I)->getSelector());
|
|
|
|
assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
|
|
|
|
"Expected to find the method through lookup as well");
|
|
|
|
ObjCMethodDecl *MethodDecl = *I;
|
2009-05-02 04:07:12 +08:00
|
|
|
// ImpMethodDecl may be null as in a @dynamic property.
|
2011-07-29 07:19:50 +08:00
|
|
|
if (ImpMethodDecl) {
|
|
|
|
if (!WarnExactMatch)
|
|
|
|
WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
|
|
|
|
isa<ObjCProtocolDecl>(CDecl));
|
2011-08-26 06:58:42 +08:00
|
|
|
else if (!MethodDecl->isSynthesized())
|
2011-07-29 07:19:50 +08:00
|
|
|
WarnExactTypedMethods(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-08-31 03:43:21 +08:00
|
|
|
assert(CDecl->getClassMethod((*I)->getSelector()) &&
|
|
|
|
"Expected to find the method through lookup as well");
|
|
|
|
ObjCMethodDecl *MethodDecl = *I;
|
2011-07-29 07:19:50 +08:00
|
|
|
if (!WarnExactMatch)
|
|
|
|
WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
|
|
|
|
isa<ObjCProtocolDecl>(CDecl));
|
|
|
|
else
|
|
|
|
WarnExactTypedMethods(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),
|
2011-07-29 07:19:50 +08:00
|
|
|
IncompleteImpl, false, WarnExactMatch);
|
2010-10-09 06:59:25 +08:00
|
|
|
|
2009-05-02 04:07:12 +08:00
|
|
|
// Check for any implementation of a methods declared in protocol.
|
2010-09-01 09:21:15 +08:00
|
|
|
for (ObjCInterfaceDecl::all_protocol_iterator
|
|
|
|
PI = I->all_referenced_protocol_begin(),
|
|
|
|
E = I->all_referenced_protocol_end(); PI != E; ++PI)
|
2009-09-09 23:08:12 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
|
|
|
IMPDecl,
|
2011-07-29 07:19:50 +08:00
|
|
|
(*PI), IncompleteImpl, false, WarnExactMatch);
|
2011-07-25 04:53:26 +08:00
|
|
|
|
2011-07-29 07:19:50 +08:00
|
|
|
// FIXME. For now, we are not checking for extact match of methods
|
|
|
|
// in category implementation and its primary class's super class.
|
|
|
|
if (!WarnExactMatch && I->getSuperClass())
|
2009-05-02 04:07:12 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
2009-09-09 23:08:12 +08:00
|
|
|
IMPDecl,
|
2009-05-02 04:07:12 +08:00
|
|
|
I->getSuperClass(), IncompleteImpl, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 07:19:50 +08:00
|
|
|
/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
|
|
|
|
/// category matches with those implemented in its primary class and
|
|
|
|
/// warns each time an exact match is found.
|
|
|
|
void Sema::CheckCategoryVsClassMethodMatches(
|
|
|
|
ObjCCategoryImplDecl *CatIMPDecl) {
|
|
|
|
llvm::DenseSet<Selector> InsMap, ClsMap;
|
|
|
|
|
|
|
|
for (ObjCImplementationDecl::instmeth_iterator
|
|
|
|
I = CatIMPDecl->instmeth_begin(),
|
|
|
|
E = CatIMPDecl->instmeth_end(); I!=E; ++I)
|
|
|
|
InsMap.insert((*I)->getSelector());
|
|
|
|
|
|
|
|
for (ObjCImplementationDecl::classmeth_iterator
|
|
|
|
I = CatIMPDecl->classmeth_begin(),
|
|
|
|
E = CatIMPDecl->classmeth_end(); I != E; ++I)
|
|
|
|
ClsMap.insert((*I)->getSelector());
|
|
|
|
if (InsMap.empty() && ClsMap.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get category's primary class.
|
|
|
|
ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
|
|
|
|
if (!CatDecl)
|
|
|
|
return;
|
|
|
|
ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
|
|
|
|
if (!IDecl)
|
|
|
|
return;
|
|
|
|
llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
|
|
|
|
bool IncompleteImpl = false;
|
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
|
|
|
CatIMPDecl, IDecl,
|
|
|
|
IncompleteImpl, false, true /*WarnExactMatch*/);
|
|
|
|
}
|
2011-07-25 04:53:26 +08:00
|
|
|
|
2010-05-06 05:52:17 +08:00
|
|
|
void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCContainerDecl* CDecl,
|
2009-03-01 08:56:52 +08:00
|
|
|
bool IncompleteImpl) {
|
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);
|
2011-08-04 02:21:12 +08:00
|
|
|
|
2011-07-29 07:19:50 +08:00
|
|
|
// check all methods implemented in category against those declared
|
|
|
|
// in its primary class.
|
|
|
|
if (ObjCCategoryImplDecl *CatDecl =
|
|
|
|
dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
|
|
|
|
CheckCategoryVsClassMethodMatches(CatDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// Check the protocol list for unimplemented methods in the @implementation
|
|
|
|
// class.
|
2009-05-02 04:07:12 +08:00
|
|
|
// Check and see if class methods in class interface have been
|
|
|
|
// implemented in the implementation class.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-01 08:56:52 +08:00
|
|
|
if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
|
2010-09-01 09:21:15 +08:00
|
|
|
for (ObjCInterfaceDecl::all_protocol_iterator
|
|
|
|
PI = I->all_referenced_protocol_begin(),
|
|
|
|
E = I->all_referenced_protocol_end(); PI != E; ++PI)
|
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
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("invalid ObjCContainerDecl type.");
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// ActOnForwardClassDeclaration -
|
2011-08-28 04:50:59 +08:00
|
|
|
Sema::DeclGroupPtrTy
|
2007-12-12 15:09:47 +08:00
|
|
|
Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
|
2009-02-17 03:25:52 +08:00
|
|
|
IdentifierInfo **IdentList,
|
2009-11-18 07:12:20 +08:00
|
|
|
SourceLocation *IdentLocs,
|
2009-02-17 03:25:52 +08:00
|
|
|
unsigned NumElts) {
|
2011-08-28 04:50:59 +08:00
|
|
|
SmallVector<Decl *, 8> DeclsInGroup;
|
2007-12-12 15:09:47 +08:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
// Check for another declaration kind with the same name.
|
2009-10-10 05:13:30 +08:00
|
|
|
NamedDecl *PrevDecl
|
2010-04-16 06:33:43 +08:00
|
|
|
= LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
|
2010-04-16 07:40:53 +08:00
|
|
|
LookupOrdinaryName, ForRedeclaration);
|
2008-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);
|
|
|
|
}
|
2011-08-28 04:50:59 +08:00
|
|
|
ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
|
|
|
|
IDecl, IdentLocs[i]);
|
|
|
|
CurContext->addDecl(CDecl);
|
|
|
|
CheckObjCDeclScope(CDecl);
|
|
|
|
DeclsInGroup.push_back(CDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2011-08-28 04:50:59 +08:00
|
|
|
|
|
|
|
return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2011-06-16 09:15:19 +08:00
|
|
|
static bool tryMatchRecordTypes(ASTContext &Context,
|
|
|
|
Sema::MethodMatchStrategy strategy,
|
|
|
|
const Type *left, const Type *right);
|
|
|
|
|
2011-06-16 07:02:42 +08:00
|
|
|
static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
|
|
|
|
QualType leftQT, QualType rightQT) {
|
|
|
|
const Type *left =
|
|
|
|
Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
|
|
|
|
const Type *right =
|
|
|
|
Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
|
|
|
|
|
|
|
|
if (left == right) return true;
|
|
|
|
|
|
|
|
// If we're doing a strict match, the types have to match exactly.
|
|
|
|
if (strategy == Sema::MMS_strict) return false;
|
|
|
|
|
|
|
|
if (left->isIncompleteType() || right->isIncompleteType()) return false;
|
|
|
|
|
|
|
|
// Otherwise, use this absurdly complicated algorithm to try to
|
|
|
|
// validate the basic, low-level compatibility of the two types.
|
|
|
|
|
|
|
|
// As a minimum, require the sizes and alignments to match.
|
|
|
|
if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Consider all the kinds of non-dependent canonical types:
|
|
|
|
// - functions and arrays aren't possible as return and parameter types
|
|
|
|
|
|
|
|
// - vector types of equal size can be arbitrarily mixed
|
|
|
|
if (isa<VectorType>(left)) return isa<VectorType>(right);
|
|
|
|
if (isa<VectorType>(right)) return false;
|
|
|
|
|
|
|
|
// - references should only match references of identical type
|
2011-06-16 09:15:19 +08:00
|
|
|
// - structs, unions, and Objective-C objects must match more-or-less
|
|
|
|
// exactly
|
2011-06-16 07:02:42 +08:00
|
|
|
// - everything else should be a scalar
|
|
|
|
if (!left->isScalarType() || !right->isScalarType())
|
2011-06-16 09:15:19 +08:00
|
|
|
return tryMatchRecordTypes(Context, strategy, left, right);
|
2011-06-16 07:02:42 +08:00
|
|
|
|
2011-09-09 13:25:32 +08:00
|
|
|
// Make scalars agree in kind, except count bools as chars, and group
|
|
|
|
// all non-member pointers together.
|
2011-06-16 07:02:42 +08:00
|
|
|
Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
|
|
|
|
Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
|
|
|
|
if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
|
|
|
|
if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
|
2011-09-09 13:25:32 +08:00
|
|
|
if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
|
|
|
|
leftSK = Type::STK_ObjCObjectPointer;
|
|
|
|
if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
|
|
|
|
rightSK = Type::STK_ObjCObjectPointer;
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
// Note that data member pointers and function member pointers don't
|
|
|
|
// intermix because of the size differences.
|
|
|
|
|
|
|
|
return (leftSK == rightSK);
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
|
2011-06-16 09:15:19 +08:00
|
|
|
static bool tryMatchRecordTypes(ASTContext &Context,
|
|
|
|
Sema::MethodMatchStrategy strategy,
|
|
|
|
const Type *lt, const Type *rt) {
|
|
|
|
assert(lt && rt && lt != rt);
|
|
|
|
|
|
|
|
if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
|
|
|
|
RecordDecl *left = cast<RecordType>(lt)->getDecl();
|
|
|
|
RecordDecl *right = cast<RecordType>(rt)->getDecl();
|
|
|
|
|
|
|
|
// Require union-hood to match.
|
|
|
|
if (left->isUnion() != right->isUnion()) return false;
|
|
|
|
|
|
|
|
// Require an exact match if either is non-POD.
|
|
|
|
if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
|
|
|
|
(isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Require size and alignment to match.
|
|
|
|
if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
|
|
|
|
|
|
|
|
// Require fields to match.
|
|
|
|
RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
|
|
|
|
RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
|
|
|
|
for (; li != le && ri != re; ++li, ++ri) {
|
|
|
|
if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (li == le && ri == re);
|
|
|
|
}
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
|
|
|
|
/// returns true, or false, accordingly.
|
|
|
|
/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
|
2011-06-16 07:02:42 +08:00
|
|
|
bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
|
|
|
|
const ObjCMethodDecl *right,
|
|
|
|
MethodMatchStrategy strategy) {
|
|
|
|
if (!matchTypes(Context, strategy,
|
|
|
|
left->getResultType(), right->getResultType()))
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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-10-03 14:37:04 +08:00
|
|
|
ObjCMethodDecl::param_const_iterator
|
2011-06-16 07:02:42 +08:00
|
|
|
li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
|
|
|
|
|
|
|
|
for (; li != le; ++li, ++ri) {
|
|
|
|
assert(ri != right->param_end() && "Param mismatch");
|
2011-10-03 14:37:04 +08:00
|
|
|
const ParmVarDecl *lparm = *li, *rparm = *ri;
|
2011-06-16 07:02:42 +08:00
|
|
|
|
|
|
|
if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
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()) !=
|
2011-09-26 07:23:43 +08:00
|
|
|
DiagnosticsEngine::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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-06 17:25:23 +08:00
|
|
|
Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
|
|
|
|
switch (CurContext->getDeclKind()) {
|
|
|
|
case Decl::ObjCInterface:
|
|
|
|
return Sema::OCK_Interface;
|
|
|
|
case Decl::ObjCProtocol:
|
|
|
|
return Sema::OCK_Protocol;
|
|
|
|
case Decl::ObjCCategory:
|
|
|
|
if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
|
|
|
|
return Sema::OCK_ClassExtension;
|
|
|
|
else
|
|
|
|
return Sema::OCK_Category;
|
|
|
|
case Decl::ObjCImplementation:
|
|
|
|
return Sema::OCK_Implementation;
|
|
|
|
case Decl::ObjCCategoryImpl:
|
|
|
|
return Sema::OCK_CategoryImplementation;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return Sema::OCK_None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-18 09:30:32 +08:00
|
|
|
// Note: For class/category implemenations, allMethods/allProperties is
|
|
|
|
// always null.
|
2011-12-06 17:25:23 +08:00
|
|
|
Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
|
|
|
|
Decl **allMethods, unsigned allNum,
|
|
|
|
Decl **allProperties, unsigned pNum,
|
|
|
|
DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
|
|
|
|
|
|
|
|
if (getObjCContainerKind() == Sema::OCK_None)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
assert(AtEnd.isValid() && "Invalid location for '@end'");
|
2011-08-22 23:54:49 +08:00
|
|
|
|
|
|
|
ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
|
|
|
|
Decl *ClassDecl = cast<Decl>(OCD);
|
2009-11-17 02:57:01 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool isInterfaceDeclKind =
|
2008-03-17 05:17:37 +08:00
|
|
|
isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
|
|
|
|
|| isa<ObjCProtocolDecl>(ClassDecl);
|
2008-01-08 03:49:32 +08:00
|
|
|
bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
|
2009-01-09 23:36:25 +08:00
|
|
|
|
2009-01-09 01:28:14 +08:00
|
|
|
// FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
|
|
|
|
llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
|
|
|
|
llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
|
|
|
|
|
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 {
|
2011-10-14 14:48:06 +08:00
|
|
|
if (PrevMethod)
|
2011-10-14 16:02:31 +08:00
|
|
|
Method->setAsRedeclaration(PrevMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
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 {
|
2011-10-14 14:48:06 +08:00
|
|
|
if (PrevMethod)
|
2011-10-14 16:02:31 +08:00
|
|
|
Method->setAsRedeclaration(PrevMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
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-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
|
|
|
}
|
2011-08-30 01:33:12 +08:00
|
|
|
ActOnObjCContainerFinishDefinition();
|
2011-10-18 03:48:13 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0; i != tuvNum; i++) {
|
|
|
|
DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
|
2011-11-24 04:27:36 +08:00
|
|
|
for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
|
|
|
|
(*I)->setTopLevelDeclInObjCContainer();
|
2011-10-18 03:48:13 +08:00
|
|
|
Consumer.HandleTopLevelDeclInObjCContainer(DG);
|
|
|
|
}
|
2011-12-06 17:25:23 +08:00
|
|
|
|
|
|
|
return ClassDecl;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
|
|
|
|
/// objective-c's type qualifier from the parser version of the same info.
|
2009-09-09 23:08:12 +08:00
|
|
|
static Decl::ObjCDeclQualifier
|
2008-01-08 03:49:32 +08:00
|
|
|
CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
|
2011-05-01 11:04:29 +08:00
|
|
|
return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2010-04-18 12:59:38 +08:00
|
|
|
static inline
|
2011-10-22 09:21:15 +08:00
|
|
|
bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
|
|
|
|
const AttrVec &A) {
|
|
|
|
// If method is only declared in implementation (private method),
|
|
|
|
// No need to issue any diagnostics on method definition with attributes.
|
2011-10-22 09:56:45 +08:00
|
|
|
if (!IMD)
|
2011-10-22 09:21:15 +08:00
|
|
|
return false;
|
|
|
|
|
2011-10-22 09:56:45 +08:00
|
|
|
// method declared in interface has no attribute.
|
|
|
|
// But implementation has attributes. This is invalid
|
|
|
|
if (!IMD->hasAttrs())
|
|
|
|
return true;
|
|
|
|
|
2011-10-22 09:21:15 +08:00
|
|
|
const AttrVec &D = IMD->getAttrs();
|
|
|
|
if (D.size() != A.size())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// attributes on method declaration and definition must match exactly.
|
|
|
|
// Note that we have at most a couple of attributes on methods, so this
|
|
|
|
// n*n search is good enough.
|
|
|
|
for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
|
|
|
|
bool match = false;
|
|
|
|
for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
|
|
|
|
if ((*i)->getKind() == (*i1)->getKind()) {
|
|
|
|
match = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!match)
|
2010-08-19 07:23:40 +08:00
|
|
|
return true;
|
2011-10-22 09:21:15 +08:00
|
|
|
}
|
2010-08-19 07:23:40 +08:00
|
|
|
return false;
|
2010-04-18 12:59:38 +08:00
|
|
|
}
|
|
|
|
|
2011-09-08 09:46:34 +08:00
|
|
|
namespace {
|
|
|
|
/// \brief Describes the compatibility of a result type with its method.
|
|
|
|
enum ResultTypeCompatibilityKind {
|
|
|
|
RTC_Compatible,
|
|
|
|
RTC_Incompatible,
|
|
|
|
RTC_Unknown
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
///
|
2011-09-08 09:46:34 +08:00
|
|
|
static ResultTypeCompatibilityKind
|
2011-06-11 09:09:30 +08:00
|
|
|
CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
|
|
|
|
ObjCInterfaceDecl *CurrentClass) {
|
|
|
|
QualType ResultType = Method->getResultType();
|
|
|
|
|
|
|
|
// If an Objective-C method inherits its related result type, then its
|
|
|
|
// declared result type must be compatible with its own class type. The
|
|
|
|
// declared result type is compatible if:
|
|
|
|
if (const ObjCObjectPointerType *ResultObjectType
|
|
|
|
= ResultType->getAs<ObjCObjectPointerType>()) {
|
|
|
|
// - it is id or qualified id, or
|
|
|
|
if (ResultObjectType->isObjCIdType() ||
|
|
|
|
ResultObjectType->isObjCQualifiedIdType())
|
2011-09-08 09:46:34 +08:00
|
|
|
return RTC_Compatible;
|
2011-06-11 09:09:30 +08:00
|
|
|
|
|
|
|
if (CurrentClass) {
|
|
|
|
if (ObjCInterfaceDecl *ResultClass
|
|
|
|
= ResultObjectType->getInterfaceDecl()) {
|
|
|
|
// - it is the same as the method's class type, or
|
|
|
|
if (CurrentClass == ResultClass)
|
2011-09-08 09:46:34 +08:00
|
|
|
return RTC_Compatible;
|
2011-06-11 09:09:30 +08:00
|
|
|
|
|
|
|
// - it is a superclass of the method's class type
|
|
|
|
if (ResultClass->isSuperClassOf(CurrentClass))
|
2011-09-08 09:46:34 +08:00
|
|
|
return RTC_Compatible;
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
2011-09-08 09:46:34 +08:00
|
|
|
} else {
|
|
|
|
// Any Objective-C pointer type might be acceptable for a protocol
|
|
|
|
// method; we just don't know.
|
|
|
|
return RTC_Unknown;
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-08 09:46:34 +08:00
|
|
|
return RTC_Incompatible;
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
|
|
|
|
2011-07-22 10:45:48 +08:00
|
|
|
namespace {
|
|
|
|
/// A helper class for searching for methods which a particular method
|
|
|
|
/// overrides.
|
|
|
|
class OverrideSearch {
|
|
|
|
Sema &S;
|
|
|
|
ObjCMethodDecl *Method;
|
|
|
|
llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
|
|
|
|
llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
|
|
|
|
bool Recursive;
|
|
|
|
|
|
|
|
public:
|
|
|
|
OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
|
|
|
|
Selector selector = method->getSelector();
|
|
|
|
|
|
|
|
// Bypass this search if we've never seen an instance/class method
|
|
|
|
// with this selector before.
|
|
|
|
Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
|
|
|
|
if (it == S.MethodPool.end()) {
|
|
|
|
if (!S.ExternalSource) return;
|
|
|
|
it = S.ReadMethodPool(selector);
|
|
|
|
}
|
|
|
|
ObjCMethodList &list =
|
|
|
|
method->isInstanceMethod() ? it->second.first : it->second.second;
|
|
|
|
if (!list.Method) return;
|
|
|
|
|
|
|
|
ObjCContainerDecl *container
|
|
|
|
= cast<ObjCContainerDecl>(method->getDeclContext());
|
|
|
|
|
|
|
|
// Prevent the search from reaching this container again. This is
|
|
|
|
// important with categories, which override methods from the
|
|
|
|
// interface and each other.
|
|
|
|
Searched.insert(container);
|
|
|
|
searchFromContainer(container);
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
2011-07-22 10:45:48 +08:00
|
|
|
|
|
|
|
typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
|
|
|
|
iterator begin() const { return Overridden.begin(); }
|
|
|
|
iterator end() const { return Overridden.end(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void searchFromContainer(ObjCContainerDecl *container) {
|
|
|
|
if (container->isInvalidDecl()) return;
|
|
|
|
|
|
|
|
switch (container->getDeclKind()) {
|
|
|
|
#define OBJCCONTAINER(type, base) \
|
|
|
|
case Decl::type: \
|
|
|
|
searchFrom(cast<type##Decl>(container)); \
|
|
|
|
break;
|
|
|
|
#define ABSTRACT_DECL(expansion)
|
|
|
|
#define DECL(type, base) \
|
|
|
|
case Decl::type:
|
|
|
|
#include "clang/AST/DeclNodes.inc"
|
|
|
|
llvm_unreachable("not an ObjC container!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void searchFrom(ObjCProtocolDecl *protocol) {
|
|
|
|
// A method in a protocol declaration overrides declarations from
|
|
|
|
// referenced ("parent") protocols.
|
|
|
|
search(protocol->getReferencedProtocols());
|
|
|
|
}
|
|
|
|
|
|
|
|
void searchFrom(ObjCCategoryDecl *category) {
|
|
|
|
// A method in a category declaration overrides declarations from
|
|
|
|
// the main class and from protocols the category references.
|
|
|
|
search(category->getClassInterface());
|
|
|
|
search(category->getReferencedProtocols());
|
|
|
|
}
|
|
|
|
|
|
|
|
void searchFrom(ObjCCategoryImplDecl *impl) {
|
|
|
|
// A method in a category definition that has a category
|
|
|
|
// declaration overrides declarations from the category
|
|
|
|
// declaration.
|
|
|
|
if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
|
|
|
|
search(category);
|
|
|
|
|
|
|
|
// Otherwise it overrides declarations from the class.
|
|
|
|
} else {
|
|
|
|
search(impl->getClassInterface());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void searchFrom(ObjCInterfaceDecl *iface) {
|
|
|
|
// A method in a class declaration overrides declarations from
|
|
|
|
|
|
|
|
// - categories,
|
|
|
|
for (ObjCCategoryDecl *category = iface->getCategoryList();
|
|
|
|
category; category = category->getNextClassCategory())
|
|
|
|
search(category);
|
|
|
|
|
|
|
|
// - the super class, and
|
|
|
|
if (ObjCInterfaceDecl *super = iface->getSuperClass())
|
|
|
|
search(super);
|
|
|
|
|
|
|
|
// - any referenced protocols.
|
|
|
|
search(iface->getReferencedProtocols());
|
|
|
|
}
|
|
|
|
|
|
|
|
void searchFrom(ObjCImplementationDecl *impl) {
|
|
|
|
// A method in a class implementation overrides declarations from
|
|
|
|
// the class interface.
|
|
|
|
search(impl->getClassInterface());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void search(const ObjCProtocolList &protocols) {
|
|
|
|
for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
|
|
|
|
i != e; ++i)
|
|
|
|
search(*i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void search(ObjCContainerDecl *container) {
|
|
|
|
// Abort if we've already searched this container.
|
|
|
|
if (!Searched.insert(container)) return;
|
|
|
|
|
|
|
|
// Check for a method in this container which matches this selector.
|
|
|
|
ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
|
|
|
|
Method->isInstanceMethod());
|
|
|
|
|
|
|
|
// If we find one, record it and bail out.
|
|
|
|
if (meth) {
|
|
|
|
Overridden.insert(meth);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, search for methods that a hypothetical method here
|
|
|
|
// would have overridden.
|
|
|
|
|
|
|
|
// Note that we're now in a recursive case.
|
|
|
|
Recursive = true;
|
|
|
|
|
|
|
|
searchFromContainer(container);
|
|
|
|
}
|
|
|
|
};
|
2011-06-11 09:09:30 +08:00
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::ActOnMethodDeclaration(
|
2011-02-10 06:20:01 +08:00
|
|
|
Scope *S,
|
2007-12-12 15:09:47 +08:00
|
|
|
SourceLocation MethodLoc, SourceLocation EndLoc,
|
2011-08-22 23:54:49 +08:00
|
|
|
tok::TokenKind MethodType,
|
2010-08-24 13:47:05 +08:00
|
|
|
ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
|
2011-10-03 14:36:36 +08:00
|
|
|
ArrayRef<SourceLocation> SelectorLocs,
|
2007-12-12 15:09:47 +08:00
|
|
|
Selector Sel,
|
|
|
|
// optional arguments. The number of types/arguments is obtained
|
|
|
|
// from the Sel.getNumArgs().
|
2009-04-12 02:57:04 +08:00
|
|
|
ObjCArgInfo *ArgInfo,
|
2010-04-08 08:30:06 +08:00
|
|
|
DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
|
2007-12-12 15:09:47 +08:00
|
|
|
AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
|
2011-03-13 02:54:30 +08:00
|
|
|
bool isVariadic, bool MethodDefinition) {
|
2008-03-01 05:48:07 +08:00
|
|
|
// Make sure we can establish a context for the method.
|
2011-08-22 23:54:49 +08:00
|
|
|
if (!CurContext->isObjCContainer()) {
|
2008-03-01 05:48:07 +08:00
|
|
|
Diag(MethodLoc, diag::error_missing_method_context);
|
2010-08-21 17:40:31 +08:00
|
|
|
return 0;
|
2008-03-01 05:48:07 +08:00
|
|
|
}
|
2011-08-22 23:54:49 +08:00
|
|
|
ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
|
|
|
|
Decl *ClassDecl = cast<Decl>(OCD);
|
2008-04-04 14:12:32 +08:00
|
|
|
QualType resultDeclType;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-09-08 09:46:34 +08:00
|
|
|
bool HasRelatedResultType = false;
|
2010-03-08 22:59:44 +08:00
|
|
|
TypeSourceInfo *ResultTInfo = 0;
|
2009-02-21 06:59:16 +08:00
|
|
|
if (ReturnType) {
|
2010-03-08 22:59:44 +08:00
|
|
|
resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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-09-08 09:46:34 +08:00
|
|
|
|
|
|
|
HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
|
2011-07-22 01:00:47 +08:00
|
|
|
} else { // get the type for "id".
|
2008-04-04 14:12:32 +08:00
|
|
|
resultDeclType = Context.getObjCIdType();
|
2011-07-22 01:38:14 +08:00
|
|
|
Diag(MethodLoc, diag::warn_missing_method_return_type)
|
2011-10-03 14:36:36 +08:00
|
|
|
<< FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
|
2011-07-22 01:00:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ObjCMethodDecl* ObjCMethod =
|
2011-10-03 14:37:04 +08:00
|
|
|
ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
|
2011-10-03 14:36:36 +08:00
|
|
|
resultDeclType,
|
2010-03-08 22:59:44 +08:00
|
|
|
ResultTInfo,
|
2011-08-22 23:54:49 +08:00
|
|
|
CurContext,
|
2008-04-04 14:12:32 +08:00
|
|
|
MethodType == tok::minus, isVariadic,
|
2011-08-18 03:25:08 +08:00
|
|
|
/*isSynthesized=*/false,
|
|
|
|
/*isImplicitlyDeclared=*/false, /*isDefined=*/false,
|
2011-06-11 09:09:30 +08:00
|
|
|
MethodDeclKind == tok::objc_optional
|
|
|
|
? ObjCMethodDecl::Optional
|
|
|
|
: ObjCMethodDecl::Required,
|
2011-09-08 09:46:34 +08:00
|
|
|
HasRelatedResultType);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVector<ParmVarDecl*, 16> Params;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 03:42:43 +08:00
|
|
|
for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
|
2009-10-24 05:48:59 +08:00
|
|
|
QualType ArgType;
|
2009-12-07 10:54:59 +08:00
|
|
|
TypeSourceInfo *DI;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-10-03 14:37:04 +08:00
|
|
|
ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCMethod->setObjCDeclQualifier(
|
|
|
|
CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
|
2008-09-26 12:12:28 +08:00
|
|
|
|
|
|
|
if (AttrList)
|
2009-06-18 05:51:59 +08:00
|
|
|
ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
Fix a major inconsistency in the representation of Objective-C
classes, categories, protocols, and class extensions, where the
methods and properties of these entities would be inserted into the
DeclContext in an ordering that doesn't necessarily reflect source
order. The culprits were Sema::ActOnMethodDeclaration(), which did not
perform the insertion of the just-created method declaration into
the DeclContext for these Objective-C entities, and
Sema::ActOnAtEnd(), which inserted all method declarations at the
*end* of the DeclContext.
With this fix in hand, clean up the code-completion actions for
property setters/getters that worked around this brokenness in the AST.
Fixes <rdar://problem/8062781>, where this problem manifested as poor
token-annotation information, but this would have struck again in many
other places.
llvm-svn: 122347
2010-12-22 01:34:17 +08:00
|
|
|
// Add the method now.
|
2011-07-22 10:45:48 +08:00
|
|
|
const ObjCMethodDecl *PrevMethod = 0;
|
|
|
|
if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
|
2007-12-12 15:09:47 +08:00
|
|
|
if (MethodType == tok::minus) {
|
2009-06-30 10:36:12 +08:00
|
|
|
PrevMethod = ImpDecl->getInstanceMethod(Sel);
|
|
|
|
ImpDecl->addInstanceMethod(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
} else {
|
2009-06-30 10:36:12 +08:00
|
|
|
PrevMethod = ImpDecl->getClassMethod(Sel);
|
|
|
|
ImpDecl->addClassMethod(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2011-06-11 09:09:30 +08:00
|
|
|
|
2011-10-22 09:21:15 +08:00
|
|
|
ObjCMethodDecl *IMD = 0;
|
|
|
|
if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
|
|
|
|
IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
|
|
|
|
ObjCMethod->isInstanceMethod());
|
2010-08-19 07:23:40 +08:00
|
|
|
if (ObjCMethod->hasAttrs() &&
|
2011-12-06 08:02:41 +08:00
|
|
|
containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
|
2009-05-13 05:36:23 +08:00
|
|
|
Diag(EndLoc, diag::warn_attribute_method_def);
|
2011-12-06 08:02:41 +08:00
|
|
|
Diag(IMD->getLocation(), diag::note_method_declared_at);
|
|
|
|
}
|
Fix a major inconsistency in the representation of Objective-C
classes, categories, protocols, and class extensions, where the
methods and properties of these entities would be inserted into the
DeclContext in an ordering that doesn't necessarily reflect source
order. The culprits were Sema::ActOnMethodDeclaration(), which did not
perform the insertion of the just-created method declaration into
the DeclContext for these Objective-C entities, and
Sema::ActOnAtEnd(), which inserted all method declarations at the
*end* of the DeclContext.
With this fix in hand, clean up the code-completion actions for
property setters/getters that worked around this brokenness in the AST.
Fixes <rdar://problem/8062781>, where this problem manifested as poor
token-annotation information, but this would have struck again in many
other places.
llvm-svn: 122347
2010-12-22 01:34:17 +08:00
|
|
|
} else {
|
|
|
|
cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2011-07-22 10:45:48 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
if (PrevMethod) {
|
|
|
|
// You can never have two method definitions with the same name.
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
|
2008-11-24 11:33:13 +08:00
|
|
|
<< ObjCMethod->getDeclName();
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-11-04 10:18:39 +08:00
|
|
|
|
2011-06-11 09:09:30 +08:00
|
|
|
// If this Objective-C method does not have a related result type, but we
|
|
|
|
// are allowed to infer related result types, try to do so based on the
|
|
|
|
// method family.
|
|
|
|
ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
|
|
|
|
if (!CurrentClass) {
|
|
|
|
if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
|
|
|
|
CurrentClass = Cat->getClassInterface();
|
|
|
|
else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
|
|
|
|
CurrentClass = Impl->getClassInterface();
|
|
|
|
else if (ObjCCategoryImplDecl *CatImpl
|
|
|
|
= dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
|
|
|
|
CurrentClass = CatImpl->getClassInterface();
|
|
|
|
}
|
2011-07-22 10:45:48 +08:00
|
|
|
|
2011-09-08 09:46:34 +08:00
|
|
|
ResultTypeCompatibilityKind RTC
|
|
|
|
= CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
|
2011-07-22 10:45:48 +08:00
|
|
|
|
|
|
|
// Search for overridden methods and merge information down from them.
|
|
|
|
OverrideSearch overrides(*this, ObjCMethod);
|
|
|
|
for (OverrideSearch::iterator
|
|
|
|
i = overrides.begin(), e = overrides.end(); i != e; ++i) {
|
|
|
|
ObjCMethodDecl *overridden = *i;
|
|
|
|
|
|
|
|
// Propagate down the 'related result type' bit from overridden methods.
|
2011-09-08 09:46:34 +08:00
|
|
|
if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
|
2011-06-11 09:09:30 +08:00
|
|
|
ObjCMethod->SetRelatedResultType();
|
2011-07-22 10:45:48 +08:00
|
|
|
|
|
|
|
// Then merge the declarations.
|
|
|
|
mergeObjCMethodDecls(ObjCMethod, overridden);
|
2011-08-11 01:16:30 +08:00
|
|
|
|
|
|
|
// Check for overriding methods
|
|
|
|
if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
|
2011-10-11 01:53:29 +08:00
|
|
|
isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
|
|
|
|
CheckConflictingOverridingMethod(ObjCMethod, overridden,
|
|
|
|
isa<ObjCProtocolDecl>(overridden->getDeclContext()));
|
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);
|
|
|
|
|
2011-09-08 09:46:34 +08:00
|
|
|
// Infer the related result type when possible.
|
|
|
|
if (!ARCError && RTC == RTC_Compatible &&
|
|
|
|
!ObjCMethod->hasRelatedResultType() &&
|
|
|
|
LangOpts.ObjCInferRelatedResultType) {
|
2011-06-11 09:09:30 +08:00
|
|
|
bool InferRelatedResultType = false;
|
|
|
|
switch (ObjCMethod->getMethodFamily()) {
|
|
|
|
case OMF_None:
|
|
|
|
case OMF_copy:
|
|
|
|
case OMF_dealloc:
|
2011-08-29 06:35:17 +08:00
|
|
|
case OMF_finalize:
|
2011-06-11 09:09:30 +08:00
|
|
|
case OMF_mutableCopy:
|
|
|
|
case OMF_release:
|
|
|
|
case OMF_retainCount:
|
2011-07-06 06:38:59 +08:00
|
|
|
case OMF_performSelector:
|
2011-06-11 09:09:30 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OMF_alloc:
|
|
|
|
case OMF_new:
|
|
|
|
InferRelatedResultType = ObjCMethod->isClassMethod();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OMF_init:
|
|
|
|
case OMF_autorelease:
|
|
|
|
case OMF_retain:
|
|
|
|
case OMF_self:
|
|
|
|
InferRelatedResultType = ObjCMethod->isInstanceMethod();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-07-22 10:45:48 +08:00
|
|
|
if (InferRelatedResultType)
|
2011-06-11 09:09:30 +08:00
|
|
|
ObjCMethod->SetRelatedResultType();
|
|
|
|
}
|
|
|
|
|
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;
|
2011-08-23 02:34:22 +08:00
|
|
|
// Following is also an error. But it is caused by a missing @end
|
|
|
|
// and diagnostic is issued elsewhere.
|
2011-08-22 23:54:49 +08:00
|
|
|
if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
|
|
|
|
D->setInvalidDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-12-17 15:13:27 +08:00
|
|
|
|
|
|
|
/// 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,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<Decl*> &Decls) {
|
2008-12-17 15:13:27 +08:00
|
|
|
// Check that ClassName is a valid class
|
2010-04-16 06:33:43 +08:00
|
|
|
ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
|
2008-12-17 15:13:27 +08:00
|
|
|
if (!Class) {
|
|
|
|
Diag(DeclStart, diag::err_undef_interface) << ClassName;
|
|
|
|
return;
|
|
|
|
}
|
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
|
2011-07-22 10:08:32 +08:00
|
|
|
SmallVector<const ObjCIvarDecl*, 32> Ivars;
|
2010-08-21 05:21:08 +08:00
|
|
|
Context.DeepCollectObjCIvars(Class, true, Ivars);
|
2009-06-05 01:08:55 +08:00
|
|
|
// For each ivar, create a fresh ObjCAtDefsFieldDecl.
|
2010-08-21 05:21:08 +08:00
|
|
|
for (unsigned i = 0; i < Ivars.size(); i++) {
|
2011-07-22 10:08:32 +08:00
|
|
|
const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
|
2010-08-21 17:40:31 +08:00
|
|
|
RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
|
2011-03-08 16:55:46 +08:00
|
|
|
Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
|
|
|
|
/*FIXME: StartL=*/ID->getLocation(),
|
|
|
|
ID->getLocation(),
|
2009-06-05 01:08:55 +08:00
|
|
|
ID->getIdentifier(), ID->getType(),
|
|
|
|
ID->getBitWidth());
|
2010-08-21 17:40:31 +08:00
|
|
|
Decls.push_back(FD);
|
2009-06-05 01:08:55 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-12-17 15:13:27 +08:00
|
|
|
// Introduce all of these fields into the appropriate scope.
|
2011-07-23 18:55:15 +08:00
|
|
|
for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
|
2008-12-17 15:13:27 +08:00
|
|
|
D != Decls.end(); ++D) {
|
2010-08-21 17:40:31 +08:00
|
|
|
FieldDecl *FD = cast<FieldDecl>(*D);
|
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);
|
|
|
|
|
2011-12-10 09:22:52 +08:00
|
|
|
// In ARC, infer 'retaining' for variables of retainable type.
|
|
|
|
if (getLangOptions().ObjCAutoRefCount && inferObjCARCLifetime(New))
|
|
|
|
Invalid = true;
|
|
|
|
|
2010-04-27 01:32:49 +08:00
|
|
|
if (Invalid)
|
|
|
|
New->setInvalidDecl();
|
|
|
|
return New;
|
|
|
|
}
|
|
|
|
|
2010-08-21 17:40:31 +08:00
|
|
|
Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
|
2010-04-27 01:32:49 +08:00
|
|
|
const DeclSpec &DS = D.getDeclSpec();
|
|
|
|
|
|
|
|
// We allow the "register" storage class on exception variables because
|
|
|
|
// GCC did, but we drop it completely. Any other storage class is an error.
|
|
|
|
if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
|
|
|
|
Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
|
|
|
|
<< FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
|
|
|
|
} 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,
|
2011-07-23 18:55:15 +08:00
|
|
|
SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
|
2010-08-21 05:21:08 +08:00
|
|
|
for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
|
|
|
|
Iv= Iv->getNextIvar()) {
|
2010-04-28 01:18:58 +08:00
|
|
|
QualType QT = Context.getBaseElementType(Iv->getType());
|
2010-05-20 10:24:22 +08:00
|
|
|
if (QT->isRecordType())
|
2010-08-21 05:21:08 +08:00
|
|
|
Ivars.push_back(Iv);
|
2010-04-28 01:18:58 +08:00
|
|
|
}
|
|
|
|
}
|
2010-04-29 00:11:27 +08:00
|
|
|
|
2010-07-23 02:24:20 +08:00
|
|
|
void Sema::DiagnoseUseOfUnimplementedSelectors() {
|
2011-07-28 22:54:22 +08:00
|
|
|
// Load referenced selectors from the external source.
|
|
|
|
if (ExternalSource) {
|
|
|
|
SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
|
|
|
|
ExternalSource->ReadReferencedSelectors(Sels);
|
|
|
|
for (unsigned I = 0, N = Sels.size(); I != N; ++I)
|
|
|
|
ReferencedSelectors[Sels[I].first] = Sels[I].second;
|
|
|
|
}
|
|
|
|
|
2011-02-05 07:19:27 +08:00
|
|
|
// Warning will be issued only when selector table is
|
|
|
|
// generated (which means there is at lease one implementation
|
|
|
|
// in the TU). This is to match gcc's behavior.
|
|
|
|
if (ReferencedSelectors.empty() ||
|
|
|
|
!Context.AnyObjCImplementation())
|
2010-07-23 02:24:20 +08:00
|
|
|
return;
|
|
|
|
for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
|
|
|
|
ReferencedSelectors.begin(),
|
|
|
|
E = ReferencedSelectors.end(); S != E; ++S) {
|
|
|
|
Selector Sel = (*S).first;
|
|
|
|
if (!LookupImplementedMethodInGlobalPool(Sel))
|
|
|
|
Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|