blob: 13e42edaae6edc5cfe3085449140ad93a2e1ae0c [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
Douglas Gregore737f502010-08-12 20:07:10 +000014#include "clang/Sema/Sema.h"
15#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000017#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
Steve Naroffca331292009-03-03 14:49:36 +000019#include "clang/AST/Expr.h"
Chris Lattner4d391482007-12-12 07:09:47 +000020#include "clang/AST/ASTContext.h"
21#include "clang/AST/DeclObjC.h"
John McCall19510852010-08-20 18:27:03 +000022#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000023#include "llvm/ADT/DenseSet.h"
24
Chris Lattner4d391482007-12-12 07:09:47 +000025using namespace clang;
26
Steve Naroffebf64432009-02-28 16:59:13 +000027/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +000028/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +000029void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +000030 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +000031 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +000032
Steve Naroff394f3f42008-07-25 17:57:26 +000033 // If we don't have a valid method decl, simply return.
34 if (!MDecl)
35 return;
Steve Naroffa56f6162007-12-18 01:30:32 +000036
37 // Allow the rest of sema to find private method decl implementations.
Douglas Gregorf8d49f62009-01-09 17:18:27 +000038 if (MDecl->isInstanceMethod())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000039 AddInstanceMethodToGlobalPool(MDecl, true);
Steve Naroffa56f6162007-12-18 01:30:32 +000040 else
Fariborz Jahanian3fe10412010-07-22 18:24:20 +000041 AddFactoryMethodToGlobalPool(MDecl, true);
42
Chris Lattner4d391482007-12-12 07:09:47 +000043 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +000044 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +000045 PushFunctionScope();
46
Chris Lattner4d391482007-12-12 07:09:47 +000047 // Create Decl objects for each parameter, entrring them in the scope for
48 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +000049
50 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +000051 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +000052
Daniel Dunbar451318c2008-08-26 06:07:48 +000053 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
54 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +000055
Chris Lattner8123a952008-04-10 02:22:51 +000056 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +000057 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
58 E = MDecl->param_end(); PI != E; ++PI)
59 if ((*PI)->getIdentifier())
60 PushOnScopeChains(*PI, FnBodyScope);
Chris Lattner4d391482007-12-12 07:09:47 +000061}
62
John McCalld226f652010-08-21 09:40:31 +000063Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +000064ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
65 IdentifierInfo *ClassName, SourceLocation ClassLoc,
66 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +000067 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +000068 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +000069 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +000070 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chris Lattner4d391482007-12-12 07:09:47 +000072 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +000073 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +000074 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +000075
Ted Kremeneka526c5c2008-01-07 19:49:32 +000076 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +000077 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +000078 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +000079 }
Mike Stump1eb44332009-09-09 15:08:12 +000080
Douglas Gregordeacbdc2010-08-11 12:19:30 +000081 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
82 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +000083 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +000084 if (!IDecl->isForwardDecl()) {
85 IDecl->setInvalidDecl();
86 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
87 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +000088
Douglas Gregordeacbdc2010-08-11 12:19:30 +000089 // Return the previous class interface.
90 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +000091 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +000092 } else {
93 IDecl->setLocation(AtInterfaceLoc);
94 IDecl->setForwardDecl(false);
95 IDecl->setClassLoc(ClassLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +000096 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +000097 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +000098 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +000099
100 // Since this ObjCInterfaceDecl was created by a forward declaration,
101 // we now add it to the DeclContext since it wasn't added before
102 // (see ActOnForwardClassDeclaration).
103 IDecl->setLexicalDeclContext(CurContext);
104 CurContext->addDecl(IDecl);
105
106 if (AttrList)
107 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000108 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000109 } else {
110 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
111 ClassName, ClassLoc);
112 if (AttrList)
113 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
114
115 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000116 }
Mike Stump1eb44332009-09-09 15:08:12 +0000117
Chris Lattner4d391482007-12-12 07:09:47 +0000118 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000119 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000120 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
121 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000122
123 if (!PrevDecl) {
124 // Try to correct for a typo in the superclass name.
125 LookupResult R(*this, SuperName, SuperLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000126 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000127 (PrevDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
128 Diag(SuperLoc, diag::err_undef_superclass_suggest)
129 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000130 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
131 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000132 }
133 }
134
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000135 if (PrevDecl == IDecl) {
136 Diag(SuperLoc, diag::err_recursive_superclass)
137 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
138 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000139 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000140 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000141 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000142
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000143 // Diagnose classes that inherit from deprecated classes.
144 if (SuperClassDecl)
145 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000146
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000147 if (PrevDecl && SuperClassDecl == 0) {
148 // The previous declaration was not a class decl. Check if we have a
149 // typedef. If we do, get the underlying class type.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000150 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000151 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000152 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000153 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
154 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000155 }
156 }
Mike Stump1eb44332009-09-09 15:08:12 +0000157
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000158 // This handles the following case:
159 //
160 // typedef int SuperClass;
161 // @interface MyClass : SuperClass {} @end
162 //
163 if (!SuperClassDecl) {
164 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
165 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000166 }
167 }
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000169 if (!dyn_cast_or_null<TypedefDecl>(PrevDecl)) {
170 if (!SuperClassDecl)
171 Diag(SuperLoc, diag::err_undef_superclass)
172 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
173 else if (SuperClassDecl->isForwardDecl())
174 Diag(SuperLoc, diag::err_undef_superclass)
175 << SuperClassDecl->getDeclName() << ClassName
176 << SourceRange(AtInterfaceLoc, ClassLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000177 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000178 IDecl->setSuperClass(SuperClassDecl);
179 IDecl->setSuperClassLoc(SuperLoc);
180 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000181 }
Chris Lattner4d391482007-12-12 07:09:47 +0000182 } else { // we have a root class.
183 IDecl->setLocEnd(ClassLoc);
184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Sebastian Redl0b17c612010-08-13 00:28:03 +0000186 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000187 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000188 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000189 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000190 IDecl->setLocEnd(EndProtoLoc);
191 }
Mike Stump1eb44332009-09-09 15:08:12 +0000192
Anders Carlsson15281452008-11-04 16:57:32 +0000193 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000194 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000195}
196
197/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000198/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000199Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
200 IdentifierInfo *AliasName,
201 SourceLocation AliasLocation,
202 IdentifierInfo *ClassName,
203 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000204 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000205 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000206 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000207 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000208 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000209 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000210 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000211 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000212 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000213 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000214 }
215 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000216 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000217 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000218 if (const TypedefDecl *TDecl = dyn_cast_or_null<TypedefDecl>(CDeclU)) {
219 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000220 if (T->isObjCObjectType()) {
221 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000222 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000223 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000224 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000225 }
226 }
227 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000228 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
229 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000230 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000231 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000232 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000233 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000234 }
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000236 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000237 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000238 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Anders Carlsson15281452008-11-04 16:57:32 +0000240 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000241 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000242
John McCalld226f652010-08-21 09:40:31 +0000243 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000244}
245
Steve Naroff61d68522009-03-05 15:22:01 +0000246void Sema::CheckForwardProtocolDeclarationForCircularDependency(
247 IdentifierInfo *PName,
248 SourceLocation &Ploc, SourceLocation PrevLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000249 const ObjCList<ObjCProtocolDecl> &PList) {
Steve Naroff61d68522009-03-05 15:22:01 +0000250 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
251 E = PList.end(); I != E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Douglas Gregorc83c6872010-04-15 22:33:43 +0000253 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
254 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000255 if (PDecl->getIdentifier() == PName) {
256 Diag(Ploc, diag::err_protocol_has_circular_dependency);
257 Diag(PrevLoc, diag::note_previous_definition);
258 }
Mike Stump1eb44332009-09-09 15:08:12 +0000259 CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
Steve Naroff61d68522009-03-05 15:22:01 +0000260 PDecl->getLocation(), PDecl->getReferencedProtocols());
261 }
262 }
263}
264
John McCalld226f652010-08-21 09:40:31 +0000265Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000266Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
267 IdentifierInfo *ProtocolName,
268 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000269 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000270 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000271 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000272 SourceLocation EndProtoLoc,
273 AttributeList *AttrList) {
274 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000275 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000276 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000277 if (PDecl) {
278 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000279 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000280 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000281 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000282 // Just return the protocol we already had.
283 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000284 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000285 }
Steve Naroff61d68522009-03-05 15:22:01 +0000286 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000287 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Steve Naroff61d68522009-03-05 15:22:01 +0000288 CheckForwardProtocolDeclarationForCircularDependency(
289 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Steve Narofff11b5082008-08-13 16:39:22 +0000291 // Make sure the cached decl gets a valid start location.
292 PDecl->setLocation(AtProtoInterfaceLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000293 PDecl->setForwardDecl(false);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000294 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000295 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000296 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000297 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000298 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000299 AtProtoInterfaceLoc,ProtocolName);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000300 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000301 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000302 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000303 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000304 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000305 if (NumProtoRefs) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000306 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000307 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
308 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000309 PDecl->setLocEnd(EndProtoLoc);
310 }
Mike Stump1eb44332009-09-09 15:08:12 +0000311
312 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000313 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000314}
315
316/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000317/// issues an error if they are not declared. It returns list of
318/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000319void
Chris Lattnere13b9592008-07-26 04:03:38 +0000320Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000321 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000322 unsigned NumProtocols,
John McCalld226f652010-08-21 09:40:31 +0000323 llvm::SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000324 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000325 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
326 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000327 if (!PDecl) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000328 LookupResult R(*this, ProtocolId[i].first, ProtocolId[i].second,
329 LookupObjCProtocolName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000330 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000331 (PDecl = R.getAsSingle<ObjCProtocolDecl>())) {
332 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
333 << ProtocolId[i].first << R.getLookupName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000334 Diag(PDecl->getLocation(), diag::note_previous_decl)
335 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000336 }
337 }
338
339 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000340 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000341 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000342 continue;
343 }
Mike Stump1eb44332009-09-09 15:08:12 +0000344
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000345 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000346
347 // If this is a forward declaration and we are supposed to warn in this
348 // case, do it.
349 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000350 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000351 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000352 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000353 }
354}
355
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000356/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000357/// a class method in its extension.
358///
Mike Stump1eb44332009-09-09 15:08:12 +0000359void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000360 ObjCInterfaceDecl *ID) {
361 if (!ID)
362 return; // Possibly due to previous error
363
364 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000365 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
366 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000367 ObjCMethodDecl *MD = *i;
368 MethodMap[MD->getSelector()] = MD;
369 }
370
371 if (MethodMap.empty())
372 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000373 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
374 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000375 ObjCMethodDecl *Method = *i;
376 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
377 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
378 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
379 << Method->getDeclName();
380 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
381 }
382 }
383}
384
Chris Lattner58fe03b2009-04-12 08:43:13 +0000385/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000386Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000387Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000388 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000389 unsigned NumElts,
390 AttributeList *attrList) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000391 llvm::SmallVector<ObjCProtocolDecl*, 32> Protocols;
Douglas Gregor18df52b2010-01-16 15:02:53 +0000392 llvm::SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000393
Chris Lattner4d391482007-12-12 07:09:47 +0000394 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000395 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000396 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000397 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000398 if (PDecl == 0) { // Not already seen?
Mike Stump1eb44332009-09-09 15:08:12 +0000399 PDecl = ObjCProtocolDecl::Create(Context, CurContext,
Douglas Gregord0434102009-01-09 00:49:46 +0000400 IdentList[i].second, Ident);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000401 PushOnScopeChains(PDecl, TUScope, false);
402 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000403 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000404 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000405 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000406 if (!isNew)
407 PDecl->setChangedSinceDeserialization(true);
408 }
Chris Lattner4d391482007-12-12 07:09:47 +0000409 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000410 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000411 }
Mike Stump1eb44332009-09-09 15:08:12 +0000412
413 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000414 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000415 Protocols.data(), Protocols.size(),
416 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000417 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000418 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000419 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000420}
421
John McCalld226f652010-08-21 09:40:31 +0000422Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000423ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
424 IdentifierInfo *ClassName, SourceLocation ClassLoc,
425 IdentifierInfo *CategoryName,
426 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000427 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000428 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000429 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000430 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000431 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000432 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000433
434 /// Check that class of this category is already completely declared.
435 if (!IDecl || IDecl->isForwardDecl()) {
436 // Create an invalid ObjCCategoryDecl to serve as context for
437 // the enclosing method declarations. We mark the decl invalid
438 // to make it clear that this isn't a valid AST.
439 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
440 ClassLoc, CategoryLoc, CategoryName);
441 CDecl->setInvalidDecl();
442 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld226f652010-08-21 09:40:31 +0000443 return CDecl;
Ted Kremenek09b68972010-02-23 19:39:46 +0000444 }
445
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000446 if (!CategoryName && IDecl->getImplementation()) {
447 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
448 Diag(IDecl->getImplementation()->getLocation(),
449 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000450 }
451
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000452 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
453 ClassLoc, CategoryLoc, CategoryName);
454 // FIXME: PushOnScopeChains?
455 CurContext->addDecl(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000456
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000457 CDecl->setClassInterface(IDecl);
458 // Insert class extension to the list of class's categories.
459 if (!CategoryName)
460 CDecl->insertNextClassCategory();
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Chris Lattner16b34b42009-02-16 21:30:01 +0000462 // If the interface is deprecated, warn about it.
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000463 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
Chris Lattner70f19542009-02-16 21:26:43 +0000464
Fariborz Jahanian25760612010-02-15 21:55:26 +0000465 if (CategoryName) {
466 /// Check for duplicate interface declaration for this category
467 ObjCCategoryDecl *CDeclChain;
468 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
469 CDeclChain = CDeclChain->getNextClassCategory()) {
470 if (CDeclChain->getIdentifier() == CategoryName) {
471 // Class extensions can be declared multiple times.
472 Diag(CategoryLoc, diag::warn_dup_category_def)
473 << ClassName << CategoryName;
474 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
475 break;
476 }
Chris Lattner70f19542009-02-16 21:26:43 +0000477 }
Fariborz Jahanian25760612010-02-15 21:55:26 +0000478 if (!CDeclChain)
479 CDecl->insertNextClassCategory();
Chris Lattner70f19542009-02-16 21:26:43 +0000480 }
Chris Lattner70f19542009-02-16 21:26:43 +0000481
Chris Lattner4d391482007-12-12 07:09:47 +0000482 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000483 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000484 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000485 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000486 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000487 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000488 NumProtoRefs, ProtoLocs,
489 Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Anders Carlsson15281452008-11-04 16:57:32 +0000492 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000493 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000494}
495
496/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000497/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000498/// object.
John McCalld226f652010-08-21 09:40:31 +0000499Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000500 SourceLocation AtCatImplLoc,
501 IdentifierInfo *ClassName, SourceLocation ClassLoc,
502 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000503 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000504 ObjCCategoryDecl *CatIDecl = 0;
505 if (IDecl) {
506 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
507 if (!CatIDecl) {
508 // Category @implementation with no corresponding @interface.
509 // Create and install one.
510 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000511 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000512 CatName);
513 CatIDecl->setClassInterface(IDecl);
514 CatIDecl->insertNextClassCategory();
515 }
516 }
517
Mike Stump1eb44332009-09-09 15:08:12 +0000518 ObjCCategoryImplDecl *CDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000519 ObjCCategoryImplDecl::Create(Context, CurContext, AtCatImplLoc, CatName,
520 IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000521 /// Check that class of this category is already completely declared.
522 if (!IDecl || IDecl->isForwardDecl())
Chris Lattner3c73c412008-11-19 08:23:25 +0000523 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000524
Douglas Gregord0434102009-01-09 00:49:46 +0000525 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000526 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000527
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000528 /// Check that CatName, category name, is not used in another implementation.
529 if (CatIDecl) {
530 if (CatIDecl->getImplementation()) {
531 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
532 << CatName;
533 Diag(CatIDecl->getImplementation()->getLocation(),
534 diag::note_previous_definition);
535 } else
536 CatIDecl->setImplementation(CDecl);
537 }
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Anders Carlsson15281452008-11-04 16:57:32 +0000539 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000540 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000541}
542
John McCalld226f652010-08-21 09:40:31 +0000543Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000544 SourceLocation AtClassImplLoc,
545 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000546 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000547 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000548 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000549 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000550 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000551 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
552 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000553 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000554 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000555 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000556 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
557 // If this is a forward declaration of an interface, warn.
558 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000559 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000560 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000561 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000562 } else {
563 // We did not find anything with the name ClassName; try to correct for
564 // typos in the class name.
565 LookupResult R(*this, ClassName, ClassLoc, LookupOrdinaryName);
Douglas Gregoraaf87162010-04-14 20:04:41 +0000566 if (CorrectTypo(R, TUScope, 0, 0, false, CTC_NoKeywords) &&
Douglas Gregor95ff7422010-01-04 17:27:12 +0000567 (IDecl = R.getAsSingle<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000568 // Suggest the (potentially) correct interface name. However, put the
569 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000570 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000571 // provide a code-modification hint or use the typo name for recovery,
572 // because this is just a warning. The program may actually be correct.
573 Diag(ClassLoc, diag::warn_undef_interface_suggest)
574 << ClassName << R.getLookupName();
Douglas Gregora6f26382010-01-06 23:44:25 +0000575 Diag(IDecl->getLocation(), diag::note_previous_decl)
576 << R.getLookupName()
Douglas Gregor849b2432010-03-31 17:46:05 +0000577 << FixItHint::CreateReplacement(ClassLoc,
578 R.getLookupName().getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000579 IDecl = 0;
580 } else {
581 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
582 }
Chris Lattner4d391482007-12-12 07:09:47 +0000583 }
Mike Stump1eb44332009-09-09 15:08:12 +0000584
Chris Lattner4d391482007-12-12 07:09:47 +0000585 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000586 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000587 if (SuperClassname) {
588 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000589 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
590 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000591 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000592 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
593 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000594 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000595 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000596 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000597 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000598 Diag(SuperClassLoc, diag::err_undef_superclass)
599 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000600 else if (IDecl && IDecl->getSuperClass() != SDecl) {
601 // This implementation and its interface do not have the same
602 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000603 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000604 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000605 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000606 }
607 }
608 }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Chris Lattner4d391482007-12-12 07:09:47 +0000610 if (!IDecl) {
611 // Legacy case of @implementation with no corresponding @interface.
612 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000613
Mike Stump390b4cc2009-05-16 07:39:55 +0000614 // FIXME: Do we support attributes on the @implementation? If so we should
615 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000616 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000617 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000618 IDecl->setSuperClass(SDecl);
619 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000620
621 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000622 } else {
623 // Mark the interface as being completed, even if it was just as
624 // @class ....;
625 // declaration; the user cannot reopen it.
626 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000627 }
Mike Stump1eb44332009-09-09 15:08:12 +0000628
629 ObjCImplementationDecl* IMPDecl =
630 ObjCImplementationDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000631 IDecl, SDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000632
Anders Carlsson15281452008-11-04 16:57:32 +0000633 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000634 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Chris Lattner4d391482007-12-12 07:09:47 +0000636 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000637 if (IDecl->getImplementation()) {
638 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000639 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000640 Diag(IDecl->getImplementation()->getLocation(),
641 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000642 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000643 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000644 PushOnScopeChains(IMPDecl, TUScope);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000645 }
John McCalld226f652010-08-21 09:40:31 +0000646 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000647}
648
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000649void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
650 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000651 SourceLocation RBrace) {
652 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000653 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000654 if (!IDecl)
655 return;
656 /// Check case of non-existing @interface decl.
657 /// (legacy objective-c @implementation decl without an @interface decl).
658 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000659 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000660 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000661 // Add ivar's to class's DeclContext.
662 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000663 ivars[i]->setLexicalDeclContext(ImpDecl);
664 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000665 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000666 }
667
Chris Lattner4d391482007-12-12 07:09:47 +0000668 return;
669 }
670 // If implementation has empty ivar list, just return.
671 if (numIvars == 0)
672 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Chris Lattner4d391482007-12-12 07:09:47 +0000674 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000675 if (LangOpts.ObjCNonFragileABI2) {
676 if (ImpDecl->getSuperClass())
677 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
678 for (unsigned i = 0; i < numIvars; i++) {
679 ObjCIvarDecl* ImplIvar = ivars[i];
680 if (const ObjCIvarDecl *ClsIvar =
681 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
682 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
683 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
684 continue;
685 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000686 // Instance ivar to Implementation's DeclContext.
687 ImplIvar->setLexicalDeclContext(ImpDecl);
688 IDecl->makeDeclVisibleInContext(ImplIvar, false);
689 ImpDecl->addDecl(ImplIvar);
690 }
691 return;
692 }
Chris Lattner4d391482007-12-12 07:09:47 +0000693 // Check interface's Ivar list against those in the implementation.
694 // names and types must match.
695 //
Chris Lattner4d391482007-12-12 07:09:47 +0000696 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000697 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +0000698 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
699 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000700 ObjCIvarDecl* ImplIvar = ivars[j++];
701 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +0000702 assert (ImplIvar && "missing implementation ivar");
703 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Steve Naroffca331292009-03-03 14:49:36 +0000705 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +0000706 if (Context.getCanonicalType(ImplIvar->getType()) !=
707 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000708 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +0000709 << ImplIvar->getIdentifier()
710 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000711 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +0000712 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
713 Expr *ImplBitWidth = ImplIvar->getBitWidth();
714 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000715 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
716 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +0000717 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
718 << ImplIvar->getIdentifier();
719 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
720 }
Mike Stump1eb44332009-09-09 15:08:12 +0000721 }
Steve Naroffca331292009-03-03 14:49:36 +0000722 // Make sure the names are identical.
723 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000724 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +0000725 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000726 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000727 }
728 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +0000729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
Chris Lattner609e4c72007-12-12 18:11:49 +0000731 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +0000732 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +0000733 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +0000734 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +0000735}
736
Steve Naroff3c2eb662008-02-10 21:38:56 +0000737void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +0000738 bool &IncompleteImpl, unsigned DiagID) {
Steve Naroff3c2eb662008-02-10 21:38:56 +0000739 if (!IncompleteImpl) {
740 Diag(ImpLoc, diag::warn_incomplete_impl);
741 IncompleteImpl = true;
742 }
Fariborz Jahanian52146832010-03-31 18:23:33 +0000743 Diag(method->getLocation(), DiagID)
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000744 << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +0000745}
746
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000747void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
748 ObjCMethodDecl *IntfMethodDecl) {
Chris Lattner5272b7f2009-04-11 18:01:59 +0000749 if (!Context.typesAreCompatible(IntfMethodDecl->getResultType(),
Fariborz Jahanian2574a682009-05-14 23:52:54 +0000750 ImpMethodDecl->getResultType()) &&
Steve Naroff4084c302009-07-23 01:01:38 +0000751 !Context.QualifiedIdConformsQualifiedId(IntfMethodDecl->getResultType(),
752 ImpMethodDecl->getResultType())) {
Mike Stump1eb44332009-09-09 15:08:12 +0000753 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_ret_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000754 << ImpMethodDecl->getDeclName() << IntfMethodDecl->getResultType()
755 << ImpMethodDecl->getResultType();
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000756 Diag(IntfMethodDecl->getLocation(), diag::note_previous_definition);
757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chris Lattner3aff9192009-04-11 19:58:42 +0000759 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
760 IF = IntfMethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
761 IM != EM; ++IM, ++IF) {
Fariborz Jahanian3393f812009-11-18 18:56:09 +0000762 QualType ParmDeclTy = (*IF)->getType().getUnqualifiedType();
763 QualType ParmImpTy = (*IM)->getType().getUnqualifiedType();
764 if (Context.typesAreCompatible(ParmDeclTy, ParmImpTy) ||
765 Context.QualifiedIdConformsQualifiedId(ParmDeclTy, ParmImpTy))
Chris Lattner3aff9192009-04-11 19:58:42 +0000766 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000767
768 Diag((*IM)->getLocation(), diag::warn_conflicting_param_types)
Chris Lattner3aff9192009-04-11 19:58:42 +0000769 << ImpMethodDecl->getDeclName() << (*IF)->getType()
770 << (*IM)->getType();
Chris Lattnerd1e0f5a2009-04-11 20:14:49 +0000771 Diag((*IF)->getLocation(), diag::note_previous_definition);
Chris Lattner3aff9192009-04-11 19:58:42 +0000772 }
Fariborz Jahanian561da7e2010-05-21 23:28:58 +0000773 if (ImpMethodDecl->isVariadic() != IntfMethodDecl->isVariadic()) {
774 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
775 Diag(IntfMethodDecl->getLocation(), diag::note_previous_declaration);
776 }
Fariborz Jahanian8daab972008-12-05 18:18:52 +0000777}
778
Mike Stump390b4cc2009-05-16 07:39:55 +0000779/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
780/// improve the efficiency of selector lookups and type checking by associating
781/// with each protocol / interface / category the flattened instance tables. If
782/// we used an immutable set to keep the table then it wouldn't add significant
783/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +0000784
Steve Naroffefe7f362008-02-08 22:06:17 +0000785/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +0000786/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +0000787void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
788 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +0000789 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +0000790 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000791 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000792 ObjCContainerDecl *CDecl) {
793 ObjCInterfaceDecl *IDecl;
794 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
795 IDecl = C->getClassInterface();
796 else
797 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
798 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
799
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000800 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000801 ObjCInterfaceDecl *NSIDecl = 0;
802 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +0000803 // check to see if class implements forwardInvocation method and objects
804 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000805 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +0000806 // Under such conditions, which means that every method possible is
807 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000808 // found" warnings.
809 // FIXME: Use a general GetUnarySelector method for this.
810 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
811 Selector fISelector = Context.Selectors.getSelector(1, &II);
812 if (InsMap.count(fISelector))
813 // Is IDecl derived from 'NSProxy'? If so, no instance methods
814 // need be implemented in the implementation.
815 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
816 }
Mike Stump1eb44332009-09-09 15:08:12 +0000817
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000818 // If a method lookup fails locally we still need to look and see if
819 // the method was implemented by a base class or an inherited
820 // protocol. This lookup is slow, but occurs rarely in correct code
821 // and otherwise would terminate in a warning.
822
Chris Lattner4d391482007-12-12 07:09:47 +0000823 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000824 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +0000825 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000826 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000827 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +0000828 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000829 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +0000830 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000831 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000832 // Ugly, but necessary. Method declared in protcol might have
833 // have been synthesized due to a property declared in the class which
834 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +0000835 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000836 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000837 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000838 unsigned DIAG = diag::warn_unimplemented_protocol_method;
839 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
840 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
841 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
842 << PDecl->getDeclName();
843 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000844 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +0000845 }
846 }
Chris Lattner4d391482007-12-12 07:09:47 +0000847 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +0000848 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000849 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +0000850 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000851 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000852 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
853 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000854 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +0000855 unsigned DIAG = diag::warn_unimplemented_protocol_method;
856 if (Diags.getDiagnosticLevel(DIAG) != Diagnostic::Ignored) {
857 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
858 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
859 PDecl->getDeclName();
860 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +0000861 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +0000862 }
Chris Lattner780f3292008-07-21 21:32:27 +0000863 // Check on this protocols's referenced protocols, recursively.
864 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
865 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000866 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000867}
868
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000869/// MatchAllMethodDeclarations - Check methods declaraed in interface or
870/// or protocol against those declared in their implementations.
871///
872void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
873 const llvm::DenseSet<Selector> &ClsMap,
874 llvm::DenseSet<Selector> &InsMapSeen,
875 llvm::DenseSet<Selector> &ClsMapSeen,
876 ObjCImplDecl* IMPDecl,
877 ObjCContainerDecl* CDecl,
878 bool &IncompleteImpl,
Mike Stump1eb44332009-09-09 15:08:12 +0000879 bool ImmediateClass) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000880 // Check and see if instance methods in class interface have been
881 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000882 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
883 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000884 if (InsMapSeen.count((*I)->getSelector()))
885 continue;
886 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000887 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000888 !InsMap.count((*I)->getSelector())) {
889 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +0000890 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
891 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000892 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000893 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000894 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000895 IMPDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000896 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000897 CDecl->getInstanceMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000898 assert(IntfMethodDecl &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000899 "IntfMethodDecl is null in ImplMethodsVsClassMethods");
900 // ImpMethodDecl may be null as in a @dynamic property.
901 if (ImpMethodDecl)
902 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
903 }
904 }
Mike Stump1eb44332009-09-09 15:08:12 +0000905
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000906 // Check and see if class methods in class interface have been
907 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +0000908 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000909 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000910 if (ClsMapSeen.count((*I)->getSelector()))
911 continue;
912 ClsMapSeen.insert((*I)->getSelector());
913 if (!ClsMap.count((*I)->getSelector())) {
914 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +0000915 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
916 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000917 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000918 ObjCMethodDecl *ImpMethodDecl =
919 IMPDecl->getClassMethod((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000920 ObjCMethodDecl *IntfMethodDecl =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000921 CDecl->getClassMethod((*I)->getSelector());
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000922 WarnConflictingTypedMethods(ImpMethodDecl, IntfMethodDecl);
923 }
924 }
925 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
926 // Check for any implementation of a methods declared in protocol.
927 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
928 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +0000929 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
930 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000931 (*PI), IncompleteImpl, false);
932 if (I->getSuperClass())
933 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +0000934 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000935 I->getSuperClass(), IncompleteImpl, false);
936 }
937}
938
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000939void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +0000940 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +0000941 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000942 llvm::DenseSet<Selector> InsMap;
943 // Check and see if instance methods in class interface have been
944 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +0000945 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000946 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000947 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000948
Fariborz Jahanian12bac252009-04-14 23:15:21 +0000949 // Check and see if properties declared in the interface have either 1)
950 // an implementation or 2) there is a @synthesize/@dynamic implementation
951 // of the property in the @implementation.
Fariborz Jahanian509d4772010-05-14 18:35:57 +0000952 if (isa<ObjCInterfaceDecl>(CDecl) && !LangOpts.ObjCNonFragileABI2)
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000953 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +0000954
Chris Lattner4d391482007-12-12 07:09:47 +0000955 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +0000956 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000957 I = IMPDecl->classmeth_begin(),
958 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +0000959 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000961 // Check for type conflict of methods declared in a class/protocol and
962 // its implementation; if any.
963 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +0000964 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
965 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000966 IncompleteImpl, true);
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Chris Lattner4d391482007-12-12 07:09:47 +0000968 // Check the protocol list for unimplemented methods in the @implementation
969 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000970 // Check and see if class methods in class interface have been
971 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +0000972
Chris Lattnercddc8882009-03-01 00:56:52 +0000973 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +0000974 for (ObjCInterfaceDecl::protocol_iterator PI = I->protocol_begin(),
Chris Lattnercddc8882009-03-01 00:56:52 +0000975 E = I->protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +0000976 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +0000977 InsMap, ClsMap, I);
978 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000979 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
980 Categories; Categories = Categories->getNextClassExtension())
981 ImplMethodsVsClassMethods(S, IMPDecl,
982 const_cast<ObjCCategoryDecl*>(Categories),
983 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +0000984 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000985 // For extended class, unimplemented methods in its protocols will
986 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000987 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000988 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
989 E = C->protocol_end(); PI != E; ++PI)
990 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +0000991 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +0000992 // Report unimplemented properties in the category as well.
993 // When reporting on missing setter/getters, do not report when
994 // setter/getter is implemented in category's primary class
995 // implementation.
996 if (ObjCInterfaceDecl *ID = C->getClassInterface())
997 if (ObjCImplDecl *IMP = ID->getImplementation()) {
998 for (ObjCImplementationDecl::instmeth_iterator
999 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1000 InsMap.insert((*I)->getSelector());
1001 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001002 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001003 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001004 } else
1005 assert(false && "invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001006}
1007
Mike Stump1eb44332009-09-09 15:08:12 +00001008/// ActOnForwardClassDeclaration -
John McCalld226f652010-08-21 09:40:31 +00001009Decl *
Chris Lattner4d391482007-12-12 07:09:47 +00001010Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001011 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001012 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001013 unsigned NumElts) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001014 llvm::SmallVector<ObjCInterfaceDecl*, 32> Interfaces;
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Chris Lattner4d391482007-12-12 07:09:47 +00001016 for (unsigned i = 0; i != NumElts; ++i) {
1017 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001018 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001019 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001020 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001021 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001022 // Maybe we will complain about the shadowed template parameter.
1023 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1024 // Just pretend that we didn't see the previous declaration.
1025 PrevDecl = 0;
1026 }
1027
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001028 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001029 // GCC apparently allows the following idiom:
1030 //
1031 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1032 // @class XCElementToggler;
1033 //
Mike Stump1eb44332009-09-09 15:08:12 +00001034 // FIXME: Make an extension?
Steve Naroffc7333882008-06-05 22:57:10 +00001035 TypedefDecl *TDD = dyn_cast<TypedefDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001036 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001037 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001038 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001039 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001040 // a forward class declaration matching a typedef name of a class refers
1041 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001042 if (const ObjCObjectType *OI =
1043 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1044 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001045 }
Chris Lattner4d391482007-12-12 07:09:47 +00001046 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001047 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1048 if (!IDecl) { // Not already seen? Make a forward decl.
1049 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1050 IdentList[i], IdentLocs[i], true);
1051
1052 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1053 // the current DeclContext. This prevents clients that walk DeclContext
1054 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1055 // declared later (if at all). We also take care to explicitly make
1056 // sure this declaration is visible for name lookup.
1057 PushOnScopeChains(IDecl, TUScope, false);
1058 CurContext->makeDeclVisibleInContext(IDecl, true);
1059 }
Chris Lattner4d391482007-12-12 07:09:47 +00001060
1061 Interfaces.push_back(IDecl);
1062 }
Mike Stump1eb44332009-09-09 15:08:12 +00001063
Ted Kremenek321c22f2009-11-18 00:28:11 +00001064 assert(Interfaces.size() == NumElts);
Douglas Gregord0434102009-01-09 00:49:46 +00001065 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
Ted Kremenek321c22f2009-11-18 00:28:11 +00001066 Interfaces.data(), IdentLocs,
Anders Carlsson15281452008-11-04 16:57:32 +00001067 Interfaces.size());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001068 CurContext->addDecl(CDecl);
Anders Carlsson15281452008-11-04 16:57:32 +00001069 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +00001070 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00001071}
1072
1073
1074/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1075/// returns true, or false, accordingly.
1076/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
Mike Stump1eb44332009-09-09 15:08:12 +00001077bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *Method,
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001078 const ObjCMethodDecl *PrevMethod,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001079 bool matchBasedOnSizeAndAlignment,
1080 bool matchBasedOnStrictEqulity) {
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001081 QualType T1 = Context.getCanonicalType(Method->getResultType());
1082 QualType T2 = Context.getCanonicalType(PrevMethod->getResultType());
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001084 if (T1 != T2) {
1085 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001086 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Chris Lattner4d391482007-12-12 07:09:47 +00001087 return false;
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001088 // Incomplete types don't have a size and alignment.
1089 if (T1->isIncompleteType() || T2->isIncompleteType())
1090 return false;
1091 // Check is based on size and alignment.
1092 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1093 return false;
1094 }
Mike Stump1eb44332009-09-09 15:08:12 +00001095
Chris Lattner89951a82009-02-20 18:43:26 +00001096 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1097 E = Method->param_end();
1098 ObjCMethodDecl::param_iterator PrevI = PrevMethod->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Chris Lattner89951a82009-02-20 18:43:26 +00001100 for (; ParamI != E; ++ParamI, ++PrevI) {
1101 assert(PrevI != PrevMethod->param_end() && "Param mismatch");
1102 T1 = Context.getCanonicalType((*ParamI)->getType());
1103 T2 = Context.getCanonicalType((*PrevI)->getType());
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001104 if (T1 != T2) {
1105 // The result types are different.
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001106 if (!matchBasedOnSizeAndAlignment || matchBasedOnStrictEqulity)
Steve Narofffe6b0dc2008-10-21 10:37:50 +00001107 return false;
1108 // Incomplete types don't have a size and alignment.
1109 if (T1->isIncompleteType() || T2->isIncompleteType())
1110 return false;
1111 // Check is based on size and alignment.
1112 if (Context.getTypeInfo(T1) != Context.getTypeInfo(T2))
1113 return false;
1114 }
Chris Lattner4d391482007-12-12 07:09:47 +00001115 }
1116 return true;
1117}
1118
Sebastian Redldb9d2142010-08-02 23:18:59 +00001119/// \brief Read the contents of the method pool for a given selector from
1120/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001121///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001122/// This routine should only be called once, when the method pool has no entry
1123/// for this selector.
1124Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001125 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001126 assert(MethodPool.find(Sel) == MethodPool.end() &&
1127 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001128
1129 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001130 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Sebastian Redldb9d2142010-08-02 23:18:59 +00001132 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001133}
1134
Sebastian Redldb9d2142010-08-02 23:18:59 +00001135void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1136 bool instance) {
1137 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1138 if (Pos == MethodPool.end()) {
1139 if (ExternalSource)
1140 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001141 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001142 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1143 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001144 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001145 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001146 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001147 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001148 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001149 Entry.Method = Method;
1150 Entry.Next = 0;
1151 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001152 }
Mike Stump1eb44332009-09-09 15:08:12 +00001153
Chris Lattnerb25df352009-03-04 05:16:45 +00001154 // We've seen a method with this name, see if we have already seen this type
1155 // signature.
1156 for (ObjCMethodList *List = &Entry; List; List = List->Next)
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001157 if (MatchTwoMethodDeclarations(Method, List->Method)) {
1158 List->Method->setDefined(impl);
Chris Lattnerb25df352009-03-04 05:16:45 +00001159 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001160 }
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Chris Lattnerb25df352009-03-04 05:16:45 +00001162 // We have a new signature for an existing method - add it.
1163 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001164 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1165 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001166}
1167
Sebastian Redldb9d2142010-08-02 23:18:59 +00001168ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001169 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001170 bool warn, bool instance) {
1171 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1172 if (Pos == MethodPool.end()) {
1173 if (ExternalSource)
1174 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001175 else
1176 return 0;
1177 }
1178
Sebastian Redldb9d2142010-08-02 23:18:59 +00001179 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001181 bool strictSelectorMatch = receiverIdOrClass && warn &&
1182 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl) !=
1183 Diagnostic::Ignored);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001184 if (warn && MethList.Method && MethList.Next) {
1185 bool issueWarning = false;
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001186 if (strictSelectorMatch)
1187 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1188 // This checks if the methods differ in type mismatch.
1189 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, false, true))
1190 issueWarning = true;
1191 }
1192
1193 if (!issueWarning)
1194 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
1195 // This checks if the methods differ by size & alignment.
1196 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method, true))
1197 issueWarning = true;
1198 }
1199
Sebastian Redldb9d2142010-08-02 23:18:59 +00001200 if (issueWarning) {
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001201 if (strictSelectorMatch)
1202 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
1203 else
1204 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Sebastian Redldb9d2142010-08-02 23:18:59 +00001205 Diag(MethList.Method->getLocStart(), diag::note_using)
1206 << MethList.Method->getSourceRange();
1207 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
1208 Diag(Next->Method->getLocStart(), diag::note_also_found)
1209 << Next->Method->getSourceRange();
1210 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001211 }
1212 return MethList.Method;
1213}
1214
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001215ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00001216 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1217 if (Pos == MethodPool.end())
1218 return 0;
1219
1220 GlobalMethods &Methods = Pos->second;
1221
1222 if (Methods.first.Method && Methods.first.Method->isDefined())
1223 return Methods.first.Method;
1224 if (Methods.second.Method && Methods.second.Method->isDefined())
1225 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001226 return 0;
1227}
1228
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001229/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
1230/// identical selector names in current and its super classes and issues
1231/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001232void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
1233 ObjCMethodDecl *Method,
1234 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001235 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
1236 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001238 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001239 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001240 SD->lookupMethod(Method->getSelector(), IsInstance);
1241 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001242 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001243 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001244 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001245 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
1246 E = Method->param_end();
1247 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
1248 for (; ParamI != E; ++ParamI, ++PrevI) {
1249 // Number of parameters are the same and is guaranteed by selector match.
1250 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
1251 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
1252 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
1253 // If type of arguement of method in this class does not match its
1254 // respective argument type in the super class method, issue warning;
1255 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001256 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001257 << T1 << T2;
1258 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
1259 return;
1260 }
1261 }
1262 ID = SD;
1263 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001264}
1265
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001266/// DiagnoseDuplicateIvars -
1267/// Check for duplicate ivars in the entire class at the start of
1268/// @implementation. This becomes necesssary because class extension can
1269/// add ivars to a class in random order which will not be known until
1270/// class's @implementation is seen.
1271void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
1272 ObjCInterfaceDecl *SID) {
1273 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
1274 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
1275 ObjCIvarDecl* Ivar = (*IVI);
1276 if (Ivar->isInvalidDecl())
1277 continue;
1278 if (IdentifierInfo *II = Ivar->getIdentifier()) {
1279 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
1280 if (prevIvar) {
1281 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
1282 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
1283 Ivar->setInvalidDecl();
1284 }
1285 }
1286 }
1287}
1288
Steve Naroffa56f6162007-12-18 01:30:32 +00001289// Note: For class/category implemenations, allMethods/allProperties is
1290// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001291void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00001292 Decl *ClassDecl,
1293 Decl **allMethods, unsigned allNum,
1294 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00001295 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Steve Naroffa56f6162007-12-18 01:30:32 +00001296 // FIXME: If we don't have a ClassDecl, we have an error. We should consider
1297 // always passing in a decl. If the decl has an error, isInvalidDecl()
Chris Lattner4d391482007-12-12 07:09:47 +00001298 // should be true.
1299 if (!ClassDecl)
1300 return;
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001301
Mike Stump1eb44332009-09-09 15:08:12 +00001302 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00001303 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
1304 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001305 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00001306
Ted Kremenek782f2f52010-01-07 01:20:12 +00001307 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
1308 // FIXME: This is wrong. We shouldn't be pretending that there is
1309 // an '@end' in the declaration.
1310 SourceLocation L = ClassDecl->getLocation();
1311 AtEnd.setBegin(L);
1312 AtEnd.setEnd(L);
1313 Diag(L, diag::warn_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00001314 }
1315
Steve Naroff0701bbb2009-01-08 17:28:14 +00001316 DeclContext *DC = dyn_cast<DeclContext>(ClassDecl);
Steve Naroff0701bbb2009-01-08 17:28:14 +00001317
1318 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
1319 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
1320 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
1321
Chris Lattner4d391482007-12-12 07:09:47 +00001322 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001323 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00001324 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00001325
1326 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00001327 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00001328 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001329 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001330 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001331 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001332 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001333 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001334 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001335 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001336 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001337 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001338 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001339 InsMap[Method->getSelector()] = Method;
1340 /// The following allows us to typecheck messages to "id".
1341 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001342 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001343 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001344 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00001345 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001346 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00001347 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001348 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00001349 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00001350 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00001351 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00001352 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00001353 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001354 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001355 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Chris Lattner4d391482007-12-12 07:09:47 +00001356 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001357 DC->addDecl(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00001358 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00001359 /// The following allows us to typecheck messages to "Class".
1360 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00001361 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00001362 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00001363 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001364 }
1365 }
1366 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001367 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00001368 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00001369 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00001370 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00001371 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00001372 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001373 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00001374 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00001375 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001376
Fariborz Jahanian107089f2010-01-18 18:41:16 +00001377 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00001378 CompareProperties(C, C);
Fariborz Jahanian25760612010-02-15 21:55:26 +00001379 if (C->IsClassExtension())
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +00001380 DiagnoseClassExtensionDupMethods(C, C->getClassInterface());
Chris Lattner4d391482007-12-12 07:09:47 +00001381 }
Steve Naroff09c47192009-01-09 15:36:25 +00001382 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00001383 if (CDecl->getIdentifier())
1384 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
1385 // user-defined setter/getter. It also synthesizes setter/getter methods
1386 // and adds them to the DeclContext and global method pools.
1387 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
1388 E = CDecl->prop_end();
1389 I != E; ++I)
1390 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00001391 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00001392 }
1393 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001394 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001395 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanian509d4772010-05-14 18:35:57 +00001396 if (LangOpts.ObjCNonFragileABI2)
1397 DefaultSynthesizeProperties(S, IC, IDecl);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001398 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001399 AtomicPropertySetterGetterRules(IC, IDecl);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001400
Fariborz Jahanianf914b972010-02-23 23:41:11 +00001401 if (LangOpts.ObjCNonFragileABI2)
1402 while (IDecl->getSuperClass()) {
1403 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
1404 IDecl = IDecl->getSuperClass();
1405 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00001406 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001407 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00001408 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00001409 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00001410 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00001411
Chris Lattner4d391482007-12-12 07:09:47 +00001412 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001413 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00001414 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001415 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00001416 Categories; Categories = Categories->getNextClassCategory()) {
1417 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001418 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00001419 break;
1420 }
1421 }
1422 }
1423 }
Chris Lattner682bf922009-03-29 16:50:03 +00001424 if (isInterfaceDeclKind) {
1425 // Reject invalid vardecls.
1426 for (unsigned i = 0; i != tuvNum; i++) {
1427 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
1428 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
1429 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00001430 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00001431 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00001432 }
Chris Lattner682bf922009-03-29 16:50:03 +00001433 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00001434 }
Chris Lattner4d391482007-12-12 07:09:47 +00001435}
1436
1437
1438/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
1439/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00001440static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001441CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
1442 Decl::ObjCDeclQualifier ret = Decl::OBJC_TQ_None;
1443 if (PQTVal & ObjCDeclSpec::DQ_In)
1444 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_In);
1445 if (PQTVal & ObjCDeclSpec::DQ_Inout)
1446 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Inout);
1447 if (PQTVal & ObjCDeclSpec::DQ_Out)
1448 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Out);
1449 if (PQTVal & ObjCDeclSpec::DQ_Bycopy)
1450 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Bycopy);
1451 if (PQTVal & ObjCDeclSpec::DQ_Byref)
1452 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Byref);
1453 if (PQTVal & ObjCDeclSpec::DQ_Oneway)
1454 ret = (Decl::ObjCDeclQualifier)(ret | Decl::OBJC_TQ_Oneway);
Chris Lattner4d391482007-12-12 07:09:47 +00001455
1456 return ret;
1457}
1458
Ted Kremenek422bae72010-04-18 04:59:38 +00001459static inline
Sean Huntcf807c42010-08-18 23:23:40 +00001460bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00001461 // The 'ibaction' attribute is allowed on method definitions because of
1462 // how the IBAction macro is used on both method declarations and definitions.
1463 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00001464 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
1465 if ((*i)->getKind() != attr::IBAction)
1466 return true;
1467 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00001468}
1469
John McCalld226f652010-08-21 09:40:31 +00001470Decl *Sema::ActOnMethodDeclaration(
Chris Lattner4d391482007-12-12 07:09:47 +00001471 SourceLocation MethodLoc, SourceLocation EndLoc,
John McCalld226f652010-08-21 09:40:31 +00001472 tok::TokenKind MethodType, Decl *ClassDecl,
John McCallb3d87482010-08-24 05:47:05 +00001473 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Chris Lattner4d391482007-12-12 07:09:47 +00001474 Selector Sel,
1475 // optional arguments. The number of types/arguments is obtained
1476 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00001477 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001478 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00001479 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
1480 bool isVariadic) {
Steve Naroffda323ad2008-02-29 21:48:07 +00001481 // Make sure we can establish a context for the method.
1482 if (!ClassDecl) {
1483 Diag(MethodLoc, diag::error_missing_method_context);
John McCall781472f2010-08-25 08:40:02 +00001484 getCurFunction()->LabelMap.clear();
John McCalld226f652010-08-21 09:40:31 +00001485 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00001486 }
Chris Lattner4d391482007-12-12 07:09:47 +00001487 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001489 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001490 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001491 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00001492
Steve Naroffccef3712009-02-20 22:59:16 +00001493 // Methods cannot return interface types. All ObjC objects are
1494 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00001495 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00001496 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
1497 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00001498 return 0;
Steve Naroffccef3712009-02-20 22:59:16 +00001499 }
1500 } else // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001501 resultDeclType = Context.getObjCIdType();
Mike Stump1eb44332009-09-09 15:08:12 +00001502
1503 ObjCMethodDecl* ObjCMethod =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001504 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel, resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001505 ResultTInfo,
Mike Stump1eb44332009-09-09 15:08:12 +00001506 cast<DeclContext>(ClassDecl),
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001507 MethodType == tok::minus, isVariadic,
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001508 false, false,
Mike Stump1eb44332009-09-09 15:08:12 +00001509 MethodDeclKind == tok::objc_optional ?
1510 ObjCMethodDecl::Optional :
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001511 ObjCMethodDecl::Required);
Mike Stump1eb44332009-09-09 15:08:12 +00001512
Chris Lattner0ed844b2008-04-04 06:12:32 +00001513 llvm::SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00001514
Chris Lattner7db638d2009-04-11 19:42:43 +00001515 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00001516 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00001517 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Chris Lattnere294d3f2009-04-11 18:57:04 +00001519 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00001520 ArgType = Context.getObjCIdType();
1521 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00001522 } else {
John McCall58e46772009-10-23 21:48:59 +00001523 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00001524 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001525 ArgType = adjustParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00001526 }
Mike Stump1eb44332009-09-09 15:08:12 +00001527
John McCall58e46772009-10-23 21:48:59 +00001528 ParmVarDecl* Param
1529 = ParmVarDecl::Create(Context, ObjCMethod, ArgInfo[i].NameLoc,
1530 ArgInfo[i].Name, ArgType, DI,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001531 VarDecl::None, VarDecl::None, 0);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
John McCallc12c5bb2010-05-15 11:32:37 +00001533 if (ArgType->isObjCObjectType()) {
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001534 Diag(ArgInfo[i].NameLoc,
1535 diag::err_object_cannot_be_passed_returned_by_value)
1536 << 1 << ArgType;
1537 Param->setInvalidDecl();
1538 }
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Chris Lattner0ed844b2008-04-04 06:12:32 +00001540 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00001541 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00001542
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00001543 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001544 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Chris Lattner0ed844b2008-04-04 06:12:32 +00001546 Params.push_back(Param);
1547 }
1548
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001549 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00001550 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001551 QualType ArgType = Param->getType();
1552 if (ArgType.isNull())
1553 ArgType = Context.getObjCIdType();
1554 else
1555 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
1556 ArgType = adjustParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00001557 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001558 Diag(Param->getLocation(),
1559 diag::err_object_cannot_be_passed_returned_by_value)
1560 << 1 << ArgType;
1561 Param->setInvalidDecl();
1562 }
1563 Param->setDeclContext(ObjCMethod);
Douglas Gregor59c451e2010-08-07 12:29:18 +00001564 if (Param->getDeclName())
1565 IdResolver.RemoveDecl(Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00001566 Params.push_back(Param);
1567 }
1568
Fariborz Jahanian4ecb25f2010-04-09 15:40:42 +00001569 ObjCMethod->setMethodParams(Context, Params.data(), Params.size(),
1570 Sel.getNumArgs());
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001571 ObjCMethod->setObjCDeclQualifier(
1572 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
1573 const ObjCMethodDecl *PrevMethod = 0;
Daniel Dunbar35682492008-09-26 04:12:28 +00001574
1575 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001576 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00001577
John McCall54abf7d2009-11-04 02:18:39 +00001578 const ObjCMethodDecl *InterfaceMD = 0;
1579
Mike Stump1eb44332009-09-09 15:08:12 +00001580 // For implementations (which can be very "coarse grain"), we add the
1581 // method now. This allows the AST to implement lookup methods that work
1582 // incrementally (without waiting until we parse the @end). It also allows
Chris Lattner4d391482007-12-12 07:09:47 +00001583 // us to flag multiple declaration errors as they occur.
Mike Stump1eb44332009-09-09 15:08:12 +00001584 if (ObjCImplementationDecl *ImpDecl =
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00001585 dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001586 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001587 PrevMethod = ImpDecl->getInstanceMethod(Sel);
1588 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001589 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001590 PrevMethod = ImpDecl->getClassMethod(Sel);
1591 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001592 }
John McCall54abf7d2009-11-04 02:18:39 +00001593 InterfaceMD = ImpDecl->getClassInterface()->getMethod(Sel,
1594 MethodType == tok::minus);
Sean Huntcf807c42010-08-18 23:23:40 +00001595 if (ObjCMethod->hasAttrs() &&
1596 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001597 Diag(EndLoc, diag::warn_attribute_method_def);
Mike Stump1eb44332009-09-09 15:08:12 +00001598 } else if (ObjCCategoryImplDecl *CatImpDecl =
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001599 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001600 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001601 PrevMethod = CatImpDecl->getInstanceMethod(Sel);
1602 CatImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001603 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001604 PrevMethod = CatImpDecl->getClassMethod(Sel);
1605 CatImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00001606 }
Sean Huntcf807c42010-08-18 23:23:40 +00001607 if (ObjCMethod->hasAttrs() &&
1608 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00001609 Diag(EndLoc, diag::warn_attribute_method_def);
Chris Lattner4d391482007-12-12 07:09:47 +00001610 }
1611 if (PrevMethod) {
1612 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00001613 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00001614 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001615 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00001616 }
John McCall54abf7d2009-11-04 02:18:39 +00001617
1618 // If the interface declared this method, and it was deprecated there,
1619 // mark it deprecated here.
Sean Huntcf807c42010-08-18 23:23:40 +00001620 if (InterfaceMD)
1621 if (Attr *DA = InterfaceMD->getAttr<DeprecatedAttr>())
1622 ObjCMethod->addAttr(::new (Context) DeprecatedAttr(DA->getLocation(),
1623 Context));
John McCall54abf7d2009-11-04 02:18:39 +00001624
John McCalld226f652010-08-21 09:40:31 +00001625 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00001626}
1627
Chris Lattnercc98eac2008-12-17 07:13:27 +00001628bool Sema::CheckObjCDeclScope(Decl *D) {
Douglas Gregorce356072009-01-06 23:51:29 +00001629 if (isa<TranslationUnitDecl>(CurContext->getLookupContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00001630 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Anders Carlsson15281452008-11-04 16:57:32 +00001632 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
1633 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Anders Carlsson15281452008-11-04 16:57:32 +00001635 return true;
1636}
Chris Lattnercc98eac2008-12-17 07:13:27 +00001637
Chris Lattnercc98eac2008-12-17 07:13:27 +00001638/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
1639/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00001640void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00001641 IdentifierInfo *ClassName,
John McCalld226f652010-08-21 09:40:31 +00001642 llvm::SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00001643 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00001644 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001645 if (!Class) {
1646 Diag(DeclStart, diag::err_undef_interface) << ClassName;
1647 return;
1648 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00001649 if (LangOpts.ObjCNonFragileABI) {
1650 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
1651 return;
1652 }
Mike Stump1eb44332009-09-09 15:08:12 +00001653
Chris Lattnercc98eac2008-12-17 07:13:27 +00001654 // Collect the instance variables
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001655 llvm::SmallVector<ObjCIvarDecl*, 32> Ivars;
1656 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001657 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001658 for (unsigned i = 0; i < Ivars.size(); i++) {
1659 FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00001660 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001661 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record, ID->getLocation(),
1662 ID->getIdentifier(), ID->getType(),
1663 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00001664 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00001665 }
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Chris Lattnercc98eac2008-12-17 07:13:27 +00001667 // Introduce all of these fields into the appropriate scope.
John McCalld226f652010-08-21 09:40:31 +00001668 for (llvm::SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00001669 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00001670 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001671 if (getLangOptions().CPlusPlus)
1672 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00001673 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001674 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00001675 }
1676}
1677
Douglas Gregor160b5632010-04-26 17:32:49 +00001678/// \brief Build a type-check a new Objective-C exception variable declaration.
1679VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo,
1680 QualType T,
1681 IdentifierInfo *Name,
1682 SourceLocation NameLoc,
1683 bool Invalid) {
1684 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
1685 // duration shall not be qualified by an address-space qualifier."
1686 // Since all parameters have automatic store duration, they can not have
1687 // an address space.
1688 if (T.getAddressSpace() != 0) {
1689 Diag(NameLoc, diag::err_arg_with_address_space);
1690 Invalid = true;
1691 }
1692
1693 // An @catch parameter must be an unqualified object pointer type;
1694 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
1695 if (Invalid) {
1696 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00001697 } else if (T->isDependentType()) {
1698 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00001699 } else if (!T->isObjCObjectPointerType()) {
1700 Invalid = true;
1701 Diag(NameLoc ,diag::err_catch_param_not_objc_type);
1702 } else if (T->isObjCQualifiedIdType()) {
1703 Invalid = true;
1704 Diag(NameLoc, diag::err_illegal_qualifiers_on_catch_parm);
1705 }
1706
1707 VarDecl *New = VarDecl::Create(Context, CurContext, NameLoc, Name, T, TInfo,
Douglas Gregor324b54d2010-05-03 18:51:14 +00001708 VarDecl::None, VarDecl::None);
1709 New->setExceptionVariable(true);
1710
Douglas Gregor160b5632010-04-26 17:32:49 +00001711 if (Invalid)
1712 New->setInvalidDecl();
1713 return New;
1714}
1715
John McCalld226f652010-08-21 09:40:31 +00001716Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00001717 const DeclSpec &DS = D.getDeclSpec();
1718
1719 // We allow the "register" storage class on exception variables because
1720 // GCC did, but we drop it completely. Any other storage class is an error.
1721 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
1722 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
1723 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
1724 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
1725 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
1726 << DS.getStorageClassSpec();
1727 }
1728 if (D.getDeclSpec().isThreadSpecified())
1729 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
1730 D.getMutableDeclSpec().ClearStorageClassSpecs();
1731
1732 DiagnoseFunctionSpecifiers(D);
1733
1734 // Check that there are no default arguments inside the type of this
1735 // exception object (C++ only).
1736 if (getLangOptions().CPlusPlus)
1737 CheckExtraCXXDefaultArguments(D);
1738
Douglas Gregor160b5632010-04-26 17:32:49 +00001739 TagDecl *OwnedDecl = 0;
John McCallbf1a0282010-06-04 23:28:52 +00001740 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S, &OwnedDecl);
1741 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00001742
1743 if (getLangOptions().CPlusPlus && OwnedDecl && OwnedDecl->isDefinition()) {
1744 // Objective-C++: Types shall not be defined in exception types.
1745 Diag(OwnedDecl->getLocation(), diag::err_type_defined_in_param_type)
1746 << Context.getTypeDeclType(OwnedDecl);
1747 }
1748
1749 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType, D.getIdentifier(),
1750 D.getIdentifierLoc(),
1751 D.isInvalidType());
1752
1753 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
1754 if (D.getCXXScopeSpec().isSet()) {
1755 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
1756 << D.getCXXScopeSpec().getRange();
1757 New->setInvalidDecl();
1758 }
1759
1760 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00001761 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00001762 if (D.getIdentifier())
1763 IdResolver.AddDecl(New);
1764
1765 ProcessDeclAttributes(S, New, D);
1766
1767 if (New->hasAttr<BlocksAttr>())
1768 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00001769 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00001770}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001771
1772/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001773/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001774void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001775 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001776 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
1777 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001778 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00001779 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00001780 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00001781 }
1782}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00001783
1784void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
1785 CXXBaseOrMemberInitializer ** initializers,
1786 unsigned numInitializers) {
1787 if (numInitializers > 0) {
1788 NumIvarInitializers = numInitializers;
1789 CXXBaseOrMemberInitializer **ivarInitializers =
1790 new (C) CXXBaseOrMemberInitializer*[NumIvarInitializers];
1791 memcpy(ivarInitializers, initializers,
1792 numInitializers * sizeof(CXXBaseOrMemberInitializer*));
1793 IvarInitializers = ivarInitializers;
1794 }
1795}
1796
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001797void Sema::DiagnoseUseOfUnimplementedSelectors() {
1798 if (ReferencedSelectors.empty())
1799 return;
1800 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
1801 ReferencedSelectors.begin(),
1802 E = ReferencedSelectors.end(); S != E; ++S) {
1803 Selector Sel = (*S).first;
1804 if (!LookupImplementedMethodInGlobalPool(Sel))
1805 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
1806 }
1807 return;
1808}