blob: 9b492600ca1cedacac2da9842447e67454ab66ff [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
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#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"
John McCallf85e1932011-06-15 23:02:42 +000019#include "clang/AST/ASTConsumer.h"
Steve Naroffca331292009-03-03 14:49:36 +000020#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/AST/ExprObjC.h"
Chris Lattner4d391482007-12-12 07:09:47 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +000024#include "clang/AST/ASTMutationListener.h"
John McCallf85e1932011-06-15 23:02:42 +000025#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000027#include "llvm/ADT/DenseSet.h"
28
Chris Lattner4d391482007-12-12 07:09:47 +000029using namespace clang;
30
John McCallf85e1932011-06-15 23:02:42 +000031/// Check whether the given method, which must be in the 'init'
32/// family, is a valid member of that family.
33///
34/// \param receiverTypeIfCall - if null, check this as if declaring it;
35/// if non-null, check this as if making a call to it with the given
36/// receiver type
37///
38/// \return true to indicate that there was an error and appropriate
39/// actions were taken
40bool Sema::checkInitMethod(ObjCMethodDecl *method,
41 QualType receiverTypeIfCall) {
42 if (method->isInvalidDecl()) return true;
43
44 // This castAs is safe: methods that don't return an object
45 // pointer won't be inferred as inits and will reject an explicit
46 // objc_method_family(init).
47
48 // We ignore protocols here. Should we? What about Class?
49
50 const ObjCObjectType *result = method->getResultType()
51 ->castAs<ObjCObjectPointerType>()->getObjectType();
52
53 if (result->isObjCId()) {
54 return false;
55 } else if (result->isObjCClass()) {
56 // fall through: always an error
57 } else {
58 ObjCInterfaceDecl *resultClass = result->getInterface();
59 assert(resultClass && "unexpected object type!");
60
61 // It's okay for the result type to still be a forward declaration
62 // if we're checking an interface declaration.
Douglas Gregor7723fec2011-12-15 20:29:51 +000063 if (!resultClass->hasDefinition()) {
John McCallf85e1932011-06-15 23:02:42 +000064 if (receiverTypeIfCall.isNull() &&
65 !isa<ObjCImplementationDecl>(method->getDeclContext()))
66 return false;
67
68 // Otherwise, we try to compare class types.
69 } else {
70 // If this method was declared in a protocol, we can't check
71 // anything unless we have a receiver type that's an interface.
72 const ObjCInterfaceDecl *receiverClass = 0;
73 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
74 if (receiverTypeIfCall.isNull())
75 return false;
76
77 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
78 ->getInterfaceDecl();
79
80 // This can be null for calls to e.g. id<Foo>.
81 if (!receiverClass) return false;
82 } else {
83 receiverClass = method->getClassInterface();
84 assert(receiverClass && "method not associated with a class!");
85 }
86
87 // If either class is a subclass of the other, it's fine.
88 if (receiverClass->isSuperClassOf(resultClass) ||
89 resultClass->isSuperClassOf(receiverClass))
90 return false;
91 }
92 }
93
94 SourceLocation loc = method->getLocation();
95
96 // If we're in a system header, and this is not a call, just make
97 // the method unusable.
98 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
99 method->addAttr(new (Context) UnavailableAttr(loc, Context,
100 "init method returns a type unrelated to its receiver type"));
101 return true;
102 }
103
104 // Otherwise, it's an error.
105 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
106 method->setInvalidDecl();
107 return true;
108}
109
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000110void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000111 const ObjCMethodDecl *Overridden,
112 bool IsImplementation) {
113 if (Overridden->hasRelatedResultType() &&
114 !NewMethod->hasRelatedResultType()) {
115 // This can only happen when the method follows a naming convention that
116 // implies a related result type, and the original (overridden) method has
117 // a suitable return type, but the new (overriding) method does not have
118 // a suitable return type.
119 QualType ResultType = NewMethod->getResultType();
120 SourceRange ResultTypeRange;
121 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000122 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000123 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
124
125 // Figure out which class this method is part of, if any.
126 ObjCInterfaceDecl *CurrentClass
127 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
128 if (!CurrentClass) {
129 DeclContext *DC = NewMethod->getDeclContext();
130 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
131 CurrentClass = Cat->getClassInterface();
132 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
133 CurrentClass = Impl->getClassInterface();
134 else if (ObjCCategoryImplDecl *CatImpl
135 = dyn_cast<ObjCCategoryImplDecl>(DC))
136 CurrentClass = CatImpl->getClassInterface();
137 }
138
139 if (CurrentClass) {
140 Diag(NewMethod->getLocation(),
141 diag::warn_related_result_type_compatibility_class)
142 << Context.getObjCInterfaceType(CurrentClass)
143 << ResultType
144 << ResultTypeRange;
145 } else {
146 Diag(NewMethod->getLocation(),
147 diag::warn_related_result_type_compatibility_protocol)
148 << ResultType
149 << ResultTypeRange;
150 }
151
Douglas Gregore97179c2011-09-08 01:46:34 +0000152 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
153 Diag(Overridden->getLocation(),
154 diag::note_related_result_type_overridden_family)
155 << Family;
156 else
157 Diag(Overridden->getLocation(),
158 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000159 }
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000160 if (getLangOptions().ObjCAutoRefCount) {
161 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
162 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
163 Diag(NewMethod->getLocation(),
164 diag::err_nsreturns_retained_attribute_mismatch) << 1;
165 Diag(Overridden->getLocation(), diag::note_previous_decl)
166 << "method";
167 }
168 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
169 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
170 Diag(NewMethod->getLocation(),
171 diag::err_nsreturns_retained_attribute_mismatch) << 0;
172 Diag(Overridden->getLocation(), diag::note_previous_decl)
173 << "method";
174 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000175 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin();
176 for (ObjCMethodDecl::param_iterator
177 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000178 ni != ne; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000179 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000180 ParmVarDecl *newDecl = (*ni);
181 if (newDecl->hasAttr<NSConsumedAttr>() !=
182 oldDecl->hasAttr<NSConsumedAttr>()) {
183 Diag(newDecl->getLocation(),
184 diag::err_nsconsumed_attribute_mismatch);
185 Diag(oldDecl->getLocation(), diag::note_previous_decl)
186 << "parameter";
187 }
188 }
189 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000190}
191
John McCallf85e1932011-06-15 23:02:42 +0000192/// \brief Check a method declaration for compatibility with the Objective-C
193/// ARC conventions.
194static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
195 ObjCMethodFamily family = method->getMethodFamily();
196 switch (family) {
197 case OMF_None:
198 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000199 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000200 case OMF_retain:
201 case OMF_release:
202 case OMF_autorelease:
203 case OMF_retainCount:
204 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000205 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000206 return false;
207
208 case OMF_init:
209 // If the method doesn't obey the init rules, don't bother annotating it.
210 if (S.checkInitMethod(method, QualType()))
211 return true;
212
213 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
214 S.Context));
215
216 // Don't add a second copy of this attribute, but otherwise don't
217 // let it be suppressed.
218 if (method->hasAttr<NSReturnsRetainedAttr>())
219 return false;
220 break;
221
222 case OMF_alloc:
223 case OMF_copy:
224 case OMF_mutableCopy:
225 case OMF_new:
226 if (method->hasAttr<NSReturnsRetainedAttr>() ||
227 method->hasAttr<NSReturnsNotRetainedAttr>() ||
228 method->hasAttr<NSReturnsAutoreleasedAttr>())
229 return false;
230 break;
231 }
232
233 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
234 S.Context));
235 return false;
236}
237
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000238static void DiagnoseObjCImplementedDeprecations(Sema &S,
239 NamedDecl *ND,
240 SourceLocation ImplLoc,
241 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000242 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000243 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000244 if (select == 0)
Ted Kremenek3306ec12012-02-27 22:55:11 +0000245 S.Diag(ND->getLocation(), diag::note_method_declared_at)
246 << ND->getDeclName();
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000247 else
248 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
249 }
250}
251
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000252/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
253/// pool.
254void Sema::AddAnyMethodToGlobalPool(Decl *D) {
255 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
256
257 // If we don't have a valid method decl, simply return.
258 if (!MDecl)
259 return;
260 if (MDecl->isInstanceMethod())
261 AddInstanceMethodToGlobalPool(MDecl, true);
262 else
263 AddFactoryMethodToGlobalPool(MDecl, true);
264}
265
Steve Naroffebf64432009-02-28 16:59:13 +0000266/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000267/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000268void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000269 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000270 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Steve Naroff394f3f42008-07-25 17:57:26 +0000272 // If we don't have a valid method decl, simply return.
273 if (!MDecl)
274 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000275
Chris Lattner4d391482007-12-12 07:09:47 +0000276 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000277 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000278 PushFunctionScope();
279
Chris Lattner4d391482007-12-12 07:09:47 +0000280 // Create Decl objects for each parameter, entrring them in the scope for
281 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000282
283 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000284 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000285
Daniel Dunbar451318c2008-08-26 06:07:48 +0000286 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
287 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000288
Chris Lattner8123a952008-04-10 02:22:51 +0000289 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000290 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000291 E = MDecl->param_end(); PI != E; ++PI) {
292 ParmVarDecl *Param = (*PI);
293 if (!Param->isInvalidDecl() &&
294 RequireCompleteType(Param->getLocation(), Param->getType(),
295 diag::err_typecheck_decl_incomplete_type))
296 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000297 if ((*PI)->getIdentifier())
298 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000299 }
John McCallf85e1932011-06-15 23:02:42 +0000300
301 // In ARC, disallow definition of retain/release/autorelease/retainCount
302 if (getLangOptions().ObjCAutoRefCount) {
303 switch (MDecl->getMethodFamily()) {
304 case OMF_retain:
305 case OMF_retainCount:
306 case OMF_release:
307 case OMF_autorelease:
308 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
309 << MDecl->getSelector();
310 break;
311
312 case OMF_None:
313 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000314 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000315 case OMF_alloc:
316 case OMF_init:
317 case OMF_mutableCopy:
318 case OMF_copy:
319 case OMF_new:
320 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000321 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000322 break;
323 }
324 }
325
Nico Weber9a1ecf02011-08-22 17:25:57 +0000326 // Warn on deprecated methods under -Wdeprecated-implementations,
327 // and prepare for warning on missing super calls.
328 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000329 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000330 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000331 DiagnoseObjCImplementedDeprecations(*this,
332 dyn_cast<NamedDecl>(IMD),
333 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000334
Nico Weber80cb6e62011-08-28 22:35:17 +0000335 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000336 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
337 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
338 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000339 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000340 ObjCShouldCallSuperDealloc =
Ted Kremenek8cd8de42011-09-28 19:32:29 +0000341 !(Context.getLangOptions().ObjCAutoRefCount ||
342 Context.getLangOptions().getGC() == LangOptions::GCOnly) &&
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000343 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000344 ObjCShouldCallSuperFinalize =
Ted Kremenek8cd8de42011-09-28 19:32:29 +0000345 Context.getLangOptions().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000346 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000347 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000348 }
Chris Lattner4d391482007-12-12 07:09:47 +0000349}
350
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000351namespace {
352
353// Callback to only accept typo corrections that are Objective-C classes.
354// If an ObjCInterfaceDecl* is given to the constructor, then the validation
355// function will reject corrections to that class.
356class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
357 public:
358 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
359 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
360 : CurrentIDecl(IDecl) {}
361
362 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
363 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
364 return ID && !declaresSameEntity(ID, CurrentIDecl);
365 }
366
367 private:
368 ObjCInterfaceDecl *CurrentIDecl;
369};
370
371}
372
John McCalld226f652010-08-21 09:40:31 +0000373Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000374ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
375 IdentifierInfo *ClassName, SourceLocation ClassLoc,
376 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000377 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000378 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000379 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000380 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000381
Chris Lattner4d391482007-12-12 07:09:47 +0000382 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000383 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000384 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000385
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000386 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000387 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000388 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Douglas Gregor7723fec2011-12-15 20:29:51 +0000391 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000392 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000393 ObjCInterfaceDecl *IDecl
394 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000395 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000396
Douglas Gregor7723fec2011-12-15 20:29:51 +0000397 if (PrevIDecl) {
398 // Class already seen. Was it a definition?
399 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
400 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
401 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000402 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000403 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000404 }
Chris Lattner4d391482007-12-12 07:09:47 +0000405 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000406
407 if (AttrList)
408 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
409 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000410
Douglas Gregor7723fec2011-12-15 20:29:51 +0000411 // Start the definition of this class. If we're in a redefinition case, there
412 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000413 if (!IDecl->hasDefinition())
414 IDecl->startDefinition();
415
Chris Lattner4d391482007-12-12 07:09:47 +0000416 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000417 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000418 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
419 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000420
421 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000422 // Try to correct for a typo in the superclass name without correcting
423 // to the class we're defining.
424 ObjCInterfaceValidatorCCC Validator(IDecl);
425 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000426 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000427 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000428 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
429 Diag(SuperLoc, diag::err_undef_superclass_suggest)
430 << SuperName << ClassName << PrevDecl->getDeclName();
431 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
432 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000433 }
434 }
435
Douglas Gregor60ef3082011-12-15 00:29:59 +0000436 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000437 Diag(SuperLoc, diag::err_recursive_superclass)
438 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000439 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000440 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000441 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000442 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000443
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000444 // Diagnose classes that inherit from deprecated classes.
445 if (SuperClassDecl)
446 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000448 if (PrevDecl && SuperClassDecl == 0) {
449 // The previous declaration was not a class decl. Check if we have a
450 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000451 if (const TypedefNameDecl *TDecl =
452 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000453 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000454 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000455 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
456 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000457 }
458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000460 // This handles the following case:
461 //
462 // typedef int SuperClass;
463 // @interface MyClass : SuperClass {} @end
464 //
465 if (!SuperClassDecl) {
466 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
467 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000468 }
469 }
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Richard Smith162e1c12011-04-15 14:24:37 +0000471 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000472 if (!SuperClassDecl)
473 Diag(SuperLoc, diag::err_undef_superclass)
474 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000475 else if (RequireCompleteType(SuperLoc,
476 Context.getObjCInterfaceType(SuperClassDecl),
477 PDiag(diag::err_forward_superclass)
478 << SuperClassDecl->getDeclName()
479 << ClassName
480 << SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000481 SuperClassDecl = 0;
482 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000483 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000484 IDecl->setSuperClass(SuperClassDecl);
485 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000486 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000487 }
Chris Lattner4d391482007-12-12 07:09:47 +0000488 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000489 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000490 }
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Sebastian Redl0b17c612010-08-13 00:28:03 +0000492 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000493 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000494 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000495 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000496 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000497 }
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Anders Carlsson15281452008-11-04 16:57:32 +0000499 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000500 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000501}
502
503/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000504/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000505Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
506 IdentifierInfo *AliasName,
507 SourceLocation AliasLocation,
508 IdentifierInfo *ClassName,
509 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000510 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000511 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000512 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000513 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000514 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000515 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000516 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000517 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000518 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000519 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000520 }
521 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000522 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000523 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000524 if (const TypedefNameDecl *TDecl =
525 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000526 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000527 if (T->isObjCObjectType()) {
528 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000529 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000530 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000531 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000532 }
533 }
534 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000535 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
536 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000537 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000538 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000539 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000540 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000541 }
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000543 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000544 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000545 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Anders Carlsson15281452008-11-04 16:57:32 +0000547 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000548 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000549
John McCalld226f652010-08-21 09:40:31 +0000550 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000551}
552
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000553bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000554 IdentifierInfo *PName,
555 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000556 const ObjCList<ObjCProtocolDecl> &PList) {
557
558 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000559 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
560 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000561 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
562 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000563 if (PDecl->getIdentifier() == PName) {
564 Diag(Ploc, diag::err_protocol_has_circular_dependency);
565 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000566 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000567 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000568
569 if (!PDecl->hasDefinition())
570 continue;
571
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000572 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
573 PDecl->getLocation(), PDecl->getReferencedProtocols()))
574 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000575 }
576 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000577 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000578}
579
John McCalld226f652010-08-21 09:40:31 +0000580Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000581Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
582 IdentifierInfo *ProtocolName,
583 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000584 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000585 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000586 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000587 SourceLocation EndProtoLoc,
588 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000589 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000590 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000591 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000592 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
593 ForRedeclaration);
594 ObjCProtocolDecl *PDecl = 0;
595 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
596 // If we already have a definition, complain.
597 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
598 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Douglas Gregor27c6da22012-01-01 20:30:41 +0000600 // Create a new protocol that is completely distinct from previous
601 // declarations, and do not make this protocol available for name lookup.
602 // That way, we'll end up completely ignoring the duplicate.
603 // FIXME: Can we turn this into an error?
604 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
605 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000606 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000607 PDecl->startDefinition();
608 } else {
609 if (PrevDecl) {
610 // Check for circular dependencies among protocol declarations. This can
611 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000612 ObjCList<ObjCProtocolDecl> PList;
613 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
614 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000615 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000616 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000617
618 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000619 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000620 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000621 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000622
Douglas Gregor6e378de2009-04-23 23:18:26 +0000623 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000624 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000625 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000626
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000627 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000628 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000629
630 // Merge attributes from previous declarations.
631 if (PrevDecl)
632 mergeDeclAttributes(PDecl, PrevDecl);
633
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000634 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000635 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000636 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
637 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000638 }
Mike Stump1eb44332009-09-09 15:08:12 +0000639
640 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000641 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000642}
643
644/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000645/// issues an error if they are not declared. It returns list of
646/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000647void
Chris Lattnere13b9592008-07-26 04:03:38 +0000648Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000649 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000650 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000651 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000652 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000653 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
654 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000655 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000656 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000657 TypoCorrection Corrected = CorrectTypo(
658 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000659 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000660 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000661 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000662 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000663 Diag(PDecl->getLocation(), diag::note_previous_decl)
664 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000665 }
666 }
667
668 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000669 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000670 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000671 continue;
672 }
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000674 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000675
676 // If this is a forward declaration and we are supposed to warn in this
677 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000678 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000679 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000680 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000681 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000682 }
683}
684
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000685/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000686/// a class method in its extension.
687///
Mike Stump1eb44332009-09-09 15:08:12 +0000688void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000689 ObjCInterfaceDecl *ID) {
690 if (!ID)
691 return; // Possibly due to previous error
692
693 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000694 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
695 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000696 ObjCMethodDecl *MD = *i;
697 MethodMap[MD->getSelector()] = MD;
698 }
699
700 if (MethodMap.empty())
701 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000702 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
703 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000704 ObjCMethodDecl *Method = *i;
705 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
706 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
707 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
708 << Method->getDeclName();
709 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
710 }
711 }
712}
713
Chris Lattner58fe03b2009-04-12 08:43:13 +0000714/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000715Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000716Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000717 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000718 unsigned NumElts,
719 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000720 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000721 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000722 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000723 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
724 ForRedeclaration);
725 ObjCProtocolDecl *PDecl
726 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
727 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000728 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000729
730 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000731 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000732
Douglas Gregor3937f872012-01-01 20:33:24 +0000733 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000734 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000735
736 if (PrevDecl)
737 mergeDeclAttributes(PDecl, PrevDecl);
738
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000739 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000742 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000743}
744
John McCalld226f652010-08-21 09:40:31 +0000745Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000746ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
747 IdentifierInfo *ClassName, SourceLocation ClassLoc,
748 IdentifierInfo *CategoryName,
749 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000750 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000751 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000752 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000753 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000754 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000755 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000756
757 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000758
759 if (!IDecl
760 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
761 PDiag(diag::err_category_forward_interface)
762 << (CategoryName == 0))) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000763 // Create an invalid ObjCCategoryDecl to serve as context for
764 // the enclosing method declarations. We mark the decl invalid
765 // to make it clear that this isn't a valid AST.
766 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000767 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000768 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000769
770 if (!IDecl)
771 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000772 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000773 }
774
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000775 if (!CategoryName && IDecl->getImplementation()) {
776 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
777 Diag(IDecl->getImplementation()->getLocation(),
778 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000779 }
780
Fariborz Jahanian25760612010-02-15 21:55:26 +0000781 if (CategoryName) {
782 /// Check for duplicate interface declaration for this category
783 ObjCCategoryDecl *CDeclChain;
784 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
785 CDeclChain = CDeclChain->getNextClassCategory()) {
786 if (CDeclChain->getIdentifier() == CategoryName) {
787 // Class extensions can be declared multiple times.
788 Diag(CategoryLoc, diag::warn_dup_category_def)
789 << ClassName << CategoryName;
790 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
791 break;
792 }
Chris Lattner70f19542009-02-16 21:26:43 +0000793 }
794 }
Chris Lattner70f19542009-02-16 21:26:43 +0000795
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000796 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
797 ClassLoc, CategoryLoc, CategoryName, IDecl);
798 // FIXME: PushOnScopeChains?
799 CurContext->addDecl(CDecl);
800
Chris Lattner4d391482007-12-12 07:09:47 +0000801 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000802 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000803 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000804 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000805 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000806 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000807 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000808 }
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Anders Carlsson15281452008-11-04 16:57:32 +0000810 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000811 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000812}
813
814/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000815/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000816/// object.
John McCalld226f652010-08-21 09:40:31 +0000817Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000818 SourceLocation AtCatImplLoc,
819 IdentifierInfo *ClassName, SourceLocation ClassLoc,
820 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000821 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000822 ObjCCategoryDecl *CatIDecl = 0;
823 if (IDecl) {
824 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
825 if (!CatIDecl) {
826 // Category @implementation with no corresponding @interface.
827 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000828 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
829 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000830 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000831 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000832 }
833 }
834
Mike Stump1eb44332009-09-09 15:08:12 +0000835 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000836 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000837 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000838 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000839 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000840 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000841 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000842 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
843 diag::err_undef_interface)) {
844 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000845 }
Chris Lattner4d391482007-12-12 07:09:47 +0000846
Douglas Gregord0434102009-01-09 00:49:46 +0000847 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000848 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000849
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000850 // If the interface is deprecated/unavailable, warn/error about it.
851 if (IDecl)
852 DiagnoseUseOfDecl(IDecl, ClassLoc);
853
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000854 /// Check that CatName, category name, is not used in another implementation.
855 if (CatIDecl) {
856 if (CatIDecl->getImplementation()) {
857 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
858 << CatName;
859 Diag(CatIDecl->getImplementation()->getLocation(),
860 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000861 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000862 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000863 // Warn on implementating category of deprecated class under
864 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000865 DiagnoseObjCImplementedDeprecations(*this,
866 dyn_cast<NamedDecl>(IDecl),
867 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000868 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000869 }
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Anders Carlsson15281452008-11-04 16:57:32 +0000871 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000872 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000873}
874
John McCalld226f652010-08-21 09:40:31 +0000875Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000876 SourceLocation AtClassImplLoc,
877 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000878 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000879 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000880 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000881 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000882 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000883 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
884 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000885 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000886 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000887 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000888 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000889 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
890 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000891 } else {
892 // We did not find anything with the name ClassName; try to correct for
893 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000894 ObjCInterfaceValidatorCCC Validator;
895 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000896 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000897 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000898 // Suggest the (potentially) correct interface name. However, put the
899 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000900 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000901 // provide a code-modification hint or use the typo name for recovery,
902 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000903 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000904 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000905 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000906 << ClassName << CorrectedName;
907 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
908 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000909 IDecl = 0;
910 } else {
911 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
912 }
Chris Lattner4d391482007-12-12 07:09:47 +0000913 }
Mike Stump1eb44332009-09-09 15:08:12 +0000914
Chris Lattner4d391482007-12-12 07:09:47 +0000915 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000916 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000917 if (SuperClassname) {
918 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000919 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
920 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000921 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000922 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
923 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000924 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000925 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000926 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000927 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000928 Diag(SuperClassLoc, diag::err_undef_superclass)
929 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000930 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000931 // This implementation and its interface do not have the same
932 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000933 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000934 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000935 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000936 }
937 }
938 }
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Chris Lattner4d391482007-12-12 07:09:47 +0000940 if (!IDecl) {
941 // Legacy case of @implementation with no corresponding @interface.
942 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000943
Mike Stump390b4cc2009-05-16 07:39:55 +0000944 // FIXME: Do we support attributes on the @implementation? If so we should
945 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000946 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000947 ClassName, /*PrevDecl=*/0, ClassLoc,
948 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000949 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000950 if (SDecl) {
951 IDecl->setSuperClass(SDecl);
952 IDecl->setSuperClassLoc(SuperClassLoc);
953 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
954 } else {
955 IDecl->setEndOfDefinitionLoc(ClassLoc);
956 }
957
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000958 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000959 } else {
960 // Mark the interface as being completed, even if it was just as
961 // @class ....;
962 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000963 if (!IDecl->hasDefinition())
964 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000965 }
Mike Stump1eb44332009-09-09 15:08:12 +0000966
967 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000968 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
969 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Anders Carlsson15281452008-11-04 16:57:32 +0000971 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000972 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Chris Lattner4d391482007-12-12 07:09:47 +0000974 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000975 if (IDecl->getImplementation()) {
976 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000977 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000978 Diag(IDecl->getImplementation()->getLocation(),
979 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000980 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000981 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000982 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000983 // Warn on implementating deprecated class under
984 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000985 DiagnoseObjCImplementedDeprecations(*this,
986 dyn_cast<NamedDecl>(IDecl),
987 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000988 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000989 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000990}
991
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +0000992Sema::DeclGroupPtrTy
993Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
994 SmallVector<Decl *, 64> DeclsInGroup;
995 DeclsInGroup.reserve(Decls.size() + 1);
996
997 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
998 Decl *Dcl = Decls[i];
999 if (!Dcl)
1000 continue;
1001 if (Dcl->getDeclContext()->isFileContext())
1002 Dcl->setTopLevelDeclInObjCContainer();
1003 DeclsInGroup.push_back(Dcl);
1004 }
1005
1006 DeclsInGroup.push_back(ObjCImpDecl);
1007
1008 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1009}
1010
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001011void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1012 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001013 SourceLocation RBrace) {
1014 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001015 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001016 if (!IDecl)
1017 return;
1018 /// Check case of non-existing @interface decl.
1019 /// (legacy objective-c @implementation decl without an @interface decl).
1020 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001021 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001022 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001023 // Add ivar's to class's DeclContext.
1024 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001025 ivars[i]->setLexicalDeclContext(ImpDecl);
1026 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001027 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001028 }
1029
Chris Lattner4d391482007-12-12 07:09:47 +00001030 return;
1031 }
1032 // If implementation has empty ivar list, just return.
1033 if (numIvars == 0)
1034 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Chris Lattner4d391482007-12-12 07:09:47 +00001036 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001037 if (LangOpts.ObjCNonFragileABI2) {
1038 if (ImpDecl->getSuperClass())
1039 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1040 for (unsigned i = 0; i < numIvars; i++) {
1041 ObjCIvarDecl* ImplIvar = ivars[i];
1042 if (const ObjCIvarDecl *ClsIvar =
1043 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1044 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1045 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1046 continue;
1047 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001048 // Instance ivar to Implementation's DeclContext.
1049 ImplIvar->setLexicalDeclContext(ImpDecl);
1050 IDecl->makeDeclVisibleInContext(ImplIvar, false);
1051 ImpDecl->addDecl(ImplIvar);
1052 }
1053 return;
1054 }
Chris Lattner4d391482007-12-12 07:09:47 +00001055 // Check interface's Ivar list against those in the implementation.
1056 // names and types must match.
1057 //
Chris Lattner4d391482007-12-12 07:09:47 +00001058 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001059 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001060 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1061 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001062 ObjCIvarDecl* ImplIvar = ivars[j++];
1063 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001064 assert (ImplIvar && "missing implementation ivar");
1065 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Steve Naroffca331292009-03-03 14:49:36 +00001067 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001068 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001069 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001070 << ImplIvar->getIdentifier()
1071 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001072 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001073 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1074 ImplIvar->getBitWidthValue(Context) !=
1075 ClsIvar->getBitWidthValue(Context)) {
1076 Diag(ImplIvar->getBitWidth()->getLocStart(),
1077 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1078 Diag(ClsIvar->getBitWidth()->getLocStart(),
1079 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001080 }
Steve Naroffca331292009-03-03 14:49:36 +00001081 // Make sure the names are identical.
1082 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001083 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001084 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001085 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001086 }
1087 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001088 }
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Chris Lattner609e4c72007-12-12 18:11:49 +00001090 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001091 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001092 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001093 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001094}
1095
Steve Naroff3c2eb662008-02-10 21:38:56 +00001096void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001097 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001098 // No point warning no definition of method which is 'unavailable'.
1099 if (method->hasAttr<UnavailableAttr>())
1100 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001101 if (!IncompleteImpl) {
1102 Diag(ImpLoc, diag::warn_incomplete_impl);
1103 IncompleteImpl = true;
1104 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001105 if (DiagID == diag::warn_unimplemented_protocol_method)
1106 Diag(ImpLoc, DiagID) << method->getDeclName();
1107 else
1108 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001109}
1110
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001111/// Determines if type B can be substituted for type A. Returns true if we can
1112/// guarantee that anything that the user will do to an object of type A can
1113/// also be done to an object of type B. This is trivially true if the two
1114/// types are the same, or if B is a subclass of A. It becomes more complex
1115/// in cases where protocols are involved.
1116///
1117/// Object types in Objective-C describe the minimum requirements for an
1118/// object, rather than providing a complete description of a type. For
1119/// example, if A is a subclass of B, then B* may refer to an instance of A.
1120/// The principle of substitutability means that we may use an instance of A
1121/// anywhere that we may use an instance of B - it will implement all of the
1122/// ivars of B and all of the methods of B.
1123///
1124/// This substitutability is important when type checking methods, because
1125/// the implementation may have stricter type definitions than the interface.
1126/// The interface specifies minimum requirements, but the implementation may
1127/// have more accurate ones. For example, a method may privately accept
1128/// instances of B, but only publish that it accepts instances of A. Any
1129/// object passed to it will be type checked against B, and so will implicitly
1130/// by a valid A*. Similarly, a method may return a subclass of the class that
1131/// it is declared as returning.
1132///
1133/// This is most important when considering subclassing. A method in a
1134/// subclass must accept any object as an argument that its superclass's
1135/// implementation accepts. It may, however, accept a more general type
1136/// without breaking substitutability (i.e. you can still use the subclass
1137/// anywhere that you can use the superclass, but not vice versa). The
1138/// converse requirement applies to return types: the return type for a
1139/// subclass method must be a valid object of the kind that the superclass
1140/// advertises, but it may be specified more accurately. This avoids the need
1141/// for explicit down-casting by callers.
1142///
1143/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001144static bool isObjCTypeSubstitutable(ASTContext &Context,
1145 const ObjCObjectPointerType *A,
1146 const ObjCObjectPointerType *B,
1147 bool rejectId) {
1148 // Reject a protocol-unqualified id.
1149 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001150
1151 // If B is a qualified id, then A must also be a qualified id and it must
1152 // implement all of the protocols in B. It may not be a qualified class.
1153 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1154 // stricter definition so it is not substitutable for id<A>.
1155 if (B->isObjCQualifiedIdType()) {
1156 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001157 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1158 QualType(B,0),
1159 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001160 }
1161
1162 /*
1163 // id is a special type that bypasses type checking completely. We want a
1164 // warning when it is used in one place but not another.
1165 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1166
1167
1168 // If B is a qualified id, then A must also be a qualified id (which it isn't
1169 // if we've got this far)
1170 if (B->isObjCQualifiedIdType()) return false;
1171 */
1172
1173 // Now we know that A and B are (potentially-qualified) class types. The
1174 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001175 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001176}
1177
John McCall10302c02010-10-28 02:34:38 +00001178static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1179 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1180}
1181
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001182static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001183 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001184 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001185 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001186 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001187 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001188 if (IsProtocolMethodDecl &&
1189 (MethodDecl->getObjCDeclQualifier() !=
1190 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001191 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001192 S.Diag(MethodImpl->getLocation(),
1193 (IsOverridingMode ?
1194 diag::warn_conflicting_overriding_ret_type_modifiers
1195 : diag::warn_conflicting_ret_type_modifiers))
1196 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001197 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1198 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1199 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1200 }
1201 else
1202 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001203 }
1204
John McCall10302c02010-10-28 02:34:38 +00001205 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001206 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001207 return true;
1208 if (!Warn)
1209 return false;
John McCall10302c02010-10-28 02:34:38 +00001210
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001211 unsigned DiagID =
1212 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1213 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001214
1215 // Mismatches between ObjC pointers go into a different warning
1216 // category, and sometimes they're even completely whitelisted.
1217 if (const ObjCObjectPointerType *ImplPtrTy =
1218 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1219 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001220 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001221 // Allow non-matching return types as long as they don't violate
1222 // the principle of substitutability. Specifically, we permit
1223 // return types that are subclasses of the declared return type,
1224 // or that are more-qualified versions of the declared type.
1225 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001226 return false;
John McCall10302c02010-10-28 02:34:38 +00001227
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001228 DiagID =
1229 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1230 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001231 }
1232 }
1233
1234 S.Diag(MethodImpl->getLocation(), DiagID)
1235 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001236 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001237 << MethodImpl->getResultType()
1238 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001239 S.Diag(MethodDecl->getLocation(),
1240 IsOverridingMode ? diag::note_previous_declaration
1241 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001242 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001243 return false;
John McCall10302c02010-10-28 02:34:38 +00001244}
1245
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001246static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001247 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001248 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001249 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001250 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001251 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001252 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001253 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001254 if (IsProtocolMethodDecl &&
1255 (ImplVar->getObjCDeclQualifier() !=
1256 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001257 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001258 if (IsOverridingMode)
1259 S.Diag(ImplVar->getLocation(),
1260 diag::warn_conflicting_overriding_param_modifiers)
1261 << getTypeRange(ImplVar->getTypeSourceInfo())
1262 << MethodImpl->getDeclName();
1263 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001264 diag::warn_conflicting_param_modifiers)
1265 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001266 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001267 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1268 << getTypeRange(IfaceVar->getTypeSourceInfo());
1269 }
1270 else
1271 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001272 }
1273
John McCall10302c02010-10-28 02:34:38 +00001274 QualType ImplTy = ImplVar->getType();
1275 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001276
John McCall10302c02010-10-28 02:34:38 +00001277 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001278 return true;
1279
1280 if (!Warn)
1281 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001282 unsigned DiagID =
1283 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1284 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001285
1286 // Mismatches between ObjC pointers go into a different warning
1287 // category, and sometimes they're even completely whitelisted.
1288 if (const ObjCObjectPointerType *ImplPtrTy =
1289 ImplTy->getAs<ObjCObjectPointerType>()) {
1290 if (const ObjCObjectPointerType *IfacePtrTy =
1291 IfaceTy->getAs<ObjCObjectPointerType>()) {
1292 // Allow non-matching argument types as long as they don't
1293 // violate the principle of substitutability. Specifically, the
1294 // implementation must accept any objects that the superclass
1295 // accepts, however it may also accept others.
1296 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001297 return false;
John McCall10302c02010-10-28 02:34:38 +00001298
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001299 DiagID =
1300 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1301 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001302 }
1303 }
1304
1305 S.Diag(ImplVar->getLocation(), DiagID)
1306 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001307 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1308 S.Diag(IfaceVar->getLocation(),
1309 (IsOverridingMode ? diag::note_previous_declaration
1310 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001311 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001312 return false;
John McCall10302c02010-10-28 02:34:38 +00001313}
John McCallf85e1932011-06-15 23:02:42 +00001314
1315/// In ARC, check whether the conventional meanings of the two methods
1316/// match. If they don't, it's a hard error.
1317static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1318 ObjCMethodDecl *decl) {
1319 ObjCMethodFamily implFamily = impl->getMethodFamily();
1320 ObjCMethodFamily declFamily = decl->getMethodFamily();
1321 if (implFamily == declFamily) return false;
1322
1323 // Since conventions are sorted by selector, the only possibility is
1324 // that the types differ enough to cause one selector or the other
1325 // to fall out of the family.
1326 assert(implFamily == OMF_None || declFamily == OMF_None);
1327
1328 // No further diagnostics required on invalid declarations.
1329 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1330
1331 const ObjCMethodDecl *unmatched = impl;
1332 ObjCMethodFamily family = declFamily;
1333 unsigned errorID = diag::err_arc_lost_method_convention;
1334 unsigned noteID = diag::note_arc_lost_method_convention;
1335 if (declFamily == OMF_None) {
1336 unmatched = decl;
1337 family = implFamily;
1338 errorID = diag::err_arc_gained_method_convention;
1339 noteID = diag::note_arc_gained_method_convention;
1340 }
1341
1342 // Indexes into a %select clause in the diagnostic.
1343 enum FamilySelector {
1344 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1345 };
1346 FamilySelector familySelector = FamilySelector();
1347
1348 switch (family) {
1349 case OMF_None: llvm_unreachable("logic error, no method convention");
1350 case OMF_retain:
1351 case OMF_release:
1352 case OMF_autorelease:
1353 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001354 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001355 case OMF_retainCount:
1356 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001357 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001358 // Mismatches for these methods don't change ownership
1359 // conventions, so we don't care.
1360 return false;
1361
1362 case OMF_init: familySelector = F_init; break;
1363 case OMF_alloc: familySelector = F_alloc; break;
1364 case OMF_copy: familySelector = F_copy; break;
1365 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1366 case OMF_new: familySelector = F_new; break;
1367 }
1368
1369 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1370 ReasonSelector reasonSelector;
1371
1372 // The only reason these methods don't fall within their families is
1373 // due to unusual result types.
1374 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1375 reasonSelector = R_UnrelatedReturn;
1376 } else {
1377 reasonSelector = R_NonObjectReturn;
1378 }
1379
1380 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1381 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1382
1383 return true;
1384}
John McCall10302c02010-10-28 02:34:38 +00001385
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001386void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001387 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001388 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001389 if (getLangOptions().ObjCAutoRefCount &&
1390 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1391 return;
1392
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001393 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001394 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001395 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001396
Chris Lattner3aff9192009-04-11 19:58:42 +00001397 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001398 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001399 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001400 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001401 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001402 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001403
Fariborz Jahanian21121902011-08-08 18:03:17 +00001404 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001405 Diag(ImpMethodDecl->getLocation(),
1406 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001407 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001408 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001409}
1410
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001411void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1412 ObjCMethodDecl *Overridden,
1413 bool IsProtocolMethodDecl) {
1414
1415 CheckMethodOverrideReturn(*this, Method, Overridden,
1416 IsProtocolMethodDecl, true,
1417 true);
1418
1419 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
1420 IF = Overridden->param_begin(), EM = Method->param_end();
1421 IM != EM; ++IM, ++IF) {
1422 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1423 IsProtocolMethodDecl, true, true);
1424 }
1425
1426 if (Method->isVariadic() != Overridden->isVariadic()) {
1427 Diag(Method->getLocation(),
1428 diag::warn_conflicting_overriding_variadic);
1429 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1430 }
1431}
1432
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001433/// WarnExactTypedMethods - This routine issues a warning if method
1434/// implementation declaration matches exactly that of its declaration.
1435void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1436 ObjCMethodDecl *MethodDecl,
1437 bool IsProtocolMethodDecl) {
1438 // don't issue warning when protocol method is optional because primary
1439 // class is not required to implement it and it is safe for protocol
1440 // to implement it.
1441 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1442 return;
1443 // don't issue warning when primary class's method is
1444 // depecated/unavailable.
1445 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1446 MethodDecl->hasAttr<DeprecatedAttr>())
1447 return;
1448
1449 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1450 IsProtocolMethodDecl, false, false);
1451 if (match)
1452 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1453 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1454 IM != EM; ++IM, ++IF) {
1455 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1456 *IM, *IF,
1457 IsProtocolMethodDecl, false, false);
1458 if (!match)
1459 break;
1460 }
1461 if (match)
1462 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001463 if (match)
1464 match = !(MethodDecl->isClassMethod() &&
1465 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001466
1467 if (match) {
1468 Diag(ImpMethodDecl->getLocation(),
1469 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001470 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1471 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001472 }
1473}
1474
Mike Stump390b4cc2009-05-16 07:39:55 +00001475/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1476/// improve the efficiency of selector lookups and type checking by associating
1477/// with each protocol / interface / category the flattened instance tables. If
1478/// we used an immutable set to keep the table then it wouldn't add significant
1479/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001480
Steve Naroffefe7f362008-02-08 22:06:17 +00001481/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001482/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001483void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1484 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001485 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001486 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001487 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001488 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001489 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1490 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1491 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001492 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1493
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001494 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001495 ObjCInterfaceDecl *NSIDecl = 0;
1496 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001497 // check to see if class implements forwardInvocation method and objects
1498 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001499 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001500 // Under such conditions, which means that every method possible is
1501 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001502 // found" warnings.
1503 // FIXME: Use a general GetUnarySelector method for this.
1504 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1505 Selector fISelector = Context.Selectors.getSelector(1, &II);
1506 if (InsMap.count(fISelector))
1507 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1508 // need be implemented in the implementation.
1509 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1510 }
Mike Stump1eb44332009-09-09 15:08:12 +00001511
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001512 // If a method lookup fails locally we still need to look and see if
1513 // the method was implemented by a base class or an inherited
1514 // protocol. This lookup is slow, but occurs rarely in correct code
1515 // and otherwise would terminate in a warning.
1516
Chris Lattner4d391482007-12-12 07:09:47 +00001517 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001518 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001519 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001520 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001521 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001522 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001523 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001524 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001525 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001526 // If a method is not implemented in the category implementation but
1527 // has been declared in its primary class, superclass,
1528 // or in one of their protocols, no need to issue the warning.
1529 // This is because method will be implemented in the primary class
1530 // or one of its super class implementation.
1531
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001532 // Ugly, but necessary. Method declared in protcol might have
1533 // have been synthesized due to a property declared in the class which
1534 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001535 if (ObjCMethodDecl *MethodInClass =
1536 IDecl->lookupInstanceMethod(method->getSelector(),
1537 true /*noCategoryLookup*/))
1538 if (C || MethodInClass->isSynthesized())
1539 continue;
1540 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1541 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1542 != DiagnosticsEngine::Ignored) {
1543 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001544 Diag(method->getLocation(), diag::note_method_declared_at)
1545 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001546 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1547 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001548 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001549 }
1550 }
Chris Lattner4d391482007-12-12 07:09:47 +00001551 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001552 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001553 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001554 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001555 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001556 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1557 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001558 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001559 // See above comment for instance method lookups.
1560 if (C && IDecl->lookupClassMethod(method->getSelector(),
1561 true /*noCategoryLookup*/))
1562 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001563 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001564 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1565 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001566 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001567 Diag(method->getLocation(), diag::note_method_declared_at)
1568 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001569 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1570 PDecl->getDeclName();
1571 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001572 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001573 }
Chris Lattner780f3292008-07-21 21:32:27 +00001574 // Check on this protocols's referenced protocols, recursively.
1575 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1576 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001577 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001578}
1579
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001580/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001581/// or protocol against those declared in their implementations.
1582///
1583void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1584 const llvm::DenseSet<Selector> &ClsMap,
1585 llvm::DenseSet<Selector> &InsMapSeen,
1586 llvm::DenseSet<Selector> &ClsMapSeen,
1587 ObjCImplDecl* IMPDecl,
1588 ObjCContainerDecl* CDecl,
1589 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001590 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001591 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001592 // Check and see if instance methods in class interface have been
1593 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001594 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1595 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001596 if (InsMapSeen.count((*I)->getSelector()))
1597 continue;
1598 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001599 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001600 !InsMap.count((*I)->getSelector())) {
1601 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001602 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1603 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001604 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001605 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001606 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001607 IMPDecl->getInstanceMethod((*I)->getSelector());
1608 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1609 "Expected to find the method through lookup as well");
1610 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001611 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001612 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001613 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001614 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1615 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001616 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001617 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001618 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001619 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001620 }
1621 }
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001623 // Check and see if class methods in class interface have been
1624 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001625 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001626 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001627 if (ClsMapSeen.count((*I)->getSelector()))
1628 continue;
1629 ClsMapSeen.insert((*I)->getSelector());
1630 if (!ClsMap.count((*I)->getSelector())) {
1631 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001632 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1633 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001634 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001635 ObjCMethodDecl *ImpMethodDecl =
1636 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001637 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1638 "Expected to find the method through lookup as well");
1639 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001640 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001641 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1642 isa<ObjCProtocolDecl>(CDecl));
1643 else
1644 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001645 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001646 }
1647 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001648
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001649 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001650 // Also methods in class extensions need be looked at next.
1651 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1652 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1653 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1654 IMPDecl,
1655 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001656 IncompleteImpl, false,
1657 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001658
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001659 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001660 for (ObjCInterfaceDecl::all_protocol_iterator
1661 PI = I->all_referenced_protocol_begin(),
1662 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001663 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1664 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001665 (*PI), IncompleteImpl, false,
1666 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001667
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001668 // FIXME. For now, we are not checking for extact match of methods
1669 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001670 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001671 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001672 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001673 I->getSuperClass(), IncompleteImpl, false);
1674 }
1675}
1676
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001677/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1678/// category matches with those implemented in its primary class and
1679/// warns each time an exact match is found.
1680void Sema::CheckCategoryVsClassMethodMatches(
1681 ObjCCategoryImplDecl *CatIMPDecl) {
1682 llvm::DenseSet<Selector> InsMap, ClsMap;
1683
1684 for (ObjCImplementationDecl::instmeth_iterator
1685 I = CatIMPDecl->instmeth_begin(),
1686 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1687 InsMap.insert((*I)->getSelector());
1688
1689 for (ObjCImplementationDecl::classmeth_iterator
1690 I = CatIMPDecl->classmeth_begin(),
1691 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1692 ClsMap.insert((*I)->getSelector());
1693 if (InsMap.empty() && ClsMap.empty())
1694 return;
1695
1696 // Get category's primary class.
1697 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1698 if (!CatDecl)
1699 return;
1700 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1701 if (!IDecl)
1702 return;
1703 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1704 bool IncompleteImpl = false;
1705 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1706 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001707 IncompleteImpl, false,
1708 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001709}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001710
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001711void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001712 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001713 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001714 llvm::DenseSet<Selector> InsMap;
1715 // Check and see if instance methods in class interface have been
1716 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001717 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001718 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001719 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001720
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001721 // Check and see if properties declared in the interface have either 1)
1722 // an implementation or 2) there is a @synthesize/@dynamic implementation
1723 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001724 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1725 if (!(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2) ||
Ted Kremenek71207fc2012-01-05 22:47:47 +00001726 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001727 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001728
Chris Lattner4d391482007-12-12 07:09:47 +00001729 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001730 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001731 I = IMPDecl->classmeth_begin(),
1732 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001733 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001735 // Check for type conflict of methods declared in a class/protocol and
1736 // its implementation; if any.
1737 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001738 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1739 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001740 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001741
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001742 // check all methods implemented in category against those declared
1743 // in its primary class.
1744 if (ObjCCategoryImplDecl *CatDecl =
1745 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1746 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Chris Lattner4d391482007-12-12 07:09:47 +00001748 // Check the protocol list for unimplemented methods in the @implementation
1749 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001750 // Check and see if class methods in class interface have been
1751 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001752
Chris Lattnercddc8882009-03-01 00:56:52 +00001753 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001754 for (ObjCInterfaceDecl::all_protocol_iterator
1755 PI = I->all_referenced_protocol_begin(),
1756 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001757 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001758 InsMap, ClsMap, I);
1759 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001760 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1761 Categories; Categories = Categories->getNextClassExtension())
1762 ImplMethodsVsClassMethods(S, IMPDecl,
1763 const_cast<ObjCCategoryDecl*>(Categories),
1764 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001765 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001766 // For extended class, unimplemented methods in its protocols will
1767 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001768 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001769 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1770 E = C->protocol_end(); PI != E; ++PI)
1771 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001772 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001773 // Report unimplemented properties in the category as well.
1774 // When reporting on missing setter/getters, do not report when
1775 // setter/getter is implemented in category's primary class
1776 // implementation.
1777 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1778 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1779 for (ObjCImplementationDecl::instmeth_iterator
1780 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1781 InsMap.insert((*I)->getSelector());
1782 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001783 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001784 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001785 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001786 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001787}
1788
Mike Stump1eb44332009-09-09 15:08:12 +00001789/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001790Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001791Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001792 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001793 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001794 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001795 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001796 for (unsigned i = 0; i != NumElts; ++i) {
1797 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001798 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001799 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001800 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001801 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001802 // Maybe we will complain about the shadowed template parameter.
1803 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1804 // Just pretend that we didn't see the previous declaration.
1805 PrevDecl = 0;
1806 }
1807
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001808 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001809 // GCC apparently allows the following idiom:
1810 //
1811 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1812 // @class XCElementToggler;
1813 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001814 // Here we have chosen to ignore the forward class declaration
1815 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001816 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001817 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001818 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001819 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001820 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001821 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001822 // to the underlying class. Just ignore the forward class with a warning
1823 // as this will force the intended behavior which is to lookup the typedef
1824 // name.
1825 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1826 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1827 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1828 continue;
1829 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001830 }
Chris Lattner4d391482007-12-12 07:09:47 +00001831 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001832
1833 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001834 ObjCInterfaceDecl *PrevIDecl
1835 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001836 ObjCInterfaceDecl *IDecl
1837 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001838 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001839 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001840
Douglas Gregor7723fec2011-12-15 20:29:51 +00001841 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001842 CheckObjCDeclScope(IDecl);
1843 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001844 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001845
1846 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001847}
1848
John McCall0f4c4c42011-06-16 01:15:19 +00001849static bool tryMatchRecordTypes(ASTContext &Context,
1850 Sema::MethodMatchStrategy strategy,
1851 const Type *left, const Type *right);
1852
John McCallf85e1932011-06-15 23:02:42 +00001853static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1854 QualType leftQT, QualType rightQT) {
1855 const Type *left =
1856 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1857 const Type *right =
1858 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1859
1860 if (left == right) return true;
1861
1862 // If we're doing a strict match, the types have to match exactly.
1863 if (strategy == Sema::MMS_strict) return false;
1864
1865 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1866
1867 // Otherwise, use this absurdly complicated algorithm to try to
1868 // validate the basic, low-level compatibility of the two types.
1869
1870 // As a minimum, require the sizes and alignments to match.
1871 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1872 return false;
1873
1874 // Consider all the kinds of non-dependent canonical types:
1875 // - functions and arrays aren't possible as return and parameter types
1876
1877 // - vector types of equal size can be arbitrarily mixed
1878 if (isa<VectorType>(left)) return isa<VectorType>(right);
1879 if (isa<VectorType>(right)) return false;
1880
1881 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001882 // - structs, unions, and Objective-C objects must match more-or-less
1883 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001884 // - everything else should be a scalar
1885 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001886 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001887
John McCall1d9b3b22011-09-09 05:25:32 +00001888 // Make scalars agree in kind, except count bools as chars, and group
1889 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001890 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1891 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1892 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1893 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001894 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1895 leftSK = Type::STK_ObjCObjectPointer;
1896 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1897 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001898
1899 // Note that data member pointers and function member pointers don't
1900 // intermix because of the size differences.
1901
1902 return (leftSK == rightSK);
1903}
Chris Lattner4d391482007-12-12 07:09:47 +00001904
John McCall0f4c4c42011-06-16 01:15:19 +00001905static bool tryMatchRecordTypes(ASTContext &Context,
1906 Sema::MethodMatchStrategy strategy,
1907 const Type *lt, const Type *rt) {
1908 assert(lt && rt && lt != rt);
1909
1910 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1911 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1912 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1913
1914 // Require union-hood to match.
1915 if (left->isUnion() != right->isUnion()) return false;
1916
1917 // Require an exact match if either is non-POD.
1918 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1919 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1920 return false;
1921
1922 // Require size and alignment to match.
1923 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1924
1925 // Require fields to match.
1926 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1927 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1928 for (; li != le && ri != re; ++li, ++ri) {
1929 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1930 return false;
1931 }
1932 return (li == le && ri == re);
1933}
1934
Chris Lattner4d391482007-12-12 07:09:47 +00001935/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1936/// returns true, or false, accordingly.
1937/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001938bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1939 const ObjCMethodDecl *right,
1940 MethodMatchStrategy strategy) {
1941 if (!matchTypes(Context, strategy,
1942 left->getResultType(), right->getResultType()))
1943 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001944
John McCallf85e1932011-06-15 23:02:42 +00001945 if (getLangOptions().ObjCAutoRefCount &&
1946 (left->hasAttr<NSReturnsRetainedAttr>()
1947 != right->hasAttr<NSReturnsRetainedAttr>() ||
1948 left->hasAttr<NSConsumesSelfAttr>()
1949 != right->hasAttr<NSConsumesSelfAttr>()))
1950 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001951
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001952 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001953 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001954
John McCallf85e1932011-06-15 23:02:42 +00001955 for (; li != le; ++li, ++ri) {
1956 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001957 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001958
1959 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1960 return false;
1961
1962 if (getLangOptions().ObjCAutoRefCount &&
1963 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1964 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001965 }
1966 return true;
1967}
1968
Douglas Gregor5ac4b692012-01-25 00:49:42 +00001969void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00001970 // If the list is empty, make it a singleton list.
1971 if (List->Method == 0) {
1972 List->Method = Method;
1973 List->Next = 0;
1974 return;
1975 }
1976
1977 // We've seen a method with this name, see if we have already seen this type
1978 // signature.
1979 ObjCMethodList *Previous = List;
1980 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00001981 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00001982 continue;
1983
1984 ObjCMethodDecl *PrevObjCMethod = List->Method;
1985
1986 // Propagate the 'defined' bit.
1987 if (Method->isDefined())
1988 PrevObjCMethod->setDefined(true);
1989
1990 // If a method is deprecated, push it in the global pool.
1991 // This is used for better diagnostics.
1992 if (Method->isDeprecated()) {
1993 if (!PrevObjCMethod->isDeprecated())
1994 List->Method = Method;
1995 }
1996 // If new method is unavailable, push it into global pool
1997 // unless previous one is deprecated.
1998 if (Method->isUnavailable()) {
1999 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2000 List->Method = Method;
2001 }
2002
2003 return;
2004 }
2005
2006 // We have a new signature for an existing method - add it.
2007 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002008 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002009 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2010}
2011
Sebastian Redldb9d2142010-08-02 23:18:59 +00002012/// \brief Read the contents of the method pool for a given selector from
2013/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002014void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002015 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002016 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002017}
2018
Sebastian Redldb9d2142010-08-02 23:18:59 +00002019void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
2020 bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002021 if (ExternalSource)
2022 ReadMethodPool(Method->getSelector());
2023
Sebastian Redldb9d2142010-08-02 23:18:59 +00002024 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002025 if (Pos == MethodPool.end())
2026 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2027 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002028
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002029 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002030
Sebastian Redldb9d2142010-08-02 23:18:59 +00002031 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002032 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002033}
2034
John McCallf85e1932011-06-15 23:02:42 +00002035/// Determines if this is an "acceptable" loose mismatch in the global
2036/// method pool. This exists mostly as a hack to get around certain
2037/// global mismatches which we can't afford to make warnings / errors.
2038/// Really, what we want is a way to take a method out of the global
2039/// method pool.
2040static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2041 ObjCMethodDecl *other) {
2042 if (!chosen->isInstanceMethod())
2043 return false;
2044
2045 Selector sel = chosen->getSelector();
2046 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2047 return false;
2048
2049 // Don't complain about mismatches for -length if the method we
2050 // chose has an integral result type.
2051 return (chosen->getResultType()->isIntegerType());
2052}
2053
Sebastian Redldb9d2142010-08-02 23:18:59 +00002054ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002055 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002056 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002057 if (ExternalSource)
2058 ReadMethodPool(Sel);
2059
Sebastian Redldb9d2142010-08-02 23:18:59 +00002060 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002061 if (Pos == MethodPool.end())
2062 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002063
Sebastian Redldb9d2142010-08-02 23:18:59 +00002064 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Sebastian Redldb9d2142010-08-02 23:18:59 +00002066 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002067 bool issueDiagnostic = false, issueError = false;
2068
2069 // We support a warning which complains about *any* difference in
2070 // method signature.
2071 bool strictSelectorMatch =
2072 (receiverIdOrClass && warn &&
2073 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2074 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002075 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002076 if (strictSelectorMatch)
2077 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002078 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2079 MMS_strict)) {
2080 issueDiagnostic = true;
2081 break;
2082 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002083 }
2084
John McCallf85e1932011-06-15 23:02:42 +00002085 // If we didn't see any strict differences, we won't see any loose
2086 // differences. In ARC, however, we also need to check for loose
2087 // mismatches, because most of them are errors.
2088 if (!strictSelectorMatch ||
2089 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002090 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002091 // This checks if the methods differ in type mismatch.
2092 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2093 MMS_loose) &&
2094 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2095 issueDiagnostic = true;
2096 if (getLangOptions().ObjCAutoRefCount)
2097 issueError = true;
2098 break;
2099 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002100 }
2101
John McCallf85e1932011-06-15 23:02:42 +00002102 if (issueDiagnostic) {
2103 if (issueError)
2104 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2105 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002106 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2107 else
2108 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002109
2110 Diag(MethList.Method->getLocStart(),
2111 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002112 << MethList.Method->getSourceRange();
2113 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2114 Diag(Next->Method->getLocStart(), diag::note_also_found)
2115 << Next->Method->getSourceRange();
2116 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002117 }
2118 return MethList.Method;
2119}
2120
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002121ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002122 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2123 if (Pos == MethodPool.end())
2124 return 0;
2125
2126 GlobalMethods &Methods = Pos->second;
2127
2128 if (Methods.first.Method && Methods.first.Method->isDefined())
2129 return Methods.first.Method;
2130 if (Methods.second.Method && Methods.second.Method->isDefined())
2131 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002132 return 0;
2133}
2134
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002135/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2136/// identical selector names in current and its super classes and issues
2137/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002138void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2139 ObjCMethodDecl *Method,
2140 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002141 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2142 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002143
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002144 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002145 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002146 SD->lookupMethod(Method->getSelector(), IsInstance);
2147 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002148 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002149 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002150 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002151 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2152 E = Method->param_end();
2153 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2154 for (; ParamI != E; ++ParamI, ++PrevI) {
2155 // Number of parameters are the same and is guaranteed by selector match.
2156 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2157 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2158 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002159 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002160 // respective argument type in the super class method, issue warning;
2161 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002162 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002163 << T1 << T2;
2164 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2165 return;
2166 }
2167 }
2168 ID = SD;
2169 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002170}
2171
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002172/// DiagnoseDuplicateIvars -
2173/// Check for duplicate ivars in the entire class at the start of
2174/// @implementation. This becomes necesssary because class extension can
2175/// add ivars to a class in random order which will not be known until
2176/// class's @implementation is seen.
2177void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2178 ObjCInterfaceDecl *SID) {
2179 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2180 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2181 ObjCIvarDecl* Ivar = (*IVI);
2182 if (Ivar->isInvalidDecl())
2183 continue;
2184 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2185 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2186 if (prevIvar) {
2187 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2188 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2189 Ivar->setInvalidDecl();
2190 }
2191 }
2192 }
2193}
2194
Erik Verbruggend64251f2011-12-06 09:25:23 +00002195Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2196 switch (CurContext->getDeclKind()) {
2197 case Decl::ObjCInterface:
2198 return Sema::OCK_Interface;
2199 case Decl::ObjCProtocol:
2200 return Sema::OCK_Protocol;
2201 case Decl::ObjCCategory:
2202 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2203 return Sema::OCK_ClassExtension;
2204 else
2205 return Sema::OCK_Category;
2206 case Decl::ObjCImplementation:
2207 return Sema::OCK_Implementation;
2208 case Decl::ObjCCategoryImpl:
2209 return Sema::OCK_CategoryImplementation;
2210
2211 default:
2212 return Sema::OCK_None;
2213 }
2214}
2215
Steve Naroffa56f6162007-12-18 01:30:32 +00002216// Note: For class/category implemenations, allMethods/allProperties is
2217// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002218Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2219 Decl **allMethods, unsigned allNum,
2220 Decl **allProperties, unsigned pNum,
2221 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002222
Erik Verbruggend64251f2011-12-06 09:25:23 +00002223 if (getObjCContainerKind() == Sema::OCK_None)
2224 return 0;
2225
2226 assert(AtEnd.isValid() && "Invalid location for '@end'");
2227
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002228 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2229 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002230
Mike Stump1eb44332009-09-09 15:08:12 +00002231 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002232 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2233 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002234 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002235
Steve Naroff0701bbb2009-01-08 17:28:14 +00002236 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2237 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2238 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2239
Chris Lattner4d391482007-12-12 07:09:47 +00002240 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002241 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002242 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002243
2244 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002245 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002246 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002247 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002248 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002249 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002250 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002251 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002252 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002253 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002254 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002255 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002256 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002257 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002258 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002259 if (!Context.getSourceManager().isInSystemHeader(
2260 Method->getLocation()))
2261 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2262 << Method->getDeclName();
2263 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2264 }
Chris Lattner4d391482007-12-12 07:09:47 +00002265 InsMap[Method->getSelector()] = Method;
2266 /// The following allows us to typecheck messages to "id".
2267 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002268 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002269 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002270 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002271 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002272 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002273 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002274 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002275 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002276 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002277 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002278 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002279 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002280 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002281 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002282 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002283 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002284 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002285 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002286 if (!Context.getSourceManager().isInSystemHeader(
2287 Method->getLocation()))
2288 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2289 << Method->getDeclName();
2290 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2291 }
Chris Lattner4d391482007-12-12 07:09:47 +00002292 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002293 /// The following allows us to typecheck messages to "Class".
2294 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002295 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002296 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002297 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002298 }
2299 }
2300 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002301 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002302 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002303 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002304 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002305 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002306 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002307 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002308 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002309 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002310
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002311 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002312 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002313 if (C->IsClassExtension()) {
2314 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2315 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002316 }
Chris Lattner4d391482007-12-12 07:09:47 +00002317 }
Steve Naroff09c47192009-01-09 15:36:25 +00002318 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002319 if (CDecl->getIdentifier())
2320 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2321 // user-defined setter/getter. It also synthesizes setter/getter methods
2322 // and adds them to the DeclContext and global method pools.
2323 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2324 E = CDecl->prop_end();
2325 I != E; ++I)
2326 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002327 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002328 }
2329 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002330 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002331 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002332 // Any property declared in a class extension might have user
2333 // declared setter or getter in current class extension or one
2334 // of the other class extensions. Mark them as synthesized as
2335 // property will be synthesized when property with same name is
2336 // seen in the @implementation.
2337 for (const ObjCCategoryDecl *ClsExtDecl =
2338 IDecl->getFirstClassExtension();
2339 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2340 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2341 E = ClsExtDecl->prop_end(); I != E; ++I) {
2342 ObjCPropertyDecl *Property = (*I);
2343 // Skip over properties declared @dynamic
2344 if (const ObjCPropertyImplDecl *PIDecl
2345 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2346 if (PIDecl->getPropertyImplementation()
2347 == ObjCPropertyImplDecl::Dynamic)
2348 continue;
2349
2350 for (const ObjCCategoryDecl *CExtDecl =
2351 IDecl->getFirstClassExtension();
2352 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2353 if (ObjCMethodDecl *GetterMethod =
2354 CExtDecl->getInstanceMethod(Property->getGetterName()))
2355 GetterMethod->setSynthesized(true);
2356 if (!Property->isReadOnly())
2357 if (ObjCMethodDecl *SetterMethod =
2358 CExtDecl->getInstanceMethod(Property->getSetterName()))
2359 SetterMethod->setSynthesized(true);
2360 }
2361 }
2362 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002363 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002364 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002365 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002366
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002367 if (LangOpts.ObjCNonFragileABI2)
2368 while (IDecl->getSuperClass()) {
2369 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2370 IDecl = IDecl->getSuperClass();
2371 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002372 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002373 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002374 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002375 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002376 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002377
Chris Lattner4d391482007-12-12 07:09:47 +00002378 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002379 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002380 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002381 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002382 Categories; Categories = Categories->getNextClassCategory()) {
2383 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002384 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002385 break;
2386 }
2387 }
2388 }
2389 }
Chris Lattner682bf922009-03-29 16:50:03 +00002390 if (isInterfaceDeclKind) {
2391 // Reject invalid vardecls.
2392 for (unsigned i = 0; i != tuvNum; i++) {
2393 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2394 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2395 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002396 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002397 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002398 }
Chris Lattner682bf922009-03-29 16:50:03 +00002399 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002400 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002401 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002402
2403 for (unsigned i = 0; i != tuvNum; i++) {
2404 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002405 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2406 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002407 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2408 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002409
2410 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002411}
2412
2413
2414/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2415/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002416static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002417CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002418 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002419}
2420
Ted Kremenek422bae72010-04-18 04:59:38 +00002421static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002422bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2423 const AttrVec &A) {
2424 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002425 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002426 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002427 return false;
2428
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002429 // method declared in interface has no attribute.
2430 // But implementation has attributes. This is invalid
2431 if (!IMD->hasAttrs())
2432 return true;
2433
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002434 const AttrVec &D = IMD->getAttrs();
2435 if (D.size() != A.size())
2436 return true;
2437
2438 // attributes on method declaration and definition must match exactly.
2439 // Note that we have at most a couple of attributes on methods, so this
2440 // n*n search is good enough.
2441 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2442 bool match = false;
2443 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2444 if ((*i)->getKind() == (*i1)->getKind()) {
2445 match = true;
2446 break;
2447 }
2448 }
2449 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002450 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002451 }
Sean Huntcf807c42010-08-18 23:23:40 +00002452 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002453}
2454
Douglas Gregore97179c2011-09-08 01:46:34 +00002455namespace {
2456 /// \brief Describes the compatibility of a result type with its method.
2457 enum ResultTypeCompatibilityKind {
2458 RTC_Compatible,
2459 RTC_Incompatible,
2460 RTC_Unknown
2461 };
2462}
2463
Douglas Gregor926df6c2011-06-11 01:09:30 +00002464/// \brief Check whether the declared result type of the given Objective-C
2465/// method declaration is compatible with the method's class.
2466///
Douglas Gregore97179c2011-09-08 01:46:34 +00002467static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002468CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2469 ObjCInterfaceDecl *CurrentClass) {
2470 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002471
2472 // If an Objective-C method inherits its related result type, then its
2473 // declared result type must be compatible with its own class type. The
2474 // declared result type is compatible if:
2475 if (const ObjCObjectPointerType *ResultObjectType
2476 = ResultType->getAs<ObjCObjectPointerType>()) {
2477 // - it is id or qualified id, or
2478 if (ResultObjectType->isObjCIdType() ||
2479 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002480 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002481
2482 if (CurrentClass) {
2483 if (ObjCInterfaceDecl *ResultClass
2484 = ResultObjectType->getInterfaceDecl()) {
2485 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002486 if (declaresSameEntity(CurrentClass, ResultClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002487 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002488
2489 // - it is a superclass of the method's class type
2490 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002491 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002492 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002493 } else {
2494 // Any Objective-C pointer type might be acceptable for a protocol
2495 // method; we just don't know.
2496 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002497 }
2498 }
2499
Douglas Gregore97179c2011-09-08 01:46:34 +00002500 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002501}
2502
John McCall6c2c2502011-07-22 02:45:48 +00002503namespace {
2504/// A helper class for searching for methods which a particular method
2505/// overrides.
2506class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002507public:
John McCall6c2c2502011-07-22 02:45:48 +00002508 Sema &S;
2509 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002510 llvm::SmallPtrSet<ObjCContainerDecl*, 128> Searched;
2511 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002512 bool Recursive;
2513
2514public:
2515 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2516 Selector selector = method->getSelector();
2517
2518 // Bypass this search if we've never seen an instance/class method
2519 // with this selector before.
2520 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2521 if (it == S.MethodPool.end()) {
2522 if (!S.ExternalSource) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002523 S.ReadMethodPool(selector);
2524
2525 it = S.MethodPool.find(selector);
2526 if (it == S.MethodPool.end())
2527 return;
John McCall6c2c2502011-07-22 02:45:48 +00002528 }
2529 ObjCMethodList &list =
2530 method->isInstanceMethod() ? it->second.first : it->second.second;
2531 if (!list.Method) return;
2532
2533 ObjCContainerDecl *container
2534 = cast<ObjCContainerDecl>(method->getDeclContext());
2535
2536 // Prevent the search from reaching this container again. This is
2537 // important with categories, which override methods from the
2538 // interface and each other.
2539 Searched.insert(container);
2540 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002541 }
John McCall6c2c2502011-07-22 02:45:48 +00002542
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002543 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002544 iterator begin() const { return Overridden.begin(); }
2545 iterator end() const { return Overridden.end(); }
2546
2547private:
2548 void searchFromContainer(ObjCContainerDecl *container) {
2549 if (container->isInvalidDecl()) return;
2550
2551 switch (container->getDeclKind()) {
2552#define OBJCCONTAINER(type, base) \
2553 case Decl::type: \
2554 searchFrom(cast<type##Decl>(container)); \
2555 break;
2556#define ABSTRACT_DECL(expansion)
2557#define DECL(type, base) \
2558 case Decl::type:
2559#include "clang/AST/DeclNodes.inc"
2560 llvm_unreachable("not an ObjC container!");
2561 }
2562 }
2563
2564 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002565 if (!protocol->hasDefinition())
2566 return;
2567
John McCall6c2c2502011-07-22 02:45:48 +00002568 // A method in a protocol declaration overrides declarations from
2569 // referenced ("parent") protocols.
2570 search(protocol->getReferencedProtocols());
2571 }
2572
2573 void searchFrom(ObjCCategoryDecl *category) {
2574 // A method in a category declaration overrides declarations from
2575 // the main class and from protocols the category references.
2576 search(category->getClassInterface());
2577 search(category->getReferencedProtocols());
2578 }
2579
2580 void searchFrom(ObjCCategoryImplDecl *impl) {
2581 // A method in a category definition that has a category
2582 // declaration overrides declarations from the category
2583 // declaration.
2584 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2585 search(category);
2586
2587 // Otherwise it overrides declarations from the class.
2588 } else {
2589 search(impl->getClassInterface());
2590 }
2591 }
2592
2593 void searchFrom(ObjCInterfaceDecl *iface) {
2594 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002595 if (!iface->hasDefinition())
2596 return;
2597
John McCall6c2c2502011-07-22 02:45:48 +00002598 // - categories,
2599 for (ObjCCategoryDecl *category = iface->getCategoryList();
2600 category; category = category->getNextClassCategory())
2601 search(category);
2602
2603 // - the super class, and
2604 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2605 search(super);
2606
2607 // - any referenced protocols.
2608 search(iface->getReferencedProtocols());
2609 }
2610
2611 void searchFrom(ObjCImplementationDecl *impl) {
2612 // A method in a class implementation overrides declarations from
2613 // the class interface.
2614 search(impl->getClassInterface());
2615 }
2616
2617
2618 void search(const ObjCProtocolList &protocols) {
2619 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2620 i != e; ++i)
2621 search(*i);
2622 }
2623
2624 void search(ObjCContainerDecl *container) {
2625 // Abort if we've already searched this container.
2626 if (!Searched.insert(container)) return;
2627
2628 // Check for a method in this container which matches this selector.
2629 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2630 Method->isInstanceMethod());
2631
2632 // If we find one, record it and bail out.
2633 if (meth) {
2634 Overridden.insert(meth);
2635 return;
2636 }
2637
2638 // Otherwise, search for methods that a hypothetical method here
2639 // would have overridden.
2640
2641 // Note that we're now in a recursive case.
2642 Recursive = true;
2643
2644 searchFromContainer(container);
2645 }
2646};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002647}
2648
John McCalld226f652010-08-21 09:40:31 +00002649Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002650 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002651 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002652 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002653 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002654 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002655 Selector Sel,
2656 // optional arguments. The number of types/arguments is obtained
2657 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002658 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002659 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002660 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002661 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002662 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002663 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002664 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002665 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002666 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002667 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2668 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002669 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002670
Douglas Gregore97179c2011-09-08 01:46:34 +00002671 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002672 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002673 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002674 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002675
Steve Naroffccef3712009-02-20 22:59:16 +00002676 // Methods cannot return interface types. All ObjC objects are
2677 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002678 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002679 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2680 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002681 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002682 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002683
2684 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002685 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002686 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002687 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002688 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002689 }
Mike Stump1eb44332009-09-09 15:08:12 +00002690
2691 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002692 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002693 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002694 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002695 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002696 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002697 /*isSynthesized=*/false,
2698 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002699 MethodDeclKind == tok::objc_optional
2700 ? ObjCMethodDecl::Optional
2701 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002702 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002703
Chris Lattner5f9e2722011-07-23 10:55:15 +00002704 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002705
Chris Lattner7db638d2009-04-11 19:42:43 +00002706 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002707 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002708 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002709
Chris Lattnere294d3f2009-04-11 18:57:04 +00002710 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002711 ArgType = Context.getObjCIdType();
2712 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002713 } else {
John McCall58e46772009-10-23 21:48:59 +00002714 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002715 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002716 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002717 }
Mike Stump1eb44332009-09-09 15:08:12 +00002718
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002719 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2720 LookupOrdinaryName, ForRedeclaration);
2721 LookupName(R, S);
2722 if (R.isSingleResult()) {
2723 NamedDecl *PrevDecl = R.getFoundDecl();
2724 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002725 Diag(ArgInfo[i].NameLoc,
2726 (MethodDefinition ? diag::warn_method_param_redefinition
2727 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002728 << ArgInfo[i].Name;
2729 Diag(PrevDecl->getLocation(),
2730 diag::note_previous_declaration);
2731 }
2732 }
2733
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002734 SourceLocation StartLoc = DI
2735 ? DI->getTypeLoc().getBeginLoc()
2736 : ArgInfo[i].NameLoc;
2737
John McCall81ef3e62011-04-23 02:46:06 +00002738 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2739 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2740 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002741
John McCall70798862011-05-02 00:30:12 +00002742 Param->setObjCMethodScopeInfo(i);
2743
Chris Lattner0ed844b2008-04-04 06:12:32 +00002744 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002745 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002746
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002747 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002748 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002749
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002750 if (Param->hasAttr<BlocksAttr>()) {
2751 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2752 Param->setInvalidDecl();
2753 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002754 S->AddDecl(Param);
2755 IdResolver.AddDecl(Param);
2756
Chris Lattner0ed844b2008-04-04 06:12:32 +00002757 Params.push_back(Param);
2758 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002759
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002760 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002761 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002762 QualType ArgType = Param->getType();
2763 if (ArgType.isNull())
2764 ArgType = Context.getObjCIdType();
2765 else
2766 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002767 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002768 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002769 Diag(Param->getLocation(),
2770 diag::err_object_cannot_be_passed_returned_by_value)
2771 << 1 << ArgType;
2772 Param->setInvalidDecl();
2773 }
2774 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002775
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002776 Params.push_back(Param);
2777 }
2778
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002779 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002780 ObjCMethod->setObjCDeclQualifier(
2781 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002782
2783 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002784 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002785
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002786 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002787 const ObjCMethodDecl *PrevMethod = 0;
2788 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002789 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002790 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2791 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002792 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002793 PrevMethod = ImpDecl->getClassMethod(Sel);
2794 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002795 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002796
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002797 ObjCMethodDecl *IMD = 0;
2798 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2799 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2800 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002801 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002802 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002803 SourceLocation MethodLoc = IMD->getLocation();
2804 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2805 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002806 Diag(MethodLoc, diag::note_method_declared_at)
2807 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002808 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002809 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002810 } else {
2811 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002812 }
John McCall6c2c2502011-07-22 02:45:48 +00002813
Chris Lattner4d391482007-12-12 07:09:47 +00002814 if (PrevMethod) {
2815 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002816 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002817 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002818 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002819 }
John McCall54abf7d2009-11-04 02:18:39 +00002820
Douglas Gregor926df6c2011-06-11 01:09:30 +00002821 // If this Objective-C method does not have a related result type, but we
2822 // are allowed to infer related result types, try to do so based on the
2823 // method family.
2824 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2825 if (!CurrentClass) {
2826 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2827 CurrentClass = Cat->getClassInterface();
2828 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2829 CurrentClass = Impl->getClassInterface();
2830 else if (ObjCCategoryImplDecl *CatImpl
2831 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2832 CurrentClass = CatImpl->getClassInterface();
2833 }
John McCall6c2c2502011-07-22 02:45:48 +00002834
Douglas Gregore97179c2011-09-08 01:46:34 +00002835 ResultTypeCompatibilityKind RTC
2836 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002837
2838 // Search for overridden methods and merge information down from them.
2839 OverrideSearch overrides(*this, ObjCMethod);
2840 for (OverrideSearch::iterator
2841 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2842 ObjCMethodDecl *overridden = *i;
2843
2844 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002845 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002846 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002847
2848 // Then merge the declarations.
2849 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002850
2851 // Check for overriding methods
2852 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00002853 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2854 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2855 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
Douglas Gregor926df6c2011-06-11 01:09:30 +00002856 }
2857
John McCallf85e1932011-06-15 23:02:42 +00002858 bool ARCError = false;
2859 if (getLangOptions().ObjCAutoRefCount)
2860 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2861
Douglas Gregore97179c2011-09-08 01:46:34 +00002862 // Infer the related result type when possible.
2863 if (!ARCError && RTC == RTC_Compatible &&
2864 !ObjCMethod->hasRelatedResultType() &&
2865 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002866 bool InferRelatedResultType = false;
2867 switch (ObjCMethod->getMethodFamily()) {
2868 case OMF_None:
2869 case OMF_copy:
2870 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002871 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002872 case OMF_mutableCopy:
2873 case OMF_release:
2874 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002875 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002876 break;
2877
2878 case OMF_alloc:
2879 case OMF_new:
2880 InferRelatedResultType = ObjCMethod->isClassMethod();
2881 break;
2882
2883 case OMF_init:
2884 case OMF_autorelease:
2885 case OMF_retain:
2886 case OMF_self:
2887 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2888 break;
2889 }
2890
John McCall6c2c2502011-07-22 02:45:48 +00002891 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002892 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002893 }
2894
John McCalld226f652010-08-21 09:40:31 +00002895 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002896}
2897
Chris Lattnercc98eac2008-12-17 07:13:27 +00002898bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002899 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002900 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002901 // Following is also an error. But it is caused by a missing @end
2902 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002903 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2904 return false;
2905 }
2906
Anders Carlsson15281452008-11-04 16:57:32 +00002907 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2908 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002909
Anders Carlsson15281452008-11-04 16:57:32 +00002910 return true;
2911}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002912
Chris Lattnercc98eac2008-12-17 07:13:27 +00002913/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2914/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002915void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002916 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002917 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002918 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002919 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002920 if (!Class) {
2921 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2922 return;
2923 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002924 if (LangOpts.ObjCNonFragileABI) {
2925 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2926 return;
2927 }
Mike Stump1eb44332009-09-09 15:08:12 +00002928
Chris Lattnercc98eac2008-12-17 07:13:27 +00002929 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002930 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002931 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002932 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002933 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002934 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002935 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002936 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2937 /*FIXME: StartL=*/ID->getLocation(),
2938 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002939 ID->getIdentifier(), ID->getType(),
2940 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002941 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002942 }
Mike Stump1eb44332009-09-09 15:08:12 +00002943
Chris Lattnercc98eac2008-12-17 07:13:27 +00002944 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002945 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002946 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002947 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002948 if (getLangOptions().CPlusPlus)
2949 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002950 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002951 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002952 }
2953}
2954
Douglas Gregor160b5632010-04-26 17:32:49 +00002955/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002956VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2957 SourceLocation StartLoc,
2958 SourceLocation IdLoc,
2959 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002960 bool Invalid) {
2961 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2962 // duration shall not be qualified by an address-space qualifier."
2963 // Since all parameters have automatic store duration, they can not have
2964 // an address space.
2965 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002966 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002967 Invalid = true;
2968 }
2969
2970 // An @catch parameter must be an unqualified object pointer type;
2971 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2972 if (Invalid) {
2973 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002974 } else if (T->isDependentType()) {
2975 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002976 } else if (!T->isObjCObjectPointerType()) {
2977 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002978 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002979 } else if (T->isObjCQualifiedIdType()) {
2980 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002981 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002982 }
2983
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002984 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2985 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002986 New->setExceptionVariable(true);
2987
Douglas Gregor9aab9c42011-12-10 01:22:52 +00002988 // In ARC, infer 'retaining' for variables of retainable type.
2989 if (getLangOptions().ObjCAutoRefCount && inferObjCARCLifetime(New))
2990 Invalid = true;
2991
Douglas Gregor160b5632010-04-26 17:32:49 +00002992 if (Invalid)
2993 New->setInvalidDecl();
2994 return New;
2995}
2996
John McCalld226f652010-08-21 09:40:31 +00002997Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002998 const DeclSpec &DS = D.getDeclSpec();
2999
3000 // We allow the "register" storage class on exception variables because
3001 // GCC did, but we drop it completely. Any other storage class is an error.
3002 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3003 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3004 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3005 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3006 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3007 << DS.getStorageClassSpec();
3008 }
3009 if (D.getDeclSpec().isThreadSpecified())
3010 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3011 D.getMutableDeclSpec().ClearStorageClassSpecs();
3012
3013 DiagnoseFunctionSpecifiers(D);
3014
3015 // Check that there are no default arguments inside the type of this
3016 // exception object (C++ only).
3017 if (getLangOptions().CPlusPlus)
3018 CheckExtraCXXDefaultArguments(D);
3019
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003020 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003021 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003022
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003023 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3024 D.getSourceRange().getBegin(),
3025 D.getIdentifierLoc(),
3026 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003027 D.isInvalidType());
3028
3029 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3030 if (D.getCXXScopeSpec().isSet()) {
3031 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3032 << D.getCXXScopeSpec().getRange();
3033 New->setInvalidDecl();
3034 }
3035
3036 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003037 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003038 if (D.getIdentifier())
3039 IdResolver.AddDecl(New);
3040
3041 ProcessDeclAttributes(S, New, D);
3042
3043 if (New->hasAttr<BlocksAttr>())
3044 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003045 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003046}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003047
3048/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003049/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003050void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003051 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003052 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3053 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003054 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003055 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003056 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003057 }
3058}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003059
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003060void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003061 // Load referenced selectors from the external source.
3062 if (ExternalSource) {
3063 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3064 ExternalSource->ReadReferencedSelectors(Sels);
3065 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3066 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3067 }
3068
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003069 // Warning will be issued only when selector table is
3070 // generated (which means there is at lease one implementation
3071 // in the TU). This is to match gcc's behavior.
3072 if (ReferencedSelectors.empty() ||
3073 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003074 return;
3075 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3076 ReferencedSelectors.begin(),
3077 E = ReferencedSelectors.end(); S != E; ++S) {
3078 Selector Sel = (*S).first;
3079 if (!LookupImplementedMethodInGlobalPool(Sel))
3080 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3081 }
3082 return;
3083}