blob: 078a0e3ab9fb3662b44a472bd4f7ea41f4bdf11c [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,
Ted Kremenek33e430f2013-12-13 06:26:14 +00001644 ObjCContainerDecl *CDecl,
1645 bool isExplicitProtocol = true) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001646 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1647 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1648 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +00001649 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1650
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001651 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001652 ObjCInterfaceDecl *NSIDecl = 0;
Ted Kremenek285ee852013-12-13 06:26:10 +00001653 if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump11289f42009-09-09 15:08:12 +00001654 // check to see if class implements forwardInvocation method and objects
1655 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001656 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +00001657 // Under such conditions, which means that every method possible is
1658 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001659 // found" warnings.
1660 // FIXME: Use a general GetUnarySelector method for this.
Ted Kremenek285ee852013-12-13 06:26:10 +00001661 IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation");
1662 Selector fISelector = S.Context.Selectors.getSelector(1, &II);
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001663 if (InsMap.count(fISelector))
1664 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1665 // need be implemented in the implementation.
Ted Kremenek285ee852013-12-13 06:26:10 +00001666 NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy"));
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001667 }
Mike Stump11289f42009-09-09 15:08:12 +00001668
Fariborz Jahanianc41cf052013-01-07 19:21:03 +00001669 // If this is a forward protocol declaration, get its definition.
1670 if (!PDecl->isThisDeclarationADefinition() &&
1671 PDecl->getDefinition())
1672 PDecl = PDecl->getDefinition();
1673
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001674 // If a method lookup fails locally we still need to look and see if
1675 // the method was implemented by a base class or an inherited
1676 // protocol. This lookup is slow, but occurs rarely in correct code
1677 // and otherwise would terminate in a warning.
Ted Kremenek33e430f2013-12-13 06:26:14 +00001678 if (isExplicitProtocol && PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001679 Super = NULL;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001680
Chris Lattnerda463fe2007-12-12 07:09:47 +00001681 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001682 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +00001683 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001684 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001685 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001686 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rosed01e83a2012-10-10 16:42:25 +00001687 !method->isPropertyAccessor() &&
1688 !InsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001689 (!Super || !Super->lookupMethod(method->getSelector(),
1690 true /* instance */,
1691 false /* shallowCategory */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001692 true /* followsSuper */,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001693 NULL /* category */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001694 // If a method is not implemented in the category implementation but
1695 // has been declared in its primary class, superclass,
1696 // or in one of their protocols, no need to issue the warning.
1697 // This is because method will be implemented in the primary class
1698 // or one of its super class implementation.
1699
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001700 // Ugly, but necessary. Method declared in protcol might have
1701 // have been synthesized due to a property declared in the class which
1702 // uses the protocol.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001703 if (ObjCMethodDecl *MethodInClass =
Ted Kremenek00781502013-11-23 01:01:29 +00001704 IDecl->lookupMethod(method->getSelector(),
1705 true /* instance */,
1706 true /* shallowCategoryLookup */,
1707 false /* followSuper */))
Jordan Rosed01e83a2012-10-10 16:42:25 +00001708 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001709 continue;
1710 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Ted Kremenek285ee852013-12-13 06:26:10 +00001711 if (S.Diags.getDiagnosticLevel(DIAG, ImpLoc)
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001712 != DiagnosticsEngine::Ignored) {
Ted Kremenek285ee852013-12-13 06:26:10 +00001713 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG,
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001714 PDecl);
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001715 }
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001716 }
1717 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001718 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +00001719 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001720 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001721 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001722 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001723 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1724 !ClsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001725 (!Super || !Super->lookupMethod(method->getSelector(),
1726 false /* class method */,
1727 false /* shallowCategoryLookup */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001728 true /* followSuper */,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001729 NULL /* category */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001730 // See above comment for instance method lookups.
Ted Kremenek00781502013-11-23 01:01:29 +00001731 if (C && IDecl->lookupMethod(method->getSelector(),
1732 false /* class */,
1733 true /* shallowCategoryLookup */,
1734 false /* followSuper */))
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001735 continue;
Ted Kremenek00781502013-11-23 01:01:29 +00001736
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001737 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Ted Kremenek285ee852013-12-13 06:26:10 +00001738 if (S.Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
David Blaikie9c902b52011-09-25 23:23:43 +00001739 DiagnosticsEngine::Ignored) {
Ted Kremenek285ee852013-12-13 06:26:10 +00001740 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl);
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001741 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001742 }
Steve Naroff3ce37a62007-12-14 23:37:57 +00001743 }
Chris Lattner390d39a2008-07-21 21:32:27 +00001744 // Check on this protocols's referenced protocols, recursively.
1745 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1746 E = PDecl->protocol_end(); PI != E; ++PI)
Ted Kremenek285ee852013-12-13 06:26:10 +00001747 CheckProtocolMethodDefs(S, ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap,
Ted Kremenek33e430f2013-12-13 06:26:14 +00001748 CDecl, /* isExplicitProtocl */ false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001749}
1750
Fariborz Jahanianf9ae68a2011-07-16 00:08:33 +00001751/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001752/// or protocol against those declared in their implementations.
1753///
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001754void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1755 const SelectorSet &ClsMap,
1756 SelectorSet &InsMapSeen,
1757 SelectorSet &ClsMapSeen,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001758 ObjCImplDecl* IMPDecl,
1759 ObjCContainerDecl* CDecl,
1760 bool &IncompleteImpl,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001761 bool ImmediateClass,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001762 bool WarnCategoryMethodImpl) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001763 // Check and see if instance methods in class interface have been
1764 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001765 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1766 E = CDecl->instmeth_end(); I != E; ++I) {
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001767 if (!InsMapSeen.insert((*I)->getSelector()))
1768 continue;
Jordan Rosed01e83a2012-10-10 16:42:25 +00001769 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001770 !InsMap.count((*I)->getSelector())) {
1771 if (ImmediateClass)
Ted Kremenekf87decd2013-12-13 05:58:44 +00001772 WarnUndefinedMethod(*this, IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001773 diag::warn_undef_method_impl);
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001774 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +00001775 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001776 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001777 IMPDecl->getInstanceMethod((*I)->getSelector());
1778 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1779 "Expected to find the method through lookup as well");
1780 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001781 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001782 if (ImpMethodDecl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001783 if (!WarnCategoryMethodImpl)
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001784 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1785 isa<ObjCProtocolDecl>(CDecl));
Jordan Rosed01e83a2012-10-10 16:42:25 +00001786 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001787 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001788 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001789 }
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001790 }
1791 }
Mike Stump11289f42009-09-09 15:08:12 +00001792
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001793 // Check and see if class methods in class interface have been
1794 // implemented in the implementation class. If so, their types match.
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001795 for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
1796 E = CDecl->classmeth_end();
1797 I != E; ++I) {
1798 if (!ClsMapSeen.insert((*I)->getSelector()))
1799 continue;
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001800 if (!ClsMap.count((*I)->getSelector())) {
1801 if (ImmediateClass)
Ted Kremenekf87decd2013-12-13 05:58:44 +00001802 WarnUndefinedMethod(*this, IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001803 diag::warn_undef_method_impl);
Mike Stump12b8ce12009-08-04 21:02:39 +00001804 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001805 ObjCMethodDecl *ImpMethodDecl =
1806 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001807 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1808 "Expected to find the method through lookup as well");
1809 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001810 if (!WarnCategoryMethodImpl)
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001811 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1812 isa<ObjCProtocolDecl>(CDecl));
1813 else
1814 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001815 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001816 }
1817 }
Fariborz Jahanian73853e52010-10-08 22:59:25 +00001818
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001819 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) {
1820 // Also, check for methods declared in protocols inherited by
1821 // this protocol.
1822 for (ObjCProtocolDecl::protocol_iterator
1823 PI = PD->protocol_begin(), E = PD->protocol_end(); PI != E; ++PI)
1824 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1825 IMPDecl, (*PI), IncompleteImpl, false,
1826 WarnCategoryMethodImpl);
1827 }
1828
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001829 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001830 // when checking that methods in implementation match their declaration,
1831 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1832 // extension; as well as those in categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001833 if (!WarnCategoryMethodImpl) {
1834 for (ObjCInterfaceDecl::visible_categories_iterator
1835 Cat = I->visible_categories_begin(),
1836 CatEnd = I->visible_categories_end();
1837 Cat != CatEnd; ++Cat) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001838 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001839 IMPDecl, *Cat, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001840 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001841 }
1842 } else {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001843 // Also methods in class extensions need be looked at next.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001844 for (ObjCInterfaceDecl::visible_extensions_iterator
1845 Ext = I->visible_extensions_begin(),
1846 ExtEnd = I->visible_extensions_end();
1847 Ext != ExtEnd; ++Ext) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001848 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001849 IMPDecl, *Ext, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001850 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001851 }
1852 }
1853
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001854 // Check for any implementation of a methods declared in protocol.
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001855 for (ObjCInterfaceDecl::all_protocol_iterator
1856 PI = I->all_referenced_protocol_begin(),
1857 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001858 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1859 IMPDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001860 (*PI), IncompleteImpl, false,
1861 WarnCategoryMethodImpl);
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001862
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001863 // FIXME. For now, we are not checking for extact match of methods
1864 // in category implementation and its primary class's super class.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001865 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001866 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +00001867 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001868 I->getSuperClass(), IncompleteImpl, false);
1869 }
1870}
1871
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001872/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1873/// category matches with those implemented in its primary class and
1874/// warns each time an exact match is found.
1875void Sema::CheckCategoryVsClassMethodMatches(
1876 ObjCCategoryImplDecl *CatIMPDecl) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001877 // Get category's primary class.
1878 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1879 if (!CatDecl)
1880 return;
1881 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1882 if (!IDecl)
1883 return;
Fariborz Jahanianf3077a22013-12-05 20:52:31 +00001884 ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass();
1885 SelectorSet InsMap, ClsMap;
1886
1887 for (ObjCImplementationDecl::instmeth_iterator
1888 I = CatIMPDecl->instmeth_begin(),
1889 E = CatIMPDecl->instmeth_end(); I!=E; ++I) {
1890 Selector Sel = (*I)->getSelector();
1891 // When checking for methods implemented in the category, skip over
1892 // those declared in category class's super class. This is because
1893 // the super class must implement the method.
1894 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true))
1895 continue;
1896 InsMap.insert(Sel);
1897 }
1898
1899 for (ObjCImplementationDecl::classmeth_iterator
1900 I = CatIMPDecl->classmeth_begin(),
1901 E = CatIMPDecl->classmeth_end(); I != E; ++I) {
1902 Selector Sel = (*I)->getSelector();
1903 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false))
1904 continue;
1905 ClsMap.insert(Sel);
1906 }
1907 if (InsMap.empty() && ClsMap.empty())
1908 return;
1909
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001910 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001911 bool IncompleteImpl = false;
1912 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1913 CatIMPDecl, IDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001914 IncompleteImpl, false,
1915 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001916}
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001917
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001918void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump11289f42009-09-09 15:08:12 +00001919 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001920 bool IncompleteImpl) {
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001921 SelectorSet InsMap;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001922 // Check and see if instance methods in class interface have been
1923 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001924 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001925 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001926 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001927
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001928 // Check and see if properties declared in the interface have either 1)
1929 // an implementation or 2) there is a @synthesize/@dynamic implementation
1930 // of the property in the @implementation.
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001931 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall5fb5df92012-06-20 06:18:46 +00001932 if (!(LangOpts.ObjCDefaultSynthProperties &&
1933 LangOpts.ObjCRuntime.isNonFragile()) ||
1934 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001935 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001936
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001937 SelectorSet ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +00001938 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001939 I = IMPDecl->classmeth_begin(),
1940 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001941 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001942
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001943 // Check for type conflict of methods declared in a class/protocol and
1944 // its implementation; if any.
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001945 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +00001946 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1947 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001948 IncompleteImpl, true);
Fariborz Jahanian2bda1b62011-08-03 18:21:12 +00001949
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001950 // check all methods implemented in category against those declared
1951 // in its primary class.
1952 if (ObjCCategoryImplDecl *CatDecl =
1953 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1954 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001955
Chris Lattnerda463fe2007-12-12 07:09:47 +00001956 // Check the protocol list for unimplemented methods in the @implementation
1957 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001958 // Check and see if class methods in class interface have been
1959 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001960
Chris Lattner9ef10f42009-03-01 00:56:52 +00001961 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001962 for (ObjCInterfaceDecl::all_protocol_iterator
1963 PI = I->all_referenced_protocol_begin(),
1964 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Ted Kremenek285ee852013-12-13 06:26:10 +00001965 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), *PI,
1966 IncompleteImpl, InsMap, ClsMap, I);
Chris Lattner9ef10f42009-03-01 00:56:52 +00001967 // Check class extensions (unnamed categories)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001968 for (ObjCInterfaceDecl::visible_extensions_iterator
1969 Ext = I->visible_extensions_begin(),
1970 ExtEnd = I->visible_extensions_end();
1971 Ext != ExtEnd; ++Ext) {
1972 ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
1973 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001974 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001975 // For extended class, unimplemented methods in its protocols will
1976 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001977 if (!C->IsClassExtension()) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001978 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1979 E = C->protocol_end(); PI != E; ++PI)
Ted Kremenek285ee852013-12-13 06:26:10 +00001980 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), *PI,
1981 IncompleteImpl, InsMap, ClsMap, CDecl);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001982 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00001983 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001984 } else
David Blaikie83d382b2011-09-23 05:06:16 +00001985 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00001986}
1987
Mike Stump11289f42009-09-09 15:08:12 +00001988/// ActOnForwardClassDeclaration -
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00001989Sema::DeclGroupPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00001990Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00001991 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00001992 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00001993 unsigned NumElts) {
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00001994 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001995 for (unsigned i = 0; i != NumElts; ++i) {
1996 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001997 NamedDecl *PrevDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001998 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001999 LookupOrdinaryName, ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002000 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00002001 // GCC apparently allows the following idiom:
2002 //
2003 // typedef NSObject < XCElementTogglerP > XCElementToggler;
2004 // @class XCElementToggler;
2005 //
Fariborz Jahanian04c44552012-01-24 00:40:15 +00002006 // Here we have chosen to ignore the forward class declaration
2007 // with a warning. Since this is the implied behavior.
Richard Smithdda56e42011-04-15 14:24:37 +00002008 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCall8b07ec22010-05-15 11:32:37 +00002009 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002010 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00002011 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCall8b07ec22010-05-15 11:32:37 +00002012 } else {
Mike Stump12b8ce12009-08-04 21:02:39 +00002013 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahanian04c44552012-01-24 00:40:15 +00002014 // to the underlying class. Just ignore the forward class with a warning
2015 // as this will force the intended behavior which is to lookup the typedef
2016 // name.
2017 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
2018 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
2019 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
2020 continue;
2021 }
Fariborz Jahanian0d451812009-05-07 21:49:26 +00002022 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002023 }
Douglas Gregordc9166c2011-12-15 20:29:51 +00002024
2025 // Create a declaration to describe this forward declaration.
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00002026 ObjCInterfaceDecl *PrevIDecl
2027 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002028
2029 IdentifierInfo *ClassName = IdentList[i];
2030 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
2031 // A previous decl with a different name is because of
2032 // @compatibility_alias, for example:
2033 // \code
2034 // @class NewImage;
2035 // @compatibility_alias OldImage NewImage;
2036 // \endcode
2037 // A lookup for 'OldImage' will return the 'NewImage' decl.
2038 //
2039 // In such a case use the real declaration name, instead of the alias one,
2040 // otherwise we will break IdentifierResolver and redecls-chain invariants.
2041 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
2042 // has been aliased.
2043 ClassName = PrevIDecl->getIdentifier();
2044 }
2045
Douglas Gregordc9166c2011-12-15 20:29:51 +00002046 ObjCInterfaceDecl *IDecl
2047 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002048 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002049 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002050
Douglas Gregordc9166c2011-12-15 20:29:51 +00002051 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeafd0b2011-12-27 22:43:10 +00002052 CheckObjCDeclScope(IDecl);
2053 DeclsInGroup.push_back(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002054 }
Rafael Espindolaab417692013-07-09 12:05:01 +00002055
2056 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002057}
2058
John McCall54507ab2011-06-16 01:15:19 +00002059static bool tryMatchRecordTypes(ASTContext &Context,
2060 Sema::MethodMatchStrategy strategy,
2061 const Type *left, const Type *right);
2062
John McCall31168b02011-06-15 23:02:42 +00002063static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
2064 QualType leftQT, QualType rightQT) {
2065 const Type *left =
2066 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2067 const Type *right =
2068 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2069
2070 if (left == right) return true;
2071
2072 // If we're doing a strict match, the types have to match exactly.
2073 if (strategy == Sema::MMS_strict) return false;
2074
2075 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2076
2077 // Otherwise, use this absurdly complicated algorithm to try to
2078 // validate the basic, low-level compatibility of the two types.
2079
2080 // As a minimum, require the sizes and alignments to match.
2081 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
2082 return false;
2083
2084 // Consider all the kinds of non-dependent canonical types:
2085 // - functions and arrays aren't possible as return and parameter types
2086
2087 // - vector types of equal size can be arbitrarily mixed
2088 if (isa<VectorType>(left)) return isa<VectorType>(right);
2089 if (isa<VectorType>(right)) return false;
2090
2091 // - references should only match references of identical type
John McCall54507ab2011-06-16 01:15:19 +00002092 // - structs, unions, and Objective-C objects must match more-or-less
2093 // exactly
John McCall31168b02011-06-15 23:02:42 +00002094 // - everything else should be a scalar
2095 if (!left->isScalarType() || !right->isScalarType())
John McCall54507ab2011-06-16 01:15:19 +00002096 return tryMatchRecordTypes(Context, strategy, left, right);
John McCall31168b02011-06-15 23:02:42 +00002097
John McCall9320b872011-09-09 05:25:32 +00002098 // Make scalars agree in kind, except count bools as chars, and group
2099 // all non-member pointers together.
John McCall31168b02011-06-15 23:02:42 +00002100 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2101 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2102 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2103 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall9320b872011-09-09 05:25:32 +00002104 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2105 leftSK = Type::STK_ObjCObjectPointer;
2106 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2107 rightSK = Type::STK_ObjCObjectPointer;
John McCall31168b02011-06-15 23:02:42 +00002108
2109 // Note that data member pointers and function member pointers don't
2110 // intermix because of the size differences.
2111
2112 return (leftSK == rightSK);
2113}
Chris Lattnerda463fe2007-12-12 07:09:47 +00002114
John McCall54507ab2011-06-16 01:15:19 +00002115static bool tryMatchRecordTypes(ASTContext &Context,
2116 Sema::MethodMatchStrategy strategy,
2117 const Type *lt, const Type *rt) {
2118 assert(lt && rt && lt != rt);
2119
2120 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2121 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2122 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2123
2124 // Require union-hood to match.
2125 if (left->isUnion() != right->isUnion()) return false;
2126
2127 // Require an exact match if either is non-POD.
2128 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2129 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2130 return false;
2131
2132 // Require size and alignment to match.
2133 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2134
2135 // Require fields to match.
2136 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2137 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2138 for (; li != le && ri != re; ++li, ++ri) {
2139 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2140 return false;
2141 }
2142 return (li == le && ri == re);
2143}
2144
Chris Lattnerda463fe2007-12-12 07:09:47 +00002145/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2146/// returns true, or false, accordingly.
2147/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCall31168b02011-06-15 23:02:42 +00002148bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2149 const ObjCMethodDecl *right,
2150 MethodMatchStrategy strategy) {
2151 if (!matchTypes(Context, strategy,
2152 left->getResultType(), right->getResultType()))
2153 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002154
Douglas Gregor560b7fa2013-02-07 19:13:24 +00002155 // If either is hidden, it is not considered to match.
2156 if (left->isHidden() || right->isHidden())
2157 return false;
2158
David Blaikiebbafb8a2012-03-11 07:00:24 +00002159 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002160 (left->hasAttr<NSReturnsRetainedAttr>()
2161 != right->hasAttr<NSReturnsRetainedAttr>() ||
2162 left->hasAttr<NSConsumesSelfAttr>()
2163 != right->hasAttr<NSConsumesSelfAttr>()))
2164 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002165
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002166 ObjCMethodDecl::param_const_iterator
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002167 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2168 re = right->param_end();
Mike Stump11289f42009-09-09 15:08:12 +00002169
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002170 for (; li != le && ri != re; ++li, ++ri) {
John McCall31168b02011-06-15 23:02:42 +00002171 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002172 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCall31168b02011-06-15 23:02:42 +00002173
2174 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2175 return false;
2176
David Blaikiebbafb8a2012-03-11 07:00:24 +00002177 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002178 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2179 return false;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002180 }
2181 return true;
2182}
2183
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002184void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002185 // Record at the head of the list whether there were 0, 1, or >= 2 methods
2186 // inside categories.
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00002187 if (ObjCCategoryDecl *
2188 CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
2189 if (!CD->IsClassExtension() && List->getBits() < 2)
2190 List->setBits(List->getBits()+1);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002191
Douglas Gregorc454afe2012-01-25 00:19:56 +00002192 // If the list is empty, make it a singleton list.
2193 if (List->Method == 0) {
2194 List->Method = Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002195 List->setNext(0);
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002196 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002197 }
2198
2199 // We've seen a method with this name, see if we have already seen this type
2200 // signature.
2201 ObjCMethodList *Previous = List;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002202 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregor600a2f52013-06-21 00:20:25 +00002203 // If we are building a module, keep all of the methods.
2204 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2205 continue;
2206
Douglas Gregore1716012012-01-25 00:49:42 +00002207 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregorc454afe2012-01-25 00:19:56 +00002208 continue;
2209
2210 ObjCMethodDecl *PrevObjCMethod = List->Method;
2211
2212 // Propagate the 'defined' bit.
2213 if (Method->isDefined())
2214 PrevObjCMethod->setDefined(true);
2215
2216 // If a method is deprecated, push it in the global pool.
2217 // This is used for better diagnostics.
2218 if (Method->isDeprecated()) {
2219 if (!PrevObjCMethod->isDeprecated())
2220 List->Method = Method;
2221 }
2222 // If new method is unavailable, push it into global pool
2223 // unless previous one is deprecated.
2224 if (Method->isUnavailable()) {
2225 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2226 List->Method = Method;
2227 }
2228
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002229 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002230 }
2231
2232 // We have a new signature for an existing method - add it.
2233 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregore1716012012-01-25 00:49:42 +00002234 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002235 Previous->setNext(new (Mem) ObjCMethodList(Method, 0));
Douglas Gregorc454afe2012-01-25 00:19:56 +00002236}
2237
Sebastian Redl75d8a322010-08-02 23:18:59 +00002238/// \brief Read the contents of the method pool for a given selector from
2239/// external storage.
Douglas Gregore1716012012-01-25 00:49:42 +00002240void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002241 assert(ExternalSource && "We need an external AST source");
Douglas Gregore1716012012-01-25 00:49:42 +00002242 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002243}
2244
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002245void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002246 bool instance) {
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002247 // Ignore methods of invalid containers.
2248 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002249 return;
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002250
Douglas Gregor70f449b2012-01-25 00:59:09 +00002251 if (ExternalSource)
2252 ReadMethodPool(Method->getSelector());
2253
Sebastian Redl75d8a322010-08-02 23:18:59 +00002254 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor70f449b2012-01-25 00:59:09 +00002255 if (Pos == MethodPool.end())
2256 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2257 GlobalMethods())).first;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002258
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002259 Method->setDefined(impl);
Douglas Gregorc454afe2012-01-25 00:19:56 +00002260
Sebastian Redl75d8a322010-08-02 23:18:59 +00002261 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002262 addMethodToGlobalList(&Entry, Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002263}
2264
John McCall31168b02011-06-15 23:02:42 +00002265/// Determines if this is an "acceptable" loose mismatch in the global
2266/// method pool. This exists mostly as a hack to get around certain
2267/// global mismatches which we can't afford to make warnings / errors.
2268/// Really, what we want is a way to take a method out of the global
2269/// method pool.
2270static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2271 ObjCMethodDecl *other) {
2272 if (!chosen->isInstanceMethod())
2273 return false;
2274
2275 Selector sel = chosen->getSelector();
2276 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2277 return false;
2278
2279 // Don't complain about mismatches for -length if the method we
2280 // chose has an integral result type.
2281 return (chosen->getResultType()->isIntegerType());
2282}
2283
Sebastian Redl75d8a322010-08-02 23:18:59 +00002284ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00002285 bool receiverIdOrClass,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002286 bool warn, bool instance) {
Douglas Gregor70f449b2012-01-25 00:59:09 +00002287 if (ExternalSource)
2288 ReadMethodPool(Sel);
2289
Sebastian Redl75d8a322010-08-02 23:18:59 +00002290 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor70f449b2012-01-25 00:59:09 +00002291 if (Pos == MethodPool.end())
2292 return 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002293
Douglas Gregor77f49a42013-01-16 18:47:38 +00002294 // Gather the non-hidden methods.
Sebastian Redl75d8a322010-08-02 23:18:59 +00002295 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Robert Wilhelmb869a8f2013-08-10 12:33:24 +00002296 SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002297 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Douglas Gregor77f49a42013-01-16 18:47:38 +00002298 if (M->Method && !M->Method->isHidden()) {
2299 // If we're not supposed to warn about mismatches, we're done.
2300 if (!warn)
2301 return M->Method;
Mike Stump11289f42009-09-09 15:08:12 +00002302
Douglas Gregor77f49a42013-01-16 18:47:38 +00002303 Methods.push_back(M->Method);
Sebastian Redl75d8a322010-08-02 23:18:59 +00002304 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00002305 }
Douglas Gregor77f49a42013-01-16 18:47:38 +00002306
2307 // If there aren't any visible methods, we're done.
2308 // FIXME: Recover if there are any known-but-hidden methods?
2309 if (Methods.empty())
2310 return 0;
2311
2312 if (Methods.size() == 1)
2313 return Methods[0];
2314
2315 // We found multiple methods, so we may have to complain.
2316 bool issueDiagnostic = false, issueError = false;
2317
2318 // We support a warning which complains about *any* difference in
2319 // method signature.
2320 bool strictSelectorMatch =
2321 (receiverIdOrClass && warn &&
2322 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2323 R.getBegin())
2324 != DiagnosticsEngine::Ignored));
2325 if (strictSelectorMatch) {
2326 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2327 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2328 issueDiagnostic = true;
2329 break;
2330 }
2331 }
2332 }
2333
2334 // If we didn't see any strict differences, we won't see any loose
2335 // differences. In ARC, however, we also need to check for loose
2336 // mismatches, because most of them are errors.
2337 if (!strictSelectorMatch ||
2338 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2339 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2340 // This checks if the methods differ in type mismatch.
2341 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2342 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2343 issueDiagnostic = true;
2344 if (getLangOpts().ObjCAutoRefCount)
2345 issueError = true;
2346 break;
2347 }
2348 }
2349
2350 if (issueDiagnostic) {
2351 if (issueError)
2352 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2353 else if (strictSelectorMatch)
2354 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2355 else
2356 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2357
2358 Diag(Methods[0]->getLocStart(),
2359 issueError ? diag::note_possibility : diag::note_using)
2360 << Methods[0]->getSourceRange();
2361 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2362 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2363 << Methods[I]->getSourceRange();
2364 }
2365 }
2366 return Methods[0];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002367}
2368
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002369ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redl75d8a322010-08-02 23:18:59 +00002370 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2371 if (Pos == MethodPool.end())
2372 return 0;
2373
2374 GlobalMethods &Methods = Pos->second;
2375
2376 if (Methods.first.Method && Methods.first.Method->isDefined())
2377 return Methods.first.Method;
2378 if (Methods.second.Method && Methods.second.Method->isDefined())
2379 return Methods.second.Method;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002380 return 0;
2381}
2382
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002383static void
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002384HelperSelectorsForTypoCorrection(
2385 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2386 StringRef Typo, const ObjCMethodDecl * Method) {
2387 const unsigned MaxEditDistance = 1;
2388 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieuea8d3702013-06-06 02:22:29 +00002389 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002390
2391 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2392 if (MinPossibleEditDistance > 0 &&
2393 Typo.size() / MinPossibleEditDistance < 1)
2394 return;
2395 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2396 if (EditDistance > MaxEditDistance)
2397 return;
2398 if (EditDistance == BestEditDistance)
2399 BestMethod.push_back(Method);
2400 else if (EditDistance < BestEditDistance) {
2401 BestMethod.clear();
2402 BestMethod.push_back(Method);
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002403 }
2404}
2405
Fariborz Jahanian75481672013-06-17 17:10:54 +00002406static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2407 QualType ObjectType) {
2408 if (ObjectType.isNull())
2409 return true;
2410 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2411 return true;
2412 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != 0;
2413}
2414
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002415const ObjCMethodDecl *
Fariborz Jahanian75481672013-06-17 17:10:54 +00002416Sema::SelectorsForTypoCorrection(Selector Sel,
2417 QualType ObjectType) {
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002418 unsigned NumArgs = Sel.getNumArgs();
2419 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002420 bool ObjectIsId = true, ObjectIsClass = true;
2421 if (ObjectType.isNull())
2422 ObjectIsId = ObjectIsClass = false;
2423 else if (!ObjectType->isObjCObjectPointerType())
2424 return 0;
2425 else if (const ObjCObjectPointerType *ObjCPtr =
2426 ObjectType->getAsObjCInterfacePointerType()) {
2427 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2428 ObjectIsId = ObjectIsClass = false;
2429 }
2430 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2431 ObjectIsClass = false;
2432 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2433 ObjectIsId = false;
2434 else
2435 return 0;
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002436
2437 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2438 e = MethodPool.end(); b != e; b++) {
2439 // instance methods
2440 for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
2441 if (M->Method &&
Fariborz Jahanian06499232013-06-18 17:10:58 +00002442 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2443 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002444 if (ObjectIsId)
2445 Methods.push_back(M->Method);
2446 else if (!ObjectIsClass &&
2447 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2448 Methods.push_back(M->Method);
2449 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002450 // class methods
2451 for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
2452 if (M->Method &&
Fariborz Jahanian06499232013-06-18 17:10:58 +00002453 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2454 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002455 if (ObjectIsClass)
2456 Methods.push_back(M->Method);
2457 else if (!ObjectIsId &&
2458 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2459 Methods.push_back(M->Method);
2460 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002461 }
2462
2463 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2464 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2465 HelperSelectorsForTypoCorrection(SelectedMethods,
2466 Sel.getAsString(), Methods[i]);
2467 }
2468 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : NULL;
2469}
2470
2471static void
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002472HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S,
2473 ObjCMethodList &MethList) {
2474 ObjCMethodList *M = &MethList;
2475 ObjCMethodDecl *TargetMethod = M->Method;
2476 while (TargetMethod &&
2477 isa<ObjCImplDecl>(TargetMethod->getDeclContext())) {
2478 M = M->getNext();
2479 TargetMethod = M ? M->Method : 0;
2480 }
2481 if (!TargetMethod)
2482 return;
2483 bool FirstTime = true;
2484 for (M = M->getNext(); M; M=M->getNext()) {
2485 ObjCMethodDecl *MatchingMethodDecl = M->Method;
2486 if (isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext()))
2487 continue;
2488 if (!S.MatchTwoMethodDeclarations(TargetMethod,
2489 MatchingMethodDecl, Sema::MMS_loose)) {
2490 if (FirstTime) {
2491 FirstTime = false;
2492 S.Diag(TargetMethod->getLocation(), diag::warning_multiple_selectors)
2493 << TargetMethod->getSelector();
2494 }
2495 S.Diag(MatchingMethodDecl->getLocation(), diag::note_also_found);
2496 }
2497 }
2498}
2499
2500void Sema::DiagnoseMismatchedMethodsInGlobalPool() {
2501 unsigned DIAG = diag::warning_multiple_selectors;
2502 if (Diags.getDiagnosticLevel(DIAG, SourceLocation())
2503 == DiagnosticsEngine::Ignored)
2504 return;
2505 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2506 e = MethodPool.end(); b != e; b++) {
2507 // first, instance methods
2508 ObjCMethodList &InstMethList = b->second.first;
2509 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, InstMethList);
Fariborz Jahanianc07e8932013-05-30 21:52:50 +00002510 // second, class methods
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002511 ObjCMethodList &ClsMethList = b->second.second;
2512 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, ClsMethList);
2513 }
2514}
2515
2516/// DiagnoseDuplicateIvars -
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002517/// Check for duplicate ivars in the entire class at the start of
James Dennett634962f2012-06-14 21:40:34 +00002518/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002519/// add ivars to a class in random order which will not be known until
James Dennett634962f2012-06-14 21:40:34 +00002520/// class's \@implementation is seen.
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002521void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2522 ObjCInterfaceDecl *SID) {
2523 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2524 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie40ed2972012-06-06 20:45:41 +00002525 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002526 if (Ivar->isInvalidDecl())
2527 continue;
2528 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2529 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2530 if (prevIvar) {
2531 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2532 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2533 Ivar->setInvalidDecl();
2534 }
2535 }
2536 }
2537}
2538
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002539Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2540 switch (CurContext->getDeclKind()) {
2541 case Decl::ObjCInterface:
2542 return Sema::OCK_Interface;
2543 case Decl::ObjCProtocol:
2544 return Sema::OCK_Protocol;
2545 case Decl::ObjCCategory:
2546 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2547 return Sema::OCK_ClassExtension;
2548 else
2549 return Sema::OCK_Category;
2550 case Decl::ObjCImplementation:
2551 return Sema::OCK_Implementation;
2552 case Decl::ObjCCategoryImpl:
2553 return Sema::OCK_CategoryImplementation;
2554
2555 default:
2556 return Sema::OCK_None;
2557 }
2558}
2559
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002560// Note: For class/category implementations, allMethods is always null.
Robert Wilhelm57c67112013-07-17 21:14:35 +00002561Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods,
Fariborz Jahaniandfb76872013-07-17 00:05:08 +00002562 ArrayRef<DeclGroupPtrTy> allTUVars) {
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002563 if (getObjCContainerKind() == Sema::OCK_None)
2564 return 0;
2565
2566 assert(AtEnd.isValid() && "Invalid location for '@end'");
2567
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002568 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2569 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002570
Mike Stump11289f42009-09-09 15:08:12 +00002571 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00002572 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2573 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002574 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00002575
Steve Naroff35c62ae2009-01-08 17:28:14 +00002576 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2577 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2578 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2579
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002580 for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002581 ObjCMethodDecl *Method =
John McCall48871652010-08-21 09:40:31 +00002582 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002583
2584 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00002585 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002586 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002587 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002588 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002589 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002590 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002591 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002592 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002593 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002594 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002595 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002596 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002597 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002598 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002599 if (!Context.getSourceManager().isInSystemHeader(
2600 Method->getLocation()))
2601 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2602 << Method->getDeclName();
2603 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2604 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002605 InsMap[Method->getSelector()] = Method;
2606 /// The following allows us to typecheck messages to "id".
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002607 AddInstanceMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002608 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002609 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002610 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002611 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002612 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002613 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002614 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002615 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002616 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002617 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002618 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002619 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002620 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002621 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002622 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002623 if (!Context.getSourceManager().isInSystemHeader(
2624 Method->getLocation()))
2625 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2626 << Method->getDeclName();
2627 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2628 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002629 ClsMap[Method->getSelector()] = Method;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002630 AddFactoryMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002631 }
2632 }
2633 }
Douglas Gregorb8982092013-01-21 19:42:21 +00002634 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2635 // Nothing to do here.
Steve Naroffb3a87982009-01-09 15:36:25 +00002636 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002637 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00002638 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002639 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00002640
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002641 if (C->IsClassExtension()) {
2642 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2643 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002644 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002645 }
Steve Naroffb3a87982009-01-09 15:36:25 +00002646 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002647 if (CDecl->getIdentifier())
2648 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2649 // user-defined setter/getter. It also synthesizes setter/getter methods
2650 // and adds them to the DeclContext and global method pools.
2651 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2652 E = CDecl->prop_end();
2653 I != E; ++I)
David Blaikie40ed2972012-06-06 20:45:41 +00002654 ProcessPropertyDecl(*I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00002655 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00002656 }
2657 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002658 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002659 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002660 // Any property declared in a class extension might have user
2661 // declared setter or getter in current class extension or one
2662 // of the other class extensions. Mark them as synthesized as
2663 // property will be synthesized when property with same name is
2664 // seen in the @implementation.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002665 for (ObjCInterfaceDecl::visible_extensions_iterator
2666 Ext = IDecl->visible_extensions_begin(),
2667 ExtEnd = IDecl->visible_extensions_end();
2668 Ext != ExtEnd; ++Ext) {
2669 for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
2670 E = Ext->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002671 ObjCPropertyDecl *Property = *I;
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002672 // Skip over properties declared @dynamic
2673 if (const ObjCPropertyImplDecl *PIDecl
2674 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2675 if (PIDecl->getPropertyImplementation()
2676 == ObjCPropertyImplDecl::Dynamic)
2677 continue;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002678
2679 for (ObjCInterfaceDecl::visible_extensions_iterator
2680 Ext = IDecl->visible_extensions_begin(),
2681 ExtEnd = IDecl->visible_extensions_end();
2682 Ext != ExtEnd; ++Ext) {
2683 if (ObjCMethodDecl *GetterMethod
2684 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002685 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002686 if (!Property->isReadOnly())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002687 if (ObjCMethodDecl *SetterMethod
2688 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002689 SetterMethod->setPropertyAccessor(true);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002690 }
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002691 }
2692 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00002693 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002694 AtomicPropertySetterGetterRules(IC, IDecl);
John McCall31168b02011-06-15 23:02:42 +00002695 DiagnoseOwningPropertyGetterSynthesis(IC);
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002696 if (IDecl->hasDesignatedInitializers())
2697 DiagnoseMissingDesignatedInitOverrides(IC, IDecl);
2698
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002699 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2700 if (IDecl->getSuperClass() == NULL) {
2701 // This class has no superclass, so check that it has been marked with
2702 // __attribute((objc_root_class)).
2703 if (!HasRootClassAttr) {
2704 SourceLocation DeclLoc(IDecl->getLocation());
2705 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2706 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2707 << IDecl->getIdentifier();
2708 // See if NSObject is in the current scope, and if it is, suggest
2709 // adding " : NSObject " to the class declaration.
2710 NamedDecl *IF = LookupSingleName(TUScope,
2711 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2712 DeclLoc, LookupOrdinaryName);
2713 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2714 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2715 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2716 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2717 } else {
2718 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2719 }
2720 }
2721 } else if (HasRootClassAttr) {
2722 // Complain that only root classes may have this attribute.
2723 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2724 }
2725
John McCall5fb5df92012-06-20 06:18:46 +00002726 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002727 while (IDecl->getSuperClass()) {
2728 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2729 IDecl = IDecl->getSuperClass();
2730 }
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002731 }
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002732 }
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00002733 SetIvarInitializers(IC);
Mike Stump11289f42009-09-09 15:08:12 +00002734 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00002735 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002736 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00002737
Chris Lattnerda463fe2007-12-12 07:09:47 +00002738 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00002739 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00002740 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002741 if (ObjCCategoryDecl *Cat
2742 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2743 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002744 }
2745 }
2746 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002747 if (isInterfaceDeclKind) {
2748 // Reject invalid vardecls.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002749 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002750 DeclGroupRef DG = allTUVars[i].get();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002751 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2752 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00002753 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00002754 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00002755 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002756 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00002757 }
Fariborz Jahanian4327b322011-08-29 17:33:12 +00002758 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002759
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002760 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002761 DeclGroupRef DG = allTUVars[i].get();
Argyrios Kyrtzidis8ad3bab2011-11-23 20:27:36 +00002762 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2763 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002764 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2765 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002766
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +00002767 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002768 return ClassDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002769}
2770
2771
2772/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2773/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00002774static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002775CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCallca872902011-05-01 03:04:29 +00002776 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002777}
2778
Douglas Gregor33823722011-06-11 01:09:30 +00002779/// \brief Check whether the declared result type of the given Objective-C
2780/// method declaration is compatible with the method's class.
2781///
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002782static Sema::ResultTypeCompatibilityKind
Douglas Gregor33823722011-06-11 01:09:30 +00002783CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2784 ObjCInterfaceDecl *CurrentClass) {
2785 QualType ResultType = Method->getResultType();
Douglas Gregor33823722011-06-11 01:09:30 +00002786
2787 // If an Objective-C method inherits its related result type, then its
2788 // declared result type must be compatible with its own class type. The
2789 // declared result type is compatible if:
2790 if (const ObjCObjectPointerType *ResultObjectType
2791 = ResultType->getAs<ObjCObjectPointerType>()) {
2792 // - it is id or qualified id, or
2793 if (ResultObjectType->isObjCIdType() ||
2794 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002795 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002796
2797 if (CurrentClass) {
2798 if (ObjCInterfaceDecl *ResultClass
2799 = ResultObjectType->getInterfaceDecl()) {
2800 // - it is the same as the method's class type, or
Douglas Gregor0b144e12011-12-15 00:29:59 +00002801 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002802 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002803
2804 // - it is a superclass of the method's class type
2805 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002806 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002807 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00002808 } else {
2809 // Any Objective-C pointer type might be acceptable for a protocol
2810 // method; we just don't know.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002811 return Sema::RTC_Unknown;
Douglas Gregor33823722011-06-11 01:09:30 +00002812 }
2813 }
2814
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002815 return Sema::RTC_Incompatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002816}
2817
John McCalld2930c22011-07-22 02:45:48 +00002818namespace {
2819/// A helper class for searching for methods which a particular method
2820/// overrides.
2821class OverrideSearch {
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002822public:
John McCalld2930c22011-07-22 02:45:48 +00002823 Sema &S;
2824 ObjCMethodDecl *Method;
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002825 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCalld2930c22011-07-22 02:45:48 +00002826 bool Recursive;
2827
2828public:
2829 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2830 Selector selector = method->getSelector();
2831
2832 // Bypass this search if we've never seen an instance/class method
2833 // with this selector before.
2834 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2835 if (it == S.MethodPool.end()) {
Axel Naumanndd433f02012-10-18 19:05:02 +00002836 if (!S.getExternalSource()) return;
Douglas Gregore1716012012-01-25 00:49:42 +00002837 S.ReadMethodPool(selector);
2838
2839 it = S.MethodPool.find(selector);
2840 if (it == S.MethodPool.end())
2841 return;
John McCalld2930c22011-07-22 02:45:48 +00002842 }
2843 ObjCMethodList &list =
2844 method->isInstanceMethod() ? it->second.first : it->second.second;
2845 if (!list.Method) return;
2846
2847 ObjCContainerDecl *container
2848 = cast<ObjCContainerDecl>(method->getDeclContext());
2849
2850 // Prevent the search from reaching this container again. This is
2851 // important with categories, which override methods from the
2852 // interface and each other.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002853 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2854 searchFromContainer(container);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002855 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2856 searchFromContainer(Interface);
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002857 } else {
2858 searchFromContainer(container);
2859 }
Douglas Gregor33823722011-06-11 01:09:30 +00002860 }
John McCalld2930c22011-07-22 02:45:48 +00002861
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002862 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCalld2930c22011-07-22 02:45:48 +00002863 iterator begin() const { return Overridden.begin(); }
2864 iterator end() const { return Overridden.end(); }
2865
2866private:
2867 void searchFromContainer(ObjCContainerDecl *container) {
2868 if (container->isInvalidDecl()) return;
2869
2870 switch (container->getDeclKind()) {
2871#define OBJCCONTAINER(type, base) \
2872 case Decl::type: \
2873 searchFrom(cast<type##Decl>(container)); \
2874 break;
2875#define ABSTRACT_DECL(expansion)
2876#define DECL(type, base) \
2877 case Decl::type:
2878#include "clang/AST/DeclNodes.inc"
2879 llvm_unreachable("not an ObjC container!");
2880 }
2881 }
2882
2883 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00002884 if (!protocol->hasDefinition())
2885 return;
2886
John McCalld2930c22011-07-22 02:45:48 +00002887 // A method in a protocol declaration overrides declarations from
2888 // referenced ("parent") protocols.
2889 search(protocol->getReferencedProtocols());
2890 }
2891
2892 void searchFrom(ObjCCategoryDecl *category) {
2893 // A method in a category declaration overrides declarations from
2894 // the main class and from protocols the category references.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002895 // The main class is handled in the constructor.
John McCalld2930c22011-07-22 02:45:48 +00002896 search(category->getReferencedProtocols());
2897 }
2898
2899 void searchFrom(ObjCCategoryImplDecl *impl) {
2900 // A method in a category definition that has a category
2901 // declaration overrides declarations from the category
2902 // declaration.
2903 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2904 search(category);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002905 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2906 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002907
2908 // Otherwise it overrides declarations from the class.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002909 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2910 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002911 }
2912 }
2913
2914 void searchFrom(ObjCInterfaceDecl *iface) {
2915 // A method in a class declaration overrides declarations from
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00002916 if (!iface->hasDefinition())
2917 return;
2918
John McCalld2930c22011-07-22 02:45:48 +00002919 // - categories,
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00002920 for (ObjCInterfaceDecl::known_categories_iterator
2921 cat = iface->known_categories_begin(),
2922 catEnd = iface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002923 cat != catEnd; ++cat) {
2924 search(*cat);
2925 }
John McCalld2930c22011-07-22 02:45:48 +00002926
2927 // - the super class, and
2928 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2929 search(super);
2930
2931 // - any referenced protocols.
2932 search(iface->getReferencedProtocols());
2933 }
2934
2935 void searchFrom(ObjCImplementationDecl *impl) {
2936 // A method in a class implementation overrides declarations from
2937 // the class interface.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002938 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2939 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002940 }
2941
2942
2943 void search(const ObjCProtocolList &protocols) {
2944 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2945 i != e; ++i)
2946 search(*i);
2947 }
2948
2949 void search(ObjCContainerDecl *container) {
John McCalld2930c22011-07-22 02:45:48 +00002950 // Check for a method in this container which matches this selector.
2951 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00002952 Method->isInstanceMethod(),
2953 /*AllowHidden=*/true);
John McCalld2930c22011-07-22 02:45:48 +00002954
2955 // If we find one, record it and bail out.
2956 if (meth) {
2957 Overridden.insert(meth);
2958 return;
2959 }
2960
2961 // Otherwise, search for methods that a hypothetical method here
2962 // would have overridden.
2963
2964 // Note that we're now in a recursive case.
2965 Recursive = true;
2966
2967 searchFromContainer(container);
2968 }
2969};
Douglas Gregor33823722011-06-11 01:09:30 +00002970}
2971
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002972void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2973 ObjCInterfaceDecl *CurrentClass,
2974 ResultTypeCompatibilityKind RTC) {
2975 // Search for overridden methods and merge information down from them.
2976 OverrideSearch overrides(*this, ObjCMethod);
2977 // Keep track if the method overrides any method in the class's base classes,
2978 // its protocols, or its categories' protocols; we will keep that info
2979 // in the ObjCMethodDecl.
2980 // For this info, a method in an implementation is not considered as
2981 // overriding the same method in the interface or its categories.
2982 bool hasOverriddenMethodsInBaseOrProtocol = false;
2983 for (OverrideSearch::iterator
2984 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2985 ObjCMethodDecl *overridden = *i;
2986
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00002987 if (!hasOverriddenMethodsInBaseOrProtocol) {
2988 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2989 CurrentClass != overridden->getClassInterface() ||
2990 overridden->isOverriding()) {
2991 hasOverriddenMethodsInBaseOrProtocol = true;
2992
2993 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
2994 // OverrideSearch will return as "overridden" the same method in the
2995 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
2996 // check whether a category of a base class introduced a method with the
2997 // same selector, after the interface method declaration.
2998 // To avoid unnecessary lookups in the majority of cases, we use the
2999 // extra info bits in GlobalMethodPool to check whether there were any
3000 // category methods with this selector.
3001 GlobalMethodPool::iterator It =
3002 MethodPool.find(ObjCMethod->getSelector());
3003 if (It != MethodPool.end()) {
3004 ObjCMethodList &List =
3005 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
3006 unsigned CategCount = List.getBits();
3007 if (CategCount > 0) {
3008 // If the method is in a category we'll do lookup if there were at
3009 // least 2 category methods recorded, otherwise only one will do.
3010 if (CategCount > 1 ||
3011 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
3012 OverrideSearch overrides(*this, overridden);
3013 for (OverrideSearch::iterator
3014 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
3015 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00003016 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
3017 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00003018 hasOverriddenMethodsInBaseOrProtocol = true;
3019 overridden->setOverriding(true);
3020 break;
3021 }
3022 }
3023 }
3024 }
3025 }
3026 }
3027 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003028
3029 // Propagate down the 'related result type' bit from overridden methods.
3030 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
3031 ObjCMethod->SetRelatedResultType();
3032
3033 // Then merge the declarations.
3034 mergeObjCMethodDecls(ObjCMethod, overridden);
3035
3036 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3037 continue; // Conflicting properties are detected elsewhere.
3038
3039 // Check for overriding methods
3040 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3041 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3042 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3043 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3044
3045 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanian31a25682012-07-05 22:26:07 +00003046 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3047 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003048 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3049 E = ObjCMethod->param_end();
Douglas Gregor0bf70f42012-05-17 23:13:29 +00003050 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3051 PrevE = overridden->param_end();
3052 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003053 assert(PrevI != overridden->param_end() && "Param mismatch");
3054 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3055 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3056 // If type of argument of method in this class does not match its
3057 // respective argument type in the super class method, issue warning;
3058 if (!Context.typesAreCompatible(T1, T2)) {
3059 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3060 << T1 << T2;
3061 Diag(overridden->getLocation(), diag::note_previous_declaration);
3062 break;
3063 }
3064 }
3065 }
3066 }
3067
3068 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3069}
3070
John McCall48871652010-08-21 09:40:31 +00003071Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003072 Scope *S,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003073 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003074 tok::TokenKind MethodType,
John McCallba7bf592010-08-24 05:47:05 +00003075 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003076 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003077 Selector Sel,
3078 // optional arguments. The number of types/arguments is obtained
3079 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003080 ObjCArgInfo *ArgInfo,
Fariborz Jahanian60462092010-04-08 00:30:06 +00003081 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattnerda463fe2007-12-12 07:09:47 +00003082 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003083 bool isVariadic, bool MethodDefinition) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003084 // Make sure we can establish a context for the method.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003085 if (!CurContext->isObjCContainer()) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003086 Diag(MethodLoc, diag::error_missing_method_context);
John McCall48871652010-08-21 09:40:31 +00003087 return 0;
Steve Naroff83777fe2008-02-29 21:48:07 +00003088 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003089 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3090 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003091 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00003092
Douglas Gregorbab8a962011-09-08 01:46:34 +00003093 bool HasRelatedResultType = false;
Douglas Gregor12852d92010-03-08 14:59:44 +00003094 TypeSourceInfo *ResultTInfo = 0;
Steve Naroff32606412009-02-20 22:59:16 +00003095 if (ReturnType) {
Douglas Gregor12852d92010-03-08 14:59:44 +00003096 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump11289f42009-09-09 15:08:12 +00003097
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003098 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
John McCall48871652010-08-21 09:40:31 +00003099 return 0;
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003100
Douglas Gregorbab8a962011-09-08 01:46:34 +00003101 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003102 } else { // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003103 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianb21138f2011-07-21 17:38:14 +00003104 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003105 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003106 }
Mike Stump11289f42009-09-09 15:08:12 +00003107
3108 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003109 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003110 resultDeclType,
Douglas Gregor12852d92010-03-08 14:59:44 +00003111 ResultTInfo,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003112 CurContext,
Chris Lattner8d8829e2008-03-16 00:49:28 +00003113 MethodType == tok::minus, isVariadic,
Jordan Rosed01e83a2012-10-10 16:42:25 +00003114 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003115 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor33823722011-06-11 01:09:30 +00003116 MethodDeclKind == tok::objc_optional
3117 ? ObjCMethodDecl::Optional
3118 : ObjCMethodDecl::Required,
Douglas Gregorbab8a962011-09-08 01:46:34 +00003119 HasRelatedResultType);
Mike Stump11289f42009-09-09 15:08:12 +00003120
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003121 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00003122
Chris Lattner23b0faf2009-04-11 19:42:43 +00003123 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00003124 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00003125 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00003126
David Blaikie7d170102013-05-15 07:37:26 +00003127 if (!ArgInfo[i].Type) {
John McCall856bbea2009-10-23 21:48:59 +00003128 ArgType = Context.getObjCIdType();
3129 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003130 } else {
John McCall856bbea2009-10-23 21:48:59 +00003131 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003132 }
Mike Stump11289f42009-09-09 15:08:12 +00003133
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003134 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3135 LookupOrdinaryName, ForRedeclaration);
3136 LookupName(R, S);
3137 if (R.isSingleResult()) {
3138 NamedDecl *PrevDecl = R.getFoundDecl();
3139 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003140 Diag(ArgInfo[i].NameLoc,
3141 (MethodDefinition ? diag::warn_method_param_redefinition
3142 : diag::warn_method_param_declaration))
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003143 << ArgInfo[i].Name;
3144 Diag(PrevDecl->getLocation(),
3145 diag::note_previous_declaration);
3146 }
3147 }
3148
Abramo Bagnaradff19302011-03-08 08:55:46 +00003149 SourceLocation StartLoc = DI
3150 ? DI->getTypeLoc().getBeginLoc()
3151 : ArgInfo[i].NameLoc;
3152
John McCalld44f4d72011-04-23 02:46:06 +00003153 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3154 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003155 ArgType, DI, SC_None);
Mike Stump11289f42009-09-09 15:08:12 +00003156
John McCall82490832011-05-02 00:30:12 +00003157 Param->setObjCMethodScopeInfo(i);
3158
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003159 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003160 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00003161
Chris Lattner9713a1c2009-04-11 19:34:56 +00003162 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00003163 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00003164
Fariborz Jahanian52d02f62012-01-14 18:44:35 +00003165 if (Param->hasAttr<BlocksAttr>()) {
3166 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3167 Param->setInvalidDecl();
3168 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003169 S->AddDecl(Param);
3170 IdResolver.AddDecl(Param);
3171
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003172 Params.push_back(Param);
3173 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003174
Fariborz Jahanian60462092010-04-08 00:30:06 +00003175 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCall48871652010-08-21 09:40:31 +00003176 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003177 QualType ArgType = Param->getType();
3178 if (ArgType.isNull())
3179 ArgType = Context.getObjCIdType();
3180 else
3181 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor84280642011-07-12 04:42:08 +00003182 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003183
Fariborz Jahanian60462092010-04-08 00:30:06 +00003184 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003185 Params.push_back(Param);
3186 }
3187
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003188 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003189 ObjCMethod->setObjCDeclQualifier(
3190 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00003191
3192 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00003193 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00003194
Douglas Gregor87e92752010-12-21 17:34:17 +00003195 // Add the method now.
John McCalld2930c22011-07-22 02:45:48 +00003196 const ObjCMethodDecl *PrevMethod = 0;
3197 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00003198 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003199 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3200 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003201 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003202 PrevMethod = ImpDecl->getClassMethod(Sel);
3203 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003204 }
Douglas Gregor33823722011-06-11 01:09:30 +00003205
Fariborz Jahanian512a4cc92011-10-22 01:21:15 +00003206 ObjCMethodDecl *IMD = 0;
3207 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3208 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3209 ObjCMethod->isInstanceMethod());
Fariborz Jahaniandb4fc282013-07-09 22:02:20 +00003210 if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
3211 !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
3212 // merge the attribute into implementation.
3213 ObjCMethod->addAttr(
3214 new (Context) ObjCRequiresSuperAttr(ObjCMethod->getLocation(), Context));
3215 }
Fariborz Jahanian7e350d22013-12-17 22:44:28 +00003216 if (isa<ObjCCategoryImplDecl>(ImpDecl)) {
3217 ObjCMethodFamily family = ObjCMethod->getMethodFamily();
Fariborz Jahanian1b30b592013-12-18 00:52:54 +00003218 if (family == OMF_dealloc && IMD && IMD->isOverriding())
Fariborz Jahanian7e350d22013-12-17 22:44:28 +00003219 Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category)
3220 << ObjCMethod->getDeclName();
Fariborz Jahanian7e350d22013-12-17 22:44:28 +00003221 }
Douglas Gregor87e92752010-12-21 17:34:17 +00003222 } else {
3223 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003224 }
John McCalld2930c22011-07-22 02:45:48 +00003225
Chris Lattnerda463fe2007-12-12 07:09:47 +00003226 if (PrevMethod) {
3227 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00003228 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00003229 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00003230 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian096f7c12013-05-13 17:27:00 +00003231 ObjCMethod->setInvalidDecl();
3232 return ObjCMethod;
Mike Stump11289f42009-09-09 15:08:12 +00003233 }
John McCall28a6aea2009-11-04 02:18:39 +00003234
Douglas Gregor33823722011-06-11 01:09:30 +00003235 // If this Objective-C method does not have a related result type, but we
3236 // are allowed to infer related result types, try to do so based on the
3237 // method family.
3238 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3239 if (!CurrentClass) {
3240 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3241 CurrentClass = Cat->getClassInterface();
3242 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3243 CurrentClass = Impl->getClassInterface();
3244 else if (ObjCCategoryImplDecl *CatImpl
3245 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3246 CurrentClass = CatImpl->getClassInterface();
3247 }
John McCalld2930c22011-07-22 02:45:48 +00003248
Douglas Gregorbab8a962011-09-08 01:46:34 +00003249 ResultTypeCompatibilityKind RTC
3250 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCalld2930c22011-07-22 02:45:48 +00003251
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003252 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCalld2930c22011-07-22 02:45:48 +00003253
John McCall31168b02011-06-15 23:02:42 +00003254 bool ARCError = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003255 if (getLangOpts().ObjCAutoRefCount)
John McCalle48f3892013-04-04 01:38:37 +00003256 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCall31168b02011-06-15 23:02:42 +00003257
Douglas Gregorbab8a962011-09-08 01:46:34 +00003258 // Infer the related result type when possible.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003259 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregorbab8a962011-09-08 01:46:34 +00003260 !ObjCMethod->hasRelatedResultType() &&
3261 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor33823722011-06-11 01:09:30 +00003262 bool InferRelatedResultType = false;
3263 switch (ObjCMethod->getMethodFamily()) {
3264 case OMF_None:
3265 case OMF_copy:
3266 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00003267 case OMF_finalize:
Douglas Gregor33823722011-06-11 01:09:30 +00003268 case OMF_mutableCopy:
3269 case OMF_release:
3270 case OMF_retainCount:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00003271 case OMF_performSelector:
Douglas Gregor33823722011-06-11 01:09:30 +00003272 break;
3273
3274 case OMF_alloc:
3275 case OMF_new:
3276 InferRelatedResultType = ObjCMethod->isClassMethod();
3277 break;
3278
3279 case OMF_init:
3280 case OMF_autorelease:
3281 case OMF_retain:
3282 case OMF_self:
3283 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3284 break;
3285 }
3286
John McCalld2930c22011-07-22 02:45:48 +00003287 if (InferRelatedResultType)
Douglas Gregor33823722011-06-11 01:09:30 +00003288 ObjCMethod->SetRelatedResultType();
Douglas Gregor33823722011-06-11 01:09:30 +00003289 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00003290
3291 ActOnDocumentableDecl(ObjCMethod);
3292
John McCall48871652010-08-21 09:40:31 +00003293 return ObjCMethod;
Chris Lattnerda463fe2007-12-12 07:09:47 +00003294}
3295
Chris Lattner438e5012008-12-17 07:13:27 +00003296bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +00003297 // Following is also an error. But it is caused by a missing @end
3298 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003299 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003300 return false;
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003301
3302 // If we switched context to translation unit while we are still lexically in
3303 // an objc container, it means the parser missed emitting an error.
3304 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3305 return false;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003306
Anders Carlssona6b508a2008-11-04 16:57:32 +00003307 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3308 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003309
Anders Carlssona6b508a2008-11-04 16:57:32 +00003310 return true;
3311}
Chris Lattner438e5012008-12-17 07:13:27 +00003312
James Dennett634962f2012-06-14 21:40:34 +00003313/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattner438e5012008-12-17 07:13:27 +00003314/// instance variables of ClassName into Decls.
John McCall48871652010-08-21 09:40:31 +00003315void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00003316 IdentifierInfo *ClassName,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003317 SmallVectorImpl<Decl*> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00003318 // Check that ClassName is a valid class
Douglas Gregorb2ccf012010-04-15 22:33:43 +00003319 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattner438e5012008-12-17 07:13:27 +00003320 if (!Class) {
3321 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3322 return;
3323 }
John McCall5fb5df92012-06-20 06:18:46 +00003324 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00003325 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3326 return;
3327 }
Mike Stump11289f42009-09-09 15:08:12 +00003328
Chris Lattner438e5012008-12-17 07:13:27 +00003329 // Collect the instance variables
Jordy Rosea91768e2011-07-22 02:08:32 +00003330 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003331 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003332 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003333 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003334 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCall48871652010-08-21 09:40:31 +00003335 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaradff19302011-03-08 08:55:46 +00003336 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3337 /*FIXME: StartL=*/ID->getLocation(),
3338 ID->getLocation(),
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003339 ID->getIdentifier(), ID->getType(),
3340 ID->getBitWidth());
John McCall48871652010-08-21 09:40:31 +00003341 Decls.push_back(FD);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003342 }
Mike Stump11289f42009-09-09 15:08:12 +00003343
Chris Lattner438e5012008-12-17 07:13:27 +00003344 // Introduce all of these fields into the appropriate scope.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003345 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00003346 D != Decls.end(); ++D) {
John McCall48871652010-08-21 09:40:31 +00003347 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003348 if (getLangOpts().CPlusPlus)
Chris Lattner438e5012008-12-17 07:13:27 +00003349 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCall48871652010-08-21 09:40:31 +00003350 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003351 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00003352 }
3353}
3354
Douglas Gregorf3564192010-04-26 17:32:49 +00003355/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaradff19302011-03-08 08:55:46 +00003356VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3357 SourceLocation StartLoc,
3358 SourceLocation IdLoc,
3359 IdentifierInfo *Id,
Douglas Gregorf3564192010-04-26 17:32:49 +00003360 bool Invalid) {
3361 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3362 // duration shall not be qualified by an address-space qualifier."
3363 // Since all parameters have automatic store duration, they can not have
3364 // an address space.
3365 if (T.getAddressSpace() != 0) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003366 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregorf3564192010-04-26 17:32:49 +00003367 Invalid = true;
3368 }
3369
3370 // An @catch parameter must be an unqualified object pointer type;
3371 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3372 if (Invalid) {
3373 // Don't do any further checking.
Douglas Gregorf4e837f2010-04-26 17:57:08 +00003374 } else if (T->isDependentType()) {
3375 // Okay: we don't know what this type will instantiate to.
Douglas Gregorf3564192010-04-26 17:32:49 +00003376 } else if (!T->isObjCObjectPointerType()) {
3377 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003378 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregorf3564192010-04-26 17:32:49 +00003379 } else if (T->isObjCQualifiedIdType()) {
3380 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003381 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregorf3564192010-04-26 17:32:49 +00003382 }
3383
Abramo Bagnaradff19302011-03-08 08:55:46 +00003384 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003385 T, TInfo, SC_None);
Douglas Gregor3f324d562010-05-03 18:51:14 +00003386 New->setExceptionVariable(true);
3387
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003388 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003389 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003390 Invalid = true;
3391
Douglas Gregorf3564192010-04-26 17:32:49 +00003392 if (Invalid)
3393 New->setInvalidDecl();
3394 return New;
3395}
3396
John McCall48871652010-08-21 09:40:31 +00003397Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003398 const DeclSpec &DS = D.getDeclSpec();
3399
3400 // We allow the "register" storage class on exception variables because
3401 // GCC did, but we drop it completely. Any other storage class is an error.
3402 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3403 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3404 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithb4a9e862013-04-12 22:46:28 +00003405 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003406 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithb4a9e862013-04-12 22:46:28 +00003407 << DeclSpec::getSpecifierName(SCS);
3408 }
3409 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3410 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3411 diag::err_invalid_thread)
3412 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregorf3564192010-04-26 17:32:49 +00003413 D.getMutableDeclSpec().ClearStorageClassSpecs();
3414
Richard Smithb1402ae2013-03-18 22:52:47 +00003415 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregorf3564192010-04-26 17:32:49 +00003416
3417 // Check that there are no default arguments inside the type of this
3418 // exception object (C++ only).
David Blaikiebbafb8a2012-03-11 07:00:24 +00003419 if (getLangOpts().CPlusPlus)
Douglas Gregorf3564192010-04-26 17:32:49 +00003420 CheckExtraCXXDefaultArguments(D);
3421
Argyrios Kyrtzidisef7022f2011-06-28 03:01:15 +00003422 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCall8cb7bdf2010-06-04 23:28:52 +00003423 QualType ExceptionType = TInfo->getType();
Douglas Gregorf3564192010-04-26 17:32:49 +00003424
Abramo Bagnaradff19302011-03-08 08:55:46 +00003425 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3426 D.getSourceRange().getBegin(),
3427 D.getIdentifierLoc(),
3428 D.getIdentifier(),
Douglas Gregorf3564192010-04-26 17:32:49 +00003429 D.isInvalidType());
3430
3431 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3432 if (D.getCXXScopeSpec().isSet()) {
3433 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3434 << D.getCXXScopeSpec().getRange();
3435 New->setInvalidDecl();
3436 }
3437
3438 // Add the parameter declaration into this scope.
John McCall48871652010-08-21 09:40:31 +00003439 S->AddDecl(New);
Douglas Gregorf3564192010-04-26 17:32:49 +00003440 if (D.getIdentifier())
3441 IdResolver.AddDecl(New);
3442
3443 ProcessDeclAttributes(S, New, D);
3444
3445 if (New->hasAttr<BlocksAttr>())
3446 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCall48871652010-08-21 09:40:31 +00003447 return New;
Douglas Gregore11ee112010-04-23 23:01:43 +00003448}
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003449
3450/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003451/// initialization.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003452void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003453 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003454 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3455 Iv= Iv->getNextIvar()) {
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003456 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00003457 if (QT->isRecordType())
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003458 Ivars.push_back(Iv);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003459 }
3460}
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003461
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003462void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor72e357f2011-07-28 14:54:22 +00003463 // Load referenced selectors from the external source.
3464 if (ExternalSource) {
3465 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3466 ExternalSource->ReadReferencedSelectors(Sels);
3467 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3468 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3469 }
3470
Fariborz Jahanian42f89382013-05-30 21:48:58 +00003471 DiagnoseMismatchedMethodsInGlobalPool();
3472
Fariborz Jahanianc9b7c202011-02-04 23:19:27 +00003473 // Warning will be issued only when selector table is
3474 // generated (which means there is at lease one implementation
3475 // in the TU). This is to match gcc's behavior.
3476 if (ReferencedSelectors.empty() ||
3477 !Context.AnyObjCImplementation())
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003478 return;
3479 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3480 ReferencedSelectors.begin(),
3481 E = ReferencedSelectors.end(); S != E; ++S) {
3482 Selector Sel = (*S).first;
3483 if (!LookupImplementedMethodInGlobalPool(Sel))
3484 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3485 }
3486 return;
3487}
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003488
3489ObjCIvarDecl *
3490Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method,
3491 const ObjCPropertyDecl *&PDecl) const {
3492
3493 const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
3494 if (!IDecl)
3495 return 0;
3496 Method = IDecl->lookupMethod(Method->getSelector(), true);
3497 if (!Method || !Method->isPropertyAccessor())
3498 return 0;
Fariborz Jahanian617e49a2013-11-15 17:48:00 +00003499 if ((PDecl = Method->findPropertyDecl())) {
3500 if (!PDecl->getDeclContext())
3501 return 0;
3502 // Make sure property belongs to accessor's class and not to
3503 // one of its super classes.
3504 if (const ObjCInterfaceDecl *CID =
3505 dyn_cast<ObjCInterfaceDecl>(PDecl->getDeclContext()))
3506 if (CID != IDecl)
3507 return 0;
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003508 return PDecl->getPropertyIvarDecl();
Fariborz Jahanian617e49a2013-11-15 17:48:00 +00003509 }
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003510 return 0;
3511}
3512
3513void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S) {
Fariborz Jahanian54f87382013-12-11 00:53:48 +00003514 if (S->hasUnrecoverableErrorOccurred() || !S->isInObjcMethodOuterScope())
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003515 return;
3516
3517 const ObjCMethodDecl *CurMethod = getCurMethodDecl();
3518 if (!CurMethod)
3519 return;
3520 const ObjCPropertyDecl *PDecl;
3521 const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl);
3522 if (IV && !IV->getBackingIvarReferencedInAccessor()) {
3523 Diag(getCurMethodDecl()->getLocation(), diag::warn_unused_property_backing_ivar)
3524 << IV->getDeclName();
3525 Diag(PDecl->getLocation(), diag::note_property_declare);
3526 }
3527}