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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Sema.h"
|
2010-01-04 02:01:57 +08:00
|
|
|
#include "Lookup.h"
|
2009-04-25 05:10:55 +08:00
|
|
|
#include "clang/Sema/ExternalSemaSource.h"
|
2009-03-03 22:49:36 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2007-12-12 15:09:47 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
2008-08-11 11:27:53 +08:00
|
|
|
#include "clang/Parse/DeclSpec.h"
|
2007-12-12 15:09:47 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
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.
|
2009-03-29 03:18:32 +08:00
|
|
|
void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, DeclPtrTy D) {
|
2008-06-28 14:07:14 +08:00
|
|
|
assert(getCurMethodDecl() == 0 && "Method parsing confused");
|
2009-03-29 03:18:32 +08:00
|
|
|
ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D.getAs<Decl>());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-07-26 01:57:26 +08:00
|
|
|
// If we don't have a valid method decl, simply return.
|
|
|
|
if (!MDecl)
|
|
|
|
return;
|
2007-12-18 09:30:32 +08:00
|
|
|
|
|
|
|
// Allow the rest of sema to find private method decl implementations.
|
2009-01-10 01:18:27 +08:00
|
|
|
if (MDecl->isInstanceMethod())
|
2007-12-18 09:30:32 +08:00
|
|
|
AddInstanceMethodToGlobalPool(MDecl);
|
|
|
|
else
|
|
|
|
AddFactoryMethodToGlobalPool(MDecl);
|
2009-09-09 23:08:12 +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(),
|
|
|
|
E = MDecl->param_end(); PI != E; ++PI)
|
|
|
|
if ((*PI)->getIdentifier())
|
|
|
|
PushOnScopeChains(*PI, FnBodyScope);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2009-03-29 03:18:32 +08:00
|
|
|
Sema::DeclPtrTy Sema::
|
2008-07-22 06:17:28 +08:00
|
|
|
ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
|
|
|
|
IdentifierInfo *ClassName, SourceLocation ClassLoc,
|
|
|
|
IdentifierInfo *SuperName, SourceLocation SuperLoc,
|
2009-03-29 03:18:32 +08:00
|
|
|
const DeclPtrTy *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.
|
2009-10-10 05:13:30 +08:00
|
|
|
NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
|
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(ClassLoc, 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-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
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
if (IDecl) {
|
|
|
|
// Class already seen. Is it a forward declaration?
|
2008-11-19 03:15:30 +08:00
|
|
|
if (!IDecl->isForwardDecl()) {
|
2009-02-24 06:00:08 +08:00
|
|
|
IDecl->setInvalidDecl();
|
2008-11-24 13:29:24 +08:00
|
|
|
Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
|
2008-11-24 06:46:27 +08:00
|
|
|
Diag(IDecl->getLocation(), diag::note_previous_definition);
|
|
|
|
|
2008-11-19 03:15:30 +08:00
|
|
|
// Return the previous class interface.
|
|
|
|
// FIXME: don't leak the objects passed in!
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(IDecl);
|
2008-11-19 03:15:30 +08:00
|
|
|
} else {
|
2007-12-12 15:09:47 +08:00
|
|
|
IDecl->setLocation(AtInterfaceLoc);
|
|
|
|
IDecl->setForwardDecl(false);
|
2009-09-11 08:12:01 +08:00
|
|
|
IDecl->setClassLoc(ClassLoc);
|
2009-11-18 06:58: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).
|
|
|
|
CurContext->addDecl(IDecl);
|
|
|
|
|
2009-11-18 03:08:08 +08:00
|
|
|
if (AttrList)
|
|
|
|
ProcessDeclAttributeList(TUScope, IDecl, AttrList);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2008-07-21 15:06:49 +08:00
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
|
2008-04-12 03:35:35 +08:00
|
|
|
ClassName, ClassLoc);
|
2008-08-21 02:02:42 +08:00
|
|
|
if (AttrList)
|
2009-06-18 05:51:59 +08:00
|
|
|
ProcessDeclAttributeList(TUScope, IDecl, AttrList);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-23 23:15:40 +08:00
|
|
|
PushOnScopeChains(IDecl, TUScope);
|
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 (SuperName) {
|
|
|
|
// Check if a different kind of symbol declared in this scope.
|
2009-10-10 05:13:30 +08:00
|
|
|
PrevDecl = LookupSingleName(TUScope, SuperName, LookupOrdinaryName);
|
2010-01-04 02:01:57 +08:00
|
|
|
|
|
|
|
if (!PrevDecl) {
|
|
|
|
// Try to correct for a typo in the superclass name.
|
|
|
|
LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
|
|
|
|
if (CorrectTypo(R, TUScope, 0) &&
|
|
|
|
(PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
|
|
|
|
Diag(SuperLoc, diag::err_undef_superclass_suggest)
|
|
|
|
<< SuperName << ClassName << PrevDecl->getDeclName();
|
2010-01-07 08:17:44 +08:00
|
|
|
Diag(PrevDecl->getLocation(), diag::note_previous_decl)
|
|
|
|
<< PrevDecl->getDeclName();
|
2010-01-04 02:01:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-10 06:08:26 +08:00
|
|
|
if (PrevDecl == IDecl) {
|
|
|
|
Diag(SuperLoc, diag::err_recursive_superclass)
|
|
|
|
<< SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
|
|
|
|
IDecl->setLocEnd(ClassLoc);
|
2009-08-05 05:02:39 +08:00
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCInterfaceDecl *SuperClassDecl =
|
2009-07-10 06:08:26 +08:00
|
|
|
dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
|
2008-11-19 16:23:25 +08:00
|
|
|
|
2009-07-10 06:08:26 +08:00
|
|
|
// Diagnose classes that inherit from deprecated classes.
|
|
|
|
if (SuperClassDecl)
|
|
|
|
(void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-07-10 06:08:26 +08:00
|
|
|
if (PrevDecl && SuperClassDecl == 0) {
|
|
|
|
// The previous declaration was not a class decl. Check if we have a
|
|
|
|
// typedef. If we do, get the underlying class type.
|
|
|
|
if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
|
|
|
|
QualType T = TDecl->getUnderlyingType();
|
|
|
|
if (T->isObjCInterfaceType()) {
|
2009-09-22 07:43:11 +08:00
|
|
|
if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl())
|
2009-07-10 06:08:26 +08:00
|
|
|
SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
|
|
|
|
}
|
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
|
|
|
|
2009-07-10 06:08:26 +08:00
|
|
|
if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
|
|
|
|
if (!SuperClassDecl)
|
|
|
|
Diag(SuperLoc, diag::err_undef_superclass)
|
|
|
|
<< SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
|
|
|
|
else if (SuperClassDecl->isForwardDecl())
|
|
|
|
Diag(SuperLoc, diag::err_undef_superclass)
|
|
|
|
<< SuperClassDecl->getDeclName() << ClassName
|
|
|
|
<< SourceRange(AtInterfaceLoc, ClassLoc);
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2008-11-19 03:15:30 +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);
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(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.
|
2009-03-29 03:18:32 +08:00
|
|
|
Sema::DeclPtrTy Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
IdentifierInfo *AliasName,
|
2009-03-29 03:18:32 +08:00
|
|
|
SourceLocation AliasLocation,
|
|
|
|
IdentifierInfo *ClassName,
|
|
|
|
SourceLocation ClassLocation) {
|
2007-12-12 15:09:47 +08:00
|
|
|
// Look for previous declaration of alias name
|
2009-10-10 05:13:30 +08:00
|
|
|
NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, LookupOrdinaryName);
|
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);
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy();
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
// Check for class declaration
|
2009-10-10 05:13:30 +08:00
|
|
|
NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
|
2009-01-08 09:10:55 +08:00
|
|
|
if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
|
|
|
|
QualType T = TDecl->getUnderlyingType();
|
|
|
|
if (T->isObjCInterfaceType()) {
|
2009-09-22 07:43:11 +08:00
|
|
|
if (NamedDecl *IDecl = T->getAs<ObjCInterfaceType>()->getDecl()) {
|
2009-01-08 09:10:55 +08:00
|
|
|
ClassName = IDecl->getIdentifier();
|
2009-10-10 05:13:30 +08:00
|
|
|
CDeclU = LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
|
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);
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy();
|
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
|
|
|
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(AliasDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2009-03-05 23:22:01 +08:00
|
|
|
void Sema::CheckForwardProtocolDeclarationForCircularDependency(
|
|
|
|
IdentifierInfo *PName,
|
|
|
|
SourceLocation &Ploc, SourceLocation PrevLoc,
|
2009-09-09 23:08:12 +08:00
|
|
|
const ObjCList<ObjCProtocolDecl> &PList) {
|
2009-03-05 23:22:01 +08:00
|
|
|
for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
|
|
|
|
E = PList.end(); I != E; ++I) {
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-24 07:18:26 +08:00
|
|
|
if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier())) {
|
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);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
|
2009-03-05 23:22:01 +08:00
|
|
|
PDecl->getLocation(), PDecl->getReferencedProtocols());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-29 03:18:32 +08:00
|
|
|
Sema::DeclPtrTy
|
2008-07-26 12:03:38 +08:00
|
|
|
Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
|
|
|
|
IdentifierInfo *ProtocolName,
|
|
|
|
SourceLocation ProtocolLoc,
|
2009-03-29 03:18:32 +08:00
|
|
|
const DeclPtrTy *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) {
|
|
|
|
// FIXME: Deal with AttrList.
|
2007-12-12 15:09:47 +08:00
|
|
|
assert(ProtocolName && "Missing protocol identifier");
|
2009-04-24 07:18:26 +08:00
|
|
|
ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName);
|
2007-12-12 15:09:47 +08:00
|
|
|
if (PDecl) {
|
|
|
|
// Protocol already seen. Better be a forward protocol declaration
|
2008-03-16 09:25:17 +08:00
|
|
|
if (!PDecl->isForwardDecl()) {
|
2009-04-07 07:43:32 +08:00
|
|
|
Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
|
2008-11-24 06:46:27 +08:00
|
|
|
Diag(PDecl->getLocation(), diag::note_previous_definition);
|
2008-03-16 09:25:17 +08:00
|
|
|
// Just return the protocol we already had.
|
|
|
|
// FIXME: don't leak the objects passed in!
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(PDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-03-05 23:22:01 +08:00
|
|
|
ObjCList<ObjCProtocolDecl> PList;
|
2009-09-09 23:08:12 +08:00
|
|
|
PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
|
2009-03-05 23:22:01 +08:00
|
|
|
CheckForwardProtocolDeclarationForCircularDependency(
|
|
|
|
ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
|
|
|
|
PList.Destroy(Context);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-08-14 00:39:22 +08:00
|
|
|
// Make sure the cached decl gets a valid start location.
|
|
|
|
PDecl->setLocation(AtProtoInterfaceLoc);
|
2008-03-16 09:25:17 +08:00
|
|
|
PDecl->setForwardDecl(false);
|
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
PDecl = ObjCProtocolDecl::Create(Context, CurContext,
|
2009-01-09 08:49:46 +08:00
|
|
|
AtProtoInterfaceLoc,ProtocolName);
|
2009-04-24 07:18:26 +08:00
|
|
|
PushOnScopeChains(PDecl, TUScope);
|
2008-03-17 04:19:15 +08:00
|
|
|
PDecl->setForwardDecl(false);
|
2008-03-16 09:23:04 +08:00
|
|
|
}
|
2008-12-17 09:07:27 +08:00
|
|
|
if (AttrList)
|
2009-06-18 05:51:59 +08:00
|
|
|
ProcessDeclAttributeList(TUScope, PDecl, AttrList);
|
2007-12-12 15:09:47 +08:00
|
|
|
if (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);
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(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,
|
2009-03-29 03:18:32 +08:00
|
|
|
llvm::SmallVectorImpl<DeclPtrTy> &Protocols) {
|
2007-12-12 15:09:47 +08:00
|
|
|
for (unsigned i = 0; i != NumProtocols; ++i) {
|
2009-04-24 07:18:26 +08:00
|
|
|
ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first);
|
2010-01-04 02:01:57 +08:00
|
|
|
if (!PDecl) {
|
|
|
|
LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
|
|
|
|
LookupObjCProtocolName);
|
|
|
|
if (CorrectTypo(R, TUScope, 0) &&
|
|
|
|
(PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
|
|
|
|
Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
|
|
|
|
<< ProtocolId[i].first << R.getLookupName();
|
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;
|
2009-03-29 03:18:32 +08:00
|
|
|
Protocols.push_back(DeclPtrTy::make(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;
|
2009-03-29 03:18:32 +08:00
|
|
|
Action::DeclPtrTy
|
2007-12-12 15:09:47 +08:00
|
|
|
Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
|
2008-07-22 06:17:28 +08:00
|
|
|
const IdentifierLocPair *IdentList,
|
2008-12-17 09:07:27 +08:00
|
|
|
unsigned NumElts,
|
|
|
|
AttributeList *attrList) {
|
2008-01-08 03:49:32 +08:00
|
|
|
llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
|
2010-01-16 23:02:53 +08:00
|
|
|
llvm::SmallVector<SourceLocation, 8> ProtoLocs;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
2008-07-22 06:17:28 +08:00
|
|
|
IdentifierInfo *Ident = IdentList[i].first;
|
2009-04-24 07:18:26 +08:00
|
|
|
ObjCProtocolDecl *PDecl = LookupProtocol(Ident);
|
2009-01-09 08:49:46 +08:00
|
|
|
if (PDecl == 0) { // Not already seen?
|
2009-09-09 23:08:12 +08:00
|
|
|
PDecl = ObjCProtocolDecl::Create(Context, CurContext,
|
2009-01-09 08:49:46 +08:00
|
|
|
IdentList[i].second, Ident);
|
2009-04-24 07:18:26 +08:00
|
|
|
PushOnScopeChains(PDecl, TUScope);
|
2009-01-09 08:49:46 +08:00
|
|
|
}
|
2008-12-17 09:07:27 +08:00
|
|
|
if (attrList)
|
2009-06-18 05:51:59 +08:00
|
|
|
ProcessDeclAttributeList(TUScope, PDecl, attrList);
|
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);
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(PDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2009-03-29 03:18:32 +08:00
|
|
|
Sema::DeclPtrTy Sema::
|
2008-07-22 06:17:28 +08:00
|
|
|
ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
|
|
|
|
IdentifierInfo *ClassName, SourceLocation ClassLoc,
|
|
|
|
IdentifierInfo *CategoryName,
|
|
|
|
SourceLocation CategoryLoc,
|
2009-03-29 03:18:32 +08:00
|
|
|
const DeclPtrTy *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-02-16 05:55:26 +08:00
|
|
|
ObjCCategoryDecl *CDecl = 0;
|
2010-01-04 02:01:57 +08:00
|
|
|
ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
|
2010-02-24 03:39:46 +08:00
|
|
|
|
|
|
|
/// Check that class of this category is already completely declared.
|
|
|
|
if (!IDecl || IDecl->isForwardDecl()) {
|
|
|
|
// Create an invalid ObjCCategoryDecl to serve as context for
|
|
|
|
// the enclosing method declarations. We mark the decl invalid
|
|
|
|
// to make it clear that this isn't a valid AST.
|
|
|
|
CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
|
|
|
|
ClassLoc, CategoryLoc, CategoryName);
|
|
|
|
CDecl->setInvalidDecl();
|
|
|
|
Diag(ClassLoc, diag::err_undef_interface) << ClassName;
|
|
|
|
return DeclPtrTy::make(CDecl);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CategoryName) {
|
2010-02-16 05:55:26 +08:00
|
|
|
// Class extensions require a special treatment. Use an existing one.
|
2010-02-24 03:39:46 +08:00
|
|
|
// Note that 'getClassExtension()' can return NULL.
|
2010-02-23 09:26:30 +08:00
|
|
|
CDecl = IDecl->getClassExtension();
|
2010-02-24 03:39:46 +08:00
|
|
|
}
|
|
|
|
|
2010-02-16 05:55:26 +08:00
|
|
|
if (!CDecl) {
|
2010-02-24 03:39:46 +08:00
|
|
|
CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
|
|
|
|
ClassLoc, CategoryLoc, CategoryName);
|
2010-02-16 05:55:26 +08:00
|
|
|
// FIXME: PushOnScopeChains?
|
|
|
|
CurContext->addDecl(CDecl);
|
|
|
|
|
|
|
|
CDecl->setClassInterface(IDecl);
|
|
|
|
// Insert first use of class extension to the list of class's categories.
|
|
|
|
if (!CategoryName)
|
|
|
|
CDecl->insertNextClassCategory();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-17 05:30:01 +08:00
|
|
|
// If the interface is deprecated, warn about it.
|
2009-02-19 05:56:37 +08:00
|
|
|
(void)DiagnoseUseOfDecl(IDecl, ClassLoc);
|
2009-02-17 05:26:43 +08:00
|
|
|
|
2010-02-16 05:55:26 +08:00
|
|
|
if (CategoryName) {
|
|
|
|
/// Check for duplicate interface declaration for this category
|
|
|
|
ObjCCategoryDecl *CDeclChain;
|
|
|
|
for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
|
|
|
|
CDeclChain = CDeclChain->getNextClassCategory()) {
|
|
|
|
if (CDeclChain->getIdentifier() == CategoryName) {
|
|
|
|
// Class extensions can be declared multiple times.
|
|
|
|
Diag(CategoryLoc, diag::warn_dup_category_def)
|
|
|
|
<< ClassName << CategoryName;
|
|
|
|
Diag(CDeclChain->getLocation(), diag::note_previous_definition);
|
|
|
|
break;
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2010-02-16 05:55:26 +08:00
|
|
|
if (!CDeclChain)
|
|
|
|
CDecl->insertNextClassCategory();
|
2008-01-18 04:33:24 +08:00
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
|
|
|
|
if (NumProtoRefs) {
|
2009-10-06 05:32:49 +08:00
|
|
|
CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
|
2010-01-16 23:02:53 +08:00
|
|
|
ProtoLocs, Context);
|
2009-10-06 04:41:32 +08:00
|
|
|
// Protocols in the class extension belong to the class.
|
2010-02-16 05:55:26 +08:00
|
|
|
if (CDecl->IsClassExtension())
|
2009-10-06 04:41:32 +08:00
|
|
|
IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
|
2010-01-16 23:02:53 +08:00
|
|
|
NumProtoRefs, ProtoLocs,
|
|
|
|
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);
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(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.
|
2009-03-29 03:18:32 +08:00
|
|
|
Sema::DeclPtrTy Sema::ActOnStartCategoryImplementation(
|
2007-12-12 15:09:47 +08:00
|
|
|
SourceLocation AtCatImplLoc,
|
|
|
|
IdentifierInfo *ClassName, SourceLocation ClassLoc,
|
|
|
|
IdentifierInfo *CatName, SourceLocation CatLoc) {
|
2010-01-04 02:01:57 +08:00
|
|
|
ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc);
|
2009-07-21 08:05:53 +08:00
|
|
|
ObjCCategoryDecl *CatIDecl = 0;
|
|
|
|
if (IDecl) {
|
|
|
|
CatIDecl = IDecl->FindCategoryDeclaration(CatName);
|
|
|
|
if (!CatIDecl) {
|
|
|
|
// Category @implementation with no corresponding @interface.
|
|
|
|
// Create and install one.
|
|
|
|
CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
|
2010-01-17 00:38:58 +08:00
|
|
|
SourceLocation(), SourceLocation(),
|
2009-07-21 08:05:53 +08:00
|
|
|
CatName);
|
|
|
|
CatIDecl->setClassInterface(IDecl);
|
|
|
|
CatIDecl->insertNextClassCategory();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCCategoryImplDecl *CDecl =
|
2009-01-09 08:49:46 +08:00
|
|
|
ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
|
|
|
|
IDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
/// Check that class of this category is already completely declared.
|
|
|
|
if (!IDecl || IDecl->isForwardDecl())
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(ClassLoc, diag::err_undef_interface) << ClassName;
|
2007-12-12 15:09:47 +08:00
|
|
|
|
2009-01-09 08:49:46 +08:00
|
|
|
// FIXME: PushOnScopeChains?
|
2009-06-30 10:36:12 +08:00
|
|
|
CurContext->addDecl(CDecl);
|
2009-01-09 08:49:46 +08:00
|
|
|
|
2009-07-21 08:05:53 +08:00
|
|
|
/// Check that CatName, category name, is not used in another implementation.
|
|
|
|
if (CatIDecl) {
|
|
|
|
if (CatIDecl->getImplementation()) {
|
|
|
|
Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
|
|
|
|
<< CatName;
|
|
|
|
Diag(CatIDecl->getImplementation()->getLocation(),
|
|
|
|
diag::note_previous_definition);
|
|
|
|
} else
|
|
|
|
CatIDecl->setImplementation(CDecl);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
CheckObjCDeclScope(CDecl);
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(CDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2009-03-29 03:18:32 +08:00
|
|
|
Sema::DeclPtrTy 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) {
|
2008-01-08 03:49:32 +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
|
|
|
|
= LookupSingleName(TUScope, ClassName, LookupOrdinaryName);
|
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-01-05 01:27:12 +08:00
|
|
|
} else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
|
|
|
|
// If this is a forward declaration of an interface, warn.
|
|
|
|
if (IDecl->isForwardDecl()) {
|
2008-11-19 16:23:25 +08:00
|
|
|
Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
|
2009-04-24 05:49:04 +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.
|
|
|
|
LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
|
|
|
|
if (CorrectTypo(R, TUScope, 0) &&
|
|
|
|
(IDecl = R.getAsSingle<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
|
|
|
|
// 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.
|
|
|
|
Diag(ClassLoc, diag::warn_undef_interface_suggest)
|
|
|
|
<< ClassName << R.getLookupName();
|
2010-01-07 07:44:25 +08:00
|
|
|
Diag(IDecl->getLocation(), diag::note_previous_decl)
|
|
|
|
<< R.getLookupName()
|
2010-04-01 01:25:35 +08:00
|
|
|
<< CodeModificationHint::CreateReplacement(ClassLoc,
|
|
|
|
R.getLookupName().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.
|
2009-10-10 05:13:30 +08:00
|
|
|
PrevDecl = LookupSingleName(TUScope, SuperClassname, 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,
|
2009-01-09 08:49:46 +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);
|
2009-04-22 05:41:56 +08:00
|
|
|
} else {
|
|
|
|
// Mark the interface as being completed, even if it was just as
|
|
|
|
// @class ....;
|
|
|
|
// declaration; the user cannot reopen it.
|
|
|
|
IDecl->setForwardDecl(false);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ObjCImplementationDecl* IMPDecl =
|
|
|
|
ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
|
2009-01-20 09:17:11 +08:00
|
|
|
IDecl, SDecl);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
if (CheckObjCDeclScope(IMPDecl))
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(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.
|
2009-07-21 08:06:04 +08:00
|
|
|
if (IDecl->getImplementation()) {
|
2008-03-17 04:53:07 +08:00
|
|
|
// 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);
|
|
|
|
} 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);
|
2009-07-21 08:05:53 +08:00
|
|
|
}
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(IMPDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
|
|
|
|
ObjCIvarDecl **ivars, unsigned numIvars,
|
2007-12-12 15:09:47 +08:00
|
|
|
SourceLocation RBrace) {
|
|
|
|
assert(ImpDecl && "missing implementation decl");
|
2009-01-20 09:17:11 +08:00
|
|
|
ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
|
2007-12-12 15:09:47 +08:00
|
|
|
if (!IDecl)
|
|
|
|
return;
|
|
|
|
/// Check case of non-existing @interface decl.
|
|
|
|
/// (legacy objective-c @implementation decl without an @interface decl).
|
|
|
|
/// Add implementations's ivar to the synthesize class's ivar list.
|
2009-04-21 04:09:33 +08:00
|
|
|
if (IDecl->isImplicitInterfaceDecl()) {
|
2009-02-21 05:35:13 +08:00
|
|
|
IDecl->setLocEnd(RBrace);
|
2010-02-18 01:00:07 +08:00
|
|
|
// Add ivar's to class's DeclContext.
|
|
|
|
for (unsigned i = 0, e = numIvars; i != e; ++i) {
|
2010-02-18 02:10:54 +08:00
|
|
|
ivars[i]->setLexicalDeclContext(ImpDecl);
|
|
|
|
IDecl->makeDeclVisibleInContext(ivars[i], false);
|
2010-02-19 08:31:17 +08:00
|
|
|
ImpDecl->addDecl(ivars[i]);
|
2010-02-18 01:00:07 +08:00
|
|
|
}
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// If implementation has empty ivar list, just return.
|
|
|
|
if (numIvars == 0)
|
|
|
|
return;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
assert(ivars && "missing @implementation ivars");
|
2010-02-20 04:58:54 +08:00
|
|
|
if (LangOpts.ObjCNonFragileABI2) {
|
|
|
|
if (ImpDecl->getSuperClass())
|
|
|
|
Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
|
|
|
|
for (unsigned i = 0; i < numIvars; i++) {
|
|
|
|
ObjCIvarDecl* ImplIvar = ivars[i];
|
|
|
|
if (const ObjCIvarDecl *ClsIvar =
|
|
|
|
IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
|
|
|
|
Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
|
|
|
|
Diag(ClsIvar->getLocation(), diag::note_previous_definition);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Instance ivar to Implementation's DeclContext.
|
|
|
|
ImplIvar->setLexicalDeclContext(ImpDecl);
|
|
|
|
IDecl->makeDeclVisibleInContext(ImplIvar, false);
|
|
|
|
ImpDecl->addDecl(ImplIvar);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
// Check interface's Ivar list against those in the implementation.
|
|
|
|
// names and types must match.
|
|
|
|
//
|
|
|
|
unsigned j = 0;
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCInterfaceDecl::ivar_iterator
|
2007-12-13 01:58:05 +08:00
|
|
|
IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
|
|
|
|
for (; numIvars > 0 && IVI != IVE; ++IVI) {
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCIvarDecl* ImplIvar = ivars[j++];
|
|
|
|
ObjCIvarDecl* ClsIvar = *IVI;
|
2007-12-12 15:09:47 +08:00
|
|
|
assert (ImplIvar && "missing implementation ivar");
|
|
|
|
assert (ClsIvar && "missing class ivar");
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-03 22:49:36 +08:00
|
|
|
// First, make sure the types match.
|
2008-07-27 08:05:05 +08:00
|
|
|
if (Context.getCanonicalType(ImplIvar->getType()) !=
|
|
|
|
Context.getCanonicalType(ClsIvar->getType())) {
|
2008-11-19 13:08:23 +08:00
|
|
|
Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
|
2008-11-24 05:45:46 +08:00
|
|
|
<< ImplIvar->getIdentifier()
|
|
|
|
<< ImplIvar->getType() << ClsIvar->getType();
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(ClsIvar->getLocation(), diag::note_previous_definition);
|
2009-03-03 22:49:36 +08:00
|
|
|
} else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
|
|
|
|
Expr *ImplBitWidth = ImplIvar->getBitWidth();
|
|
|
|
Expr *ClsBitWidth = ClsIvar->getBitWidth();
|
2009-04-27 03:19:15 +08:00
|
|
|
if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
|
|
|
|
ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
|
2009-03-03 22:49:36 +08:00
|
|
|
Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
|
|
|
|
<< ImplIvar->getIdentifier();
|
|
|
|
Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-03-03 22:49:36 +08:00
|
|
|
// Make sure the names are identical.
|
|
|
|
if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
|
2008-11-19 13:08:23 +08:00
|
|
|
Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
|
2008-11-24 05:45:46 +08:00
|
|
|
<< ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
|
2008-11-24 07:12:31 +08:00
|
|
|
Diag(ClsIvar->getLocation(), diag::note_previous_definition);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
--numIvars;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-13 02:11:49 +08:00
|
|
|
if (numIvars > 0)
|
2007-12-13 02:19:52 +08:00
|
|
|
Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
|
2007-12-13 02:11:49 +08:00
|
|
|
else if (IVI != IVE)
|
2007-12-13 02:19:52 +08:00
|
|
|
Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2008-02-11 05:38:56 +08:00
|
|
|
void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
|
|
|
|
bool &IncompleteImpl) {
|
|
|
|
if (!IncompleteImpl) {
|
|
|
|
Diag(ImpLoc, diag::warn_incomplete_impl);
|
|
|
|
IncompleteImpl = true;
|
|
|
|
}
|
2010-03-28 03:02:17 +08:00
|
|
|
Diag(method->getLocation(), diag::note_undef_method_impl)
|
|
|
|
<< method->getDeclName();
|
2008-02-11 05:38:56 +08:00
|
|
|
}
|
|
|
|
|
2008-12-06 02:18:52 +08:00
|
|
|
void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
|
|
|
|
ObjCMethodDecl *IntfMethodDecl) {
|
2009-04-12 02:01:59 +08:00
|
|
|
if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
|
2009-05-15 07:52:54 +08:00
|
|
|
ImpMethodDecl->getResultType()) &&
|
2009-07-23 09:01:38 +08:00
|
|
|
!Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
|
|
|
|
ImpMethodDecl->getResultType())) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
|
2009-04-12 03:58:42 +08:00
|
|
|
<< ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
|
|
|
|
<< ImpMethodDecl->getResultType();
|
2008-12-06 02:18:52 +08:00
|
|
|
Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 03:58:42 +08:00
|
|
|
for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
|
|
|
|
IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
|
|
|
|
IM != EM; ++IM, ++IF) {
|
2009-11-19 02:56:09 +08:00
|
|
|
QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
|
|
|
|
QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
|
|
|
|
if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
|
|
|
|
Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
|
2009-04-12 03:58:42 +08:00
|
|
|
continue;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
|
2009-04-12 03:58:42 +08:00
|
|
|
<< ImpMethodDecl->getDeclName() << (*IF)->getType()
|
|
|
|
<< (*IM)->getType();
|
2009-04-12 04:14:49 +08:00
|
|
|
Diag((*IF)->getLocation(), diag::note_previous_definition);
|
2009-04-12 03:58:42 +08:00
|
|
|
}
|
2008-12-06 02:18:52 +08:00
|
|
|
}
|
|
|
|
|
2009-05-16 15:39:55 +08:00
|
|
|
/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
|
|
|
|
/// improve the efficiency of selector lookups and type checking by associating
|
|
|
|
/// with each protocol / interface / category the flattened instance tables. If
|
|
|
|
/// we used an immutable set to keep the table then it wouldn't add significant
|
|
|
|
/// memory cost and it would be handy for lookups.
|
2008-08-27 13:40:03 +08:00
|
|
|
|
2008-02-09 06:06:17 +08:00
|
|
|
/// CheckProtocolMethodDefs - This routine checks unimplemented methods
|
2007-12-12 15:09:47 +08:00
|
|
|
/// Declared in protocol, and those referenced by it.
|
2008-02-09 06:06:17 +08:00
|
|
|
void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
|
|
|
|
ObjCProtocolDecl *PDecl,
|
2007-12-12 15:09:47 +08:00
|
|
|
bool& IncompleteImpl,
|
2008-02-09 06:06:17 +08:00
|
|
|
const llvm::DenseSet<Selector> &InsMap,
|
2008-09-05 04:01:15 +08:00
|
|
|
const llvm::DenseSet<Selector> &ClsMap,
|
2010-03-28 05:10:05 +08:00
|
|
|
ObjCContainerDecl *CDecl) {
|
|
|
|
ObjCInterfaceDecl *IDecl;
|
|
|
|
if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
|
|
|
|
IDecl = C->getClassInterface();
|
|
|
|
else
|
|
|
|
IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
|
|
|
|
assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
|
|
|
|
|
2008-09-05 04:01:15 +08:00
|
|
|
ObjCInterfaceDecl *Super = IDecl->getSuperClass();
|
2009-05-23 01:12:32 +08:00
|
|
|
ObjCInterfaceDecl *NSIDecl = 0;
|
|
|
|
if (getLangOptions().NeXTRuntime) {
|
2009-09-09 23:08:12 +08:00
|
|
|
// check to see if class implements forwardInvocation method and objects
|
|
|
|
// of this class are derived from 'NSProxy' so that to forward requests
|
2009-05-23 01:12:32 +08:00
|
|
|
// from one object to another.
|
2009-09-09 23:08:12 +08:00
|
|
|
// Under such conditions, which means that every method possible is
|
|
|
|
// implemented in the class, we should not issue "Method definition not
|
2009-05-23 01:12:32 +08:00
|
|
|
// found" warnings.
|
|
|
|
// FIXME: Use a general GetUnarySelector method for this.
|
|
|
|
IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
|
|
|
|
Selector fISelector = Context.Selectors.getSelector(1, &II);
|
|
|
|
if (InsMap.count(fISelector))
|
|
|
|
// Is IDecl derived from 'NSProxy'? If so, no instance methods
|
|
|
|
// need be implemented in the implementation.
|
|
|
|
NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-05 04:01:15 +08:00
|
|
|
// If a method lookup fails locally we still need to look and see if
|
|
|
|
// the method was implemented by a base class or an inherited
|
|
|
|
// protocol. This lookup is slow, but occurs rarely in correct code
|
|
|
|
// and otherwise would terminate in a warning.
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// check unimplemented instance methods.
|
2009-05-23 01:12:32 +08:00
|
|
|
if (!NSIDecl)
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
|
2009-06-30 10:36:12 +08:00
|
|
|
E = PDecl->instmeth_end(); I != E; ++I) {
|
2009-05-23 01:12:32 +08:00
|
|
|
ObjCMethodDecl *method = *I;
|
2009-09-09 23:08:12 +08:00
|
|
|
if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
|
2009-05-23 01:12:32 +08:00
|
|
|
!method->isSynthesized() && !InsMap.count(method->getSelector()) &&
|
2009-09-09 23:08:12 +08:00
|
|
|
(!Super ||
|
2009-06-30 10:36:12 +08:00
|
|
|
!Super->lookupInstanceMethod(method->getSelector()))) {
|
2009-05-23 01:12:32 +08:00
|
|
|
// Ugly, but necessary. Method declared in protcol might have
|
|
|
|
// have been synthesized due to a property declared in the class which
|
|
|
|
// uses the protocol.
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCMethodDecl *MethodInClass =
|
2009-06-30 10:36:12 +08:00
|
|
|
IDecl->lookupInstanceMethod(method->getSelector());
|
2010-03-28 03:02:17 +08:00
|
|
|
if (!MethodInClass || !MethodInClass->isSynthesized()) {
|
2009-05-23 01:12:32 +08:00
|
|
|
WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
|
2010-03-28 05:10:05 +08:00
|
|
|
Diag(CDecl->getLocation(), diag::note_required_for_protocol_at) <<
|
2010-03-28 03:02:17 +08:00
|
|
|
PDecl->getDeclName();
|
|
|
|
}
|
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()))) {
|
2008-02-11 05:38:56 +08:00
|
|
|
WarnUndefinedMethod(ImpLoc, method, IncompleteImpl);
|
2010-03-28 03:02:17 +08:00
|
|
|
Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
|
|
|
|
PDecl->getDeclName();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2009-05-02 04:07:12 +08:00
|
|
|
/// MatchAllMethodDeclarations - Check methods declaraed in interface or
|
|
|
|
/// or protocol against those declared in their implementations.
|
|
|
|
///
|
|
|
|
void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
|
|
|
|
const llvm::DenseSet<Selector> &ClsMap,
|
|
|
|
llvm::DenseSet<Selector> &InsMapSeen,
|
|
|
|
llvm::DenseSet<Selector> &ClsMapSeen,
|
|
|
|
ObjCImplDecl* IMPDecl,
|
|
|
|
ObjCContainerDecl* CDecl,
|
|
|
|
bool &IncompleteImpl,
|
2009-09-09 23:08:12 +08:00
|
|
|
bool ImmediateClass) {
|
2009-05-02 04:07:12 +08:00
|
|
|
// Check and see if instance methods in class interface have been
|
|
|
|
// implemented in the implementation class. If so, their types match.
|
2009-06-30 10:36:12 +08:00
|
|
|
for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
|
|
|
|
E = CDecl->instmeth_end(); I != E; ++I) {
|
2009-05-02 04:07:12 +08:00
|
|
|
if (InsMapSeen.count((*I)->getSelector()))
|
|
|
|
continue;
|
|
|
|
InsMapSeen.insert((*I)->getSelector());
|
2009-09-09 23:08:12 +08:00
|
|
|
if (!(*I)->isSynthesized() &&
|
2009-05-02 04:07:12 +08:00
|
|
|
!InsMap.count((*I)->getSelector())) {
|
|
|
|
if (ImmediateClass)
|
|
|
|
WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
|
|
|
|
continue;
|
2009-08-05 05:02:39 +08:00
|
|
|
} else {
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCMethodDecl *ImpMethodDecl =
|
2009-06-30 10:36:12 +08:00
|
|
|
IMPDecl->getInstanceMethod((*I)->getSelector());
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCMethodDecl *IntfMethodDecl =
|
2009-06-30 10:36:12 +08:00
|
|
|
CDecl->getInstanceMethod((*I)->getSelector());
|
2009-09-09 23:08:12 +08:00
|
|
|
assert(IntfMethodDecl &&
|
2009-05-02 04:07:12 +08:00
|
|
|
"IntfMethodDecl is null in ImplMethodsVsClassMethods");
|
|
|
|
// ImpMethodDecl may be null as in a @dynamic property.
|
|
|
|
if (ImpMethodDecl)
|
|
|
|
WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl);
|
2009-08-05 05:02:39 +08:00
|
|
|
} else {
|
2009-06-30 10:36:12 +08:00
|
|
|
ObjCMethodDecl *ImpMethodDecl =
|
|
|
|
IMPDecl->getClassMethod((*I)->getSelector());
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCMethodDecl *IntfMethodDecl =
|
2009-06-30 10:36:12 +08:00
|
|
|
CDecl->getClassMethod((*I)->getSelector());
|
2009-05-02 04:07:12 +08:00
|
|
|
WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
|
|
|
|
// Check for any implementation of a methods declared in protocol.
|
|
|
|
for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
|
|
|
|
E = I->protocol_end(); PI != E; ++PI)
|
2009-09-09 23:08:12 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
|
|
|
IMPDecl,
|
2009-05-02 04:07:12 +08:00
|
|
|
(*PI), IncompleteImpl, false);
|
|
|
|
if (I->getSuperClass())
|
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
2009-09-09 23:08:12 +08:00
|
|
|
IMPDecl,
|
2009-05-02 04:07:12 +08:00
|
|
|
I->getSuperClass(), IncompleteImpl, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
void Sema::ImplMethodsVsClassMethods(ObjCImplDecl* IMPDecl,
|
|
|
|
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.
|
|
|
|
if (isa<ObjCInterfaceDecl>(CDecl))
|
2010-01-20 09:51:55 +08:00
|
|
|
DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
|
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
llvm::DenseSet<Selector> ClsMap;
|
2009-09-09 23:08:12 +08:00
|
|
|
for (ObjCImplementationDecl::classmeth_iterator
|
2009-06-30 10:36:12 +08:00
|
|
|
I = IMPDecl->classmeth_begin(),
|
|
|
|
E = IMPDecl->classmeth_end(); I != E; ++I)
|
2007-12-13 01:58:05 +08:00
|
|
|
ClsMap.insert((*I)->getSelector());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-05-02 04:07:12 +08:00
|
|
|
// Check for type conflict of methods declared in a class/protocol and
|
|
|
|
// its implementation; if any.
|
|
|
|
llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
|
2009-09-09 23:08:12 +08:00
|
|
|
MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
|
|
|
|
IMPDecl, CDecl,
|
2009-05-02 04:07:12 +08:00
|
|
|
IncompleteImpl, true);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
// Check the protocol list for unimplemented methods in the @implementation
|
|
|
|
// class.
|
2009-05-02 04:07:12 +08:00
|
|
|
// Check and see if class methods in class interface have been
|
|
|
|
// implemented in the implementation class.
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-01 08:56:52 +08:00
|
|
|
if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
|
2009-05-02 04:07:12 +08:00
|
|
|
for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
|
2009-03-01 08:56:52 +08:00
|
|
|
E = I->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)
|
|
|
|
for (ObjCCategoryDecl *Categories = I->getCategoryList();
|
|
|
|
Categories; Categories = Categories->getNextClassCategory()) {
|
2010-02-16 05:55:26 +08:00
|
|
|
if (Categories->IsClassExtension()) {
|
2009-03-01 08:56:52 +08:00
|
|
|
ImplMethodsVsClassMethods(IMPDecl, Categories, IncompleteImpl);
|
|
|
|
break;
|
|
|
|
}
|
2008-12-06 02:18:52 +08:00
|
|
|
}
|
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());
|
|
|
|
}
|
|
|
|
DiagnoseUnimplementedProperties(IMPDecl, CDecl, InsMap);
|
|
|
|
}
|
2009-03-01 08:56:52 +08:00
|
|
|
} else
|
|
|
|
assert(false && "invalid ObjCContainerDecl type.");
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
/// ActOnForwardClassDeclaration -
|
2009-03-29 03:18:32 +08:00
|
|
|
Action::DeclPtrTy
|
2007-12-12 15:09:47 +08:00
|
|
|
Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
|
2009-02-17 03:25:52 +08:00
|
|
|
IdentifierInfo **IdentList,
|
2009-11-18 07:12:20 +08:00
|
|
|
SourceLocation *IdentLocs,
|
2009-02-17 03:25:52 +08:00
|
|
|
unsigned NumElts) {
|
2008-01-08 03:49:32 +08:00
|
|
|
llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
for (unsigned i = 0; i != NumElts; ++i) {
|
|
|
|
// Check for another declaration kind with the same name.
|
2009-10-10 05:13:30 +08:00
|
|
|
NamedDecl *PrevDecl
|
|
|
|
= LookupSingleName(TUScope, IdentList[i], LookupOrdinaryName);
|
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?
|
2008-06-06 06:57:10 +08:00
|
|
|
TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
|
|
|
|
if (!TDD || !isa<ObjCInterfaceType>(TDD->getUnderlyingType())) {
|
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);
|
2009-08-05 05:02:39 +08:00
|
|
|
} else if (TDD) {
|
|
|
|
// a forward class declaration matching a typedef name of a class refers
|
|
|
|
// to the underlying class.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (ObjCInterfaceType * OI =
|
2009-05-08 05:49:26 +08:00
|
|
|
dyn_cast<ObjCInterfaceType>(TDD->getUnderlyingType()))
|
|
|
|
PrevDecl = OI->getDecl();
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
|
2007-12-12 15:09:47 +08:00
|
|
|
if (!IDecl) { // Not already seen? Make a forward decl.
|
2009-09-09 23:08:12 +08:00
|
|
|
IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
|
2009-11-18 08:28:11 +08:00
|
|
|
IdentList[i], IdentLocs[i], true);
|
2009-11-18 06:58:30 +08:00
|
|
|
|
|
|
|
// Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
|
|
|
|
// the current DeclContext. This prevents clients that walk DeclContext
|
|
|
|
// from seeing the imaginary ObjCInterfaceDecl until it is actually
|
|
|
|
// declared later (if at all). We also take care to explicitly make
|
|
|
|
// sure this declaration is visible for name lookup.
|
|
|
|
PushOnScopeChains(IDecl, TUScope, false);
|
|
|
|
CurContext->makeDeclVisibleInContext(IDecl, true);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Interfaces.push_back(IDecl);
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-18 08:28:11 +08:00
|
|
|
assert(Interfaces.size() == NumElts);
|
2009-01-09 08:49:46 +08:00
|
|
|
ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
|
2009-11-18 08:28:11 +08:00
|
|
|
Interfaces.data(), IdentLocs,
|
2008-11-05 00:57:32 +08:00
|
|
|
Interfaces.size());
|
2009-06-30 10:36:12 +08:00
|
|
|
CurContext->addDecl(CDecl);
|
2008-11-05 00:57:32 +08:00
|
|
|
CheckObjCDeclScope(CDecl);
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(CDecl);
|
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
|
2009-09-09 23:08:12 +08:00
|
|
|
bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
|
2008-10-21 18:37:50 +08:00
|
|
|
const ObjCMethodDecl *PrevMethod,
|
|
|
|
bool matchBasedOnSizeAndAlignment) {
|
|
|
|
QualType T1 = Context.getCanonicalType(Method->getResultType());
|
|
|
|
QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-10-21 18:37:50 +08:00
|
|
|
if (T1 != T2) {
|
|
|
|
// The result types are different.
|
|
|
|
if (!matchBasedOnSizeAndAlignment)
|
|
|
|
return false;
|
|
|
|
// Incomplete types don't have a size and alignment.
|
|
|
|
if (T1->isIncompleteType() || T2->isIncompleteType())
|
2007-12-12 15:09:47 +08:00
|
|
|
return false;
|
2008-10-21 18:37:50 +08:00
|
|
|
// Check is based on size and alignment.
|
|
|
|
if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-21 02:43:26 +08:00
|
|
|
ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
|
|
|
|
E = Method->param_end();
|
|
|
|
ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-21 02:43:26 +08:00
|
|
|
for (; ParamI != E; ++ParamI, ++PrevI) {
|
|
|
|
assert(PrevI != PrevMethod->param_end() && "Param mismatch");
|
|
|
|
T1 = Context.getCanonicalType((*ParamI)->getType());
|
|
|
|
T2 = Context.getCanonicalType((*PrevI)->getType());
|
2008-10-21 18:37:50 +08:00
|
|
|
if (T1 != T2) {
|
|
|
|
// The result types are different.
|
|
|
|
if (!matchBasedOnSizeAndAlignment)
|
|
|
|
return false;
|
|
|
|
// Incomplete types don't have a size and alignment.
|
|
|
|
if (T1->isIncompleteType() || T2->isIncompleteType())
|
|
|
|
return false;
|
|
|
|
// Check is based on size and alignment.
|
|
|
|
if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
|
|
|
|
return false;
|
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-25 05:10:55 +08:00
|
|
|
/// \brief Read the contents of the instance and factory method pools
|
|
|
|
/// for a given selector from external storage.
|
|
|
|
///
|
|
|
|
/// This routine should only be called once, when neither the instance
|
|
|
|
/// nor the factory method pool has an entry for this selector.
|
2009-09-09 23:08:12 +08:00
|
|
|
Sema::MethodPool::iterator Sema::ReadMethodPool(Selector Sel,
|
2009-04-25 05:10:55 +08:00
|
|
|
bool isInstance) {
|
|
|
|
assert(ExternalSource && "We need an external AST source");
|
|
|
|
assert(InstanceMethodPool.find(Sel) == InstanceMethodPool.end() &&
|
|
|
|
"Selector data already loaded into the instance method pool");
|
|
|
|
assert(FactoryMethodPool.find(Sel) == FactoryMethodPool.end() &&
|
|
|
|
"Selector data already loaded into the factory method pool");
|
|
|
|
|
|
|
|
// Read the method list from the external source.
|
|
|
|
std::pair<ObjCMethodList, ObjCMethodList> Methods
|
|
|
|
= ExternalSource->ReadMethodPool(Sel);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-25 05:10:55 +08:00
|
|
|
if (isInstance) {
|
|
|
|
if (Methods.second.Method)
|
|
|
|
FactoryMethodPool[Sel] = Methods.second;
|
|
|
|
return InstanceMethodPool.insert(std::make_pair(Sel, Methods.first)).first;
|
2009-09-09 23:08:12 +08:00
|
|
|
}
|
2009-04-25 05:10:55 +08:00
|
|
|
|
|
|
|
if (Methods.first.Method)
|
|
|
|
InstanceMethodPool[Sel] = Methods.first;
|
|
|
|
|
|
|
|
return FactoryMethodPool.insert(std::make_pair(Sel, Methods.second)).first;
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void Sema::AddInstanceMethodToGlobalPool(ObjCMethodDecl *Method) {
|
2009-04-25 05:10:55 +08:00
|
|
|
llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
|
|
|
|
= InstanceMethodPool.find(Method->getSelector());
|
|
|
|
if (Pos == InstanceMethodPool.end()) {
|
|
|
|
if (ExternalSource && !FactoryMethodPool.count(Method->getSelector()))
|
|
|
|
Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/true);
|
|
|
|
else
|
|
|
|
Pos = InstanceMethodPool.insert(std::make_pair(Method->getSelector(),
|
|
|
|
ObjCMethodList())).first;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCMethodList &Entry = Pos->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.
|
|
|
|
for (ObjCMethodList *List = &Entry; List; List = List->Next)
|
|
|
|
if (MatchTwoMethodDeclarations(Method, List->Method))
|
|
|
|
return;
|
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
|
|
|
}
|
|
|
|
|
2008-10-21 18:50:19 +08:00
|
|
|
// FIXME: Finish implementing -Wno-strict-selector-match.
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCMethodDecl *Sema::LookupInstanceMethodInGlobalPool(Selector Sel,
|
2009-08-23 05:13:55 +08:00
|
|
|
SourceRange R,
|
|
|
|
bool warn) {
|
2009-04-25 05:10:55 +08:00
|
|
|
llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
|
|
|
|
= InstanceMethodPool.find(Sel);
|
2009-04-25 06:23:41 +08:00
|
|
|
if (Pos == InstanceMethodPool.end()) {
|
|
|
|
if (ExternalSource && !FactoryMethodPool.count(Sel))
|
2009-04-25 05:10:55 +08:00
|
|
|
Pos = ReadMethodPool(Sel, /*isInstance=*/true);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCMethodList &MethList = Pos->second;
|
2008-10-21 18:37:50 +08:00
|
|
|
bool issueWarning = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-09-30 22:38:43 +08:00
|
|
|
if (MethList.Method && MethList.Next) {
|
2008-10-21 18:37:50 +08:00
|
|
|
for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
|
|
|
|
// This checks if the methods differ by size & alignment.
|
|
|
|
if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
|
2009-08-23 05:13:55 +08:00
|
|
|
issueWarning = warn;
|
2008-10-21 18:37:50 +08:00
|
|
|
}
|
|
|
|
if (issueWarning && (MethList.Method && MethList.Next)) {
|
2008-11-24 11:33:13 +08:00
|
|
|
Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
|
2009-12-11 03:51:03 +08:00
|
|
|
Diag(MethList.Method->getLocStart(), diag::note_using)
|
2008-11-19 13:27:50 +08:00
|
|
|
<< MethList.Method->getSourceRange();
|
2008-09-30 22:38:43 +08:00
|
|
|
for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
|
2009-12-11 03:51:03 +08:00
|
|
|
Diag(Next->Method->getLocStart(), diag::note_also_found)
|
2008-11-19 13:27:50 +08:00
|
|
|
<< Next->Method->getSourceRange();
|
2008-09-30 22:38:43 +08:00
|
|
|
}
|
|
|
|
return MethList.Method;
|
|
|
|
}
|
|
|
|
|
2008-01-08 03:49:32 +08:00
|
|
|
void Sema::AddFactoryMethodToGlobalPool(ObjCMethodDecl *Method) {
|
2009-04-25 05:10:55 +08:00
|
|
|
llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
|
|
|
|
= FactoryMethodPool.find(Method->getSelector());
|
|
|
|
if (Pos == FactoryMethodPool.end()) {
|
|
|
|
if (ExternalSource && !InstanceMethodPool.count(Method->getSelector()))
|
|
|
|
Pos = ReadMethodPool(Method->getSelector(), /*isInstance=*/false);
|
|
|
|
else
|
|
|
|
Pos = FactoryMethodPool.insert(std::make_pair(Method->getSelector(),
|
|
|
|
ObjCMethodList())).first;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCMethodList &FirstMethod = Pos->second;
|
2007-12-12 15:09:47 +08:00
|
|
|
if (!FirstMethod.Method) {
|
|
|
|
// Haven't seen a method with this selector name yet - add it.
|
|
|
|
FirstMethod.Method = Method;
|
|
|
|
FirstMethod.Next = 0;
|
|
|
|
} else {
|
|
|
|
// We've seen a method with this name, now check the type signature(s).
|
|
|
|
bool match = MatchTwoMethodDeclarations(Method, FirstMethod.Method);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
for (ObjCMethodList *Next = FirstMethod.Next; !match && Next;
|
2007-12-12 15:09:47 +08:00
|
|
|
Next = Next->Next)
|
|
|
|
match = MatchTwoMethodDeclarations(Method, Next->Method);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
if (!match) {
|
|
|
|
// 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>();
|
|
|
|
ObjCMethodList *OMI = new (Mem) ObjCMethodList(Method, FirstMethod.Next);
|
2007-12-12 15:09:47 +08:00
|
|
|
FirstMethod.Next = OMI;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
ObjCMethodDecl *Sema::LookupFactoryMethodInGlobalPool(Selector Sel,
|
2009-04-25 05:10:55 +08:00
|
|
|
SourceRange R) {
|
|
|
|
llvm::DenseMap<Selector, ObjCMethodList>::iterator Pos
|
|
|
|
= FactoryMethodPool.find(Sel);
|
|
|
|
if (Pos == FactoryMethodPool.end()) {
|
2009-09-09 23:08:12 +08:00
|
|
|
if (ExternalSource && !InstanceMethodPool.count(Sel))
|
2009-04-25 05:10:55 +08:00
|
|
|
Pos = ReadMethodPool(Sel, /*isInstance=*/false);
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ObjCMethodList &MethList = Pos->second;
|
|
|
|
bool issueWarning = false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-25 05:10:55 +08:00
|
|
|
if (MethList.Method && MethList.Next) {
|
|
|
|
for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
|
|
|
|
// This checks if the methods differ by size & alignment.
|
|
|
|
if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
|
|
|
|
issueWarning = true;
|
|
|
|
}
|
|
|
|
if (issueWarning && (MethList.Method && MethList.Next)) {
|
|
|
|
Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
|
2009-12-11 03:51:03 +08:00
|
|
|
Diag(MethList.Method->getLocStart(), diag::note_using)
|
2009-04-25 05:10:55 +08:00
|
|
|
<< MethList.Method->getSourceRange();
|
|
|
|
for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
|
2009-12-11 03:51:03 +08:00
|
|
|
Diag(Next->Method->getLocStart(), diag::note_also_found)
|
2009-04-25 05:10:55 +08:00
|
|
|
<< Next->Method->getSourceRange();
|
|
|
|
}
|
|
|
|
return MethList.Method;
|
|
|
|
}
|
|
|
|
|
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());
|
|
|
|
// If type of arguement of method in this class does not match its
|
|
|
|
// respective argument type in the super class method, issue warning;
|
|
|
|
if (!Context.typesAreCompatible(T1, T2)) {
|
2009-09-09 23:08:12 +08:00
|
|
|
Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
|
2009-08-05 01:01:09 +08:00
|
|
|
<< T1 << T2;
|
|
|
|
Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ID = SD;
|
|
|
|
}
|
2009-08-04 09:07:16 +08:00
|
|
|
}
|
|
|
|
|
2010-02-24 07:41:11 +08:00
|
|
|
/// DiagnoseDuplicateIvars -
|
|
|
|
/// Check for duplicate ivars in the entire class at the start of
|
|
|
|
/// @implementation. This becomes necesssary because class extension can
|
|
|
|
/// add ivars to a class in random order which will not be known until
|
|
|
|
/// class's @implementation is seen.
|
|
|
|
void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
|
|
|
|
ObjCInterfaceDecl *SID) {
|
|
|
|
for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
|
|
|
|
IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
|
|
|
|
ObjCIvarDecl* Ivar = (*IVI);
|
|
|
|
if (Ivar->isInvalidDecl())
|
|
|
|
continue;
|
|
|
|
if (IdentifierInfo *II = Ivar->getIdentifier()) {
|
|
|
|
ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
|
|
|
|
if (prevIvar) {
|
|
|
|
Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
|
|
|
|
Diag(prevIvar->getLocation(), diag::note_previous_declaration);
|
|
|
|
Ivar->setInvalidDecl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-18 09:30:32 +08:00
|
|
|
// Note: For class/category implemenations, allMethods/allProperties is
|
|
|
|
// always null.
|
2010-01-07 09:20:12 +08:00
|
|
|
void Sema::ActOnAtEnd(SourceRange AtEnd,
|
|
|
|
DeclPtrTy classDecl,
|
2009-03-29 03:18:32 +08:00
|
|
|
DeclPtrTy *allMethods, unsigned allNum,
|
|
|
|
DeclPtrTy *allProperties, unsigned pNum,
|
2009-03-30 00:50:03 +08:00
|
|
|
DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
|
2009-03-29 03:18:32 +08:00
|
|
|
Decl *ClassDecl = classDecl.getAs<Decl>();
|
2007-12-12 15:09:47 +08:00
|
|
|
|
2007-12-18 09:30:32 +08:00
|
|
|
// FIXME: If we don't have a ClassDecl, we have an error. We should consider
|
|
|
|
// always passing in a decl. If the decl has an error, isInvalidDecl()
|
2007-12-12 15:09:47 +08:00
|
|
|
// should be true.
|
|
|
|
if (!ClassDecl)
|
|
|
|
return;
|
2009-11-17 02:57:01 +08:00
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
bool isInterfaceDeclKind =
|
2008-03-17 05:17:37 +08:00
|
|
|
isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
|
|
|
|
|| isa<ObjCProtocolDecl>(ClassDecl);
|
2008-01-08 03:49:32 +08:00
|
|
|
bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
|
2009-01-09 23:36:25 +08:00
|
|
|
|
2010-01-07 09:20:12 +08:00
|
|
|
if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
|
|
|
|
// FIXME: This is wrong. We shouldn't be pretending that there is
|
|
|
|
// an '@end' in the declaration.
|
|
|
|
SourceLocation L = ClassDecl->getLocation();
|
|
|
|
AtEnd.setBegin(L);
|
|
|
|
AtEnd.setEnd(L);
|
|
|
|
Diag(L, diag::warn_missing_atend);
|
2009-11-17 02:57:01 +08:00
|
|
|
}
|
|
|
|
|
2009-01-09 01:28:14 +08:00
|
|
|
DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
|
|
|
|
|
|
|
|
// 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 =
|
2009-03-29 03:18:32 +08:00
|
|
|
cast_or_null<ObjCMethodDecl>(allMethods[i].getAs<Decl>());
|
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);
|
2007-12-12 15:09:47 +08:00
|
|
|
} else {
|
2009-06-30 10:36:12 +08:00
|
|
|
DC->addDecl(Method);
|
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);
|
2007-12-12 15:09:47 +08:00
|
|
|
} else {
|
2009-06-30 10:36:12 +08:00
|
|
|
DC->addDecl(Method);
|
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-01-19 02:41:16 +08:00
|
|
|
CompareProperties(I, DeclPtrTy::make(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
|
|
|
|
CompareProperties(C, DeclPtrTy::make(C));
|
2010-02-16 05:55:26 +08:00
|
|
|
if (C->IsClassExtension())
|
2009-03-03 03:05:07 +08:00
|
|
|
DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
|
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()) {
|
2007-12-12 15:09:47 +08:00
|
|
|
ImplMethodsVsClassMethods(IC, IDecl);
|
2009-11-12 06:40:11 +08:00
|
|
|
AtomicPropertySetterGetterRules(IC, IDecl);
|
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
|
|
|
}
|
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()) {
|
2009-03-01 08:56:52 +08:00
|
|
|
ImplMethodsVsClassMethods(CatImplClass, Categories);
|
2007-12-12 15:09:47 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-03-30 00:50:03 +08:00
|
|
|
if (isInterfaceDeclKind) {
|
|
|
|
// Reject invalid vardecls.
|
|
|
|
for (unsigned i = 0; i != tuvNum; i++) {
|
|
|
|
DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
|
|
|
|
for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
|
|
|
|
if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
|
2009-04-14 10:25:56 +08:00
|
|
|
if (!VDecl->hasExternalStorage())
|
2009-04-14 01:58:46 +08:00
|
|
|
Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
|
2009-03-22 02:06:45 +08:00
|
|
|
}
|
2009-03-30 00:50:03 +08:00
|
|
|
}
|
2009-03-19 06:33:24 +08:00
|
|
|
}
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
|
|
|
|
/// objective-c's type qualifier from the parser version of the same info.
|
2009-09-09 23:08:12 +08:00
|
|
|
static Decl::ObjCDeclQualifier
|
2008-01-08 03:49:32 +08:00
|
|
|
CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
|
|
|
|
Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
|
|
|
|
if (PQTVal & ObjCDeclSpec::DQ_In)
|
|
|
|
ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
|
|
|
|
if (PQTVal & ObjCDeclSpec::DQ_Inout)
|
|
|
|
ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
|
|
|
|
if (PQTVal & ObjCDeclSpec::DQ_Out)
|
|
|
|
ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
|
|
|
|
if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
|
|
|
|
ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
|
|
|
|
if (PQTVal & ObjCDeclSpec::DQ_Byref)
|
|
|
|
ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
|
|
|
|
if (PQTVal & ObjCDeclSpec::DQ_Oneway)
|
|
|
|
ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
|
2007-12-12 15:09:47 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-29 03:18:32 +08:00
|
|
|
Sema::DeclPtrTy Sema::ActOnMethodDeclaration(
|
2007-12-12 15:09:47 +08:00
|
|
|
SourceLocation MethodLoc, SourceLocation EndLoc,
|
2009-03-29 03:18:32 +08:00
|
|
|
tok::TokenKind MethodType, DeclPtrTy classDecl,
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCDeclSpec &ReturnQT, TypeTy *ReturnType,
|
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,
|
2009-01-09 08:38:19 +08:00
|
|
|
llvm::SmallVectorImpl<Declarator> &Cdecls,
|
2007-12-12 15:09:47 +08:00
|
|
|
AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
|
|
|
|
bool isVariadic) {
|
2009-03-29 03:18:32 +08:00
|
|
|
Decl *ClassDecl = classDecl.getAs<Decl>();
|
2008-03-01 05:48:07 +08:00
|
|
|
|
|
|
|
// Make sure we can establish a context for the method.
|
|
|
|
if (!ClassDecl) {
|
|
|
|
Diag(MethodLoc, diag::error_missing_method_context);
|
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
|
|
|
getLabelMap().clear();
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy();
|
2008-03-01 05:48:07 +08:00
|
|
|
}
|
2008-04-04 14:12:32 +08:00
|
|
|
QualType resultDeclType;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2010-03-08 22:59:44 +08:00
|
|
|
TypeSourceInfo *ResultTInfo = 0;
|
2009-02-21 06:59:16 +08:00
|
|
|
if (ReturnType) {
|
2010-03-08 22:59:44 +08:00
|
|
|
resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-02-21 06:59:16 +08:00
|
|
|
// Methods cannot return interface types. All ObjC objects are
|
|
|
|
// passed by reference.
|
|
|
|
if (resultDeclType->isObjCInterfaceType()) {
|
2009-04-12 03:08:56 +08:00
|
|
|
Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
|
|
|
|
<< 0 << resultDeclType;
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy();
|
2009-02-21 06:59:16 +08:00
|
|
|
}
|
|
|
|
} else // get the type for "id".
|
2008-04-04 14:12:32 +08:00
|
|
|
resultDeclType = Context.getObjCIdType();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
|
|
|
ObjCMethodDecl* ObjCMethod =
|
2008-04-04 14:12:32 +08:00
|
|
|
ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
|
2010-03-08 22:59:44 +08:00
|
|
|
ResultTInfo,
|
2009-09-09 23:08:12 +08:00
|
|
|
cast<DeclContext>(ClassDecl),
|
2008-04-04 14:12:32 +08:00
|
|
|
MethodType == tok::minus, isVariadic,
|
2008-05-08 04:53:44 +08:00
|
|
|
false,
|
2009-09-09 23:08:12 +08:00
|
|
|
MethodDeclKind == tok::objc_optional ?
|
|
|
|
ObjCMethodDecl::Optional :
|
2008-04-04 14:12:32 +08:00
|
|
|
ObjCMethodDecl::Required);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
llvm::SmallVector<ParmVarDecl*, 16> Params;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 03:42:43 +08:00
|
|
|
for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
|
2009-10-24 05:48:59 +08:00
|
|
|
QualType ArgType;
|
2009-12-07 10:54:59 +08:00
|
|
|
TypeSourceInfo *DI;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 02:57:04 +08:00
|
|
|
if (ArgInfo[i].Type == 0) {
|
2009-10-24 05:48:59 +08:00
|
|
|
ArgType = Context.getObjCIdType();
|
|
|
|
DI = 0;
|
2009-04-12 02:57:04 +08:00
|
|
|
} else {
|
2009-10-24 05:48:59 +08:00
|
|
|
ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
|
2008-12-10 03:36:17 +08:00
|
|
|
// Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
|
2009-04-12 03:34:56 +08:00
|
|
|
ArgType = adjustParameterType(ArgType);
|
2009-04-12 02:57:04 +08:00
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-10-24 05:48:59 +08:00
|
|
|
ParmVarDecl* Param
|
|
|
|
= ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
|
|
|
|
ArgInfo[i].Name, ArgType, DI,
|
|
|
|
VarDecl::None, 0);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-04-12 03:34:56 +08:00
|
|
|
if (ArgType->isObjCInterfaceType()) {
|
|
|
|
Diag(ArgInfo[i].NameLoc,
|
|
|
|
diag::err_object_cannot_be_passed_returned_by_value)
|
|
|
|
<< 1 << ArgType;
|
|
|
|
Param->setInvalidDecl();
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
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
|
|
|
|
2007-12-12 15:09:47 +08:00
|
|
|
Params.push_back(Param);
|
|
|
|
}
|
2008-04-04 14:12:32 +08:00
|
|
|
|
2009-05-21 17:52:38 +08:00
|
|
|
ObjCMethod->setMethodParams(Context, Params.data(), Sel.getNumArgs());
|
2008-01-08 03:49:32 +08:00
|
|
|
ObjCMethod->setObjCDeclQualifier(
|
|
|
|
CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
|
|
|
|
const ObjCMethodDecl *PrevMethod = 0;
|
2008-09-26 12:12:28 +08:00
|
|
|
|
|
|
|
if (AttrList)
|
2009-06-18 05:51:59 +08:00
|
|
|
ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-11-04 10:18:39 +08:00
|
|
|
const ObjCMethodDecl *InterfaceMD = 0;
|
|
|
|
|
2009-09-09 23:08:12 +08:00
|
|
|
// For implementations (which can be very "coarse grain"), we add the
|
|
|
|
// method now. This allows the AST to implement lookup methods that work
|
|
|
|
// incrementally (without waiting until we parse the @end). It also allows
|
2007-12-12 15:09:47 +08:00
|
|
|
// us to flag multiple declaration errors as they occur.
|
2009-09-09 23:08:12 +08:00
|
|
|
if (ObjCImplementationDecl *ImpDecl =
|
2008-03-16 08:49:28 +08:00
|
|
|
dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
|
2007-12-12 15:09:47 +08:00
|
|
|
if (MethodType == tok::minus) {
|
2009-06-30 10:36:12 +08:00
|
|
|
PrevMethod = ImpDecl->getInstanceMethod(Sel);
|
|
|
|
ImpDecl->addInstanceMethod(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
} else {
|
2009-06-30 10:36:12 +08:00
|
|
|
PrevMethod = ImpDecl->getClassMethod(Sel);
|
|
|
|
ImpDecl->addClassMethod(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-11-04 10:18:39 +08:00
|
|
|
InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
|
|
|
|
MethodType == tok::minus);
|
2009-05-13 05:36:23 +08:00
|
|
|
if (AttrList)
|
|
|
|
Diag(EndLoc, diag::warn_attribute_method_def);
|
2009-09-09 23:08:12 +08:00
|
|
|
} else if (ObjCCategoryImplDecl *CatImpDecl =
|
2009-08-05 05:02:39 +08:00
|
|
|
dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
|
2007-12-12 15:09:47 +08:00
|
|
|
if (MethodType == tok::minus) {
|
2009-06-30 10:36:12 +08:00
|
|
|
PrevMethod = CatImpDecl->getInstanceMethod(Sel);
|
|
|
|
CatImpDecl->addInstanceMethod(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
} else {
|
2009-06-30 10:36:12 +08:00
|
|
|
PrevMethod = CatImpDecl->getClassMethod(Sel);
|
|
|
|
CatImpDecl->addClassMethod(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
2009-05-13 05:36:23 +08:00
|
|
|
if (AttrList)
|
|
|
|
Diag(EndLoc, diag::warn_attribute_method_def);
|
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
|
|
|
|
|
|
|
// If the interface declared this method, and it was deprecated there,
|
|
|
|
// mark it deprecated here.
|
|
|
|
if (InterfaceMD && InterfaceMD->hasAttr<DeprecatedAttr>())
|
|
|
|
ObjCMethod->addAttr(::new (Context) DeprecatedAttr());
|
|
|
|
|
2009-03-29 03:18:32 +08:00
|
|
|
return DeclPtrTy::make(ObjCMethod);
|
2007-12-12 15:09:47 +08:00
|
|
|
}
|
|
|
|
|
2008-12-17 15:13:27 +08:00
|
|
|
bool Sema::CheckObjCDeclScope(Decl *D) {
|
2009-01-07 07:51:29 +08:00
|
|
|
if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
|
2008-11-05 00:57:32 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
|
|
|
|
D->setInvalidDecl();
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2008-11-05 00:57:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
2008-12-17 15:13:27 +08:00
|
|
|
|
|
|
|
/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
|
|
|
|
/// instance variables of ClassName into Decls.
|
2009-09-09 23:08:12 +08:00
|
|
|
void Sema::ActOnDefs(Scope *S, DeclPtrTy TagD, SourceLocation DeclStart,
|
2008-12-17 15:13:27 +08:00
|
|
|
IdentifierInfo *ClassName,
|
2009-03-29 03:18:32 +08:00
|
|
|
llvm::SmallVectorImpl<DeclPtrTy> &Decls) {
|
2008-12-17 15:13:27 +08:00
|
|
|
// Check that ClassName is a valid class
|
|
|
|
ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName);
|
|
|
|
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
|
2009-06-05 01:08:55 +08:00
|
|
|
llvm::SmallVector<FieldDecl*, 32> RecFields;
|
|
|
|
Context.CollectObjCIvars(Class, RecFields);
|
|
|
|
// For each ivar, create a fresh ObjCAtDefsFieldDecl.
|
|
|
|
for (unsigned i = 0; i < RecFields.size(); i++) {
|
|
|
|
FieldDecl* ID = RecFields[i];
|
|
|
|
RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>());
|
|
|
|
Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
|
|
|
|
ID->getIdentifier(), ID->getType(),
|
|
|
|
ID->getBitWidth());
|
|
|
|
Decls.push_back(Sema::DeclPtrTy::make(FD));
|
|
|
|
}
|
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.
|
2009-03-29 03:18:32 +08:00
|
|
|
for (llvm::SmallVectorImpl<DeclPtrTy>::iterator D = Decls.begin();
|
2008-12-17 15:13:27 +08:00
|
|
|
D != Decls.end(); ++D) {
|
2009-03-29 03:18:32 +08:00
|
|
|
FieldDecl *FD = cast<FieldDecl>(D->getAs<Decl>());
|
2008-12-17 15:13:27 +08:00
|
|
|
if (getLangOptions().CPlusPlus)
|
|
|
|
PushOnScopeChains(cast<FieldDecl>(FD), S);
|
2009-03-29 03:18:32 +08:00
|
|
|
else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD.getAs<Decl>()))
|
2009-06-30 10:36:12 +08:00
|
|
|
Record->addDecl(FD);
|
2008-12-17 15:13:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|