blob: 1aa36ca678ce0f07fa07aa7bb11675532f9eb7e5 [file] [log] [blame]
Chris Lattnerda463fe2007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-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 Lattnerda463fe2007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall83024632010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
John McCall31168b02011-06-15 23:02:42 +000015#include "clang/AST/ASTConsumer.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/ASTMutationListener.h"
18#include "clang/AST/DeclObjC.h"
Steve Naroff157599f2009-03-03 14:49:36 +000019#include "clang/AST/Expr.h"
John McCall31168b02011-06-15 23:02:42 +000020#include "clang/AST/ExprObjC.h"
John McCall31168b02011-06-15 23:02:42 +000021#include "clang/Basic/SourceManager.h"
Patrick Beardacfbe9e2012-04-06 18:12:22 +000022#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000023#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/ExternalSemaSource.h"
25#include "clang/Sema/Lookup.h"
26#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
John McCalla1e130b2010-08-25 07:03:20 +000028#include "llvm/ADT/DenseSet.h"
29
Chris Lattnerda463fe2007-12-12 07:09:47 +000030using namespace clang;
31
John McCall31168b02011-06-15 23:02:42 +000032/// Check whether the given method, which must be in the 'init'
33/// family, is a valid member of that family.
34///
35/// \param receiverTypeIfCall - if null, check this as if declaring it;
36/// if non-null, check this as if making a call to it with the given
37/// receiver type
38///
39/// \return true to indicate that there was an error and appropriate
40/// actions were taken
41bool Sema::checkInitMethod(ObjCMethodDecl *method,
42 QualType receiverTypeIfCall) {
43 if (method->isInvalidDecl()) return true;
44
45 // This castAs is safe: methods that don't return an object
46 // pointer won't be inferred as inits and will reject an explicit
47 // objc_method_family(init).
48
49 // We ignore protocols here. Should we? What about Class?
50
51 const ObjCObjectType *result = method->getResultType()
52 ->castAs<ObjCObjectPointerType>()->getObjectType();
53
54 if (result->isObjCId()) {
55 return false;
56 } else if (result->isObjCClass()) {
57 // fall through: always an error
58 } else {
59 ObjCInterfaceDecl *resultClass = result->getInterface();
60 assert(resultClass && "unexpected object type!");
61
62 // It's okay for the result type to still be a forward declaration
63 // if we're checking an interface declaration.
Douglas Gregordc9166c2011-12-15 20:29:51 +000064 if (!resultClass->hasDefinition()) {
John McCall31168b02011-06-15 23:02:42 +000065 if (receiverTypeIfCall.isNull() &&
66 !isa<ObjCImplementationDecl>(method->getDeclContext()))
67 return false;
68
69 // Otherwise, we try to compare class types.
70 } else {
71 // If this method was declared in a protocol, we can't check
72 // anything unless we have a receiver type that's an interface.
73 const ObjCInterfaceDecl *receiverClass = 0;
74 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
75 if (receiverTypeIfCall.isNull())
76 return false;
77
78 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
79 ->getInterfaceDecl();
80
81 // This can be null for calls to e.g. id<Foo>.
82 if (!receiverClass) return false;
83 } else {
84 receiverClass = method->getClassInterface();
85 assert(receiverClass && "method not associated with a class!");
86 }
87
88 // If either class is a subclass of the other, it's fine.
89 if (receiverClass->isSuperClassOf(resultClass) ||
90 resultClass->isSuperClassOf(receiverClass))
91 return false;
92 }
93 }
94
95 SourceLocation loc = method->getLocation();
96
97 // If we're in a system header, and this is not a call, just make
98 // the method unusable.
99 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
100 method->addAttr(new (Context) UnavailableAttr(loc, Context,
101 "init method returns a type unrelated to its receiver type"));
102 return true;
103 }
104
105 // Otherwise, it's an error.
106 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
107 method->setInvalidDecl();
108 return true;
109}
110
Fariborz Jahanianac8dbf02011-09-27 22:35:36 +0000111void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregor66a8ca02013-01-15 22:43:08 +0000112 const ObjCMethodDecl *Overridden) {
Douglas Gregor33823722011-06-11 01:09:30 +0000113 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 McCall31168b02011-06-15 23:02:42 +0000122 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor33823722011-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 Gregorbab8a962011-09-08 01:46:34 +0000152 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
153 Diag(Overridden->getLocation(),
John McCall5ec7e7d2013-03-19 07:04:25 +0000154 diag::note_related_result_type_family)
155 << /*overridden method*/ 0
Douglas Gregorbab8a962011-09-08 01:46:34 +0000156 << Family;
157 else
158 Diag(Overridden->getLocation(),
159 diag::note_related_result_type_overridden);
Douglas Gregor33823722011-06-11 01:09:30 +0000160 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000161 if (getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanianac8dbf02011-09-27 22:35:36 +0000162 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
163 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
164 Diag(NewMethod->getLocation(),
165 diag::err_nsreturns_retained_attribute_mismatch) << 1;
166 Diag(Overridden->getLocation(), diag::note_previous_decl)
167 << "method";
168 }
169 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
170 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
171 Diag(NewMethod->getLocation(),
172 diag::err_nsreturns_retained_attribute_mismatch) << 0;
173 Diag(Overridden->getLocation(), diag::note_previous_decl)
174 << "method";
175 }
Douglas Gregor0bf70f42012-05-17 23:13:29 +0000176 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
177 oe = Overridden->param_end();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000178 for (ObjCMethodDecl::param_iterator
179 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Douglas Gregor0bf70f42012-05-17 23:13:29 +0000180 ni != ne && oi != oe; ++ni, ++oi) {
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000181 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanianac8dbf02011-09-27 22:35:36 +0000182 ParmVarDecl *newDecl = (*ni);
183 if (newDecl->hasAttr<NSConsumedAttr>() !=
184 oldDecl->hasAttr<NSConsumedAttr>()) {
185 Diag(newDecl->getLocation(),
186 diag::err_nsconsumed_attribute_mismatch);
187 Diag(oldDecl->getLocation(), diag::note_previous_decl)
188 << "parameter";
189 }
190 }
191 }
Douglas Gregor33823722011-06-11 01:09:30 +0000192}
193
John McCall31168b02011-06-15 23:02:42 +0000194/// \brief Check a method declaration for compatibility with the Objective-C
195/// ARC conventions.
John McCalle48f3892013-04-04 01:38:37 +0000196bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) {
John McCall31168b02011-06-15 23:02:42 +0000197 ObjCMethodFamily family = method->getMethodFamily();
198 switch (family) {
199 case OMF_None:
Nico Weber1fb82662011-08-28 22:35:17 +0000200 case OMF_finalize:
John McCall31168b02011-06-15 23:02:42 +0000201 case OMF_retain:
202 case OMF_release:
203 case OMF_autorelease:
204 case OMF_retainCount:
205 case OMF_self:
John McCalld2930c22011-07-22 02:45:48 +0000206 case OMF_performSelector:
John McCall31168b02011-06-15 23:02:42 +0000207 return false;
208
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000209 case OMF_dealloc:
John McCalle48f3892013-04-04 01:38:37 +0000210 if (!Context.hasSameType(method->getResultType(), Context.VoidTy)) {
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000211 SourceRange ResultTypeRange;
212 if (const TypeSourceInfo *ResultTypeInfo
213 = method->getResultTypeSourceInfo())
214 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
215 if (ResultTypeRange.isInvalid())
John McCalle48f3892013-04-04 01:38:37 +0000216 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000217 << method->getResultType()
218 << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
219 else
John McCalle48f3892013-04-04 01:38:37 +0000220 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000221 << method->getResultType()
222 << FixItHint::CreateReplacement(ResultTypeRange, "void");
223 return true;
224 }
225 return false;
226
John McCall31168b02011-06-15 23:02:42 +0000227 case OMF_init:
228 // If the method doesn't obey the init rules, don't bother annotating it.
John McCalle48f3892013-04-04 01:38:37 +0000229 if (checkInitMethod(method, QualType()))
John McCall31168b02011-06-15 23:02:42 +0000230 return true;
231
John McCalle48f3892013-04-04 01:38:37 +0000232 method->addAttr(new (Context) NSConsumesSelfAttr(SourceLocation(),
233 Context));
John McCall31168b02011-06-15 23:02:42 +0000234
235 // Don't add a second copy of this attribute, but otherwise don't
236 // let it be suppressed.
237 if (method->hasAttr<NSReturnsRetainedAttr>())
238 return false;
239 break;
240
241 case OMF_alloc:
242 case OMF_copy:
243 case OMF_mutableCopy:
244 case OMF_new:
245 if (method->hasAttr<NSReturnsRetainedAttr>() ||
246 method->hasAttr<NSReturnsNotRetainedAttr>() ||
247 method->hasAttr<NSReturnsAutoreleasedAttr>())
248 return false;
249 break;
250 }
251
John McCalle48f3892013-04-04 01:38:37 +0000252 method->addAttr(new (Context) NSReturnsRetainedAttr(SourceLocation(),
253 Context));
John McCall31168b02011-06-15 23:02:42 +0000254 return false;
255}
256
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000257static void DiagnoseObjCImplementedDeprecations(Sema &S,
258 NamedDecl *ND,
259 SourceLocation ImplLoc,
260 int select) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000261 if (ND && ND->isDeprecated()) {
Fariborz Jahanian6fd94352011-02-16 00:30:31 +0000262 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000263 if (select == 0)
Ted Kremenek59b10db2012-02-27 22:55:11 +0000264 S.Diag(ND->getLocation(), diag::note_method_declared_at)
265 << ND->getDeclName();
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000266 else
267 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
268 }
269}
270
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +0000271/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
272/// pool.
273void Sema::AddAnyMethodToGlobalPool(Decl *D) {
274 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
275
276 // If we don't have a valid method decl, simply return.
277 if (!MDecl)
278 return;
279 if (MDecl->isInstanceMethod())
280 AddInstanceMethodToGlobalPool(MDecl, true);
281 else
282 AddFactoryMethodToGlobalPool(MDecl, true);
283}
284
Fariborz Jahanian1dfeace2012-09-13 18:53:14 +0000285/// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer
286/// has explicit ownership attribute; false otherwise.
287static bool
288HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) {
289 QualType T = Param->getType();
290
Fariborz Jahanian1dfeace2012-09-13 18:53:14 +0000291 if (const PointerType *PT = T->getAs<PointerType>()) {
292 T = PT->getPointeeType();
293 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
294 T = RT->getPointeeType();
295 } else {
296 return true;
297 }
298
299 // If we have a lifetime qualifier, but it's local, we must have
300 // inferred it. So, it is implicit.
301 return !T.getLocalQualifiers().hasObjCLifetime();
302}
303
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +0000304/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
305/// and user declared, in the method definition's AST.
306void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
307 assert((getCurMethodDecl() == 0) && "Methodparsing confused");
John McCall48871652010-08-21 09:40:31 +0000308 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian577574a2012-07-02 23:37:09 +0000309
Steve Naroff542cd5d2008-07-25 17:57:26 +0000310 // If we don't have a valid method decl, simply return.
311 if (!MDecl)
312 return;
Steve Naroff1d2538c2007-12-18 01:30:32 +0000313
Chris Lattnerda463fe2007-12-12 07:09:47 +0000314 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor91f84212008-12-11 16:49:14 +0000315 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9a28e842010-03-01 23:15:13 +0000316 PushFunctionScope();
317
Chris Lattnerda463fe2007-12-12 07:09:47 +0000318 // Create Decl objects for each parameter, entrring them in the scope for
319 // binding to their use.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000320
321 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000322 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump11289f42009-09-09 15:08:12 +0000323
Daniel Dunbar279d1cc2008-08-26 06:07:48 +0000324 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
325 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000326
Reid Kleckner5a115802013-06-24 14:38:26 +0000327 // The ObjC parser requires parameter names so there's no need to check.
328 CheckParmsForFunctionDef(MDecl->param_begin(), MDecl->param_end(),
329 /*CheckParameterNames=*/false);
330
Chris Lattner58258242008-04-10 02:22:51 +0000331 // Introduce all of the other parameters into this scope.
Chris Lattnera4997152009-02-20 18:43:26 +0000332 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanianb3e87122010-09-17 22:07:07 +0000333 E = MDecl->param_end(); PI != E; ++PI) {
334 ParmVarDecl *Param = (*PI);
335 if (!Param->isInvalidDecl() &&
Fariborz Jahanian1dfeace2012-09-13 18:53:14 +0000336 getLangOpts().ObjCAutoRefCount &&
337 !HasExplicitOwnershipAttr(*this, Param))
338 Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
339 Param->getType();
Fariborz Jahaniancd278ff2012-08-30 23:56:02 +0000340
Chris Lattnera4997152009-02-20 18:43:26 +0000341 if ((*PI)->getIdentifier())
342 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanianb3e87122010-09-17 22:07:07 +0000343 }
John McCall31168b02011-06-15 23:02:42 +0000344
345 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikiebbafb8a2012-03-11 07:00:24 +0000346 if (getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +0000347 switch (MDecl->getMethodFamily()) {
348 case OMF_retain:
349 case OMF_retainCount:
350 case OMF_release:
351 case OMF_autorelease:
352 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
Fariborz Jahanian39d1c422013-05-16 19:08:44 +0000353 << 0 << MDecl->getSelector();
John McCall31168b02011-06-15 23:02:42 +0000354 break;
355
356 case OMF_None:
357 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000358 case OMF_finalize:
John McCall31168b02011-06-15 23:02:42 +0000359 case OMF_alloc:
360 case OMF_init:
361 case OMF_mutableCopy:
362 case OMF_copy:
363 case OMF_new:
364 case OMF_self:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000365 case OMF_performSelector:
John McCall31168b02011-06-15 23:02:42 +0000366 break;
367 }
368 }
369
Nico Weber715abaf2011-08-22 17:25:57 +0000370 // Warn on deprecated methods under -Wdeprecated-implementations,
371 // and prepare for warning on missing super calls.
372 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +0000373 ObjCMethodDecl *IMD =
374 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
375
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000376 if (IMD) {
377 ObjCImplDecl *ImplDeclOfMethodDef =
378 dyn_cast<ObjCImplDecl>(MDecl->getDeclContext());
379 ObjCContainerDecl *ContDeclOfMethodDecl =
380 dyn_cast<ObjCContainerDecl>(IMD->getDeclContext());
381 ObjCImplDecl *ImplDeclOfMethodDecl = 0;
382 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
383 ImplDeclOfMethodDecl = OID->getImplementation();
384 else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl))
385 ImplDeclOfMethodDecl = CD->getImplementation();
386 // No need to issue deprecated warning if deprecated mehod in class/category
387 // is being implemented in its own implementation (no overriding is involved).
388 if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef)
389 DiagnoseObjCImplementedDeprecations(*this,
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000390 dyn_cast<NamedDecl>(IMD),
391 MDecl->getLocation(), 0);
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000392 }
Nico Weber715abaf2011-08-22 17:25:57 +0000393
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +0000394 if (MDecl->getMethodFamily() == OMF_init) {
395 if (MDecl->isDesignatedInitializerForTheInterface()) {
396 getCurFunction()->ObjCIsDesignatedInit = true;
397 getCurFunction()->ObjCWarnForNoDesignatedInitChain =
398 IC->getSuperClass() != 0;
399 } else if (IC->hasDesignatedInitializers()) {
400 getCurFunction()->ObjCIsSecondaryInit = true;
401 getCurFunction()->ObjCWarnForNoInitDelegation = true;
402 }
403 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000404
Nico Weber1fb82662011-08-28 22:35:17 +0000405 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber715abaf2011-08-22 17:25:57 +0000406 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
407 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
408 // Only do this if the current class actually has a superclass.
Jordan Rosed03d99d2013-03-05 01:27:54 +0000409 if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) {
Jordan Rose2afd6612012-10-19 16:05:26 +0000410 ObjCMethodFamily Family = MDecl->getMethodFamily();
411 if (Family == OMF_dealloc) {
412 if (!(getLangOpts().ObjCAutoRefCount ||
413 getLangOpts().getGC() == LangOptions::GCOnly))
414 getCurFunction()->ObjCShouldCallSuper = true;
415
416 } else if (Family == OMF_finalize) {
417 if (Context.getLangOpts().getGC() != LangOptions::NonGC)
418 getCurFunction()->ObjCShouldCallSuper = true;
419
Fariborz Jahaniance4bbb22013-11-05 00:28:21 +0000420 } else {
Jordan Rose2afd6612012-10-19 16:05:26 +0000421 const ObjCMethodDecl *SuperMethod =
Jordan Rosed03d99d2013-03-05 01:27:54 +0000422 SuperClass->lookupMethod(MDecl->getSelector(),
423 MDecl->isInstanceMethod());
Jordan Rose2afd6612012-10-19 16:05:26 +0000424 getCurFunction()->ObjCShouldCallSuper =
425 (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahaniand6876b22012-09-10 18:04:25 +0000426 }
Nico Weber1fb82662011-08-28 22:35:17 +0000427 }
Nico Weber715abaf2011-08-22 17:25:57 +0000428 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000429}
430
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000431namespace {
432
433// Callback to only accept typo corrections that are Objective-C classes.
434// If an ObjCInterfaceDecl* is given to the constructor, then the validation
435// function will reject corrections to that class.
436class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
437 public:
438 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
439 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
440 : CurrentIDecl(IDecl) {}
441
442 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
443 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
444 return ID && !declaresSameEntity(ID, CurrentIDecl);
445 }
446
447 private:
448 ObjCInterfaceDecl *CurrentIDecl;
449};
450
451}
452
John McCall48871652010-08-21 09:40:31 +0000453Decl *Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000454ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
455 IdentifierInfo *ClassName, SourceLocation ClassLoc,
456 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCall48871652010-08-21 09:40:31 +0000457 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000458 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000459 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000460 assert(ClassName && "Missing class identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000461
Chris Lattnerda463fe2007-12-12 07:09:47 +0000462 // Check for another declaration kind with the same name.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000463 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000464 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor5101c242008-12-05 18:15:24 +0000465
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000466 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000467 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000468 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000469 }
Mike Stump11289f42009-09-09 15:08:12 +0000470
Douglas Gregordc9166c2011-12-15 20:29:51 +0000471 // Create a declaration to describe this @interface.
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000472 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +0000473
474 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
475 // A previous decl with a different name is because of
476 // @compatibility_alias, for example:
477 // \code
478 // @class NewImage;
479 // @compatibility_alias OldImage NewImage;
480 // \endcode
481 // A lookup for 'OldImage' will return the 'NewImage' decl.
482 //
483 // In such a case use the real declaration name, instead of the alias one,
484 // otherwise we will break IdentifierResolver and redecls-chain invariants.
485 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
486 // has been aliased.
487 ClassName = PrevIDecl->getIdentifier();
488 }
489
Douglas Gregordc9166c2011-12-15 20:29:51 +0000490 ObjCInterfaceDecl *IDecl
491 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000492 PrevIDecl, ClassLoc);
Douglas Gregordc9166c2011-12-15 20:29:51 +0000493
Douglas Gregordc9166c2011-12-15 20:29:51 +0000494 if (PrevIDecl) {
495 // Class already seen. Was it a definition?
496 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
497 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
498 << PrevIDecl->getDeclName();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000499 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregordc9166c2011-12-15 20:29:51 +0000500 IDecl->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000501 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000502 }
Douglas Gregordc9166c2011-12-15 20:29:51 +0000503
504 if (AttrList)
505 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
506 PushOnScopeChains(IDecl, TUScope);
Mike Stump11289f42009-09-09 15:08:12 +0000507
Douglas Gregordc9166c2011-12-15 20:29:51 +0000508 // Start the definition of this class. If we're in a redefinition case, there
509 // may already be a definition, so we'll end up adding to it.
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000510 if (!IDecl->hasDefinition())
511 IDecl->startDefinition();
512
Chris Lattnerda463fe2007-12-12 07:09:47 +0000513 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000514 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000515 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
516 LookupOrdinaryName);
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000517
518 if (!PrevDecl) {
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000519 // Try to correct for a typo in the superclass name without correcting
520 // to the class we're defining.
521 ObjCInterfaceValidatorCCC Validator(IDecl);
522 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000523 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain4e8942c2012-01-31 23:49:25 +0000524 NULL, Validator)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000525 diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest)
526 << SuperName << ClassName);
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000527 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000528 }
529 }
530
Douglas Gregor0b144e12011-12-15 00:29:59 +0000531 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000532 Diag(SuperLoc, diag::err_recursive_superclass)
533 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor16408322011-12-15 22:34:59 +0000534 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stump12b8ce12009-08-04 21:02:39 +0000535 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000536 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000537 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000538
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000539 // Diagnose classes that inherit from deprecated classes.
540 if (SuperClassDecl)
541 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000542
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000543 if (PrevDecl && SuperClassDecl == 0) {
544 // The previous declaration was not a class decl. Check if we have a
545 // typedef. If we do, get the underlying class type.
Richard Smithdda56e42011-04-15 14:24:37 +0000546 if (const TypedefNameDecl *TDecl =
547 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000548 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000549 if (T->isObjCObjectType()) {
Fariborz Jahanian83f1be12013-04-04 18:45:52 +0000550 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000551 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian83f1be12013-04-04 18:45:52 +0000552 // This handles the following case:
553 // @interface NewI @end
554 // typedef NewI DeprI __attribute__((deprecated("blah")))
555 // @interface SI : DeprI /* warn here */ @end
556 (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc);
557 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000558 }
559 }
Mike Stump11289f42009-09-09 15:08:12 +0000560
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000561 // This handles the following case:
562 //
563 // typedef int SuperClass;
564 // @interface MyClass : SuperClass {} @end
565 //
566 if (!SuperClassDecl) {
567 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
568 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff189d41f2009-02-04 17:14:05 +0000569 }
570 }
Mike Stump11289f42009-09-09 15:08:12 +0000571
Richard Smithdda56e42011-04-15 14:24:37 +0000572 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000573 if (!SuperClassDecl)
574 Diag(SuperLoc, diag::err_undef_superclass)
575 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor4123a862011-11-14 22:10:01 +0000576 else if (RequireCompleteType(SuperLoc,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000577 Context.getObjCInterfaceType(SuperClassDecl),
578 diag::err_forward_superclass,
579 SuperClassDecl->getDeclName(),
580 ClassName,
581 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahanian3ee91fa2011-06-23 23:16:19 +0000582 SuperClassDecl = 0;
583 }
Steve Naroff189d41f2009-02-04 17:14:05 +0000584 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000585 IDecl->setSuperClass(SuperClassDecl);
586 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor16408322011-12-15 22:34:59 +0000587 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000588 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000589 } else { // we have a root class.
Douglas Gregor16408322011-12-15 22:34:59 +0000590 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Sebastian Redle7c1fe62010-08-13 00:28:03 +0000593 // Check then save referenced protocols.
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000594 if (NumProtoRefs) {
Roman Divackye6377112012-09-06 15:59:27 +0000595 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000596 ProtoLocs, Context);
Douglas Gregor16408322011-12-15 22:34:59 +0000597 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000598 }
Mike Stump11289f42009-09-09 15:08:12 +0000599
Anders Carlssona6b508a2008-11-04 16:57:32 +0000600 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000601 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000602}
603
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000604/// ActOnTypedefedProtocols - this action finds protocol list as part of the
605/// typedef'ed use for a qualified super class and adds them to the list
606/// of the protocols.
607void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs,
608 IdentifierInfo *SuperName,
609 SourceLocation SuperLoc) {
610 if (!SuperName)
611 return;
612 NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
613 LookupOrdinaryName);
614 if (!IDecl)
615 return;
616
617 if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) {
618 QualType T = TDecl->getUnderlyingType();
619 if (T->isObjCObjectType())
620 if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>())
621 for (ObjCObjectType::qual_iterator I = OPT->qual_begin(),
622 E = OPT->qual_end(); I != E; ++I)
623 ProtocolRefs.push_back(*I);
624 }
625}
626
Richard Smithac4e36d2012-08-08 23:32:13 +0000627/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett634962f2012-06-14 21:40:34 +0000628/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithac4e36d2012-08-08 23:32:13 +0000629Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
630 IdentifierInfo *AliasName,
631 SourceLocation AliasLocation,
632 IdentifierInfo *ClassName,
633 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000634 // Look for previous declaration of alias name
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000635 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000636 LookupOrdinaryName, ForRedeclaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000637 if (ADecl) {
Eli Friedmanfd6b3f82013-06-21 01:49:53 +0000638 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000639 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +0000640 return 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000641 }
642 // Check for class declaration
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000643 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000644 LookupOrdinaryName, ForRedeclaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000645 if (const TypedefNameDecl *TDecl =
646 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000647 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000648 if (T->isObjCObjectType()) {
649 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000650 ClassName = IDecl->getIdentifier();
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000651 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000652 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000653 }
654 }
655 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000656 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
657 if (CDecl == 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000658 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000659 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000660 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +0000661 return 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000662 }
Mike Stump11289f42009-09-09 15:08:12 +0000663
Chris Lattner219b3e92008-03-16 21:17:37 +0000664 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000665 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000666 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000667
Anders Carlssona6b508a2008-11-04 16:57:32 +0000668 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000669 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000670
John McCall48871652010-08-21 09:40:31 +0000671 return AliasDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000672}
673
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000674bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff41d09ad2009-03-05 15:22:01 +0000675 IdentifierInfo *PName,
676 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000677 const ObjCList<ObjCProtocolDecl> &PList) {
678
679 bool res = false;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000680 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
681 E = PList.end(); I != E; ++I) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000682 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
683 Ploc)) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000684 if (PDecl->getIdentifier() == PName) {
685 Diag(Ploc, diag::err_protocol_has_circular_dependency);
686 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000687 res = true;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000688 }
Douglas Gregore6e48b12012-01-01 19:29:29 +0000689
690 if (!PDecl->hasDefinition())
691 continue;
692
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000693 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
694 PDecl->getLocation(), PDecl->getReferencedProtocols()))
695 res = true;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000696 }
697 }
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000698 return res;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000699}
700
John McCall48871652010-08-21 09:40:31 +0000701Decl *
Chris Lattner3bbae002008-07-26 04:03:38 +0000702Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
703 IdentifierInfo *ProtocolName,
704 SourceLocation ProtocolLoc,
John McCall48871652010-08-21 09:40:31 +0000705 Decl * const *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000706 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000707 const SourceLocation *ProtoLocs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000708 SourceLocation EndProtoLoc,
709 AttributeList *AttrList) {
Fariborz Jahaniancadf7c52011-05-12 22:04:39 +0000710 bool err = false;
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000711 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000712 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor32c17572012-01-01 20:30:41 +0000713 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
714 ForRedeclaration);
715 ObjCProtocolDecl *PDecl = 0;
716 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
717 // If we already have a definition, complain.
718 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
719 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump11289f42009-09-09 15:08:12 +0000720
Douglas Gregor32c17572012-01-01 20:30:41 +0000721 // Create a new protocol that is completely distinct from previous
722 // declarations, and do not make this protocol available for name lookup.
723 // That way, we'll end up completely ignoring the duplicate.
724 // FIXME: Can we turn this into an error?
725 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
726 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000727 /*PrevDecl=*/0);
Douglas Gregor32c17572012-01-01 20:30:41 +0000728 PDecl->startDefinition();
729 } else {
730 if (PrevDecl) {
731 // Check for circular dependencies among protocol declarations. This can
732 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis95dfc122011-11-13 22:08:30 +0000733 ObjCList<ObjCProtocolDecl> PList;
734 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
735 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor32c17572012-01-01 20:30:41 +0000736 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis95dfc122011-11-13 22:08:30 +0000737 }
Douglas Gregor32c17572012-01-01 20:30:41 +0000738
739 // Create the new declaration.
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +0000740 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +0000741 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000742 /*PrevDecl=*/PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000743
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000744 PushOnScopeChains(PDecl, TUScope);
Douglas Gregore6e48b12012-01-01 19:29:29 +0000745 PDecl->startDefinition();
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000746 }
Douglas Gregore6e48b12012-01-01 19:29:29 +0000747
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000748 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000749 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor32c17572012-01-01 20:30:41 +0000750
751 // Merge attributes from previous declarations.
752 if (PrevDecl)
753 mergeDeclAttributes(PDecl, PrevDecl);
754
Fariborz Jahaniancadf7c52011-05-12 22:04:39 +0000755 if (!err && NumProtoRefs ) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000756 /// Check then save referenced protocols.
Roman Divackye6377112012-09-06 15:59:27 +0000757 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000758 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000759 }
Mike Stump11289f42009-09-09 15:08:12 +0000760
761 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000762 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000763}
764
765/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000766/// issues an error if they are not declared. It returns list of
767/// protocol declarations in its 'Protocols' argument.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000768void
Chris Lattner3bbae002008-07-26 04:03:38 +0000769Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000770 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000771 unsigned NumProtocols,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000772 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000773 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000774 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
775 ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000776 if (!PDecl) {
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000777 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000778 TypoCorrection Corrected = CorrectTypo(
779 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain4e8942c2012-01-31 23:49:25 +0000780 LookupObjCProtocolName, TUScope, NULL, Validator);
Richard Smithf9b15102013-08-17 00:46:16 +0000781 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>()))
782 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest)
783 << ProtocolId[i].first);
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000784 }
785
786 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000787 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000788 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000789 continue;
790 }
Fariborz Jahanianada44a22013-04-09 17:52:29 +0000791 // If this is a forward protocol declaration, get its definition.
792 if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
793 PDecl = PDecl->getDefinition();
794
Douglas Gregor171c45a2009-02-18 21:56:37 +0000795 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000796
797 // If this is a forward declaration and we are supposed to warn in this
798 // case, do it.
Douglas Gregoreed49792013-01-17 00:38:46 +0000799 // FIXME: Recover nicely in the hidden case.
800 if (WarnOnDeclarations &&
801 (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()))
Chris Lattner3b054132008-11-19 05:08:23 +0000802 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000803 << ProtocolId[i].first;
John McCall48871652010-08-21 09:40:31 +0000804 Protocols.push_back(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000805 }
806}
807
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000808/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000809/// a class method in its extension.
810///
Mike Stump11289f42009-09-09 15:08:12 +0000811void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000812 ObjCInterfaceDecl *ID) {
813 if (!ID)
814 return; // Possibly due to previous error
815
816 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000817 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
818 e = ID->meth_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +0000819 ObjCMethodDecl *MD = *i;
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000820 MethodMap[MD->getSelector()] = MD;
821 }
822
823 if (MethodMap.empty())
824 return;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000825 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
826 e = CAT->meth_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +0000827 ObjCMethodDecl *Method = *i;
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000828 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
829 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
830 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
831 << Method->getDeclName();
832 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
833 }
834 }
835}
836
James Dennett634962f2012-06-14 21:40:34 +0000837/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorf6102672012-01-01 21:23:57 +0000838Sema::DeclGroupPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000839Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000840 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000841 unsigned NumElts,
842 AttributeList *attrList) {
Douglas Gregorf6102672012-01-01 21:23:57 +0000843 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000844 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000845 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor32c17572012-01-01 20:30:41 +0000846 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
847 ForRedeclaration);
848 ObjCProtocolDecl *PDecl
849 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
850 IdentList[i].second, AtProtocolLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000851 PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000852
853 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorf6102672012-01-01 21:23:57 +0000854 CheckObjCDeclScope(PDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000855
Douglas Gregor42ff1bb2012-01-01 20:33:24 +0000856 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000857 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor32c17572012-01-01 20:30:41 +0000858
859 if (PrevDecl)
860 mergeDeclAttributes(PDecl, PrevDecl);
861
Douglas Gregorf6102672012-01-01 21:23:57 +0000862 DeclsInGroup.push_back(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000863 }
Mike Stump11289f42009-09-09 15:08:12 +0000864
Rafael Espindolaab417692013-07-09 12:05:01 +0000865 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000866}
867
John McCall48871652010-08-21 09:40:31 +0000868Decl *Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000869ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
870 IdentifierInfo *ClassName, SourceLocation ClassLoc,
871 IdentifierInfo *CategoryName,
872 SourceLocation CategoryLoc,
John McCall48871652010-08-21 09:40:31 +0000873 Decl * const *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000874 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000875 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000876 SourceLocation EndProtoLoc) {
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000877 ObjCCategoryDecl *CDecl;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000878 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek514ff702010-02-23 19:39:46 +0000879
880 /// Check that class of this category is already completely declared.
Douglas Gregor4123a862011-11-14 22:10:01 +0000881
882 if (!IDecl
883 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000884 diag::err_category_forward_interface,
885 CategoryName == 0)) {
Ted Kremenek514ff702010-02-23 19:39:46 +0000886 // Create an invalid ObjCCategoryDecl to serve as context for
887 // the enclosing method declarations. We mark the decl invalid
888 // to make it clear that this isn't a valid AST.
889 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000890 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek514ff702010-02-23 19:39:46 +0000891 CDecl->setInvalidDecl();
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +0000892 CurContext->addDecl(CDecl);
Douglas Gregor4123a862011-11-14 22:10:01 +0000893
894 if (!IDecl)
895 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000896 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek514ff702010-02-23 19:39:46 +0000897 }
898
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000899 if (!CategoryName && IDecl->getImplementation()) {
900 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
901 Diag(IDecl->getImplementation()->getLocation(),
902 diag::note_implementation_declared);
Ted Kremenek514ff702010-02-23 19:39:46 +0000903 }
904
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000905 if (CategoryName) {
906 /// Check for duplicate interface declaration for this category
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000907 if (ObjCCategoryDecl *Previous
908 = IDecl->FindCategoryDeclaration(CategoryName)) {
909 // Class extensions can be declared multiple times, categories cannot.
910 Diag(CategoryLoc, diag::warn_dup_category_def)
911 << ClassName << CategoryName;
912 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner9018ca82009-02-16 21:26:43 +0000913 }
914 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000915
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000916 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
917 ClassLoc, CategoryLoc, CategoryName, IDecl);
918 // FIXME: PushOnScopeChains?
919 CurContext->addDecl(CDecl);
920
Chris Lattnerda463fe2007-12-12 07:09:47 +0000921 if (NumProtoRefs) {
Roman Divackye6377112012-09-06 15:59:27 +0000922 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000923 ProtoLocs, Context);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000924 // Protocols in the class extension belong to the class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000925 if (CDecl->IsClassExtension())
Roman Divackye6377112012-09-06 15:59:27 +0000926 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000927 NumProtoRefs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000928 }
Mike Stump11289f42009-09-09 15:08:12 +0000929
Anders Carlssona6b508a2008-11-04 16:57:32 +0000930 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000931 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000932}
933
934/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000935/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000936/// object.
John McCall48871652010-08-21 09:40:31 +0000937Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000938 SourceLocation AtCatImplLoc,
939 IdentifierInfo *ClassName, SourceLocation ClassLoc,
940 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000941 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000942 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +0000943 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000944 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
945 if (!CatIDecl) {
946 // Category @implementation with no corresponding @interface.
947 // Create and install one.
Argyrios Kyrtzidis41fc05c2011-11-23 20:27:26 +0000948 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
949 ClassLoc, CatLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000950 CatName, IDecl);
Argyrios Kyrtzidis41fc05c2011-11-23 20:27:26 +0000951 CatIDecl->setImplicit();
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000952 }
953 }
954
Mike Stump11289f42009-09-09 15:08:12 +0000955 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +0000956 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +0000957 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000958 /// Check that class of this category is already completely declared.
Douglas Gregor4123a862011-11-14 22:10:01 +0000959 if (!IDecl) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000960 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld2930c22011-07-22 02:45:48 +0000961 CDecl->setInvalidDecl();
Douglas Gregor4123a862011-11-14 22:10:01 +0000962 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
963 diag::err_undef_interface)) {
964 CDecl->setInvalidDecl();
John McCalld2930c22011-07-22 02:45:48 +0000965 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000966
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000967 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000968 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000969
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +0000970 // If the interface is deprecated/unavailable, warn/error about it.
971 if (IDecl)
972 DiagnoseUseOfDecl(IDecl, ClassLoc);
973
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000974 /// Check that CatName, category name, is not used in another implementation.
975 if (CatIDecl) {
976 if (CatIDecl->getImplementation()) {
977 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
978 << CatName;
979 Diag(CatIDecl->getImplementation()->getLocation(),
980 diag::note_previous_definition);
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000981 CDecl->setInvalidDecl();
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +0000982 } else {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000983 CatIDecl->setImplementation(CDecl);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +0000984 // Warn on implementating category of deprecated class under
985 // -Wdeprecated-implementations flag.
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000986 DiagnoseObjCImplementedDeprecations(*this,
987 dyn_cast<NamedDecl>(IDecl),
988 CDecl->getLocation(), 2);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +0000989 }
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000990 }
Mike Stump11289f42009-09-09 15:08:12 +0000991
Anders Carlssona6b508a2008-11-04 16:57:32 +0000992 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000993 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000994}
995
John McCall48871652010-08-21 09:40:31 +0000996Decl *Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000997 SourceLocation AtClassImplLoc,
998 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000999 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001000 SourceLocation SuperClassLoc) {
Richard Smithf9b15102013-08-17 00:46:16 +00001001 ObjCInterfaceDecl *IDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001002 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001003 NamedDecl *PrevDecl
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001004 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
1005 ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001006 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001007 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +00001008 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor1c283312010-08-11 12:19:30 +00001009 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001010 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
1011 diag::warn_undef_interface);
Douglas Gregor40f7a002010-01-04 17:27:12 +00001012 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001013 // We did not find anything with the name ClassName; try to correct for
Douglas Gregor40f7a002010-01-04 17:27:12 +00001014 // typos in the class name.
Kaelyn Uhraine31b8882012-01-13 01:32:50 +00001015 ObjCInterfaceValidatorCCC Validator;
Richard Smithf9b15102013-08-17 00:46:16 +00001016 TypoCorrection Corrected =
1017 CorrectTypo(DeclarationNameInfo(ClassName, ClassLoc),
1018 LookupOrdinaryName, TUScope, NULL, Validator);
1019 if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) {
1020 // Suggest the (potentially) correct interface name. Don't provide a
1021 // code-modification hint or use the typo name for recovery, because
1022 // this is just a warning. The program may actually be correct.
1023 diagnoseTypo(Corrected,
1024 PDiag(diag::warn_undef_interface_suggest) << ClassName,
1025 /*ErrorRecovery*/false);
Douglas Gregor40f7a002010-01-04 17:27:12 +00001026 } else {
1027 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
1028 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001029 }
Mike Stump11289f42009-09-09 15:08:12 +00001030
Chris Lattnerda463fe2007-12-12 07:09:47 +00001031 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001032 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001033 if (SuperClassname) {
1034 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001035 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
1036 LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001037 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001038 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1039 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +00001040 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001041 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001042 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidis3b60cff2012-03-13 01:09:36 +00001043 if (SDecl && !SDecl->hasDefinition())
1044 SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001045 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001046 Diag(SuperClassLoc, diag::err_undef_superclass)
1047 << SuperClassname << ClassName;
Douglas Gregor0b144e12011-12-15 00:29:59 +00001048 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001049 // This implementation and its interface do not have the same
1050 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001051 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001052 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001053 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001054 }
1055 }
1056 }
Mike Stump11289f42009-09-09 15:08:12 +00001057
Chris Lattnerda463fe2007-12-12 07:09:47 +00001058 if (!IDecl) {
1059 // Legacy case of @implementation with no corresponding @interface.
1060 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +00001061
Mike Stump87c57ac2009-05-16 07:39:55 +00001062 // FIXME: Do we support attributes on the @implementation? If so we should
1063 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +00001064 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001065 ClassName, /*PrevDecl=*/0, ClassLoc,
1066 true);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001067 IDecl->startDefinition();
Douglas Gregor16408322011-12-15 22:34:59 +00001068 if (SDecl) {
1069 IDecl->setSuperClass(SDecl);
1070 IDecl->setSuperClassLoc(SuperClassLoc);
1071 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1072 } else {
1073 IDecl->setEndOfDefinitionLoc(ClassLoc);
1074 }
1075
Douglas Gregorac345a32009-04-24 00:16:12 +00001076 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor1c283312010-08-11 12:19:30 +00001077 } else {
1078 // Mark the interface as being completed, even if it was just as
1079 // @class ....;
1080 // declaration; the user cannot reopen it.
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001081 if (!IDecl->hasDefinition())
1082 IDecl->startDefinition();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001083 }
Mike Stump11289f42009-09-09 15:08:12 +00001084
1085 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001086 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001087 ClassLoc, AtClassImplLoc, SuperClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001088
Anders Carlssona6b508a2008-11-04 16:57:32 +00001089 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001090 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001091
Chris Lattnerda463fe2007-12-12 07:09:47 +00001092 // Check that there is no duplicate implementation of this class.
Douglas Gregor1c283312010-08-11 12:19:30 +00001093 if (IDecl->getImplementation()) {
1094 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001095 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +00001096 Diag(IDecl->getImplementation()->getLocation(),
1097 diag::note_previous_definition);
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +00001098 IMPDecl->setInvalidDecl();
Douglas Gregor1c283312010-08-11 12:19:30 +00001099 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001100 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +00001101 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +00001102 // Warn on implementating deprecated class under
1103 // -Wdeprecated-implementations flag.
Fariborz Jahaniand724a102011-02-15 17:49:58 +00001104 DiagnoseObjCImplementedDeprecations(*this,
1105 dyn_cast<NamedDecl>(IDecl),
1106 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001107 }
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001108 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001109}
1110
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001111Sema::DeclGroupPtrTy
1112Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1113 SmallVector<Decl *, 64> DeclsInGroup;
1114 DeclsInGroup.reserve(Decls.size() + 1);
1115
1116 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1117 Decl *Dcl = Decls[i];
1118 if (!Dcl)
1119 continue;
1120 if (Dcl->getDeclContext()->isFileContext())
1121 Dcl->setTopLevelDeclInObjCContainer();
1122 DeclsInGroup.push_back(Dcl);
1123 }
1124
1125 DeclsInGroup.push_back(ObjCImpDecl);
1126
Rafael Espindolaab417692013-07-09 12:05:01 +00001127 return BuildDeclaratorGroup(DeclsInGroup, false);
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001128}
1129
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001130void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1131 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001132 SourceLocation RBrace) {
1133 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001134 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001135 if (!IDecl)
1136 return;
James Dennett634962f2012-06-14 21:40:34 +00001137 /// Check case of non-existing \@interface decl.
1138 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattnerda463fe2007-12-12 07:09:47 +00001139 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +00001140 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor16408322011-12-15 22:34:59 +00001141 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian20912d62010-02-17 17:00:07 +00001142 // Add ivar's to class's DeclContext.
1143 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanianc0309cd2010-02-17 18:10:54 +00001144 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith05afe5e2012-03-13 03:12:56 +00001145 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanianaef66222010-02-19 00:31:17 +00001146 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian20912d62010-02-17 17:00:07 +00001147 }
1148
Chris Lattnerda463fe2007-12-12 07:09:47 +00001149 return;
1150 }
1151 // If implementation has empty ivar list, just return.
1152 if (numIvars == 0)
1153 return;
Mike Stump11289f42009-09-09 15:08:12 +00001154
Chris Lattnerda463fe2007-12-12 07:09:47 +00001155 assert(ivars && "missing @implementation ivars");
John McCall5fb5df92012-06-20 06:18:46 +00001156 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001157 if (ImpDecl->getSuperClass())
1158 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1159 for (unsigned i = 0; i < numIvars; i++) {
1160 ObjCIvarDecl* ImplIvar = ivars[i];
1161 if (const ObjCIvarDecl *ClsIvar =
1162 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1163 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1164 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1165 continue;
1166 }
Fariborz Jahaniane23f26b2013-06-26 22:10:27 +00001167 // Check class extensions (unnamed categories) for duplicate ivars.
1168 for (ObjCInterfaceDecl::visible_extensions_iterator
1169 Ext = IDecl->visible_extensions_begin(),
1170 ExtEnd = IDecl->visible_extensions_end();
1171 Ext != ExtEnd; ++Ext) {
1172 ObjCCategoryDecl *CDecl = *Ext;
1173 if (const ObjCIvarDecl *ClsExtIvar =
1174 CDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1175 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1176 Diag(ClsExtIvar->getLocation(), diag::note_previous_definition);
1177 continue;
1178 }
1179 }
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001180 // Instance ivar to Implementation's DeclContext.
1181 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith05afe5e2012-03-13 03:12:56 +00001182 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001183 ImpDecl->addDecl(ImplIvar);
1184 }
1185 return;
1186 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001187 // Check interface's Ivar list against those in the implementation.
1188 // names and types must match.
1189 //
Chris Lattnerda463fe2007-12-12 07:09:47 +00001190 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001191 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +00001192 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1193 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001194 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie40ed2972012-06-06 20:45:41 +00001195 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001196 assert (ImplIvar && "missing implementation ivar");
1197 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +00001198
Steve Naroff157599f2009-03-03 14:49:36 +00001199 // First, make sure the types match.
Richard Smithcaf33902011-10-10 18:28:20 +00001200 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +00001201 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001202 << ImplIvar->getIdentifier()
1203 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +00001204 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smithcaf33902011-10-10 18:28:20 +00001205 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1206 ImplIvar->getBitWidthValue(Context) !=
1207 ClsIvar->getBitWidthValue(Context)) {
1208 Diag(ImplIvar->getBitWidth()->getLocStart(),
1209 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1210 Diag(ClsIvar->getBitWidth()->getLocStart(),
1211 diag::note_previous_definition);
Mike Stump11289f42009-09-09 15:08:12 +00001212 }
Steve Naroff157599f2009-03-03 14:49:36 +00001213 // Make sure the names are identical.
1214 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001215 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001216 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +00001217 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001218 }
1219 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001220 }
Mike Stump11289f42009-09-09 15:08:12 +00001221
Chris Lattner0f29d982007-12-12 18:11:49 +00001222 if (numIvars > 0)
Alp Tokerfff06742013-12-02 03:50:21 +00001223 Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +00001224 else if (IVI != IVE)
Alp Tokerfff06742013-12-02 03:50:21 +00001225 Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001226}
1227
Ted Kremenekf87decd2013-12-13 05:58:44 +00001228static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc,
1229 ObjCMethodDecl *method,
1230 bool &IncompleteImpl,
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001231 unsigned DiagID,
1232 NamedDecl *NeededFor = 0) {
Fariborz Jahanian9fc39c42011-06-24 20:31:37 +00001233 // No point warning no definition of method which is 'unavailable'.
Douglas Gregorc2e3d5c2012-12-11 18:53:07 +00001234 switch (method->getAvailability()) {
1235 case AR_Available:
1236 case AR_Deprecated:
1237 break;
1238
1239 // Don't warn about unavailable or not-yet-introduced methods.
1240 case AR_NotYetIntroduced:
1241 case AR_Unavailable:
Fariborz Jahanian9fc39c42011-06-24 20:31:37 +00001242 return;
Douglas Gregorc2e3d5c2012-12-11 18:53:07 +00001243 }
1244
Ted Kremenek65d63572013-03-27 00:02:21 +00001245 // FIXME: For now ignore 'IncompleteImpl'.
1246 // Previously we grouped all unimplemented methods under a single
1247 // warning, but some users strongly voiced that they would prefer
1248 // separate warnings. We will give that approach a try, as that
1249 // matches what we do with protocols.
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001250 {
1251 const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID);
1252 B << method;
1253 if (NeededFor)
1254 B << NeededFor;
1255 }
Ted Kremenek65d63572013-03-27 00:02:21 +00001256
1257 // Issue a note to the original declaration.
1258 SourceLocation MethodLoc = method->getLocStart();
1259 if (MethodLoc.isValid())
Ted Kremenekf87decd2013-12-13 05:58:44 +00001260 S.Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff15833ed2008-02-10 21:38:56 +00001261}
1262
David Chisnallb62d15c2010-10-25 17:23:52 +00001263/// Determines if type B can be substituted for type A. Returns true if we can
1264/// guarantee that anything that the user will do to an object of type A can
1265/// also be done to an object of type B. This is trivially true if the two
1266/// types are the same, or if B is a subclass of A. It becomes more complex
1267/// in cases where protocols are involved.
1268///
1269/// Object types in Objective-C describe the minimum requirements for an
1270/// object, rather than providing a complete description of a type. For
1271/// example, if A is a subclass of B, then B* may refer to an instance of A.
1272/// The principle of substitutability means that we may use an instance of A
1273/// anywhere that we may use an instance of B - it will implement all of the
1274/// ivars of B and all of the methods of B.
1275///
1276/// This substitutability is important when type checking methods, because
1277/// the implementation may have stricter type definitions than the interface.
1278/// The interface specifies minimum requirements, but the implementation may
1279/// have more accurate ones. For example, a method may privately accept
1280/// instances of B, but only publish that it accepts instances of A. Any
1281/// object passed to it will be type checked against B, and so will implicitly
1282/// by a valid A*. Similarly, a method may return a subclass of the class that
1283/// it is declared as returning.
1284///
1285/// This is most important when considering subclassing. A method in a
1286/// subclass must accept any object as an argument that its superclass's
1287/// implementation accepts. It may, however, accept a more general type
1288/// without breaking substitutability (i.e. you can still use the subclass
1289/// anywhere that you can use the superclass, but not vice versa). The
1290/// converse requirement applies to return types: the return type for a
1291/// subclass method must be a valid object of the kind that the superclass
1292/// advertises, but it may be specified more accurately. This avoids the need
1293/// for explicit down-casting by callers.
1294///
1295/// Note: This is a stricter requirement than for assignment.
John McCall071df462010-10-28 02:34:38 +00001296static bool isObjCTypeSubstitutable(ASTContext &Context,
1297 const ObjCObjectPointerType *A,
1298 const ObjCObjectPointerType *B,
1299 bool rejectId) {
1300 // Reject a protocol-unqualified id.
1301 if (rejectId && B->isObjCIdType()) return false;
David Chisnallb62d15c2010-10-25 17:23:52 +00001302
1303 // If B is a qualified id, then A must also be a qualified id and it must
1304 // implement all of the protocols in B. It may not be a qualified class.
1305 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1306 // stricter definition so it is not substitutable for id<A>.
1307 if (B->isObjCQualifiedIdType()) {
1308 return A->isObjCQualifiedIdType() &&
John McCall071df462010-10-28 02:34:38 +00001309 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1310 QualType(B,0),
1311 false);
David Chisnallb62d15c2010-10-25 17:23:52 +00001312 }
1313
1314 /*
1315 // id is a special type that bypasses type checking completely. We want a
1316 // warning when it is used in one place but not another.
1317 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1318
1319
1320 // If B is a qualified id, then A must also be a qualified id (which it isn't
1321 // if we've got this far)
1322 if (B->isObjCQualifiedIdType()) return false;
1323 */
1324
1325 // Now we know that A and B are (potentially-qualified) class types. The
1326 // normal rules for assignment apply.
John McCall071df462010-10-28 02:34:38 +00001327 return Context.canAssignObjCInterfaces(A, B);
David Chisnallb62d15c2010-10-25 17:23:52 +00001328}
1329
John McCall071df462010-10-28 02:34:38 +00001330static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1331 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1332}
1333
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001334static bool CheckMethodOverrideReturn(Sema &S,
John McCall071df462010-10-28 02:34:38 +00001335 ObjCMethodDecl *MethodImpl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001336 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001337 bool IsProtocolMethodDecl,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001338 bool IsOverridingMode,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001339 bool Warn) {
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001340 if (IsProtocolMethodDecl &&
1341 (MethodDecl->getObjCDeclQualifier() !=
1342 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001343 if (Warn) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001344 S.Diag(MethodImpl->getLocation(),
1345 (IsOverridingMode ?
1346 diag::warn_conflicting_overriding_ret_type_modifiers
1347 : diag::warn_conflicting_ret_type_modifiers))
1348 << MethodImpl->getDeclName()
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001349 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1350 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1351 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1352 }
1353 else
1354 return false;
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001355 }
1356
John McCall071df462010-10-28 02:34:38 +00001357 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001358 MethodDecl->getResultType()))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001359 return true;
1360 if (!Warn)
1361 return false;
John McCall071df462010-10-28 02:34:38 +00001362
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001363 unsigned DiagID =
1364 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1365 : diag::warn_conflicting_ret_types;
John McCall071df462010-10-28 02:34:38 +00001366
1367 // Mismatches between ObjC pointers go into a different warning
1368 // category, and sometimes they're even completely whitelisted.
1369 if (const ObjCObjectPointerType *ImplPtrTy =
1370 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1371 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001372 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall071df462010-10-28 02:34:38 +00001373 // Allow non-matching return types as long as they don't violate
1374 // the principle of substitutability. Specifically, we permit
1375 // return types that are subclasses of the declared return type,
1376 // or that are more-qualified versions of the declared type.
1377 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001378 return false;
John McCall071df462010-10-28 02:34:38 +00001379
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001380 DiagID =
1381 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1382 : diag::warn_non_covariant_ret_types;
John McCall071df462010-10-28 02:34:38 +00001383 }
1384 }
1385
1386 S.Diag(MethodImpl->getLocation(), DiagID)
1387 << MethodImpl->getDeclName()
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001388 << MethodDecl->getResultType()
John McCall071df462010-10-28 02:34:38 +00001389 << MethodImpl->getResultType()
1390 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001391 S.Diag(MethodDecl->getLocation(),
1392 IsOverridingMode ? diag::note_previous_declaration
1393 : diag::note_previous_definition)
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001394 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001395 return false;
John McCall071df462010-10-28 02:34:38 +00001396}
1397
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001398static bool CheckMethodOverrideParam(Sema &S,
John McCall071df462010-10-28 02:34:38 +00001399 ObjCMethodDecl *MethodImpl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001400 ObjCMethodDecl *MethodDecl,
John McCall071df462010-10-28 02:34:38 +00001401 ParmVarDecl *ImplVar,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001402 ParmVarDecl *IfaceVar,
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001403 bool IsProtocolMethodDecl,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001404 bool IsOverridingMode,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001405 bool Warn) {
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001406 if (IsProtocolMethodDecl &&
1407 (ImplVar->getObjCDeclQualifier() !=
1408 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001409 if (Warn) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001410 if (IsOverridingMode)
1411 S.Diag(ImplVar->getLocation(),
1412 diag::warn_conflicting_overriding_param_modifiers)
1413 << getTypeRange(ImplVar->getTypeSourceInfo())
1414 << MethodImpl->getDeclName();
1415 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001416 diag::warn_conflicting_param_modifiers)
1417 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001418 << MethodImpl->getDeclName();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001419 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1420 << getTypeRange(IfaceVar->getTypeSourceInfo());
1421 }
1422 else
1423 return false;
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001424 }
1425
John McCall071df462010-10-28 02:34:38 +00001426 QualType ImplTy = ImplVar->getType();
1427 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001428
John McCall071df462010-10-28 02:34:38 +00001429 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001430 return true;
1431
1432 if (!Warn)
1433 return false;
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001434 unsigned DiagID =
1435 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1436 : diag::warn_conflicting_param_types;
John McCall071df462010-10-28 02:34:38 +00001437
1438 // Mismatches between ObjC pointers go into a different warning
1439 // category, and sometimes they're even completely whitelisted.
1440 if (const ObjCObjectPointerType *ImplPtrTy =
1441 ImplTy->getAs<ObjCObjectPointerType>()) {
1442 if (const ObjCObjectPointerType *IfacePtrTy =
1443 IfaceTy->getAs<ObjCObjectPointerType>()) {
1444 // Allow non-matching argument types as long as they don't
1445 // violate the principle of substitutability. Specifically, the
1446 // implementation must accept any objects that the superclass
1447 // accepts, however it may also accept others.
1448 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001449 return false;
John McCall071df462010-10-28 02:34:38 +00001450
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001451 DiagID =
1452 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1453 : diag::warn_non_contravariant_param_types;
John McCall071df462010-10-28 02:34:38 +00001454 }
1455 }
1456
1457 S.Diag(ImplVar->getLocation(), DiagID)
1458 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001459 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1460 S.Diag(IfaceVar->getLocation(),
1461 (IsOverridingMode ? diag::note_previous_declaration
1462 : diag::note_previous_definition))
John McCall071df462010-10-28 02:34:38 +00001463 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001464 return false;
John McCall071df462010-10-28 02:34:38 +00001465}
John McCall31168b02011-06-15 23:02:42 +00001466
1467/// In ARC, check whether the conventional meanings of the two methods
1468/// match. If they don't, it's a hard error.
1469static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1470 ObjCMethodDecl *decl) {
1471 ObjCMethodFamily implFamily = impl->getMethodFamily();
1472 ObjCMethodFamily declFamily = decl->getMethodFamily();
1473 if (implFamily == declFamily) return false;
1474
1475 // Since conventions are sorted by selector, the only possibility is
1476 // that the types differ enough to cause one selector or the other
1477 // to fall out of the family.
1478 assert(implFamily == OMF_None || declFamily == OMF_None);
1479
1480 // No further diagnostics required on invalid declarations.
1481 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1482
1483 const ObjCMethodDecl *unmatched = impl;
1484 ObjCMethodFamily family = declFamily;
1485 unsigned errorID = diag::err_arc_lost_method_convention;
1486 unsigned noteID = diag::note_arc_lost_method_convention;
1487 if (declFamily == OMF_None) {
1488 unmatched = decl;
1489 family = implFamily;
1490 errorID = diag::err_arc_gained_method_convention;
1491 noteID = diag::note_arc_gained_method_convention;
1492 }
1493
1494 // Indexes into a %select clause in the diagnostic.
1495 enum FamilySelector {
1496 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1497 };
1498 FamilySelector familySelector = FamilySelector();
1499
1500 switch (family) {
1501 case OMF_None: llvm_unreachable("logic error, no method convention");
1502 case OMF_retain:
1503 case OMF_release:
1504 case OMF_autorelease:
1505 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00001506 case OMF_finalize:
John McCall31168b02011-06-15 23:02:42 +00001507 case OMF_retainCount:
1508 case OMF_self:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001509 case OMF_performSelector:
John McCall31168b02011-06-15 23:02:42 +00001510 // Mismatches for these methods don't change ownership
1511 // conventions, so we don't care.
1512 return false;
1513
1514 case OMF_init: familySelector = F_init; break;
1515 case OMF_alloc: familySelector = F_alloc; break;
1516 case OMF_copy: familySelector = F_copy; break;
1517 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1518 case OMF_new: familySelector = F_new; break;
1519 }
1520
1521 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1522 ReasonSelector reasonSelector;
1523
1524 // The only reason these methods don't fall within their families is
1525 // due to unusual result types.
1526 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1527 reasonSelector = R_UnrelatedReturn;
1528 } else {
1529 reasonSelector = R_NonObjectReturn;
1530 }
1531
Joerg Sonnenbergerffc6d492013-06-26 21:31:47 +00001532 S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
1533 S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
John McCall31168b02011-06-15 23:02:42 +00001534
1535 return true;
1536}
John McCall071df462010-10-28 02:34:38 +00001537
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +00001538void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001539 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001540 bool IsProtocolMethodDecl) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001541 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00001542 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1543 return;
1544
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001545 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001546 IsProtocolMethodDecl, false,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001547 true);
Mike Stump11289f42009-09-09 15:08:12 +00001548
Chris Lattner67f35b02009-04-11 19:58:42 +00001549 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001550 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1551 EF = MethodDecl->param_end();
1552 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001553 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001554 IsProtocolMethodDecl, false, true);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001555 }
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001556
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001557 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001558 Diag(ImpMethodDecl->getLocation(),
1559 diag::warn_conflicting_variadic);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001560 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001561 }
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001562}
1563
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001564void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1565 ObjCMethodDecl *Overridden,
1566 bool IsProtocolMethodDecl) {
1567
1568 CheckMethodOverrideReturn(*this, Method, Overridden,
1569 IsProtocolMethodDecl, true,
1570 true);
1571
1572 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001573 IF = Overridden->param_begin(), EM = Method->param_end(),
1574 EF = Overridden->param_end();
1575 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001576 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1577 IsProtocolMethodDecl, true, true);
1578 }
1579
1580 if (Method->isVariadic() != Overridden->isVariadic()) {
1581 Diag(Method->getLocation(),
1582 diag::warn_conflicting_overriding_variadic);
1583 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1584 }
1585}
1586
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001587/// WarnExactTypedMethods - This routine issues a warning if method
1588/// implementation declaration matches exactly that of its declaration.
1589void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1590 ObjCMethodDecl *MethodDecl,
1591 bool IsProtocolMethodDecl) {
1592 // don't issue warning when protocol method is optional because primary
1593 // class is not required to implement it and it is safe for protocol
1594 // to implement it.
1595 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1596 return;
1597 // don't issue warning when primary class's method is
1598 // depecated/unavailable.
1599 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1600 MethodDecl->hasAttr<DeprecatedAttr>())
1601 return;
1602
1603 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1604 IsProtocolMethodDecl, false, false);
1605 if (match)
1606 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001607 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1608 EF = MethodDecl->param_end();
1609 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001610 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1611 *IM, *IF,
1612 IsProtocolMethodDecl, false, false);
1613 if (!match)
1614 break;
1615 }
1616 if (match)
1617 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall92918512011-08-08 17:32:19 +00001618 if (match)
1619 match = !(MethodDecl->isClassMethod() &&
1620 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001621
1622 if (match) {
1623 Diag(ImpMethodDecl->getLocation(),
1624 diag::warn_category_method_impl_match);
Ted Kremenek59b10db2012-02-27 22:55:11 +00001625 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1626 << MethodDecl->getDeclName();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001627 }
1628}
1629
Mike Stump87c57ac2009-05-16 07:39:55 +00001630/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1631/// improve the efficiency of selector lookups and type checking by associating
1632/// with each protocol / interface / category the flattened instance tables. If
1633/// we used an immutable set to keep the table then it wouldn't add significant
1634/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001635
Steve Naroffa36992242008-02-08 22:06:17 +00001636/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +00001637/// Declared in protocol, and those referenced by it.
Ted Kremenek285ee852013-12-13 06:26:10 +00001638static void CheckProtocolMethodDefs(Sema &S,
1639 SourceLocation ImpLoc,
1640 ObjCProtocolDecl *PDecl,
1641 bool& IncompleteImpl,
1642 const Sema::SelectorSet &InsMap,
1643 const Sema::SelectorSet &ClsMap,
1644 ObjCContainerDecl *CDecl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001645 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1646 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1647 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +00001648 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1649
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001650 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001651 ObjCInterfaceDecl *NSIDecl = 0;
Ted Kremenek285ee852013-12-13 06:26:10 +00001652 if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump11289f42009-09-09 15:08:12 +00001653 // check to see if class implements forwardInvocation method and objects
1654 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001655 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +00001656 // Under such conditions, which means that every method possible is
1657 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001658 // found" warnings.
1659 // FIXME: Use a general GetUnarySelector method for this.
Ted Kremenek285ee852013-12-13 06:26:10 +00001660 IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation");
1661 Selector fISelector = S.Context.Selectors.getSelector(1, &II);
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001662 if (InsMap.count(fISelector))
1663 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1664 // need be implemented in the implementation.
Ted Kremenek285ee852013-12-13 06:26:10 +00001665 NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy"));
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001666 }
Mike Stump11289f42009-09-09 15:08:12 +00001667
Fariborz Jahanianc41cf052013-01-07 19:21:03 +00001668 // If this is a forward protocol declaration, get its definition.
1669 if (!PDecl->isThisDeclarationADefinition() &&
1670 PDecl->getDefinition())
1671 PDecl = PDecl->getDefinition();
1672
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001673 // If a method lookup fails locally we still need to look and see if
1674 // the method was implemented by a base class or an inherited
1675 // protocol. This lookup is slow, but occurs rarely in correct code
1676 // and otherwise would terminate in a warning.
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001677 if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1678 Super = NULL;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001679
Chris Lattnerda463fe2007-12-12 07:09:47 +00001680 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001681 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +00001682 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001683 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001684 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001685 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rosed01e83a2012-10-10 16:42:25 +00001686 !method->isPropertyAccessor() &&
1687 !InsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001688 (!Super || !Super->lookupMethod(method->getSelector(),
1689 true /* instance */,
1690 false /* shallowCategory */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001691 true /* followsSuper */,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001692 NULL /* category */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001693 // If a method is not implemented in the category implementation but
1694 // has been declared in its primary class, superclass,
1695 // or in one of their protocols, no need to issue the warning.
1696 // This is because method will be implemented in the primary class
1697 // or one of its super class implementation.
1698
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001699 // Ugly, but necessary. Method declared in protcol might have
1700 // have been synthesized due to a property declared in the class which
1701 // uses the protocol.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001702 if (ObjCMethodDecl *MethodInClass =
Ted Kremenek00781502013-11-23 01:01:29 +00001703 IDecl->lookupMethod(method->getSelector(),
1704 true /* instance */,
1705 true /* shallowCategoryLookup */,
1706 false /* followSuper */))
Jordan Rosed01e83a2012-10-10 16:42:25 +00001707 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001708 continue;
1709 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Ted Kremenek285ee852013-12-13 06:26:10 +00001710 if (S.Diags.getDiagnosticLevel(DIAG, ImpLoc)
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001711 != DiagnosticsEngine::Ignored) {
Ted Kremenek285ee852013-12-13 06:26:10 +00001712 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG,
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001713 PDecl);
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001714 }
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001715 }
1716 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001717 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +00001718 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001719 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001720 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001721 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001722 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1723 !ClsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001724 (!Super || !Super->lookupMethod(method->getSelector(),
1725 false /* class method */,
1726 false /* shallowCategoryLookup */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001727 true /* followSuper */,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001728 NULL /* category */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001729 // See above comment for instance method lookups.
Ted Kremenek00781502013-11-23 01:01:29 +00001730 if (C && IDecl->lookupMethod(method->getSelector(),
1731 false /* class */,
1732 true /* shallowCategoryLookup */,
1733 false /* followSuper */))
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001734 continue;
Ted Kremenek00781502013-11-23 01:01:29 +00001735
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001736 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Ted Kremenek285ee852013-12-13 06:26:10 +00001737 if (S.Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
David Blaikie9c902b52011-09-25 23:23:43 +00001738 DiagnosticsEngine::Ignored) {
Ted Kremenek285ee852013-12-13 06:26:10 +00001739 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl);
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001740 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001741 }
Steve Naroff3ce37a62007-12-14 23:37:57 +00001742 }
Chris Lattner390d39a2008-07-21 21:32:27 +00001743 // Check on this protocols's referenced protocols, recursively.
1744 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1745 E = PDecl->protocol_end(); PI != E; ++PI)
Ted Kremenek285ee852013-12-13 06:26:10 +00001746 CheckProtocolMethodDefs(S, ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap,
1747 CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001748}
1749
Fariborz Jahanianf9ae68a2011-07-16 00:08:33 +00001750/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001751/// or protocol against those declared in their implementations.
1752///
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001753void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1754 const SelectorSet &ClsMap,
1755 SelectorSet &InsMapSeen,
1756 SelectorSet &ClsMapSeen,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001757 ObjCImplDecl* IMPDecl,
1758 ObjCContainerDecl* CDecl,
1759 bool &IncompleteImpl,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001760 bool ImmediateClass,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001761 bool WarnCategoryMethodImpl) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001762 // Check and see if instance methods in class interface have been
1763 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001764 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1765 E = CDecl->instmeth_end(); I != E; ++I) {
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001766 if (!InsMapSeen.insert((*I)->getSelector()))
1767 continue;
Jordan Rosed01e83a2012-10-10 16:42:25 +00001768 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001769 !InsMap.count((*I)->getSelector())) {
1770 if (ImmediateClass)
Ted Kremenekf87decd2013-12-13 05:58:44 +00001771 WarnUndefinedMethod(*this, IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001772 diag::warn_undef_method_impl);
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001773 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +00001774 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001775 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001776 IMPDecl->getInstanceMethod((*I)->getSelector());
1777 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1778 "Expected to find the method through lookup as well");
1779 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001780 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001781 if (ImpMethodDecl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001782 if (!WarnCategoryMethodImpl)
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001783 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1784 isa<ObjCProtocolDecl>(CDecl));
Jordan Rosed01e83a2012-10-10 16:42:25 +00001785 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001786 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001787 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001788 }
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001789 }
1790 }
Mike Stump11289f42009-09-09 15:08:12 +00001791
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001792 // Check and see if class methods in class interface have been
1793 // implemented in the implementation class. If so, their types match.
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001794 for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
1795 E = CDecl->classmeth_end();
1796 I != E; ++I) {
1797 if (!ClsMapSeen.insert((*I)->getSelector()))
1798 continue;
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001799 if (!ClsMap.count((*I)->getSelector())) {
1800 if (ImmediateClass)
Ted Kremenekf87decd2013-12-13 05:58:44 +00001801 WarnUndefinedMethod(*this, IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001802 diag::warn_undef_method_impl);
Mike Stump12b8ce12009-08-04 21:02:39 +00001803 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001804 ObjCMethodDecl *ImpMethodDecl =
1805 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001806 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1807 "Expected to find the method through lookup as well");
1808 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001809 if (!WarnCategoryMethodImpl)
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001810 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1811 isa<ObjCProtocolDecl>(CDecl));
1812 else
1813 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001814 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001815 }
1816 }
Fariborz Jahanian73853e52010-10-08 22:59:25 +00001817
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001818 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) {
1819 // Also, check for methods declared in protocols inherited by
1820 // this protocol.
1821 for (ObjCProtocolDecl::protocol_iterator
1822 PI = PD->protocol_begin(), E = PD->protocol_end(); PI != E; ++PI)
1823 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1824 IMPDecl, (*PI), IncompleteImpl, false,
1825 WarnCategoryMethodImpl);
1826 }
1827
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001828 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001829 // when checking that methods in implementation match their declaration,
1830 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1831 // extension; as well as those in categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001832 if (!WarnCategoryMethodImpl) {
1833 for (ObjCInterfaceDecl::visible_categories_iterator
1834 Cat = I->visible_categories_begin(),
1835 CatEnd = I->visible_categories_end();
1836 Cat != CatEnd; ++Cat) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001837 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001838 IMPDecl, *Cat, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001839 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001840 }
1841 } else {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001842 // Also methods in class extensions need be looked at next.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001843 for (ObjCInterfaceDecl::visible_extensions_iterator
1844 Ext = I->visible_extensions_begin(),
1845 ExtEnd = I->visible_extensions_end();
1846 Ext != ExtEnd; ++Ext) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001847 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001848 IMPDecl, *Ext, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001849 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001850 }
1851 }
1852
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001853 // Check for any implementation of a methods declared in protocol.
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001854 for (ObjCInterfaceDecl::all_protocol_iterator
1855 PI = I->all_referenced_protocol_begin(),
1856 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001857 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1858 IMPDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001859 (*PI), IncompleteImpl, false,
1860 WarnCategoryMethodImpl);
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001861
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001862 // FIXME. For now, we are not checking for extact match of methods
1863 // in category implementation and its primary class's super class.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001864 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001865 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +00001866 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001867 I->getSuperClass(), IncompleteImpl, false);
1868 }
1869}
1870
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001871/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1872/// category matches with those implemented in its primary class and
1873/// warns each time an exact match is found.
1874void Sema::CheckCategoryVsClassMethodMatches(
1875 ObjCCategoryImplDecl *CatIMPDecl) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001876 // Get category's primary class.
1877 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1878 if (!CatDecl)
1879 return;
1880 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1881 if (!IDecl)
1882 return;
Fariborz Jahanianf3077a22013-12-05 20:52:31 +00001883 ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass();
1884 SelectorSet InsMap, ClsMap;
1885
1886 for (ObjCImplementationDecl::instmeth_iterator
1887 I = CatIMPDecl->instmeth_begin(),
1888 E = CatIMPDecl->instmeth_end(); I!=E; ++I) {
1889 Selector Sel = (*I)->getSelector();
1890 // When checking for methods implemented in the category, skip over
1891 // those declared in category class's super class. This is because
1892 // the super class must implement the method.
1893 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true))
1894 continue;
1895 InsMap.insert(Sel);
1896 }
1897
1898 for (ObjCImplementationDecl::classmeth_iterator
1899 I = CatIMPDecl->classmeth_begin(),
1900 E = CatIMPDecl->classmeth_end(); I != E; ++I) {
1901 Selector Sel = (*I)->getSelector();
1902 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false))
1903 continue;
1904 ClsMap.insert(Sel);
1905 }
1906 if (InsMap.empty() && ClsMap.empty())
1907 return;
1908
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001909 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001910 bool IncompleteImpl = false;
1911 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1912 CatIMPDecl, IDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001913 IncompleteImpl, false,
1914 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001915}
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001916
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001917void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump11289f42009-09-09 15:08:12 +00001918 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001919 bool IncompleteImpl) {
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001920 SelectorSet InsMap;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001921 // Check and see if instance methods in class interface have been
1922 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001923 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001924 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001925 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001926
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001927 // Check and see if properties declared in the interface have either 1)
1928 // an implementation or 2) there is a @synthesize/@dynamic implementation
1929 // of the property in the @implementation.
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001930 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall5fb5df92012-06-20 06:18:46 +00001931 if (!(LangOpts.ObjCDefaultSynthProperties &&
1932 LangOpts.ObjCRuntime.isNonFragile()) ||
1933 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001934 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001935
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001936 SelectorSet ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +00001937 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001938 I = IMPDecl->classmeth_begin(),
1939 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001940 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001941
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001942 // Check for type conflict of methods declared in a class/protocol and
1943 // its implementation; if any.
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001944 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +00001945 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1946 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001947 IncompleteImpl, true);
Fariborz Jahanian2bda1b62011-08-03 18:21:12 +00001948
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001949 // check all methods implemented in category against those declared
1950 // in its primary class.
1951 if (ObjCCategoryImplDecl *CatDecl =
1952 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1953 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001954
Chris Lattnerda463fe2007-12-12 07:09:47 +00001955 // Check the protocol list for unimplemented methods in the @implementation
1956 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001957 // Check and see if class methods in class interface have been
1958 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001959
Chris Lattner9ef10f42009-03-01 00:56:52 +00001960 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001961 for (ObjCInterfaceDecl::all_protocol_iterator
1962 PI = I->all_referenced_protocol_begin(),
1963 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Ted Kremenek285ee852013-12-13 06:26:10 +00001964 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), *PI,
1965 IncompleteImpl, InsMap, ClsMap, I);
Chris Lattner9ef10f42009-03-01 00:56:52 +00001966 // Check class extensions (unnamed categories)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001967 for (ObjCInterfaceDecl::visible_extensions_iterator
1968 Ext = I->visible_extensions_begin(),
1969 ExtEnd = I->visible_extensions_end();
1970 Ext != ExtEnd; ++Ext) {
1971 ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
1972 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001973 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001974 // For extended class, unimplemented methods in its protocols will
1975 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001976 if (!C->IsClassExtension()) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001977 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1978 E = C->protocol_end(); PI != E; ++PI)
Ted Kremenek285ee852013-12-13 06:26:10 +00001979 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), *PI,
1980 IncompleteImpl, InsMap, ClsMap, CDecl);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001981 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00001982 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001983 } else
David Blaikie83d382b2011-09-23 05:06:16 +00001984 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00001985}
1986
Mike Stump11289f42009-09-09 15:08:12 +00001987/// ActOnForwardClassDeclaration -
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00001988Sema::DeclGroupPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00001989Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00001990 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00001991 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00001992 unsigned NumElts) {
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00001993 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001994 for (unsigned i = 0; i != NumElts; ++i) {
1995 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001996 NamedDecl *PrevDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001997 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001998 LookupOrdinaryName, ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001999 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00002000 // GCC apparently allows the following idiom:
2001 //
2002 // typedef NSObject < XCElementTogglerP > XCElementToggler;
2003 // @class XCElementToggler;
2004 //
Fariborz Jahanian04c44552012-01-24 00:40:15 +00002005 // Here we have chosen to ignore the forward class declaration
2006 // with a warning. Since this is the implied behavior.
Richard Smithdda56e42011-04-15 14:24:37 +00002007 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCall8b07ec22010-05-15 11:32:37 +00002008 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002009 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00002010 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCall8b07ec22010-05-15 11:32:37 +00002011 } else {
Mike Stump12b8ce12009-08-04 21:02:39 +00002012 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahanian04c44552012-01-24 00:40:15 +00002013 // to the underlying class. Just ignore the forward class with a warning
2014 // as this will force the intended behavior which is to lookup the typedef
2015 // name.
2016 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
2017 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
2018 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
2019 continue;
2020 }
Fariborz Jahanian0d451812009-05-07 21:49:26 +00002021 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002022 }
Douglas Gregordc9166c2011-12-15 20:29:51 +00002023
2024 // Create a declaration to describe this forward declaration.
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00002025 ObjCInterfaceDecl *PrevIDecl
2026 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002027
2028 IdentifierInfo *ClassName = IdentList[i];
2029 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
2030 // A previous decl with a different name is because of
2031 // @compatibility_alias, for example:
2032 // \code
2033 // @class NewImage;
2034 // @compatibility_alias OldImage NewImage;
2035 // \endcode
2036 // A lookup for 'OldImage' will return the 'NewImage' decl.
2037 //
2038 // In such a case use the real declaration name, instead of the alias one,
2039 // otherwise we will break IdentifierResolver and redecls-chain invariants.
2040 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
2041 // has been aliased.
2042 ClassName = PrevIDecl->getIdentifier();
2043 }
2044
Douglas Gregordc9166c2011-12-15 20:29:51 +00002045 ObjCInterfaceDecl *IDecl
2046 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002047 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002048 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002049
Douglas Gregordc9166c2011-12-15 20:29:51 +00002050 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeafd0b2011-12-27 22:43:10 +00002051 CheckObjCDeclScope(IDecl);
2052 DeclsInGroup.push_back(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002053 }
Rafael Espindolaab417692013-07-09 12:05:01 +00002054
2055 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002056}
2057
John McCall54507ab2011-06-16 01:15:19 +00002058static bool tryMatchRecordTypes(ASTContext &Context,
2059 Sema::MethodMatchStrategy strategy,
2060 const Type *left, const Type *right);
2061
John McCall31168b02011-06-15 23:02:42 +00002062static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
2063 QualType leftQT, QualType rightQT) {
2064 const Type *left =
2065 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2066 const Type *right =
2067 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2068
2069 if (left == right) return true;
2070
2071 // If we're doing a strict match, the types have to match exactly.
2072 if (strategy == Sema::MMS_strict) return false;
2073
2074 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2075
2076 // Otherwise, use this absurdly complicated algorithm to try to
2077 // validate the basic, low-level compatibility of the two types.
2078
2079 // As a minimum, require the sizes and alignments to match.
2080 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
2081 return false;
2082
2083 // Consider all the kinds of non-dependent canonical types:
2084 // - functions and arrays aren't possible as return and parameter types
2085
2086 // - vector types of equal size can be arbitrarily mixed
2087 if (isa<VectorType>(left)) return isa<VectorType>(right);
2088 if (isa<VectorType>(right)) return false;
2089
2090 // - references should only match references of identical type
John McCall54507ab2011-06-16 01:15:19 +00002091 // - structs, unions, and Objective-C objects must match more-or-less
2092 // exactly
John McCall31168b02011-06-15 23:02:42 +00002093 // - everything else should be a scalar
2094 if (!left->isScalarType() || !right->isScalarType())
John McCall54507ab2011-06-16 01:15:19 +00002095 return tryMatchRecordTypes(Context, strategy, left, right);
John McCall31168b02011-06-15 23:02:42 +00002096
John McCall9320b872011-09-09 05:25:32 +00002097 // Make scalars agree in kind, except count bools as chars, and group
2098 // all non-member pointers together.
John McCall31168b02011-06-15 23:02:42 +00002099 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2100 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2101 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2102 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall9320b872011-09-09 05:25:32 +00002103 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2104 leftSK = Type::STK_ObjCObjectPointer;
2105 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2106 rightSK = Type::STK_ObjCObjectPointer;
John McCall31168b02011-06-15 23:02:42 +00002107
2108 // Note that data member pointers and function member pointers don't
2109 // intermix because of the size differences.
2110
2111 return (leftSK == rightSK);
2112}
Chris Lattnerda463fe2007-12-12 07:09:47 +00002113
John McCall54507ab2011-06-16 01:15:19 +00002114static bool tryMatchRecordTypes(ASTContext &Context,
2115 Sema::MethodMatchStrategy strategy,
2116 const Type *lt, const Type *rt) {
2117 assert(lt && rt && lt != rt);
2118
2119 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2120 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2121 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2122
2123 // Require union-hood to match.
2124 if (left->isUnion() != right->isUnion()) return false;
2125
2126 // Require an exact match if either is non-POD.
2127 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2128 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2129 return false;
2130
2131 // Require size and alignment to match.
2132 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2133
2134 // Require fields to match.
2135 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2136 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2137 for (; li != le && ri != re; ++li, ++ri) {
2138 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2139 return false;
2140 }
2141 return (li == le && ri == re);
2142}
2143
Chris Lattnerda463fe2007-12-12 07:09:47 +00002144/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2145/// returns true, or false, accordingly.
2146/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCall31168b02011-06-15 23:02:42 +00002147bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2148 const ObjCMethodDecl *right,
2149 MethodMatchStrategy strategy) {
2150 if (!matchTypes(Context, strategy,
2151 left->getResultType(), right->getResultType()))
2152 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002153
Douglas Gregor560b7fa2013-02-07 19:13:24 +00002154 // If either is hidden, it is not considered to match.
2155 if (left->isHidden() || right->isHidden())
2156 return false;
2157
David Blaikiebbafb8a2012-03-11 07:00:24 +00002158 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002159 (left->hasAttr<NSReturnsRetainedAttr>()
2160 != right->hasAttr<NSReturnsRetainedAttr>() ||
2161 left->hasAttr<NSConsumesSelfAttr>()
2162 != right->hasAttr<NSConsumesSelfAttr>()))
2163 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002164
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002165 ObjCMethodDecl::param_const_iterator
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002166 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2167 re = right->param_end();
Mike Stump11289f42009-09-09 15:08:12 +00002168
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002169 for (; li != le && ri != re; ++li, ++ri) {
John McCall31168b02011-06-15 23:02:42 +00002170 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002171 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCall31168b02011-06-15 23:02:42 +00002172
2173 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2174 return false;
2175
David Blaikiebbafb8a2012-03-11 07:00:24 +00002176 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002177 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2178 return false;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002179 }
2180 return true;
2181}
2182
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002183void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002184 // Record at the head of the list whether there were 0, 1, or >= 2 methods
2185 // inside categories.
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00002186 if (ObjCCategoryDecl *
2187 CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
2188 if (!CD->IsClassExtension() && List->getBits() < 2)
2189 List->setBits(List->getBits()+1);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002190
Douglas Gregorc454afe2012-01-25 00:19:56 +00002191 // If the list is empty, make it a singleton list.
2192 if (List->Method == 0) {
2193 List->Method = Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002194 List->setNext(0);
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002195 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002196 }
2197
2198 // We've seen a method with this name, see if we have already seen this type
2199 // signature.
2200 ObjCMethodList *Previous = List;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002201 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregor600a2f52013-06-21 00:20:25 +00002202 // If we are building a module, keep all of the methods.
2203 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2204 continue;
2205
Douglas Gregore1716012012-01-25 00:49:42 +00002206 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregorc454afe2012-01-25 00:19:56 +00002207 continue;
2208
2209 ObjCMethodDecl *PrevObjCMethod = List->Method;
2210
2211 // Propagate the 'defined' bit.
2212 if (Method->isDefined())
2213 PrevObjCMethod->setDefined(true);
2214
2215 // If a method is deprecated, push it in the global pool.
2216 // This is used for better diagnostics.
2217 if (Method->isDeprecated()) {
2218 if (!PrevObjCMethod->isDeprecated())
2219 List->Method = Method;
2220 }
2221 // If new method is unavailable, push it into global pool
2222 // unless previous one is deprecated.
2223 if (Method->isUnavailable()) {
2224 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2225 List->Method = Method;
2226 }
2227
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002228 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002229 }
2230
2231 // We have a new signature for an existing method - add it.
2232 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregore1716012012-01-25 00:49:42 +00002233 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002234 Previous->setNext(new (Mem) ObjCMethodList(Method, 0));
Douglas Gregorc454afe2012-01-25 00:19:56 +00002235}
2236
Sebastian Redl75d8a322010-08-02 23:18:59 +00002237/// \brief Read the contents of the method pool for a given selector from
2238/// external storage.
Douglas Gregore1716012012-01-25 00:49:42 +00002239void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002240 assert(ExternalSource && "We need an external AST source");
Douglas Gregore1716012012-01-25 00:49:42 +00002241 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002242}
2243
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002244void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002245 bool instance) {
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002246 // Ignore methods of invalid containers.
2247 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002248 return;
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002249
Douglas Gregor70f449b2012-01-25 00:59:09 +00002250 if (ExternalSource)
2251 ReadMethodPool(Method->getSelector());
2252
Sebastian Redl75d8a322010-08-02 23:18:59 +00002253 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor70f449b2012-01-25 00:59:09 +00002254 if (Pos == MethodPool.end())
2255 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2256 GlobalMethods())).first;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002257
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002258 Method->setDefined(impl);
Douglas Gregorc454afe2012-01-25 00:19:56 +00002259
Sebastian Redl75d8a322010-08-02 23:18:59 +00002260 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002261 addMethodToGlobalList(&Entry, Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002262}
2263
John McCall31168b02011-06-15 23:02:42 +00002264/// Determines if this is an "acceptable" loose mismatch in the global
2265/// method pool. This exists mostly as a hack to get around certain
2266/// global mismatches which we can't afford to make warnings / errors.
2267/// Really, what we want is a way to take a method out of the global
2268/// method pool.
2269static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2270 ObjCMethodDecl *other) {
2271 if (!chosen->isInstanceMethod())
2272 return false;
2273
2274 Selector sel = chosen->getSelector();
2275 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2276 return false;
2277
2278 // Don't complain about mismatches for -length if the method we
2279 // chose has an integral result type.
2280 return (chosen->getResultType()->isIntegerType());
2281}
2282
Sebastian Redl75d8a322010-08-02 23:18:59 +00002283ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00002284 bool receiverIdOrClass,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002285 bool warn, bool instance) {
Douglas Gregor70f449b2012-01-25 00:59:09 +00002286 if (ExternalSource)
2287 ReadMethodPool(Sel);
2288
Sebastian Redl75d8a322010-08-02 23:18:59 +00002289 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor70f449b2012-01-25 00:59:09 +00002290 if (Pos == MethodPool.end())
2291 return 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002292
Douglas Gregor77f49a42013-01-16 18:47:38 +00002293 // Gather the non-hidden methods.
Sebastian Redl75d8a322010-08-02 23:18:59 +00002294 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Robert Wilhelmb869a8f2013-08-10 12:33:24 +00002295 SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002296 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Douglas Gregor77f49a42013-01-16 18:47:38 +00002297 if (M->Method && !M->Method->isHidden()) {
2298 // If we're not supposed to warn about mismatches, we're done.
2299 if (!warn)
2300 return M->Method;
Mike Stump11289f42009-09-09 15:08:12 +00002301
Douglas Gregor77f49a42013-01-16 18:47:38 +00002302 Methods.push_back(M->Method);
Sebastian Redl75d8a322010-08-02 23:18:59 +00002303 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00002304 }
Douglas Gregor77f49a42013-01-16 18:47:38 +00002305
2306 // If there aren't any visible methods, we're done.
2307 // FIXME: Recover if there are any known-but-hidden methods?
2308 if (Methods.empty())
2309 return 0;
2310
2311 if (Methods.size() == 1)
2312 return Methods[0];
2313
2314 // We found multiple methods, so we may have to complain.
2315 bool issueDiagnostic = false, issueError = false;
2316
2317 // We support a warning which complains about *any* difference in
2318 // method signature.
2319 bool strictSelectorMatch =
2320 (receiverIdOrClass && warn &&
2321 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2322 R.getBegin())
2323 != DiagnosticsEngine::Ignored));
2324 if (strictSelectorMatch) {
2325 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2326 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2327 issueDiagnostic = true;
2328 break;
2329 }
2330 }
2331 }
2332
2333 // If we didn't see any strict differences, we won't see any loose
2334 // differences. In ARC, however, we also need to check for loose
2335 // mismatches, because most of them are errors.
2336 if (!strictSelectorMatch ||
2337 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2338 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2339 // This checks if the methods differ in type mismatch.
2340 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2341 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2342 issueDiagnostic = true;
2343 if (getLangOpts().ObjCAutoRefCount)
2344 issueError = true;
2345 break;
2346 }
2347 }
2348
2349 if (issueDiagnostic) {
2350 if (issueError)
2351 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2352 else if (strictSelectorMatch)
2353 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2354 else
2355 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2356
2357 Diag(Methods[0]->getLocStart(),
2358 issueError ? diag::note_possibility : diag::note_using)
2359 << Methods[0]->getSourceRange();
2360 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2361 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2362 << Methods[I]->getSourceRange();
2363 }
2364 }
2365 return Methods[0];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002366}
2367
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002368ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redl75d8a322010-08-02 23:18:59 +00002369 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2370 if (Pos == MethodPool.end())
2371 return 0;
2372
2373 GlobalMethods &Methods = Pos->second;
2374
2375 if (Methods.first.Method && Methods.first.Method->isDefined())
2376 return Methods.first.Method;
2377 if (Methods.second.Method && Methods.second.Method->isDefined())
2378 return Methods.second.Method;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002379 return 0;
2380}
2381
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002382static void
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002383HelperSelectorsForTypoCorrection(
2384 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2385 StringRef Typo, const ObjCMethodDecl * Method) {
2386 const unsigned MaxEditDistance = 1;
2387 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieuea8d3702013-06-06 02:22:29 +00002388 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002389
2390 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2391 if (MinPossibleEditDistance > 0 &&
2392 Typo.size() / MinPossibleEditDistance < 1)
2393 return;
2394 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2395 if (EditDistance > MaxEditDistance)
2396 return;
2397 if (EditDistance == BestEditDistance)
2398 BestMethod.push_back(Method);
2399 else if (EditDistance < BestEditDistance) {
2400 BestMethod.clear();
2401 BestMethod.push_back(Method);
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002402 }
2403}
2404
Fariborz Jahanian75481672013-06-17 17:10:54 +00002405static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2406 QualType ObjectType) {
2407 if (ObjectType.isNull())
2408 return true;
2409 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2410 return true;
2411 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != 0;
2412}
2413
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002414const ObjCMethodDecl *
Fariborz Jahanian75481672013-06-17 17:10:54 +00002415Sema::SelectorsForTypoCorrection(Selector Sel,
2416 QualType ObjectType) {
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002417 unsigned NumArgs = Sel.getNumArgs();
2418 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002419 bool ObjectIsId = true, ObjectIsClass = true;
2420 if (ObjectType.isNull())
2421 ObjectIsId = ObjectIsClass = false;
2422 else if (!ObjectType->isObjCObjectPointerType())
2423 return 0;
2424 else if (const ObjCObjectPointerType *ObjCPtr =
2425 ObjectType->getAsObjCInterfacePointerType()) {
2426 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2427 ObjectIsId = ObjectIsClass = false;
2428 }
2429 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2430 ObjectIsClass = false;
2431 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2432 ObjectIsId = false;
2433 else
2434 return 0;
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002435
2436 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2437 e = MethodPool.end(); b != e; b++) {
2438 // instance methods
2439 for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
2440 if (M->Method &&
Fariborz Jahanian06499232013-06-18 17:10:58 +00002441 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2442 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002443 if (ObjectIsId)
2444 Methods.push_back(M->Method);
2445 else if (!ObjectIsClass &&
2446 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2447 Methods.push_back(M->Method);
2448 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002449 // class methods
2450 for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
2451 if (M->Method &&
Fariborz Jahanian06499232013-06-18 17:10:58 +00002452 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2453 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002454 if (ObjectIsClass)
2455 Methods.push_back(M->Method);
2456 else if (!ObjectIsId &&
2457 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2458 Methods.push_back(M->Method);
2459 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002460 }
2461
2462 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2463 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2464 HelperSelectorsForTypoCorrection(SelectedMethods,
2465 Sel.getAsString(), Methods[i]);
2466 }
2467 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : NULL;
2468}
2469
2470static void
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002471HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S,
2472 ObjCMethodList &MethList) {
2473 ObjCMethodList *M = &MethList;
2474 ObjCMethodDecl *TargetMethod = M->Method;
2475 while (TargetMethod &&
2476 isa<ObjCImplDecl>(TargetMethod->getDeclContext())) {
2477 M = M->getNext();
2478 TargetMethod = M ? M->Method : 0;
2479 }
2480 if (!TargetMethod)
2481 return;
2482 bool FirstTime = true;
2483 for (M = M->getNext(); M; M=M->getNext()) {
2484 ObjCMethodDecl *MatchingMethodDecl = M->Method;
2485 if (isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext()))
2486 continue;
2487 if (!S.MatchTwoMethodDeclarations(TargetMethod,
2488 MatchingMethodDecl, Sema::MMS_loose)) {
2489 if (FirstTime) {
2490 FirstTime = false;
2491 S.Diag(TargetMethod->getLocation(), diag::warning_multiple_selectors)
2492 << TargetMethod->getSelector();
2493 }
2494 S.Diag(MatchingMethodDecl->getLocation(), diag::note_also_found);
2495 }
2496 }
2497}
2498
2499void Sema::DiagnoseMismatchedMethodsInGlobalPool() {
2500 unsigned DIAG = diag::warning_multiple_selectors;
2501 if (Diags.getDiagnosticLevel(DIAG, SourceLocation())
2502 == DiagnosticsEngine::Ignored)
2503 return;
2504 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2505 e = MethodPool.end(); b != e; b++) {
2506 // first, instance methods
2507 ObjCMethodList &InstMethList = b->second.first;
2508 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, InstMethList);
Fariborz Jahanianc07e8932013-05-30 21:52:50 +00002509 // second, class methods
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002510 ObjCMethodList &ClsMethList = b->second.second;
2511 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, ClsMethList);
2512 }
2513}
2514
2515/// DiagnoseDuplicateIvars -
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002516/// Check for duplicate ivars in the entire class at the start of
James Dennett634962f2012-06-14 21:40:34 +00002517/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002518/// add ivars to a class in random order which will not be known until
James Dennett634962f2012-06-14 21:40:34 +00002519/// class's \@implementation is seen.
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002520void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2521 ObjCInterfaceDecl *SID) {
2522 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2523 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie40ed2972012-06-06 20:45:41 +00002524 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002525 if (Ivar->isInvalidDecl())
2526 continue;
2527 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2528 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2529 if (prevIvar) {
2530 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2531 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2532 Ivar->setInvalidDecl();
2533 }
2534 }
2535 }
2536}
2537
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002538Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2539 switch (CurContext->getDeclKind()) {
2540 case Decl::ObjCInterface:
2541 return Sema::OCK_Interface;
2542 case Decl::ObjCProtocol:
2543 return Sema::OCK_Protocol;
2544 case Decl::ObjCCategory:
2545 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2546 return Sema::OCK_ClassExtension;
2547 else
2548 return Sema::OCK_Category;
2549 case Decl::ObjCImplementation:
2550 return Sema::OCK_Implementation;
2551 case Decl::ObjCCategoryImpl:
2552 return Sema::OCK_CategoryImplementation;
2553
2554 default:
2555 return Sema::OCK_None;
2556 }
2557}
2558
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002559// Note: For class/category implementations, allMethods is always null.
Robert Wilhelm57c67112013-07-17 21:14:35 +00002560Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods,
Fariborz Jahaniandfb76872013-07-17 00:05:08 +00002561 ArrayRef<DeclGroupPtrTy> allTUVars) {
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002562 if (getObjCContainerKind() == Sema::OCK_None)
2563 return 0;
2564
2565 assert(AtEnd.isValid() && "Invalid location for '@end'");
2566
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002567 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2568 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002569
Mike Stump11289f42009-09-09 15:08:12 +00002570 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00002571 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2572 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002573 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00002574
Steve Naroff35c62ae2009-01-08 17:28:14 +00002575 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2576 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2577 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2578
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002579 for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002580 ObjCMethodDecl *Method =
John McCall48871652010-08-21 09:40:31 +00002581 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002582
2583 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00002584 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002585 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002586 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002587 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002588 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002589 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002590 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002591 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002592 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002593 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002594 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002595 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002596 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002597 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002598 if (!Context.getSourceManager().isInSystemHeader(
2599 Method->getLocation()))
2600 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2601 << Method->getDeclName();
2602 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2603 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002604 InsMap[Method->getSelector()] = Method;
2605 /// The following allows us to typecheck messages to "id".
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002606 AddInstanceMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002607 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002608 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002609 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002610 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002611 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002612 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002613 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002614 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002615 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002616 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002617 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002618 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002619 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002620 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002621 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002622 if (!Context.getSourceManager().isInSystemHeader(
2623 Method->getLocation()))
2624 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2625 << Method->getDeclName();
2626 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2627 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002628 ClsMap[Method->getSelector()] = Method;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002629 AddFactoryMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002630 }
2631 }
2632 }
Douglas Gregorb8982092013-01-21 19:42:21 +00002633 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2634 // Nothing to do here.
Steve Naroffb3a87982009-01-09 15:36:25 +00002635 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002636 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00002637 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002638 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00002639
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002640 if (C->IsClassExtension()) {
2641 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2642 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002643 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002644 }
Steve Naroffb3a87982009-01-09 15:36:25 +00002645 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002646 if (CDecl->getIdentifier())
2647 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2648 // user-defined setter/getter. It also synthesizes setter/getter methods
2649 // and adds them to the DeclContext and global method pools.
2650 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2651 E = CDecl->prop_end();
2652 I != E; ++I)
David Blaikie40ed2972012-06-06 20:45:41 +00002653 ProcessPropertyDecl(*I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00002654 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00002655 }
2656 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002657 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002658 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002659 // Any property declared in a class extension might have user
2660 // declared setter or getter in current class extension or one
2661 // of the other class extensions. Mark them as synthesized as
2662 // property will be synthesized when property with same name is
2663 // seen in the @implementation.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002664 for (ObjCInterfaceDecl::visible_extensions_iterator
2665 Ext = IDecl->visible_extensions_begin(),
2666 ExtEnd = IDecl->visible_extensions_end();
2667 Ext != ExtEnd; ++Ext) {
2668 for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
2669 E = Ext->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002670 ObjCPropertyDecl *Property = *I;
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002671 // Skip over properties declared @dynamic
2672 if (const ObjCPropertyImplDecl *PIDecl
2673 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2674 if (PIDecl->getPropertyImplementation()
2675 == ObjCPropertyImplDecl::Dynamic)
2676 continue;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002677
2678 for (ObjCInterfaceDecl::visible_extensions_iterator
2679 Ext = IDecl->visible_extensions_begin(),
2680 ExtEnd = IDecl->visible_extensions_end();
2681 Ext != ExtEnd; ++Ext) {
2682 if (ObjCMethodDecl *GetterMethod
2683 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002684 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002685 if (!Property->isReadOnly())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002686 if (ObjCMethodDecl *SetterMethod
2687 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002688 SetterMethod->setPropertyAccessor(true);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002689 }
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002690 }
2691 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00002692 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002693 AtomicPropertySetterGetterRules(IC, IDecl);
John McCall31168b02011-06-15 23:02:42 +00002694 DiagnoseOwningPropertyGetterSynthesis(IC);
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002695 if (IDecl->hasDesignatedInitializers())
2696 DiagnoseMissingDesignatedInitOverrides(IC, IDecl);
2697
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002698 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2699 if (IDecl->getSuperClass() == NULL) {
2700 // This class has no superclass, so check that it has been marked with
2701 // __attribute((objc_root_class)).
2702 if (!HasRootClassAttr) {
2703 SourceLocation DeclLoc(IDecl->getLocation());
2704 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2705 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2706 << IDecl->getIdentifier();
2707 // See if NSObject is in the current scope, and if it is, suggest
2708 // adding " : NSObject " to the class declaration.
2709 NamedDecl *IF = LookupSingleName(TUScope,
2710 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2711 DeclLoc, LookupOrdinaryName);
2712 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2713 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2714 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2715 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2716 } else {
2717 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2718 }
2719 }
2720 } else if (HasRootClassAttr) {
2721 // Complain that only root classes may have this attribute.
2722 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2723 }
2724
John McCall5fb5df92012-06-20 06:18:46 +00002725 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002726 while (IDecl->getSuperClass()) {
2727 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2728 IDecl = IDecl->getSuperClass();
2729 }
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002730 }
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002731 }
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00002732 SetIvarInitializers(IC);
Mike Stump11289f42009-09-09 15:08:12 +00002733 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00002734 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002735 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00002736
Chris Lattnerda463fe2007-12-12 07:09:47 +00002737 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00002738 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00002739 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002740 if (ObjCCategoryDecl *Cat
2741 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2742 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002743 }
2744 }
2745 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002746 if (isInterfaceDeclKind) {
2747 // Reject invalid vardecls.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002748 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002749 DeclGroupRef DG = allTUVars[i].get();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002750 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2751 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00002752 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00002753 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00002754 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002755 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00002756 }
Fariborz Jahanian4327b322011-08-29 17:33:12 +00002757 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002758
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002759 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002760 DeclGroupRef DG = allTUVars[i].get();
Argyrios Kyrtzidis8ad3bab2011-11-23 20:27:36 +00002761 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2762 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002763 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2764 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002765
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +00002766 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002767 return ClassDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002768}
2769
2770
2771/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2772/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00002773static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002774CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCallca872902011-05-01 03:04:29 +00002775 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002776}
2777
Douglas Gregor33823722011-06-11 01:09:30 +00002778/// \brief Check whether the declared result type of the given Objective-C
2779/// method declaration is compatible with the method's class.
2780///
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002781static Sema::ResultTypeCompatibilityKind
Douglas Gregor33823722011-06-11 01:09:30 +00002782CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2783 ObjCInterfaceDecl *CurrentClass) {
2784 QualType ResultType = Method->getResultType();
Douglas Gregor33823722011-06-11 01:09:30 +00002785
2786 // If an Objective-C method inherits its related result type, then its
2787 // declared result type must be compatible with its own class type. The
2788 // declared result type is compatible if:
2789 if (const ObjCObjectPointerType *ResultObjectType
2790 = ResultType->getAs<ObjCObjectPointerType>()) {
2791 // - it is id or qualified id, or
2792 if (ResultObjectType->isObjCIdType() ||
2793 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002794 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002795
2796 if (CurrentClass) {
2797 if (ObjCInterfaceDecl *ResultClass
2798 = ResultObjectType->getInterfaceDecl()) {
2799 // - it is the same as the method's class type, or
Douglas Gregor0b144e12011-12-15 00:29:59 +00002800 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002801 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002802
2803 // - it is a superclass of the method's class type
2804 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002805 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002806 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00002807 } else {
2808 // Any Objective-C pointer type might be acceptable for a protocol
2809 // method; we just don't know.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002810 return Sema::RTC_Unknown;
Douglas Gregor33823722011-06-11 01:09:30 +00002811 }
2812 }
2813
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002814 return Sema::RTC_Incompatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002815}
2816
John McCalld2930c22011-07-22 02:45:48 +00002817namespace {
2818/// A helper class for searching for methods which a particular method
2819/// overrides.
2820class OverrideSearch {
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002821public:
John McCalld2930c22011-07-22 02:45:48 +00002822 Sema &S;
2823 ObjCMethodDecl *Method;
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002824 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCalld2930c22011-07-22 02:45:48 +00002825 bool Recursive;
2826
2827public:
2828 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2829 Selector selector = method->getSelector();
2830
2831 // Bypass this search if we've never seen an instance/class method
2832 // with this selector before.
2833 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2834 if (it == S.MethodPool.end()) {
Axel Naumanndd433f02012-10-18 19:05:02 +00002835 if (!S.getExternalSource()) return;
Douglas Gregore1716012012-01-25 00:49:42 +00002836 S.ReadMethodPool(selector);
2837
2838 it = S.MethodPool.find(selector);
2839 if (it == S.MethodPool.end())
2840 return;
John McCalld2930c22011-07-22 02:45:48 +00002841 }
2842 ObjCMethodList &list =
2843 method->isInstanceMethod() ? it->second.first : it->second.second;
2844 if (!list.Method) return;
2845
2846 ObjCContainerDecl *container
2847 = cast<ObjCContainerDecl>(method->getDeclContext());
2848
2849 // Prevent the search from reaching this container again. This is
2850 // important with categories, which override methods from the
2851 // interface and each other.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002852 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2853 searchFromContainer(container);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002854 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2855 searchFromContainer(Interface);
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002856 } else {
2857 searchFromContainer(container);
2858 }
Douglas Gregor33823722011-06-11 01:09:30 +00002859 }
John McCalld2930c22011-07-22 02:45:48 +00002860
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002861 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCalld2930c22011-07-22 02:45:48 +00002862 iterator begin() const { return Overridden.begin(); }
2863 iterator end() const { return Overridden.end(); }
2864
2865private:
2866 void searchFromContainer(ObjCContainerDecl *container) {
2867 if (container->isInvalidDecl()) return;
2868
2869 switch (container->getDeclKind()) {
2870#define OBJCCONTAINER(type, base) \
2871 case Decl::type: \
2872 searchFrom(cast<type##Decl>(container)); \
2873 break;
2874#define ABSTRACT_DECL(expansion)
2875#define DECL(type, base) \
2876 case Decl::type:
2877#include "clang/AST/DeclNodes.inc"
2878 llvm_unreachable("not an ObjC container!");
2879 }
2880 }
2881
2882 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00002883 if (!protocol->hasDefinition())
2884 return;
2885
John McCalld2930c22011-07-22 02:45:48 +00002886 // A method in a protocol declaration overrides declarations from
2887 // referenced ("parent") protocols.
2888 search(protocol->getReferencedProtocols());
2889 }
2890
2891 void searchFrom(ObjCCategoryDecl *category) {
2892 // A method in a category declaration overrides declarations from
2893 // the main class and from protocols the category references.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002894 // The main class is handled in the constructor.
John McCalld2930c22011-07-22 02:45:48 +00002895 search(category->getReferencedProtocols());
2896 }
2897
2898 void searchFrom(ObjCCategoryImplDecl *impl) {
2899 // A method in a category definition that has a category
2900 // declaration overrides declarations from the category
2901 // declaration.
2902 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2903 search(category);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002904 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2905 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002906
2907 // Otherwise it overrides declarations from the class.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002908 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2909 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002910 }
2911 }
2912
2913 void searchFrom(ObjCInterfaceDecl *iface) {
2914 // A method in a class declaration overrides declarations from
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00002915 if (!iface->hasDefinition())
2916 return;
2917
John McCalld2930c22011-07-22 02:45:48 +00002918 // - categories,
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00002919 for (ObjCInterfaceDecl::known_categories_iterator
2920 cat = iface->known_categories_begin(),
2921 catEnd = iface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002922 cat != catEnd; ++cat) {
2923 search(*cat);
2924 }
John McCalld2930c22011-07-22 02:45:48 +00002925
2926 // - the super class, and
2927 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2928 search(super);
2929
2930 // - any referenced protocols.
2931 search(iface->getReferencedProtocols());
2932 }
2933
2934 void searchFrom(ObjCImplementationDecl *impl) {
2935 // A method in a class implementation overrides declarations from
2936 // the class interface.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002937 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2938 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002939 }
2940
2941
2942 void search(const ObjCProtocolList &protocols) {
2943 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2944 i != e; ++i)
2945 search(*i);
2946 }
2947
2948 void search(ObjCContainerDecl *container) {
John McCalld2930c22011-07-22 02:45:48 +00002949 // Check for a method in this container which matches this selector.
2950 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00002951 Method->isInstanceMethod(),
2952 /*AllowHidden=*/true);
John McCalld2930c22011-07-22 02:45:48 +00002953
2954 // If we find one, record it and bail out.
2955 if (meth) {
2956 Overridden.insert(meth);
2957 return;
2958 }
2959
2960 // Otherwise, search for methods that a hypothetical method here
2961 // would have overridden.
2962
2963 // Note that we're now in a recursive case.
2964 Recursive = true;
2965
2966 searchFromContainer(container);
2967 }
2968};
Douglas Gregor33823722011-06-11 01:09:30 +00002969}
2970
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002971void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2972 ObjCInterfaceDecl *CurrentClass,
2973 ResultTypeCompatibilityKind RTC) {
2974 // Search for overridden methods and merge information down from them.
2975 OverrideSearch overrides(*this, ObjCMethod);
2976 // Keep track if the method overrides any method in the class's base classes,
2977 // its protocols, or its categories' protocols; we will keep that info
2978 // in the ObjCMethodDecl.
2979 // For this info, a method in an implementation is not considered as
2980 // overriding the same method in the interface or its categories.
2981 bool hasOverriddenMethodsInBaseOrProtocol = false;
2982 for (OverrideSearch::iterator
2983 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2984 ObjCMethodDecl *overridden = *i;
2985
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00002986 if (!hasOverriddenMethodsInBaseOrProtocol) {
2987 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2988 CurrentClass != overridden->getClassInterface() ||
2989 overridden->isOverriding()) {
2990 hasOverriddenMethodsInBaseOrProtocol = true;
2991
2992 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
2993 // OverrideSearch will return as "overridden" the same method in the
2994 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
2995 // check whether a category of a base class introduced a method with the
2996 // same selector, after the interface method declaration.
2997 // To avoid unnecessary lookups in the majority of cases, we use the
2998 // extra info bits in GlobalMethodPool to check whether there were any
2999 // category methods with this selector.
3000 GlobalMethodPool::iterator It =
3001 MethodPool.find(ObjCMethod->getSelector());
3002 if (It != MethodPool.end()) {
3003 ObjCMethodList &List =
3004 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
3005 unsigned CategCount = List.getBits();
3006 if (CategCount > 0) {
3007 // If the method is in a category we'll do lookup if there were at
3008 // least 2 category methods recorded, otherwise only one will do.
3009 if (CategCount > 1 ||
3010 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
3011 OverrideSearch overrides(*this, overridden);
3012 for (OverrideSearch::iterator
3013 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
3014 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00003015 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
3016 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00003017 hasOverriddenMethodsInBaseOrProtocol = true;
3018 overridden->setOverriding(true);
3019 break;
3020 }
3021 }
3022 }
3023 }
3024 }
3025 }
3026 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003027
3028 // Propagate down the 'related result type' bit from overridden methods.
3029 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
3030 ObjCMethod->SetRelatedResultType();
3031
3032 // Then merge the declarations.
3033 mergeObjCMethodDecls(ObjCMethod, overridden);
3034
3035 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3036 continue; // Conflicting properties are detected elsewhere.
3037
3038 // Check for overriding methods
3039 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3040 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3041 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3042 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3043
3044 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanian31a25682012-07-05 22:26:07 +00003045 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3046 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003047 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3048 E = ObjCMethod->param_end();
Douglas Gregor0bf70f42012-05-17 23:13:29 +00003049 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3050 PrevE = overridden->param_end();
3051 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003052 assert(PrevI != overridden->param_end() && "Param mismatch");
3053 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3054 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3055 // If type of argument of method in this class does not match its
3056 // respective argument type in the super class method, issue warning;
3057 if (!Context.typesAreCompatible(T1, T2)) {
3058 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3059 << T1 << T2;
3060 Diag(overridden->getLocation(), diag::note_previous_declaration);
3061 break;
3062 }
3063 }
3064 }
3065 }
3066
3067 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3068}
3069
John McCall48871652010-08-21 09:40:31 +00003070Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003071 Scope *S,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003072 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003073 tok::TokenKind MethodType,
John McCallba7bf592010-08-24 05:47:05 +00003074 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003075 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003076 Selector Sel,
3077 // optional arguments. The number of types/arguments is obtained
3078 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003079 ObjCArgInfo *ArgInfo,
Fariborz Jahanian60462092010-04-08 00:30:06 +00003080 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattnerda463fe2007-12-12 07:09:47 +00003081 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003082 bool isVariadic, bool MethodDefinition) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003083 // Make sure we can establish a context for the method.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003084 if (!CurContext->isObjCContainer()) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003085 Diag(MethodLoc, diag::error_missing_method_context);
John McCall48871652010-08-21 09:40:31 +00003086 return 0;
Steve Naroff83777fe2008-02-29 21:48:07 +00003087 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003088 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3089 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003090 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00003091
Douglas Gregorbab8a962011-09-08 01:46:34 +00003092 bool HasRelatedResultType = false;
Douglas Gregor12852d92010-03-08 14:59:44 +00003093 TypeSourceInfo *ResultTInfo = 0;
Steve Naroff32606412009-02-20 22:59:16 +00003094 if (ReturnType) {
Douglas Gregor12852d92010-03-08 14:59:44 +00003095 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump11289f42009-09-09 15:08:12 +00003096
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003097 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
John McCall48871652010-08-21 09:40:31 +00003098 return 0;
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003099
Douglas Gregorbab8a962011-09-08 01:46:34 +00003100 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003101 } else { // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003102 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianb21138f2011-07-21 17:38:14 +00003103 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003104 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003105 }
Mike Stump11289f42009-09-09 15:08:12 +00003106
3107 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003108 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003109 resultDeclType,
Douglas Gregor12852d92010-03-08 14:59:44 +00003110 ResultTInfo,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003111 CurContext,
Chris Lattner8d8829e2008-03-16 00:49:28 +00003112 MethodType == tok::minus, isVariadic,
Jordan Rosed01e83a2012-10-10 16:42:25 +00003113 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003114 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor33823722011-06-11 01:09:30 +00003115 MethodDeclKind == tok::objc_optional
3116 ? ObjCMethodDecl::Optional
3117 : ObjCMethodDecl::Required,
Douglas Gregorbab8a962011-09-08 01:46:34 +00003118 HasRelatedResultType);
Mike Stump11289f42009-09-09 15:08:12 +00003119
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003120 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00003121
Chris Lattner23b0faf2009-04-11 19:42:43 +00003122 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00003123 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00003124 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00003125
David Blaikie7d170102013-05-15 07:37:26 +00003126 if (!ArgInfo[i].Type) {
John McCall856bbea2009-10-23 21:48:59 +00003127 ArgType = Context.getObjCIdType();
3128 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003129 } else {
John McCall856bbea2009-10-23 21:48:59 +00003130 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003131 }
Mike Stump11289f42009-09-09 15:08:12 +00003132
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003133 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3134 LookupOrdinaryName, ForRedeclaration);
3135 LookupName(R, S);
3136 if (R.isSingleResult()) {
3137 NamedDecl *PrevDecl = R.getFoundDecl();
3138 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003139 Diag(ArgInfo[i].NameLoc,
3140 (MethodDefinition ? diag::warn_method_param_redefinition
3141 : diag::warn_method_param_declaration))
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003142 << ArgInfo[i].Name;
3143 Diag(PrevDecl->getLocation(),
3144 diag::note_previous_declaration);
3145 }
3146 }
3147
Abramo Bagnaradff19302011-03-08 08:55:46 +00003148 SourceLocation StartLoc = DI
3149 ? DI->getTypeLoc().getBeginLoc()
3150 : ArgInfo[i].NameLoc;
3151
John McCalld44f4d72011-04-23 02:46:06 +00003152 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3153 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003154 ArgType, DI, SC_None);
Mike Stump11289f42009-09-09 15:08:12 +00003155
John McCall82490832011-05-02 00:30:12 +00003156 Param->setObjCMethodScopeInfo(i);
3157
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003158 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003159 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00003160
Chris Lattner9713a1c2009-04-11 19:34:56 +00003161 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00003162 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00003163
Fariborz Jahanian52d02f62012-01-14 18:44:35 +00003164 if (Param->hasAttr<BlocksAttr>()) {
3165 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3166 Param->setInvalidDecl();
3167 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003168 S->AddDecl(Param);
3169 IdResolver.AddDecl(Param);
3170
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003171 Params.push_back(Param);
3172 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003173
Fariborz Jahanian60462092010-04-08 00:30:06 +00003174 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCall48871652010-08-21 09:40:31 +00003175 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003176 QualType ArgType = Param->getType();
3177 if (ArgType.isNull())
3178 ArgType = Context.getObjCIdType();
3179 else
3180 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor84280642011-07-12 04:42:08 +00003181 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003182
Fariborz Jahanian60462092010-04-08 00:30:06 +00003183 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003184 Params.push_back(Param);
3185 }
3186
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003187 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003188 ObjCMethod->setObjCDeclQualifier(
3189 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00003190
3191 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00003192 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00003193
Douglas Gregor87e92752010-12-21 17:34:17 +00003194 // Add the method now.
John McCalld2930c22011-07-22 02:45:48 +00003195 const ObjCMethodDecl *PrevMethod = 0;
3196 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00003197 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003198 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3199 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003200 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003201 PrevMethod = ImpDecl->getClassMethod(Sel);
3202 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003203 }
Douglas Gregor33823722011-06-11 01:09:30 +00003204
Fariborz Jahanian512a4cc92011-10-22 01:21:15 +00003205 ObjCMethodDecl *IMD = 0;
3206 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3207 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3208 ObjCMethod->isInstanceMethod());
Fariborz Jahaniandb4fc282013-07-09 22:02:20 +00003209 if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
3210 !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
3211 // merge the attribute into implementation.
3212 ObjCMethod->addAttr(
3213 new (Context) ObjCRequiresSuperAttr(ObjCMethod->getLocation(), Context));
3214 }
Douglas Gregor87e92752010-12-21 17:34:17 +00003215 } else {
3216 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003217 }
John McCalld2930c22011-07-22 02:45:48 +00003218
Chris Lattnerda463fe2007-12-12 07:09:47 +00003219 if (PrevMethod) {
3220 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00003221 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00003222 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00003223 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian096f7c12013-05-13 17:27:00 +00003224 ObjCMethod->setInvalidDecl();
3225 return ObjCMethod;
Mike Stump11289f42009-09-09 15:08:12 +00003226 }
John McCall28a6aea2009-11-04 02:18:39 +00003227
Douglas Gregor33823722011-06-11 01:09:30 +00003228 // If this Objective-C method does not have a related result type, but we
3229 // are allowed to infer related result types, try to do so based on the
3230 // method family.
3231 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3232 if (!CurrentClass) {
3233 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3234 CurrentClass = Cat->getClassInterface();
3235 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3236 CurrentClass = Impl->getClassInterface();
3237 else if (ObjCCategoryImplDecl *CatImpl
3238 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3239 CurrentClass = CatImpl->getClassInterface();
3240 }
John McCalld2930c22011-07-22 02:45:48 +00003241
Douglas Gregorbab8a962011-09-08 01:46:34 +00003242 ResultTypeCompatibilityKind RTC
3243 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCalld2930c22011-07-22 02:45:48 +00003244
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003245 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCalld2930c22011-07-22 02:45:48 +00003246
John McCall31168b02011-06-15 23:02:42 +00003247 bool ARCError = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003248 if (getLangOpts().ObjCAutoRefCount)
John McCalle48f3892013-04-04 01:38:37 +00003249 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCall31168b02011-06-15 23:02:42 +00003250
Douglas Gregorbab8a962011-09-08 01:46:34 +00003251 // Infer the related result type when possible.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003252 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregorbab8a962011-09-08 01:46:34 +00003253 !ObjCMethod->hasRelatedResultType() &&
3254 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor33823722011-06-11 01:09:30 +00003255 bool InferRelatedResultType = false;
3256 switch (ObjCMethod->getMethodFamily()) {
3257 case OMF_None:
3258 case OMF_copy:
3259 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00003260 case OMF_finalize:
Douglas Gregor33823722011-06-11 01:09:30 +00003261 case OMF_mutableCopy:
3262 case OMF_release:
3263 case OMF_retainCount:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00003264 case OMF_performSelector:
Douglas Gregor33823722011-06-11 01:09:30 +00003265 break;
3266
3267 case OMF_alloc:
3268 case OMF_new:
3269 InferRelatedResultType = ObjCMethod->isClassMethod();
3270 break;
3271
3272 case OMF_init:
3273 case OMF_autorelease:
3274 case OMF_retain:
3275 case OMF_self:
3276 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3277 break;
3278 }
3279
John McCalld2930c22011-07-22 02:45:48 +00003280 if (InferRelatedResultType)
Douglas Gregor33823722011-06-11 01:09:30 +00003281 ObjCMethod->SetRelatedResultType();
Douglas Gregor33823722011-06-11 01:09:30 +00003282 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00003283
3284 ActOnDocumentableDecl(ObjCMethod);
3285
John McCall48871652010-08-21 09:40:31 +00003286 return ObjCMethod;
Chris Lattnerda463fe2007-12-12 07:09:47 +00003287}
3288
Chris Lattner438e5012008-12-17 07:13:27 +00003289bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +00003290 // Following is also an error. But it is caused by a missing @end
3291 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003292 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003293 return false;
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003294
3295 // If we switched context to translation unit while we are still lexically in
3296 // an objc container, it means the parser missed emitting an error.
3297 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3298 return false;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003299
Anders Carlssona6b508a2008-11-04 16:57:32 +00003300 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3301 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003302
Anders Carlssona6b508a2008-11-04 16:57:32 +00003303 return true;
3304}
Chris Lattner438e5012008-12-17 07:13:27 +00003305
James Dennett634962f2012-06-14 21:40:34 +00003306/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattner438e5012008-12-17 07:13:27 +00003307/// instance variables of ClassName into Decls.
John McCall48871652010-08-21 09:40:31 +00003308void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00003309 IdentifierInfo *ClassName,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003310 SmallVectorImpl<Decl*> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00003311 // Check that ClassName is a valid class
Douglas Gregorb2ccf012010-04-15 22:33:43 +00003312 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattner438e5012008-12-17 07:13:27 +00003313 if (!Class) {
3314 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3315 return;
3316 }
John McCall5fb5df92012-06-20 06:18:46 +00003317 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00003318 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3319 return;
3320 }
Mike Stump11289f42009-09-09 15:08:12 +00003321
Chris Lattner438e5012008-12-17 07:13:27 +00003322 // Collect the instance variables
Jordy Rosea91768e2011-07-22 02:08:32 +00003323 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003324 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003325 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003326 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003327 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCall48871652010-08-21 09:40:31 +00003328 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaradff19302011-03-08 08:55:46 +00003329 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3330 /*FIXME: StartL=*/ID->getLocation(),
3331 ID->getLocation(),
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003332 ID->getIdentifier(), ID->getType(),
3333 ID->getBitWidth());
John McCall48871652010-08-21 09:40:31 +00003334 Decls.push_back(FD);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003335 }
Mike Stump11289f42009-09-09 15:08:12 +00003336
Chris Lattner438e5012008-12-17 07:13:27 +00003337 // Introduce all of these fields into the appropriate scope.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003338 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00003339 D != Decls.end(); ++D) {
John McCall48871652010-08-21 09:40:31 +00003340 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003341 if (getLangOpts().CPlusPlus)
Chris Lattner438e5012008-12-17 07:13:27 +00003342 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCall48871652010-08-21 09:40:31 +00003343 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003344 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00003345 }
3346}
3347
Douglas Gregorf3564192010-04-26 17:32:49 +00003348/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaradff19302011-03-08 08:55:46 +00003349VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3350 SourceLocation StartLoc,
3351 SourceLocation IdLoc,
3352 IdentifierInfo *Id,
Douglas Gregorf3564192010-04-26 17:32:49 +00003353 bool Invalid) {
3354 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3355 // duration shall not be qualified by an address-space qualifier."
3356 // Since all parameters have automatic store duration, they can not have
3357 // an address space.
3358 if (T.getAddressSpace() != 0) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003359 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregorf3564192010-04-26 17:32:49 +00003360 Invalid = true;
3361 }
3362
3363 // An @catch parameter must be an unqualified object pointer type;
3364 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3365 if (Invalid) {
3366 // Don't do any further checking.
Douglas Gregorf4e837f2010-04-26 17:57:08 +00003367 } else if (T->isDependentType()) {
3368 // Okay: we don't know what this type will instantiate to.
Douglas Gregorf3564192010-04-26 17:32:49 +00003369 } else if (!T->isObjCObjectPointerType()) {
3370 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003371 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregorf3564192010-04-26 17:32:49 +00003372 } else if (T->isObjCQualifiedIdType()) {
3373 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003374 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregorf3564192010-04-26 17:32:49 +00003375 }
3376
Abramo Bagnaradff19302011-03-08 08:55:46 +00003377 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003378 T, TInfo, SC_None);
Douglas Gregor3f324d562010-05-03 18:51:14 +00003379 New->setExceptionVariable(true);
3380
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003381 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003382 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003383 Invalid = true;
3384
Douglas Gregorf3564192010-04-26 17:32:49 +00003385 if (Invalid)
3386 New->setInvalidDecl();
3387 return New;
3388}
3389
John McCall48871652010-08-21 09:40:31 +00003390Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003391 const DeclSpec &DS = D.getDeclSpec();
3392
3393 // We allow the "register" storage class on exception variables because
3394 // GCC did, but we drop it completely. Any other storage class is an error.
3395 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3396 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3397 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithb4a9e862013-04-12 22:46:28 +00003398 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003399 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithb4a9e862013-04-12 22:46:28 +00003400 << DeclSpec::getSpecifierName(SCS);
3401 }
3402 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3403 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3404 diag::err_invalid_thread)
3405 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregorf3564192010-04-26 17:32:49 +00003406 D.getMutableDeclSpec().ClearStorageClassSpecs();
3407
Richard Smithb1402ae2013-03-18 22:52:47 +00003408 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregorf3564192010-04-26 17:32:49 +00003409
3410 // Check that there are no default arguments inside the type of this
3411 // exception object (C++ only).
David Blaikiebbafb8a2012-03-11 07:00:24 +00003412 if (getLangOpts().CPlusPlus)
Douglas Gregorf3564192010-04-26 17:32:49 +00003413 CheckExtraCXXDefaultArguments(D);
3414
Argyrios Kyrtzidisef7022f2011-06-28 03:01:15 +00003415 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCall8cb7bdf2010-06-04 23:28:52 +00003416 QualType ExceptionType = TInfo->getType();
Douglas Gregorf3564192010-04-26 17:32:49 +00003417
Abramo Bagnaradff19302011-03-08 08:55:46 +00003418 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3419 D.getSourceRange().getBegin(),
3420 D.getIdentifierLoc(),
3421 D.getIdentifier(),
Douglas Gregorf3564192010-04-26 17:32:49 +00003422 D.isInvalidType());
3423
3424 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3425 if (D.getCXXScopeSpec().isSet()) {
3426 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3427 << D.getCXXScopeSpec().getRange();
3428 New->setInvalidDecl();
3429 }
3430
3431 // Add the parameter declaration into this scope.
John McCall48871652010-08-21 09:40:31 +00003432 S->AddDecl(New);
Douglas Gregorf3564192010-04-26 17:32:49 +00003433 if (D.getIdentifier())
3434 IdResolver.AddDecl(New);
3435
3436 ProcessDeclAttributes(S, New, D);
3437
3438 if (New->hasAttr<BlocksAttr>())
3439 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCall48871652010-08-21 09:40:31 +00003440 return New;
Douglas Gregore11ee112010-04-23 23:01:43 +00003441}
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003442
3443/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003444/// initialization.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003445void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003446 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003447 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3448 Iv= Iv->getNextIvar()) {
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003449 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00003450 if (QT->isRecordType())
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003451 Ivars.push_back(Iv);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003452 }
3453}
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003454
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003455void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor72e357f2011-07-28 14:54:22 +00003456 // Load referenced selectors from the external source.
3457 if (ExternalSource) {
3458 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3459 ExternalSource->ReadReferencedSelectors(Sels);
3460 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3461 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3462 }
3463
Fariborz Jahanian42f89382013-05-30 21:48:58 +00003464 DiagnoseMismatchedMethodsInGlobalPool();
3465
Fariborz Jahanianc9b7c202011-02-04 23:19:27 +00003466 // Warning will be issued only when selector table is
3467 // generated (which means there is at lease one implementation
3468 // in the TU). This is to match gcc's behavior.
3469 if (ReferencedSelectors.empty() ||
3470 !Context.AnyObjCImplementation())
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003471 return;
3472 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3473 ReferencedSelectors.begin(),
3474 E = ReferencedSelectors.end(); S != E; ++S) {
3475 Selector Sel = (*S).first;
3476 if (!LookupImplementedMethodInGlobalPool(Sel))
3477 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3478 }
3479 return;
3480}
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003481
3482ObjCIvarDecl *
3483Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method,
3484 const ObjCPropertyDecl *&PDecl) const {
3485
3486 const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
3487 if (!IDecl)
3488 return 0;
3489 Method = IDecl->lookupMethod(Method->getSelector(), true);
3490 if (!Method || !Method->isPropertyAccessor())
3491 return 0;
Fariborz Jahanian617e49a2013-11-15 17:48:00 +00003492 if ((PDecl = Method->findPropertyDecl())) {
3493 if (!PDecl->getDeclContext())
3494 return 0;
3495 // Make sure property belongs to accessor's class and not to
3496 // one of its super classes.
3497 if (const ObjCInterfaceDecl *CID =
3498 dyn_cast<ObjCInterfaceDecl>(PDecl->getDeclContext()))
3499 if (CID != IDecl)
3500 return 0;
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003501 return PDecl->getPropertyIvarDecl();
Fariborz Jahanian617e49a2013-11-15 17:48:00 +00003502 }
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003503 return 0;
3504}
3505
3506void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S) {
Fariborz Jahanian54f87382013-12-11 00:53:48 +00003507 if (S->hasUnrecoverableErrorOccurred() || !S->isInObjcMethodOuterScope())
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003508 return;
3509
3510 const ObjCMethodDecl *CurMethod = getCurMethodDecl();
3511 if (!CurMethod)
3512 return;
3513 const ObjCPropertyDecl *PDecl;
3514 const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl);
3515 if (IV && !IV->getBackingIvarReferencedInAccessor()) {
3516 Diag(getCurMethodDecl()->getLocation(), diag::warn_unused_property_backing_ivar)
3517 << IV->getDeclName();
3518 Diag(PDecl->getLocation(), diag::note_property_declare);
3519 }
3520}