blob: a41bd22cdf13fa0a023e5da7cb5b4a4d312f0ac9 [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
Steve Naroff15833ed2008-02-10 21:38:56 +00001228void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001229 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian9fc39c42011-06-24 20:31:37 +00001230 // No point warning no definition of method which is 'unavailable'.
Douglas Gregorc2e3d5c2012-12-11 18:53:07 +00001231 switch (method->getAvailability()) {
1232 case AR_Available:
1233 case AR_Deprecated:
1234 break;
1235
1236 // Don't warn about unavailable or not-yet-introduced methods.
1237 case AR_NotYetIntroduced:
1238 case AR_Unavailable:
Fariborz Jahanian9fc39c42011-06-24 20:31:37 +00001239 return;
Douglas Gregorc2e3d5c2012-12-11 18:53:07 +00001240 }
1241
Ted Kremenek65d63572013-03-27 00:02:21 +00001242 // FIXME: For now ignore 'IncompleteImpl'.
1243 // Previously we grouped all unimplemented methods under a single
1244 // warning, but some users strongly voiced that they would prefer
1245 // separate warnings. We will give that approach a try, as that
1246 // matches what we do with protocols.
1247
1248 Diag(ImpLoc, DiagID) << method->getDeclName();
1249
1250 // Issue a note to the original declaration.
1251 SourceLocation MethodLoc = method->getLocStart();
1252 if (MethodLoc.isValid())
1253 Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff15833ed2008-02-10 21:38:56 +00001254}
1255
David Chisnallb62d15c2010-10-25 17:23:52 +00001256/// Determines if type B can be substituted for type A. Returns true if we can
1257/// guarantee that anything that the user will do to an object of type A can
1258/// also be done to an object of type B. This is trivially true if the two
1259/// types are the same, or if B is a subclass of A. It becomes more complex
1260/// in cases where protocols are involved.
1261///
1262/// Object types in Objective-C describe the minimum requirements for an
1263/// object, rather than providing a complete description of a type. For
1264/// example, if A is a subclass of B, then B* may refer to an instance of A.
1265/// The principle of substitutability means that we may use an instance of A
1266/// anywhere that we may use an instance of B - it will implement all of the
1267/// ivars of B and all of the methods of B.
1268///
1269/// This substitutability is important when type checking methods, because
1270/// the implementation may have stricter type definitions than the interface.
1271/// The interface specifies minimum requirements, but the implementation may
1272/// have more accurate ones. For example, a method may privately accept
1273/// instances of B, but only publish that it accepts instances of A. Any
1274/// object passed to it will be type checked against B, and so will implicitly
1275/// by a valid A*. Similarly, a method may return a subclass of the class that
1276/// it is declared as returning.
1277///
1278/// This is most important when considering subclassing. A method in a
1279/// subclass must accept any object as an argument that its superclass's
1280/// implementation accepts. It may, however, accept a more general type
1281/// without breaking substitutability (i.e. you can still use the subclass
1282/// anywhere that you can use the superclass, but not vice versa). The
1283/// converse requirement applies to return types: the return type for a
1284/// subclass method must be a valid object of the kind that the superclass
1285/// advertises, but it may be specified more accurately. This avoids the need
1286/// for explicit down-casting by callers.
1287///
1288/// Note: This is a stricter requirement than for assignment.
John McCall071df462010-10-28 02:34:38 +00001289static bool isObjCTypeSubstitutable(ASTContext &Context,
1290 const ObjCObjectPointerType *A,
1291 const ObjCObjectPointerType *B,
1292 bool rejectId) {
1293 // Reject a protocol-unqualified id.
1294 if (rejectId && B->isObjCIdType()) return false;
David Chisnallb62d15c2010-10-25 17:23:52 +00001295
1296 // If B is a qualified id, then A must also be a qualified id and it must
1297 // implement all of the protocols in B. It may not be a qualified class.
1298 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1299 // stricter definition so it is not substitutable for id<A>.
1300 if (B->isObjCQualifiedIdType()) {
1301 return A->isObjCQualifiedIdType() &&
John McCall071df462010-10-28 02:34:38 +00001302 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1303 QualType(B,0),
1304 false);
David Chisnallb62d15c2010-10-25 17:23:52 +00001305 }
1306
1307 /*
1308 // id is a special type that bypasses type checking completely. We want a
1309 // warning when it is used in one place but not another.
1310 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1311
1312
1313 // If B is a qualified id, then A must also be a qualified id (which it isn't
1314 // if we've got this far)
1315 if (B->isObjCQualifiedIdType()) return false;
1316 */
1317
1318 // Now we know that A and B are (potentially-qualified) class types. The
1319 // normal rules for assignment apply.
John McCall071df462010-10-28 02:34:38 +00001320 return Context.canAssignObjCInterfaces(A, B);
David Chisnallb62d15c2010-10-25 17:23:52 +00001321}
1322
John McCall071df462010-10-28 02:34:38 +00001323static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1324 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1325}
1326
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001327static bool CheckMethodOverrideReturn(Sema &S,
John McCall071df462010-10-28 02:34:38 +00001328 ObjCMethodDecl *MethodImpl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001329 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001330 bool IsProtocolMethodDecl,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001331 bool IsOverridingMode,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001332 bool Warn) {
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001333 if (IsProtocolMethodDecl &&
1334 (MethodDecl->getObjCDeclQualifier() !=
1335 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001336 if (Warn) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001337 S.Diag(MethodImpl->getLocation(),
1338 (IsOverridingMode ?
1339 diag::warn_conflicting_overriding_ret_type_modifiers
1340 : diag::warn_conflicting_ret_type_modifiers))
1341 << MethodImpl->getDeclName()
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001342 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1343 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1344 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1345 }
1346 else
1347 return false;
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001348 }
1349
John McCall071df462010-10-28 02:34:38 +00001350 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001351 MethodDecl->getResultType()))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001352 return true;
1353 if (!Warn)
1354 return false;
John McCall071df462010-10-28 02:34:38 +00001355
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001356 unsigned DiagID =
1357 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1358 : diag::warn_conflicting_ret_types;
John McCall071df462010-10-28 02:34:38 +00001359
1360 // Mismatches between ObjC pointers go into a different warning
1361 // category, and sometimes they're even completely whitelisted.
1362 if (const ObjCObjectPointerType *ImplPtrTy =
1363 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1364 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001365 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall071df462010-10-28 02:34:38 +00001366 // Allow non-matching return types as long as they don't violate
1367 // the principle of substitutability. Specifically, we permit
1368 // return types that are subclasses of the declared return type,
1369 // or that are more-qualified versions of the declared type.
1370 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001371 return false;
John McCall071df462010-10-28 02:34:38 +00001372
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001373 DiagID =
1374 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1375 : diag::warn_non_covariant_ret_types;
John McCall071df462010-10-28 02:34:38 +00001376 }
1377 }
1378
1379 S.Diag(MethodImpl->getLocation(), DiagID)
1380 << MethodImpl->getDeclName()
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001381 << MethodDecl->getResultType()
John McCall071df462010-10-28 02:34:38 +00001382 << MethodImpl->getResultType()
1383 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001384 S.Diag(MethodDecl->getLocation(),
1385 IsOverridingMode ? diag::note_previous_declaration
1386 : diag::note_previous_definition)
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001387 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001388 return false;
John McCall071df462010-10-28 02:34:38 +00001389}
1390
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001391static bool CheckMethodOverrideParam(Sema &S,
John McCall071df462010-10-28 02:34:38 +00001392 ObjCMethodDecl *MethodImpl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001393 ObjCMethodDecl *MethodDecl,
John McCall071df462010-10-28 02:34:38 +00001394 ParmVarDecl *ImplVar,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001395 ParmVarDecl *IfaceVar,
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001396 bool IsProtocolMethodDecl,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001397 bool IsOverridingMode,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001398 bool Warn) {
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001399 if (IsProtocolMethodDecl &&
1400 (ImplVar->getObjCDeclQualifier() !=
1401 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001402 if (Warn) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001403 if (IsOverridingMode)
1404 S.Diag(ImplVar->getLocation(),
1405 diag::warn_conflicting_overriding_param_modifiers)
1406 << getTypeRange(ImplVar->getTypeSourceInfo())
1407 << MethodImpl->getDeclName();
1408 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001409 diag::warn_conflicting_param_modifiers)
1410 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001411 << MethodImpl->getDeclName();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001412 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1413 << getTypeRange(IfaceVar->getTypeSourceInfo());
1414 }
1415 else
1416 return false;
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001417 }
1418
John McCall071df462010-10-28 02:34:38 +00001419 QualType ImplTy = ImplVar->getType();
1420 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001421
John McCall071df462010-10-28 02:34:38 +00001422 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001423 return true;
1424
1425 if (!Warn)
1426 return false;
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001427 unsigned DiagID =
1428 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1429 : diag::warn_conflicting_param_types;
John McCall071df462010-10-28 02:34:38 +00001430
1431 // Mismatches between ObjC pointers go into a different warning
1432 // category, and sometimes they're even completely whitelisted.
1433 if (const ObjCObjectPointerType *ImplPtrTy =
1434 ImplTy->getAs<ObjCObjectPointerType>()) {
1435 if (const ObjCObjectPointerType *IfacePtrTy =
1436 IfaceTy->getAs<ObjCObjectPointerType>()) {
1437 // Allow non-matching argument types as long as they don't
1438 // violate the principle of substitutability. Specifically, the
1439 // implementation must accept any objects that the superclass
1440 // accepts, however it may also accept others.
1441 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001442 return false;
John McCall071df462010-10-28 02:34:38 +00001443
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001444 DiagID =
1445 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1446 : diag::warn_non_contravariant_param_types;
John McCall071df462010-10-28 02:34:38 +00001447 }
1448 }
1449
1450 S.Diag(ImplVar->getLocation(), DiagID)
1451 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001452 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1453 S.Diag(IfaceVar->getLocation(),
1454 (IsOverridingMode ? diag::note_previous_declaration
1455 : diag::note_previous_definition))
John McCall071df462010-10-28 02:34:38 +00001456 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001457 return false;
John McCall071df462010-10-28 02:34:38 +00001458}
John McCall31168b02011-06-15 23:02:42 +00001459
1460/// In ARC, check whether the conventional meanings of the two methods
1461/// match. If they don't, it's a hard error.
1462static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1463 ObjCMethodDecl *decl) {
1464 ObjCMethodFamily implFamily = impl->getMethodFamily();
1465 ObjCMethodFamily declFamily = decl->getMethodFamily();
1466 if (implFamily == declFamily) return false;
1467
1468 // Since conventions are sorted by selector, the only possibility is
1469 // that the types differ enough to cause one selector or the other
1470 // to fall out of the family.
1471 assert(implFamily == OMF_None || declFamily == OMF_None);
1472
1473 // No further diagnostics required on invalid declarations.
1474 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1475
1476 const ObjCMethodDecl *unmatched = impl;
1477 ObjCMethodFamily family = declFamily;
1478 unsigned errorID = diag::err_arc_lost_method_convention;
1479 unsigned noteID = diag::note_arc_lost_method_convention;
1480 if (declFamily == OMF_None) {
1481 unmatched = decl;
1482 family = implFamily;
1483 errorID = diag::err_arc_gained_method_convention;
1484 noteID = diag::note_arc_gained_method_convention;
1485 }
1486
1487 // Indexes into a %select clause in the diagnostic.
1488 enum FamilySelector {
1489 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1490 };
1491 FamilySelector familySelector = FamilySelector();
1492
1493 switch (family) {
1494 case OMF_None: llvm_unreachable("logic error, no method convention");
1495 case OMF_retain:
1496 case OMF_release:
1497 case OMF_autorelease:
1498 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00001499 case OMF_finalize:
John McCall31168b02011-06-15 23:02:42 +00001500 case OMF_retainCount:
1501 case OMF_self:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001502 case OMF_performSelector:
John McCall31168b02011-06-15 23:02:42 +00001503 // Mismatches for these methods don't change ownership
1504 // conventions, so we don't care.
1505 return false;
1506
1507 case OMF_init: familySelector = F_init; break;
1508 case OMF_alloc: familySelector = F_alloc; break;
1509 case OMF_copy: familySelector = F_copy; break;
1510 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1511 case OMF_new: familySelector = F_new; break;
1512 }
1513
1514 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1515 ReasonSelector reasonSelector;
1516
1517 // The only reason these methods don't fall within their families is
1518 // due to unusual result types.
1519 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1520 reasonSelector = R_UnrelatedReturn;
1521 } else {
1522 reasonSelector = R_NonObjectReturn;
1523 }
1524
Joerg Sonnenbergerffc6d492013-06-26 21:31:47 +00001525 S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
1526 S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
John McCall31168b02011-06-15 23:02:42 +00001527
1528 return true;
1529}
John McCall071df462010-10-28 02:34:38 +00001530
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +00001531void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001532 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001533 bool IsProtocolMethodDecl) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001534 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00001535 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1536 return;
1537
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001538 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001539 IsProtocolMethodDecl, false,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001540 true);
Mike Stump11289f42009-09-09 15:08:12 +00001541
Chris Lattner67f35b02009-04-11 19:58:42 +00001542 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001543 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1544 EF = MethodDecl->param_end();
1545 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001546 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001547 IsProtocolMethodDecl, false, true);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001548 }
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001549
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001550 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001551 Diag(ImpMethodDecl->getLocation(),
1552 diag::warn_conflicting_variadic);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001553 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001554 }
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001555}
1556
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001557void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1558 ObjCMethodDecl *Overridden,
1559 bool IsProtocolMethodDecl) {
1560
1561 CheckMethodOverrideReturn(*this, Method, Overridden,
1562 IsProtocolMethodDecl, true,
1563 true);
1564
1565 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001566 IF = Overridden->param_begin(), EM = Method->param_end(),
1567 EF = Overridden->param_end();
1568 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001569 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1570 IsProtocolMethodDecl, true, true);
1571 }
1572
1573 if (Method->isVariadic() != Overridden->isVariadic()) {
1574 Diag(Method->getLocation(),
1575 diag::warn_conflicting_overriding_variadic);
1576 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1577 }
1578}
1579
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001580/// WarnExactTypedMethods - This routine issues a warning if method
1581/// implementation declaration matches exactly that of its declaration.
1582void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1583 ObjCMethodDecl *MethodDecl,
1584 bool IsProtocolMethodDecl) {
1585 // don't issue warning when protocol method is optional because primary
1586 // class is not required to implement it and it is safe for protocol
1587 // to implement it.
1588 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1589 return;
1590 // don't issue warning when primary class's method is
1591 // depecated/unavailable.
1592 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1593 MethodDecl->hasAttr<DeprecatedAttr>())
1594 return;
1595
1596 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1597 IsProtocolMethodDecl, false, false);
1598 if (match)
1599 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001600 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1601 EF = MethodDecl->param_end();
1602 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001603 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1604 *IM, *IF,
1605 IsProtocolMethodDecl, false, false);
1606 if (!match)
1607 break;
1608 }
1609 if (match)
1610 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall92918512011-08-08 17:32:19 +00001611 if (match)
1612 match = !(MethodDecl->isClassMethod() &&
1613 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001614
1615 if (match) {
1616 Diag(ImpMethodDecl->getLocation(),
1617 diag::warn_category_method_impl_match);
Ted Kremenek59b10db2012-02-27 22:55:11 +00001618 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1619 << MethodDecl->getDeclName();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001620 }
1621}
1622
Mike Stump87c57ac2009-05-16 07:39:55 +00001623/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1624/// improve the efficiency of selector lookups and type checking by associating
1625/// with each protocol / interface / category the flattened instance tables. If
1626/// we used an immutable set to keep the table then it wouldn't add significant
1627/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001628
Steve Naroffa36992242008-02-08 22:06:17 +00001629/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +00001630/// Declared in protocol, and those referenced by it.
Steve Naroffa36992242008-02-08 22:06:17 +00001631void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1632 ObjCProtocolDecl *PDecl,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001633 bool& IncompleteImpl,
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001634 const SelectorSet &InsMap,
1635 const SelectorSet &ClsMap,
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +00001636 ObjCContainerDecl *CDecl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001637 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1638 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1639 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +00001640 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1641
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001642 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001643 ObjCInterfaceDecl *NSIDecl = 0;
John McCall5fb5df92012-06-20 06:18:46 +00001644 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump11289f42009-09-09 15:08:12 +00001645 // check to see if class implements forwardInvocation method and objects
1646 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001647 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +00001648 // Under such conditions, which means that every method possible is
1649 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001650 // found" warnings.
1651 // FIXME: Use a general GetUnarySelector method for this.
1652 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1653 Selector fISelector = Context.Selectors.getSelector(1, &II);
1654 if (InsMap.count(fISelector))
1655 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1656 // need be implemented in the implementation.
1657 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1658 }
Mike Stump11289f42009-09-09 15:08:12 +00001659
Fariborz Jahanianc41cf052013-01-07 19:21:03 +00001660 // If this is a forward protocol declaration, get its definition.
1661 if (!PDecl->isThisDeclarationADefinition() &&
1662 PDecl->getDefinition())
1663 PDecl = PDecl->getDefinition();
1664
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001665 // If a method lookup fails locally we still need to look and see if
1666 // the method was implemented by a base class or an inherited
1667 // protocol. This lookup is slow, but occurs rarely in correct code
1668 // and otherwise would terminate in a warning.
1669
Chris Lattnerda463fe2007-12-12 07:09:47 +00001670 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001671 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +00001672 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001673 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001674 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001675 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rosed01e83a2012-10-10 16:42:25 +00001676 !method->isPropertyAccessor() &&
1677 !InsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001678 (!Super || !Super->lookupMethod(method->getSelector(),
1679 true /* instance */,
1680 false /* shallowCategory */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001681 true /* followsSuper */,
1682 NULL /* category */,
1683 PDecl /* protocol */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001684 // If a method is not implemented in the category implementation but
1685 // has been declared in its primary class, superclass,
1686 // or in one of their protocols, no need to issue the warning.
1687 // This is because method will be implemented in the primary class
1688 // or one of its super class implementation.
1689
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001690 // Ugly, but necessary. Method declared in protcol might have
1691 // have been synthesized due to a property declared in the class which
1692 // uses the protocol.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001693 if (ObjCMethodDecl *MethodInClass =
Ted Kremenek00781502013-11-23 01:01:29 +00001694 IDecl->lookupMethod(method->getSelector(),
1695 true /* instance */,
1696 true /* shallowCategoryLookup */,
1697 false /* followSuper */))
Jordan Rosed01e83a2012-10-10 16:42:25 +00001698 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001699 continue;
1700 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1701 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1702 != DiagnosticsEngine::Ignored) {
1703 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001704 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1705 << PDecl->getDeclName();
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001706 }
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001707 }
1708 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001709 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +00001710 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001711 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001712 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001713 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001714 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1715 !ClsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001716 (!Super || !Super->lookupMethod(method->getSelector(),
1717 false /* class method */,
1718 false /* shallowCategoryLookup */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001719 true /* followSuper */,
1720 NULL /* category */,
1721 PDecl /* protocol */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001722 // See above comment for instance method lookups.
Ted Kremenek00781502013-11-23 01:01:29 +00001723 if (C && IDecl->lookupMethod(method->getSelector(),
1724 false /* class */,
1725 true /* shallowCategoryLookup */,
1726 false /* followSuper */))
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001727 continue;
Ted Kremenek00781502013-11-23 01:01:29 +00001728
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001729 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikie9c902b52011-09-25 23:23:43 +00001730 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1731 DiagnosticsEngine::Ignored) {
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001732 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
1733 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1734 PDecl->getDeclName();
1735 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001736 }
Steve Naroff3ce37a62007-12-14 23:37:57 +00001737 }
Chris Lattner390d39a2008-07-21 21:32:27 +00001738 // Check on this protocols's referenced protocols, recursively.
1739 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1740 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001741 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001742}
1743
Fariborz Jahanianf9ae68a2011-07-16 00:08:33 +00001744/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001745/// or protocol against those declared in their implementations.
1746///
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001747void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1748 const SelectorSet &ClsMap,
1749 SelectorSet &InsMapSeen,
1750 SelectorSet &ClsMapSeen,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001751 ObjCImplDecl* IMPDecl,
1752 ObjCContainerDecl* CDecl,
1753 bool &IncompleteImpl,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001754 bool ImmediateClass,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001755 bool WarnCategoryMethodImpl) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001756 // Check and see if instance methods in class interface have been
1757 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001758 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1759 E = CDecl->instmeth_end(); I != E; ++I) {
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001760 if (!InsMapSeen.insert((*I)->getSelector()))
1761 continue;
Jordan Rosed01e83a2012-10-10 16:42:25 +00001762 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001763 !InsMap.count((*I)->getSelector())) {
1764 if (ImmediateClass)
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001765 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001766 diag::warn_undef_method_impl);
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001767 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +00001768 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001769 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001770 IMPDecl->getInstanceMethod((*I)->getSelector());
1771 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1772 "Expected to find the method through lookup as well");
1773 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001774 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001775 if (ImpMethodDecl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001776 if (!WarnCategoryMethodImpl)
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001777 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1778 isa<ObjCProtocolDecl>(CDecl));
Jordan Rosed01e83a2012-10-10 16:42:25 +00001779 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001780 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001781 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001782 }
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001783 }
1784 }
Mike Stump11289f42009-09-09 15:08:12 +00001785
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001786 // Check and see if class methods in class interface have been
1787 // implemented in the implementation class. If so, their types match.
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001788 for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
1789 E = CDecl->classmeth_end();
1790 I != E; ++I) {
1791 if (!ClsMapSeen.insert((*I)->getSelector()))
1792 continue;
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001793 if (!ClsMap.count((*I)->getSelector())) {
1794 if (ImmediateClass)
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001795 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001796 diag::warn_undef_method_impl);
Mike Stump12b8ce12009-08-04 21:02:39 +00001797 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001798 ObjCMethodDecl *ImpMethodDecl =
1799 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001800 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1801 "Expected to find the method through lookup as well");
1802 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001803 if (!WarnCategoryMethodImpl)
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001804 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1805 isa<ObjCProtocolDecl>(CDecl));
1806 else
1807 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001808 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001809 }
1810 }
Fariborz Jahanian73853e52010-10-08 22:59:25 +00001811
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001812 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) {
1813 // Also, check for methods declared in protocols inherited by
1814 // this protocol.
1815 for (ObjCProtocolDecl::protocol_iterator
1816 PI = PD->protocol_begin(), E = PD->protocol_end(); PI != E; ++PI)
1817 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1818 IMPDecl, (*PI), IncompleteImpl, false,
1819 WarnCategoryMethodImpl);
1820 }
1821
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001822 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001823 // when checking that methods in implementation match their declaration,
1824 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1825 // extension; as well as those in categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001826 if (!WarnCategoryMethodImpl) {
1827 for (ObjCInterfaceDecl::visible_categories_iterator
1828 Cat = I->visible_categories_begin(),
1829 CatEnd = I->visible_categories_end();
1830 Cat != CatEnd; ++Cat) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001831 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001832 IMPDecl, *Cat, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001833 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001834 }
1835 } else {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001836 // Also methods in class extensions need be looked at next.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001837 for (ObjCInterfaceDecl::visible_extensions_iterator
1838 Ext = I->visible_extensions_begin(),
1839 ExtEnd = I->visible_extensions_end();
1840 Ext != ExtEnd; ++Ext) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001841 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001842 IMPDecl, *Ext, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001843 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001844 }
1845 }
1846
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001847 // Check for any implementation of a methods declared in protocol.
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001848 for (ObjCInterfaceDecl::all_protocol_iterator
1849 PI = I->all_referenced_protocol_begin(),
1850 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001851 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1852 IMPDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001853 (*PI), IncompleteImpl, false,
1854 WarnCategoryMethodImpl);
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001855
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001856 // FIXME. For now, we are not checking for extact match of methods
1857 // in category implementation and its primary class's super class.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001858 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001859 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +00001860 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001861 I->getSuperClass(), IncompleteImpl, false);
1862 }
1863}
1864
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001865/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1866/// category matches with those implemented in its primary class and
1867/// warns each time an exact match is found.
1868void Sema::CheckCategoryVsClassMethodMatches(
1869 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001870 SelectorSet InsMap, ClsMap;
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001871
1872 for (ObjCImplementationDecl::instmeth_iterator
1873 I = CatIMPDecl->instmeth_begin(),
1874 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1875 InsMap.insert((*I)->getSelector());
1876
1877 for (ObjCImplementationDecl::classmeth_iterator
1878 I = CatIMPDecl->classmeth_begin(),
1879 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1880 ClsMap.insert((*I)->getSelector());
1881 if (InsMap.empty() && ClsMap.empty())
1882 return;
1883
1884 // Get category's primary class.
1885 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1886 if (!CatDecl)
1887 return;
1888 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1889 if (!IDecl)
1890 return;
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001891 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001892 bool IncompleteImpl = false;
1893 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1894 CatIMPDecl, IDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001895 IncompleteImpl, false,
1896 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001897}
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001898
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001899void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump11289f42009-09-09 15:08:12 +00001900 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001901 bool IncompleteImpl) {
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001902 SelectorSet InsMap;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001903 // Check and see if instance methods in class interface have been
1904 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001905 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001906 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001907 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001908
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001909 // Check and see if properties declared in the interface have either 1)
1910 // an implementation or 2) there is a @synthesize/@dynamic implementation
1911 // of the property in the @implementation.
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001912 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall5fb5df92012-06-20 06:18:46 +00001913 if (!(LangOpts.ObjCDefaultSynthProperties &&
1914 LangOpts.ObjCRuntime.isNonFragile()) ||
1915 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001916 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001917
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001918 SelectorSet ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +00001919 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001920 I = IMPDecl->classmeth_begin(),
1921 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001922 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001923
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001924 // Check for type conflict of methods declared in a class/protocol and
1925 // its implementation; if any.
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001926 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +00001927 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1928 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001929 IncompleteImpl, true);
Fariborz Jahanian2bda1b62011-08-03 18:21:12 +00001930
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001931 // check all methods implemented in category against those declared
1932 // in its primary class.
1933 if (ObjCCategoryImplDecl *CatDecl =
1934 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1935 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001936
Chris Lattnerda463fe2007-12-12 07:09:47 +00001937 // Check the protocol list for unimplemented methods in the @implementation
1938 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001939 // Check and see if class methods in class interface have been
1940 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001941
Chris Lattner9ef10f42009-03-01 00:56:52 +00001942 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001943 for (ObjCInterfaceDecl::all_protocol_iterator
1944 PI = I->all_referenced_protocol_begin(),
1945 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001946 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001947 InsMap, ClsMap, I);
1948 // Check class extensions (unnamed categories)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001949 for (ObjCInterfaceDecl::visible_extensions_iterator
1950 Ext = I->visible_extensions_begin(),
1951 ExtEnd = I->visible_extensions_end();
1952 Ext != ExtEnd; ++Ext) {
1953 ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
1954 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001955 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001956 // For extended class, unimplemented methods in its protocols will
1957 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001958 if (!C->IsClassExtension()) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001959 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1960 E = C->protocol_end(); PI != E; ++PI)
1961 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +00001962 InsMap, ClsMap, CDecl);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001963 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00001964 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001965 } else
David Blaikie83d382b2011-09-23 05:06:16 +00001966 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00001967}
1968
Mike Stump11289f42009-09-09 15:08:12 +00001969/// ActOnForwardClassDeclaration -
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00001970Sema::DeclGroupPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00001971Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00001972 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00001973 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00001974 unsigned NumElts) {
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00001975 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001976 for (unsigned i = 0; i != NumElts; ++i) {
1977 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001978 NamedDecl *PrevDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001979 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001980 LookupOrdinaryName, ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001981 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00001982 // GCC apparently allows the following idiom:
1983 //
1984 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1985 // @class XCElementToggler;
1986 //
Fariborz Jahanian04c44552012-01-24 00:40:15 +00001987 // Here we have chosen to ignore the forward class declaration
1988 // with a warning. Since this is the implied behavior.
Richard Smithdda56e42011-04-15 14:24:37 +00001989 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCall8b07ec22010-05-15 11:32:37 +00001990 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001991 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00001992 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCall8b07ec22010-05-15 11:32:37 +00001993 } else {
Mike Stump12b8ce12009-08-04 21:02:39 +00001994 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahanian04c44552012-01-24 00:40:15 +00001995 // to the underlying class. Just ignore the forward class with a warning
1996 // as this will force the intended behavior which is to lookup the typedef
1997 // name.
1998 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1999 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
2000 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
2001 continue;
2002 }
Fariborz Jahanian0d451812009-05-07 21:49:26 +00002003 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002004 }
Douglas Gregordc9166c2011-12-15 20:29:51 +00002005
2006 // Create a declaration to describe this forward declaration.
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00002007 ObjCInterfaceDecl *PrevIDecl
2008 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002009
2010 IdentifierInfo *ClassName = IdentList[i];
2011 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
2012 // A previous decl with a different name is because of
2013 // @compatibility_alias, for example:
2014 // \code
2015 // @class NewImage;
2016 // @compatibility_alias OldImage NewImage;
2017 // \endcode
2018 // A lookup for 'OldImage' will return the 'NewImage' decl.
2019 //
2020 // In such a case use the real declaration name, instead of the alias one,
2021 // otherwise we will break IdentifierResolver and redecls-chain invariants.
2022 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
2023 // has been aliased.
2024 ClassName = PrevIDecl->getIdentifier();
2025 }
2026
Douglas Gregordc9166c2011-12-15 20:29:51 +00002027 ObjCInterfaceDecl *IDecl
2028 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002029 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002030 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002031
Douglas Gregordc9166c2011-12-15 20:29:51 +00002032 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeafd0b2011-12-27 22:43:10 +00002033 CheckObjCDeclScope(IDecl);
2034 DeclsInGroup.push_back(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002035 }
Rafael Espindolaab417692013-07-09 12:05:01 +00002036
2037 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002038}
2039
John McCall54507ab2011-06-16 01:15:19 +00002040static bool tryMatchRecordTypes(ASTContext &Context,
2041 Sema::MethodMatchStrategy strategy,
2042 const Type *left, const Type *right);
2043
John McCall31168b02011-06-15 23:02:42 +00002044static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
2045 QualType leftQT, QualType rightQT) {
2046 const Type *left =
2047 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2048 const Type *right =
2049 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2050
2051 if (left == right) return true;
2052
2053 // If we're doing a strict match, the types have to match exactly.
2054 if (strategy == Sema::MMS_strict) return false;
2055
2056 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2057
2058 // Otherwise, use this absurdly complicated algorithm to try to
2059 // validate the basic, low-level compatibility of the two types.
2060
2061 // As a minimum, require the sizes and alignments to match.
2062 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
2063 return false;
2064
2065 // Consider all the kinds of non-dependent canonical types:
2066 // - functions and arrays aren't possible as return and parameter types
2067
2068 // - vector types of equal size can be arbitrarily mixed
2069 if (isa<VectorType>(left)) return isa<VectorType>(right);
2070 if (isa<VectorType>(right)) return false;
2071
2072 // - references should only match references of identical type
John McCall54507ab2011-06-16 01:15:19 +00002073 // - structs, unions, and Objective-C objects must match more-or-less
2074 // exactly
John McCall31168b02011-06-15 23:02:42 +00002075 // - everything else should be a scalar
2076 if (!left->isScalarType() || !right->isScalarType())
John McCall54507ab2011-06-16 01:15:19 +00002077 return tryMatchRecordTypes(Context, strategy, left, right);
John McCall31168b02011-06-15 23:02:42 +00002078
John McCall9320b872011-09-09 05:25:32 +00002079 // Make scalars agree in kind, except count bools as chars, and group
2080 // all non-member pointers together.
John McCall31168b02011-06-15 23:02:42 +00002081 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2082 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2083 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2084 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall9320b872011-09-09 05:25:32 +00002085 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2086 leftSK = Type::STK_ObjCObjectPointer;
2087 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2088 rightSK = Type::STK_ObjCObjectPointer;
John McCall31168b02011-06-15 23:02:42 +00002089
2090 // Note that data member pointers and function member pointers don't
2091 // intermix because of the size differences.
2092
2093 return (leftSK == rightSK);
2094}
Chris Lattnerda463fe2007-12-12 07:09:47 +00002095
John McCall54507ab2011-06-16 01:15:19 +00002096static bool tryMatchRecordTypes(ASTContext &Context,
2097 Sema::MethodMatchStrategy strategy,
2098 const Type *lt, const Type *rt) {
2099 assert(lt && rt && lt != rt);
2100
2101 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2102 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2103 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2104
2105 // Require union-hood to match.
2106 if (left->isUnion() != right->isUnion()) return false;
2107
2108 // Require an exact match if either is non-POD.
2109 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2110 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2111 return false;
2112
2113 // Require size and alignment to match.
2114 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2115
2116 // Require fields to match.
2117 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2118 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2119 for (; li != le && ri != re; ++li, ++ri) {
2120 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2121 return false;
2122 }
2123 return (li == le && ri == re);
2124}
2125
Chris Lattnerda463fe2007-12-12 07:09:47 +00002126/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2127/// returns true, or false, accordingly.
2128/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCall31168b02011-06-15 23:02:42 +00002129bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2130 const ObjCMethodDecl *right,
2131 MethodMatchStrategy strategy) {
2132 if (!matchTypes(Context, strategy,
2133 left->getResultType(), right->getResultType()))
2134 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002135
Douglas Gregor560b7fa2013-02-07 19:13:24 +00002136 // If either is hidden, it is not considered to match.
2137 if (left->isHidden() || right->isHidden())
2138 return false;
2139
David Blaikiebbafb8a2012-03-11 07:00:24 +00002140 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002141 (left->hasAttr<NSReturnsRetainedAttr>()
2142 != right->hasAttr<NSReturnsRetainedAttr>() ||
2143 left->hasAttr<NSConsumesSelfAttr>()
2144 != right->hasAttr<NSConsumesSelfAttr>()))
2145 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002146
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002147 ObjCMethodDecl::param_const_iterator
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002148 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2149 re = right->param_end();
Mike Stump11289f42009-09-09 15:08:12 +00002150
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002151 for (; li != le && ri != re; ++li, ++ri) {
John McCall31168b02011-06-15 23:02:42 +00002152 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002153 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCall31168b02011-06-15 23:02:42 +00002154
2155 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2156 return false;
2157
David Blaikiebbafb8a2012-03-11 07:00:24 +00002158 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002159 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2160 return false;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002161 }
2162 return true;
2163}
2164
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002165void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002166 // Record at the head of the list whether there were 0, 1, or >= 2 methods
2167 // inside categories.
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00002168 if (ObjCCategoryDecl *
2169 CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
2170 if (!CD->IsClassExtension() && List->getBits() < 2)
2171 List->setBits(List->getBits()+1);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002172
Douglas Gregorc454afe2012-01-25 00:19:56 +00002173 // If the list is empty, make it a singleton list.
2174 if (List->Method == 0) {
2175 List->Method = Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002176 List->setNext(0);
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002177 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002178 }
2179
2180 // We've seen a method with this name, see if we have already seen this type
2181 // signature.
2182 ObjCMethodList *Previous = List;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002183 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregor600a2f52013-06-21 00:20:25 +00002184 // If we are building a module, keep all of the methods.
2185 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2186 continue;
2187
Douglas Gregore1716012012-01-25 00:49:42 +00002188 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregorc454afe2012-01-25 00:19:56 +00002189 continue;
2190
2191 ObjCMethodDecl *PrevObjCMethod = List->Method;
2192
2193 // Propagate the 'defined' bit.
2194 if (Method->isDefined())
2195 PrevObjCMethod->setDefined(true);
2196
2197 // If a method is deprecated, push it in the global pool.
2198 // This is used for better diagnostics.
2199 if (Method->isDeprecated()) {
2200 if (!PrevObjCMethod->isDeprecated())
2201 List->Method = Method;
2202 }
2203 // If new method is unavailable, push it into global pool
2204 // unless previous one is deprecated.
2205 if (Method->isUnavailable()) {
2206 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2207 List->Method = Method;
2208 }
2209
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002210 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002211 }
2212
2213 // We have a new signature for an existing method - add it.
2214 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregore1716012012-01-25 00:49:42 +00002215 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002216 Previous->setNext(new (Mem) ObjCMethodList(Method, 0));
Douglas Gregorc454afe2012-01-25 00:19:56 +00002217}
2218
Sebastian Redl75d8a322010-08-02 23:18:59 +00002219/// \brief Read the contents of the method pool for a given selector from
2220/// external storage.
Douglas Gregore1716012012-01-25 00:49:42 +00002221void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002222 assert(ExternalSource && "We need an external AST source");
Douglas Gregore1716012012-01-25 00:49:42 +00002223 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002224}
2225
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002226void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002227 bool instance) {
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002228 // Ignore methods of invalid containers.
2229 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002230 return;
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002231
Douglas Gregor70f449b2012-01-25 00:59:09 +00002232 if (ExternalSource)
2233 ReadMethodPool(Method->getSelector());
2234
Sebastian Redl75d8a322010-08-02 23:18:59 +00002235 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor70f449b2012-01-25 00:59:09 +00002236 if (Pos == MethodPool.end())
2237 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2238 GlobalMethods())).first;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002239
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002240 Method->setDefined(impl);
Douglas Gregorc454afe2012-01-25 00:19:56 +00002241
Sebastian Redl75d8a322010-08-02 23:18:59 +00002242 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002243 addMethodToGlobalList(&Entry, Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002244}
2245
John McCall31168b02011-06-15 23:02:42 +00002246/// Determines if this is an "acceptable" loose mismatch in the global
2247/// method pool. This exists mostly as a hack to get around certain
2248/// global mismatches which we can't afford to make warnings / errors.
2249/// Really, what we want is a way to take a method out of the global
2250/// method pool.
2251static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2252 ObjCMethodDecl *other) {
2253 if (!chosen->isInstanceMethod())
2254 return false;
2255
2256 Selector sel = chosen->getSelector();
2257 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2258 return false;
2259
2260 // Don't complain about mismatches for -length if the method we
2261 // chose has an integral result type.
2262 return (chosen->getResultType()->isIntegerType());
2263}
2264
Sebastian Redl75d8a322010-08-02 23:18:59 +00002265ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00002266 bool receiverIdOrClass,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002267 bool warn, bool instance) {
Douglas Gregor70f449b2012-01-25 00:59:09 +00002268 if (ExternalSource)
2269 ReadMethodPool(Sel);
2270
Sebastian Redl75d8a322010-08-02 23:18:59 +00002271 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor70f449b2012-01-25 00:59:09 +00002272 if (Pos == MethodPool.end())
2273 return 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002274
Douglas Gregor77f49a42013-01-16 18:47:38 +00002275 // Gather the non-hidden methods.
Sebastian Redl75d8a322010-08-02 23:18:59 +00002276 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Robert Wilhelmb869a8f2013-08-10 12:33:24 +00002277 SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002278 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Douglas Gregor77f49a42013-01-16 18:47:38 +00002279 if (M->Method && !M->Method->isHidden()) {
2280 // If we're not supposed to warn about mismatches, we're done.
2281 if (!warn)
2282 return M->Method;
Mike Stump11289f42009-09-09 15:08:12 +00002283
Douglas Gregor77f49a42013-01-16 18:47:38 +00002284 Methods.push_back(M->Method);
Sebastian Redl75d8a322010-08-02 23:18:59 +00002285 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00002286 }
Douglas Gregor77f49a42013-01-16 18:47:38 +00002287
2288 // If there aren't any visible methods, we're done.
2289 // FIXME: Recover if there are any known-but-hidden methods?
2290 if (Methods.empty())
2291 return 0;
2292
2293 if (Methods.size() == 1)
2294 return Methods[0];
2295
2296 // We found multiple methods, so we may have to complain.
2297 bool issueDiagnostic = false, issueError = false;
2298
2299 // We support a warning which complains about *any* difference in
2300 // method signature.
2301 bool strictSelectorMatch =
2302 (receiverIdOrClass && warn &&
2303 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2304 R.getBegin())
2305 != DiagnosticsEngine::Ignored));
2306 if (strictSelectorMatch) {
2307 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2308 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2309 issueDiagnostic = true;
2310 break;
2311 }
2312 }
2313 }
2314
2315 // If we didn't see any strict differences, we won't see any loose
2316 // differences. In ARC, however, we also need to check for loose
2317 // mismatches, because most of them are errors.
2318 if (!strictSelectorMatch ||
2319 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2320 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2321 // This checks if the methods differ in type mismatch.
2322 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2323 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2324 issueDiagnostic = true;
2325 if (getLangOpts().ObjCAutoRefCount)
2326 issueError = true;
2327 break;
2328 }
2329 }
2330
2331 if (issueDiagnostic) {
2332 if (issueError)
2333 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2334 else if (strictSelectorMatch)
2335 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2336 else
2337 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2338
2339 Diag(Methods[0]->getLocStart(),
2340 issueError ? diag::note_possibility : diag::note_using)
2341 << Methods[0]->getSourceRange();
2342 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2343 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2344 << Methods[I]->getSourceRange();
2345 }
2346 }
2347 return Methods[0];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002348}
2349
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002350ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redl75d8a322010-08-02 23:18:59 +00002351 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2352 if (Pos == MethodPool.end())
2353 return 0;
2354
2355 GlobalMethods &Methods = Pos->second;
2356
2357 if (Methods.first.Method && Methods.first.Method->isDefined())
2358 return Methods.first.Method;
2359 if (Methods.second.Method && Methods.second.Method->isDefined())
2360 return Methods.second.Method;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002361 return 0;
2362}
2363
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002364static void
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002365HelperSelectorsForTypoCorrection(
2366 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2367 StringRef Typo, const ObjCMethodDecl * Method) {
2368 const unsigned MaxEditDistance = 1;
2369 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieuea8d3702013-06-06 02:22:29 +00002370 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002371
2372 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2373 if (MinPossibleEditDistance > 0 &&
2374 Typo.size() / MinPossibleEditDistance < 1)
2375 return;
2376 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2377 if (EditDistance > MaxEditDistance)
2378 return;
2379 if (EditDistance == BestEditDistance)
2380 BestMethod.push_back(Method);
2381 else if (EditDistance < BestEditDistance) {
2382 BestMethod.clear();
2383 BestMethod.push_back(Method);
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002384 }
2385}
2386
Fariborz Jahanian75481672013-06-17 17:10:54 +00002387static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2388 QualType ObjectType) {
2389 if (ObjectType.isNull())
2390 return true;
2391 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2392 return true;
2393 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != 0;
2394}
2395
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002396const ObjCMethodDecl *
Fariborz Jahanian75481672013-06-17 17:10:54 +00002397Sema::SelectorsForTypoCorrection(Selector Sel,
2398 QualType ObjectType) {
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002399 unsigned NumArgs = Sel.getNumArgs();
2400 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002401 bool ObjectIsId = true, ObjectIsClass = true;
2402 if (ObjectType.isNull())
2403 ObjectIsId = ObjectIsClass = false;
2404 else if (!ObjectType->isObjCObjectPointerType())
2405 return 0;
2406 else if (const ObjCObjectPointerType *ObjCPtr =
2407 ObjectType->getAsObjCInterfacePointerType()) {
2408 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2409 ObjectIsId = ObjectIsClass = false;
2410 }
2411 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2412 ObjectIsClass = false;
2413 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2414 ObjectIsId = false;
2415 else
2416 return 0;
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002417
2418 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2419 e = MethodPool.end(); b != e; b++) {
2420 // instance methods
2421 for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
2422 if (M->Method &&
Fariborz Jahanian06499232013-06-18 17:10:58 +00002423 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2424 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002425 if (ObjectIsId)
2426 Methods.push_back(M->Method);
2427 else if (!ObjectIsClass &&
2428 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2429 Methods.push_back(M->Method);
2430 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002431 // class methods
2432 for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
2433 if (M->Method &&
Fariborz Jahanian06499232013-06-18 17:10:58 +00002434 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2435 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002436 if (ObjectIsClass)
2437 Methods.push_back(M->Method);
2438 else if (!ObjectIsId &&
2439 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2440 Methods.push_back(M->Method);
2441 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002442 }
2443
2444 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2445 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2446 HelperSelectorsForTypoCorrection(SelectedMethods,
2447 Sel.getAsString(), Methods[i]);
2448 }
2449 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : NULL;
2450}
2451
2452static void
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002453HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S,
2454 ObjCMethodList &MethList) {
2455 ObjCMethodList *M = &MethList;
2456 ObjCMethodDecl *TargetMethod = M->Method;
2457 while (TargetMethod &&
2458 isa<ObjCImplDecl>(TargetMethod->getDeclContext())) {
2459 M = M->getNext();
2460 TargetMethod = M ? M->Method : 0;
2461 }
2462 if (!TargetMethod)
2463 return;
2464 bool FirstTime = true;
2465 for (M = M->getNext(); M; M=M->getNext()) {
2466 ObjCMethodDecl *MatchingMethodDecl = M->Method;
2467 if (isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext()))
2468 continue;
2469 if (!S.MatchTwoMethodDeclarations(TargetMethod,
2470 MatchingMethodDecl, Sema::MMS_loose)) {
2471 if (FirstTime) {
2472 FirstTime = false;
2473 S.Diag(TargetMethod->getLocation(), diag::warning_multiple_selectors)
2474 << TargetMethod->getSelector();
2475 }
2476 S.Diag(MatchingMethodDecl->getLocation(), diag::note_also_found);
2477 }
2478 }
2479}
2480
2481void Sema::DiagnoseMismatchedMethodsInGlobalPool() {
2482 unsigned DIAG = diag::warning_multiple_selectors;
2483 if (Diags.getDiagnosticLevel(DIAG, SourceLocation())
2484 == DiagnosticsEngine::Ignored)
2485 return;
2486 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2487 e = MethodPool.end(); b != e; b++) {
2488 // first, instance methods
2489 ObjCMethodList &InstMethList = b->second.first;
2490 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, InstMethList);
Fariborz Jahanianc07e8932013-05-30 21:52:50 +00002491 // second, class methods
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002492 ObjCMethodList &ClsMethList = b->second.second;
2493 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, ClsMethList);
2494 }
2495}
2496
2497/// DiagnoseDuplicateIvars -
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002498/// Check for duplicate ivars in the entire class at the start of
James Dennett634962f2012-06-14 21:40:34 +00002499/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002500/// add ivars to a class in random order which will not be known until
James Dennett634962f2012-06-14 21:40:34 +00002501/// class's \@implementation is seen.
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002502void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2503 ObjCInterfaceDecl *SID) {
2504 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2505 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie40ed2972012-06-06 20:45:41 +00002506 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002507 if (Ivar->isInvalidDecl())
2508 continue;
2509 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2510 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2511 if (prevIvar) {
2512 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2513 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2514 Ivar->setInvalidDecl();
2515 }
2516 }
2517 }
2518}
2519
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002520Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2521 switch (CurContext->getDeclKind()) {
2522 case Decl::ObjCInterface:
2523 return Sema::OCK_Interface;
2524 case Decl::ObjCProtocol:
2525 return Sema::OCK_Protocol;
2526 case Decl::ObjCCategory:
2527 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2528 return Sema::OCK_ClassExtension;
2529 else
2530 return Sema::OCK_Category;
2531 case Decl::ObjCImplementation:
2532 return Sema::OCK_Implementation;
2533 case Decl::ObjCCategoryImpl:
2534 return Sema::OCK_CategoryImplementation;
2535
2536 default:
2537 return Sema::OCK_None;
2538 }
2539}
2540
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002541// Note: For class/category implementations, allMethods is always null.
Robert Wilhelm57c67112013-07-17 21:14:35 +00002542Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods,
Fariborz Jahaniandfb76872013-07-17 00:05:08 +00002543 ArrayRef<DeclGroupPtrTy> allTUVars) {
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002544 if (getObjCContainerKind() == Sema::OCK_None)
2545 return 0;
2546
2547 assert(AtEnd.isValid() && "Invalid location for '@end'");
2548
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002549 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2550 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002551
Mike Stump11289f42009-09-09 15:08:12 +00002552 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00002553 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2554 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002555 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00002556
Steve Naroff35c62ae2009-01-08 17:28:14 +00002557 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2558 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2559 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2560
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002561 for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002562 ObjCMethodDecl *Method =
John McCall48871652010-08-21 09:40:31 +00002563 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002564
2565 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00002566 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002567 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002568 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002569 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002570 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002571 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002572 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002573 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002574 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002575 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002576 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002577 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002578 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002579 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002580 if (!Context.getSourceManager().isInSystemHeader(
2581 Method->getLocation()))
2582 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2583 << Method->getDeclName();
2584 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2585 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002586 InsMap[Method->getSelector()] = Method;
2587 /// The following allows us to typecheck messages to "id".
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002588 AddInstanceMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002589 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002590 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002591 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002592 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002593 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002594 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002595 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002596 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002597 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002598 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002599 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002600 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002601 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002602 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002603 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002604 if (!Context.getSourceManager().isInSystemHeader(
2605 Method->getLocation()))
2606 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2607 << Method->getDeclName();
2608 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2609 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002610 ClsMap[Method->getSelector()] = Method;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002611 AddFactoryMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002612 }
2613 }
2614 }
Douglas Gregorb8982092013-01-21 19:42:21 +00002615 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2616 // Nothing to do here.
Steve Naroffb3a87982009-01-09 15:36:25 +00002617 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002618 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00002619 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002620 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00002621
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002622 if (C->IsClassExtension()) {
2623 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2624 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002625 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002626 }
Steve Naroffb3a87982009-01-09 15:36:25 +00002627 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002628 if (CDecl->getIdentifier())
2629 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2630 // user-defined setter/getter. It also synthesizes setter/getter methods
2631 // and adds them to the DeclContext and global method pools.
2632 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2633 E = CDecl->prop_end();
2634 I != E; ++I)
David Blaikie40ed2972012-06-06 20:45:41 +00002635 ProcessPropertyDecl(*I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00002636 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00002637 }
2638 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002639 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002640 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002641 // Any property declared in a class extension might have user
2642 // declared setter or getter in current class extension or one
2643 // of the other class extensions. Mark them as synthesized as
2644 // property will be synthesized when property with same name is
2645 // seen in the @implementation.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002646 for (ObjCInterfaceDecl::visible_extensions_iterator
2647 Ext = IDecl->visible_extensions_begin(),
2648 ExtEnd = IDecl->visible_extensions_end();
2649 Ext != ExtEnd; ++Ext) {
2650 for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
2651 E = Ext->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002652 ObjCPropertyDecl *Property = *I;
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002653 // Skip over properties declared @dynamic
2654 if (const ObjCPropertyImplDecl *PIDecl
2655 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2656 if (PIDecl->getPropertyImplementation()
2657 == ObjCPropertyImplDecl::Dynamic)
2658 continue;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002659
2660 for (ObjCInterfaceDecl::visible_extensions_iterator
2661 Ext = IDecl->visible_extensions_begin(),
2662 ExtEnd = IDecl->visible_extensions_end();
2663 Ext != ExtEnd; ++Ext) {
2664 if (ObjCMethodDecl *GetterMethod
2665 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002666 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002667 if (!Property->isReadOnly())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002668 if (ObjCMethodDecl *SetterMethod
2669 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002670 SetterMethod->setPropertyAccessor(true);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002671 }
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002672 }
2673 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00002674 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002675 AtomicPropertySetterGetterRules(IC, IDecl);
John McCall31168b02011-06-15 23:02:42 +00002676 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00002677
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002678 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2679 if (IDecl->getSuperClass() == NULL) {
2680 // This class has no superclass, so check that it has been marked with
2681 // __attribute((objc_root_class)).
2682 if (!HasRootClassAttr) {
2683 SourceLocation DeclLoc(IDecl->getLocation());
2684 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2685 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2686 << IDecl->getIdentifier();
2687 // See if NSObject is in the current scope, and if it is, suggest
2688 // adding " : NSObject " to the class declaration.
2689 NamedDecl *IF = LookupSingleName(TUScope,
2690 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2691 DeclLoc, LookupOrdinaryName);
2692 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2693 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2694 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2695 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2696 } else {
2697 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2698 }
2699 }
2700 } else if (HasRootClassAttr) {
2701 // Complain that only root classes may have this attribute.
2702 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2703 }
2704
John McCall5fb5df92012-06-20 06:18:46 +00002705 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002706 while (IDecl->getSuperClass()) {
2707 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2708 IDecl = IDecl->getSuperClass();
2709 }
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002710 }
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002711 }
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00002712 SetIvarInitializers(IC);
Mike Stump11289f42009-09-09 15:08:12 +00002713 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00002714 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002715 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00002716
Chris Lattnerda463fe2007-12-12 07:09:47 +00002717 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00002718 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00002719 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002720 if (ObjCCategoryDecl *Cat
2721 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2722 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002723 }
2724 }
2725 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002726 if (isInterfaceDeclKind) {
2727 // Reject invalid vardecls.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002728 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002729 DeclGroupRef DG = allTUVars[i].get();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002730 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2731 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00002732 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00002733 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00002734 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002735 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00002736 }
Fariborz Jahanian4327b322011-08-29 17:33:12 +00002737 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002738
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002739 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002740 DeclGroupRef DG = allTUVars[i].get();
Argyrios Kyrtzidis8ad3bab2011-11-23 20:27:36 +00002741 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2742 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002743 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2744 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002745
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +00002746 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002747 return ClassDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002748}
2749
2750
2751/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2752/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00002753static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002754CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCallca872902011-05-01 03:04:29 +00002755 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002756}
2757
Douglas Gregor33823722011-06-11 01:09:30 +00002758/// \brief Check whether the declared result type of the given Objective-C
2759/// method declaration is compatible with the method's class.
2760///
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002761static Sema::ResultTypeCompatibilityKind
Douglas Gregor33823722011-06-11 01:09:30 +00002762CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2763 ObjCInterfaceDecl *CurrentClass) {
2764 QualType ResultType = Method->getResultType();
Douglas Gregor33823722011-06-11 01:09:30 +00002765
2766 // If an Objective-C method inherits its related result type, then its
2767 // declared result type must be compatible with its own class type. The
2768 // declared result type is compatible if:
2769 if (const ObjCObjectPointerType *ResultObjectType
2770 = ResultType->getAs<ObjCObjectPointerType>()) {
2771 // - it is id or qualified id, or
2772 if (ResultObjectType->isObjCIdType() ||
2773 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002774 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002775
2776 if (CurrentClass) {
2777 if (ObjCInterfaceDecl *ResultClass
2778 = ResultObjectType->getInterfaceDecl()) {
2779 // - it is the same as the method's class type, or
Douglas Gregor0b144e12011-12-15 00:29:59 +00002780 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002781 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002782
2783 // - it is a superclass of the method's class type
2784 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002785 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002786 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00002787 } else {
2788 // Any Objective-C pointer type might be acceptable for a protocol
2789 // method; we just don't know.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002790 return Sema::RTC_Unknown;
Douglas Gregor33823722011-06-11 01:09:30 +00002791 }
2792 }
2793
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002794 return Sema::RTC_Incompatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002795}
2796
John McCalld2930c22011-07-22 02:45:48 +00002797namespace {
2798/// A helper class for searching for methods which a particular method
2799/// overrides.
2800class OverrideSearch {
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002801public:
John McCalld2930c22011-07-22 02:45:48 +00002802 Sema &S;
2803 ObjCMethodDecl *Method;
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002804 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCalld2930c22011-07-22 02:45:48 +00002805 bool Recursive;
2806
2807public:
2808 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2809 Selector selector = method->getSelector();
2810
2811 // Bypass this search if we've never seen an instance/class method
2812 // with this selector before.
2813 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2814 if (it == S.MethodPool.end()) {
Axel Naumanndd433f02012-10-18 19:05:02 +00002815 if (!S.getExternalSource()) return;
Douglas Gregore1716012012-01-25 00:49:42 +00002816 S.ReadMethodPool(selector);
2817
2818 it = S.MethodPool.find(selector);
2819 if (it == S.MethodPool.end())
2820 return;
John McCalld2930c22011-07-22 02:45:48 +00002821 }
2822 ObjCMethodList &list =
2823 method->isInstanceMethod() ? it->second.first : it->second.second;
2824 if (!list.Method) return;
2825
2826 ObjCContainerDecl *container
2827 = cast<ObjCContainerDecl>(method->getDeclContext());
2828
2829 // Prevent the search from reaching this container again. This is
2830 // important with categories, which override methods from the
2831 // interface and each other.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002832 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2833 searchFromContainer(container);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002834 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2835 searchFromContainer(Interface);
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002836 } else {
2837 searchFromContainer(container);
2838 }
Douglas Gregor33823722011-06-11 01:09:30 +00002839 }
John McCalld2930c22011-07-22 02:45:48 +00002840
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002841 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCalld2930c22011-07-22 02:45:48 +00002842 iterator begin() const { return Overridden.begin(); }
2843 iterator end() const { return Overridden.end(); }
2844
2845private:
2846 void searchFromContainer(ObjCContainerDecl *container) {
2847 if (container->isInvalidDecl()) return;
2848
2849 switch (container->getDeclKind()) {
2850#define OBJCCONTAINER(type, base) \
2851 case Decl::type: \
2852 searchFrom(cast<type##Decl>(container)); \
2853 break;
2854#define ABSTRACT_DECL(expansion)
2855#define DECL(type, base) \
2856 case Decl::type:
2857#include "clang/AST/DeclNodes.inc"
2858 llvm_unreachable("not an ObjC container!");
2859 }
2860 }
2861
2862 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00002863 if (!protocol->hasDefinition())
2864 return;
2865
John McCalld2930c22011-07-22 02:45:48 +00002866 // A method in a protocol declaration overrides declarations from
2867 // referenced ("parent") protocols.
2868 search(protocol->getReferencedProtocols());
2869 }
2870
2871 void searchFrom(ObjCCategoryDecl *category) {
2872 // A method in a category declaration overrides declarations from
2873 // the main class and from protocols the category references.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002874 // The main class is handled in the constructor.
John McCalld2930c22011-07-22 02:45:48 +00002875 search(category->getReferencedProtocols());
2876 }
2877
2878 void searchFrom(ObjCCategoryImplDecl *impl) {
2879 // A method in a category definition that has a category
2880 // declaration overrides declarations from the category
2881 // declaration.
2882 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2883 search(category);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002884 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2885 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002886
2887 // Otherwise it overrides declarations from the class.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002888 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2889 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002890 }
2891 }
2892
2893 void searchFrom(ObjCInterfaceDecl *iface) {
2894 // A method in a class declaration overrides declarations from
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00002895 if (!iface->hasDefinition())
2896 return;
2897
John McCalld2930c22011-07-22 02:45:48 +00002898 // - categories,
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00002899 for (ObjCInterfaceDecl::known_categories_iterator
2900 cat = iface->known_categories_begin(),
2901 catEnd = iface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002902 cat != catEnd; ++cat) {
2903 search(*cat);
2904 }
John McCalld2930c22011-07-22 02:45:48 +00002905
2906 // - the super class, and
2907 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2908 search(super);
2909
2910 // - any referenced protocols.
2911 search(iface->getReferencedProtocols());
2912 }
2913
2914 void searchFrom(ObjCImplementationDecl *impl) {
2915 // A method in a class implementation overrides declarations from
2916 // the class interface.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002917 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2918 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002919 }
2920
2921
2922 void search(const ObjCProtocolList &protocols) {
2923 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2924 i != e; ++i)
2925 search(*i);
2926 }
2927
2928 void search(ObjCContainerDecl *container) {
John McCalld2930c22011-07-22 02:45:48 +00002929 // Check for a method in this container which matches this selector.
2930 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00002931 Method->isInstanceMethod(),
2932 /*AllowHidden=*/true);
John McCalld2930c22011-07-22 02:45:48 +00002933
2934 // If we find one, record it and bail out.
2935 if (meth) {
2936 Overridden.insert(meth);
2937 return;
2938 }
2939
2940 // Otherwise, search for methods that a hypothetical method here
2941 // would have overridden.
2942
2943 // Note that we're now in a recursive case.
2944 Recursive = true;
2945
2946 searchFromContainer(container);
2947 }
2948};
Douglas Gregor33823722011-06-11 01:09:30 +00002949}
2950
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002951void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2952 ObjCInterfaceDecl *CurrentClass,
2953 ResultTypeCompatibilityKind RTC) {
2954 // Search for overridden methods and merge information down from them.
2955 OverrideSearch overrides(*this, ObjCMethod);
2956 // Keep track if the method overrides any method in the class's base classes,
2957 // its protocols, or its categories' protocols; we will keep that info
2958 // in the ObjCMethodDecl.
2959 // For this info, a method in an implementation is not considered as
2960 // overriding the same method in the interface or its categories.
2961 bool hasOverriddenMethodsInBaseOrProtocol = false;
2962 for (OverrideSearch::iterator
2963 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2964 ObjCMethodDecl *overridden = *i;
2965
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00002966 if (!hasOverriddenMethodsInBaseOrProtocol) {
2967 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2968 CurrentClass != overridden->getClassInterface() ||
2969 overridden->isOverriding()) {
2970 hasOverriddenMethodsInBaseOrProtocol = true;
2971
2972 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
2973 // OverrideSearch will return as "overridden" the same method in the
2974 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
2975 // check whether a category of a base class introduced a method with the
2976 // same selector, after the interface method declaration.
2977 // To avoid unnecessary lookups in the majority of cases, we use the
2978 // extra info bits in GlobalMethodPool to check whether there were any
2979 // category methods with this selector.
2980 GlobalMethodPool::iterator It =
2981 MethodPool.find(ObjCMethod->getSelector());
2982 if (It != MethodPool.end()) {
2983 ObjCMethodList &List =
2984 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
2985 unsigned CategCount = List.getBits();
2986 if (CategCount > 0) {
2987 // If the method is in a category we'll do lookup if there were at
2988 // least 2 category methods recorded, otherwise only one will do.
2989 if (CategCount > 1 ||
2990 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
2991 OverrideSearch overrides(*this, overridden);
2992 for (OverrideSearch::iterator
2993 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
2994 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00002995 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
2996 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00002997 hasOverriddenMethodsInBaseOrProtocol = true;
2998 overridden->setOverriding(true);
2999 break;
3000 }
3001 }
3002 }
3003 }
3004 }
3005 }
3006 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003007
3008 // Propagate down the 'related result type' bit from overridden methods.
3009 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
3010 ObjCMethod->SetRelatedResultType();
3011
3012 // Then merge the declarations.
3013 mergeObjCMethodDecls(ObjCMethod, overridden);
3014
3015 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3016 continue; // Conflicting properties are detected elsewhere.
3017
3018 // Check for overriding methods
3019 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3020 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3021 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3022 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3023
3024 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanian31a25682012-07-05 22:26:07 +00003025 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3026 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003027 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3028 E = ObjCMethod->param_end();
Douglas Gregor0bf70f42012-05-17 23:13:29 +00003029 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3030 PrevE = overridden->param_end();
3031 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003032 assert(PrevI != overridden->param_end() && "Param mismatch");
3033 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3034 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3035 // If type of argument of method in this class does not match its
3036 // respective argument type in the super class method, issue warning;
3037 if (!Context.typesAreCompatible(T1, T2)) {
3038 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3039 << T1 << T2;
3040 Diag(overridden->getLocation(), diag::note_previous_declaration);
3041 break;
3042 }
3043 }
3044 }
3045 }
3046
3047 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3048}
3049
John McCall48871652010-08-21 09:40:31 +00003050Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003051 Scope *S,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003052 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003053 tok::TokenKind MethodType,
John McCallba7bf592010-08-24 05:47:05 +00003054 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003055 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003056 Selector Sel,
3057 // optional arguments. The number of types/arguments is obtained
3058 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003059 ObjCArgInfo *ArgInfo,
Fariborz Jahanian60462092010-04-08 00:30:06 +00003060 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattnerda463fe2007-12-12 07:09:47 +00003061 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003062 bool isVariadic, bool MethodDefinition) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003063 // Make sure we can establish a context for the method.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003064 if (!CurContext->isObjCContainer()) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003065 Diag(MethodLoc, diag::error_missing_method_context);
John McCall48871652010-08-21 09:40:31 +00003066 return 0;
Steve Naroff83777fe2008-02-29 21:48:07 +00003067 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003068 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3069 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003070 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00003071
Douglas Gregorbab8a962011-09-08 01:46:34 +00003072 bool HasRelatedResultType = false;
Douglas Gregor12852d92010-03-08 14:59:44 +00003073 TypeSourceInfo *ResultTInfo = 0;
Steve Naroff32606412009-02-20 22:59:16 +00003074 if (ReturnType) {
Douglas Gregor12852d92010-03-08 14:59:44 +00003075 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump11289f42009-09-09 15:08:12 +00003076
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003077 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
John McCall48871652010-08-21 09:40:31 +00003078 return 0;
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003079
Douglas Gregorbab8a962011-09-08 01:46:34 +00003080 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003081 } else { // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003082 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianb21138f2011-07-21 17:38:14 +00003083 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003084 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003085 }
Mike Stump11289f42009-09-09 15:08:12 +00003086
3087 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003088 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003089 resultDeclType,
Douglas Gregor12852d92010-03-08 14:59:44 +00003090 ResultTInfo,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003091 CurContext,
Chris Lattner8d8829e2008-03-16 00:49:28 +00003092 MethodType == tok::minus, isVariadic,
Jordan Rosed01e83a2012-10-10 16:42:25 +00003093 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003094 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor33823722011-06-11 01:09:30 +00003095 MethodDeclKind == tok::objc_optional
3096 ? ObjCMethodDecl::Optional
3097 : ObjCMethodDecl::Required,
Douglas Gregorbab8a962011-09-08 01:46:34 +00003098 HasRelatedResultType);
Mike Stump11289f42009-09-09 15:08:12 +00003099
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003100 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00003101
Chris Lattner23b0faf2009-04-11 19:42:43 +00003102 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00003103 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00003104 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00003105
David Blaikie7d170102013-05-15 07:37:26 +00003106 if (!ArgInfo[i].Type) {
John McCall856bbea2009-10-23 21:48:59 +00003107 ArgType = Context.getObjCIdType();
3108 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003109 } else {
John McCall856bbea2009-10-23 21:48:59 +00003110 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003111 }
Mike Stump11289f42009-09-09 15:08:12 +00003112
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003113 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3114 LookupOrdinaryName, ForRedeclaration);
3115 LookupName(R, S);
3116 if (R.isSingleResult()) {
3117 NamedDecl *PrevDecl = R.getFoundDecl();
3118 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003119 Diag(ArgInfo[i].NameLoc,
3120 (MethodDefinition ? diag::warn_method_param_redefinition
3121 : diag::warn_method_param_declaration))
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003122 << ArgInfo[i].Name;
3123 Diag(PrevDecl->getLocation(),
3124 diag::note_previous_declaration);
3125 }
3126 }
3127
Abramo Bagnaradff19302011-03-08 08:55:46 +00003128 SourceLocation StartLoc = DI
3129 ? DI->getTypeLoc().getBeginLoc()
3130 : ArgInfo[i].NameLoc;
3131
John McCalld44f4d72011-04-23 02:46:06 +00003132 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3133 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003134 ArgType, DI, SC_None);
Mike Stump11289f42009-09-09 15:08:12 +00003135
John McCall82490832011-05-02 00:30:12 +00003136 Param->setObjCMethodScopeInfo(i);
3137
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003138 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003139 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00003140
Chris Lattner9713a1c2009-04-11 19:34:56 +00003141 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00003142 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00003143
Fariborz Jahanian52d02f62012-01-14 18:44:35 +00003144 if (Param->hasAttr<BlocksAttr>()) {
3145 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3146 Param->setInvalidDecl();
3147 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003148 S->AddDecl(Param);
3149 IdResolver.AddDecl(Param);
3150
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003151 Params.push_back(Param);
3152 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003153
Fariborz Jahanian60462092010-04-08 00:30:06 +00003154 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCall48871652010-08-21 09:40:31 +00003155 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003156 QualType ArgType = Param->getType();
3157 if (ArgType.isNull())
3158 ArgType = Context.getObjCIdType();
3159 else
3160 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor84280642011-07-12 04:42:08 +00003161 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003162
Fariborz Jahanian60462092010-04-08 00:30:06 +00003163 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003164 Params.push_back(Param);
3165 }
3166
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003167 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003168 ObjCMethod->setObjCDeclQualifier(
3169 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00003170
3171 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00003172 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00003173
Douglas Gregor87e92752010-12-21 17:34:17 +00003174 // Add the method now.
John McCalld2930c22011-07-22 02:45:48 +00003175 const ObjCMethodDecl *PrevMethod = 0;
3176 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00003177 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003178 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3179 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003180 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003181 PrevMethod = ImpDecl->getClassMethod(Sel);
3182 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003183 }
Douglas Gregor33823722011-06-11 01:09:30 +00003184
Fariborz Jahanian512a4cc92011-10-22 01:21:15 +00003185 ObjCMethodDecl *IMD = 0;
3186 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3187 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3188 ObjCMethod->isInstanceMethod());
Fariborz Jahaniandb4fc282013-07-09 22:02:20 +00003189 if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
3190 !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
3191 // merge the attribute into implementation.
3192 ObjCMethod->addAttr(
3193 new (Context) ObjCRequiresSuperAttr(ObjCMethod->getLocation(), Context));
3194 }
Douglas Gregor87e92752010-12-21 17:34:17 +00003195 } else {
3196 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003197 }
John McCalld2930c22011-07-22 02:45:48 +00003198
Chris Lattnerda463fe2007-12-12 07:09:47 +00003199 if (PrevMethod) {
3200 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00003201 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00003202 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00003203 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian096f7c12013-05-13 17:27:00 +00003204 ObjCMethod->setInvalidDecl();
3205 return ObjCMethod;
Mike Stump11289f42009-09-09 15:08:12 +00003206 }
John McCall28a6aea2009-11-04 02:18:39 +00003207
Douglas Gregor33823722011-06-11 01:09:30 +00003208 // If this Objective-C method does not have a related result type, but we
3209 // are allowed to infer related result types, try to do so based on the
3210 // method family.
3211 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3212 if (!CurrentClass) {
3213 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3214 CurrentClass = Cat->getClassInterface();
3215 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3216 CurrentClass = Impl->getClassInterface();
3217 else if (ObjCCategoryImplDecl *CatImpl
3218 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3219 CurrentClass = CatImpl->getClassInterface();
3220 }
John McCalld2930c22011-07-22 02:45:48 +00003221
Douglas Gregorbab8a962011-09-08 01:46:34 +00003222 ResultTypeCompatibilityKind RTC
3223 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCalld2930c22011-07-22 02:45:48 +00003224
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003225 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCalld2930c22011-07-22 02:45:48 +00003226
John McCall31168b02011-06-15 23:02:42 +00003227 bool ARCError = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003228 if (getLangOpts().ObjCAutoRefCount)
John McCalle48f3892013-04-04 01:38:37 +00003229 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCall31168b02011-06-15 23:02:42 +00003230
Douglas Gregorbab8a962011-09-08 01:46:34 +00003231 // Infer the related result type when possible.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003232 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregorbab8a962011-09-08 01:46:34 +00003233 !ObjCMethod->hasRelatedResultType() &&
3234 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor33823722011-06-11 01:09:30 +00003235 bool InferRelatedResultType = false;
3236 switch (ObjCMethod->getMethodFamily()) {
3237 case OMF_None:
3238 case OMF_copy:
3239 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00003240 case OMF_finalize:
Douglas Gregor33823722011-06-11 01:09:30 +00003241 case OMF_mutableCopy:
3242 case OMF_release:
3243 case OMF_retainCount:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00003244 case OMF_performSelector:
Douglas Gregor33823722011-06-11 01:09:30 +00003245 break;
3246
3247 case OMF_alloc:
3248 case OMF_new:
3249 InferRelatedResultType = ObjCMethod->isClassMethod();
3250 break;
3251
3252 case OMF_init:
3253 case OMF_autorelease:
3254 case OMF_retain:
3255 case OMF_self:
3256 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3257 break;
3258 }
3259
John McCalld2930c22011-07-22 02:45:48 +00003260 if (InferRelatedResultType)
Douglas Gregor33823722011-06-11 01:09:30 +00003261 ObjCMethod->SetRelatedResultType();
Douglas Gregor33823722011-06-11 01:09:30 +00003262 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00003263
3264 ActOnDocumentableDecl(ObjCMethod);
3265
John McCall48871652010-08-21 09:40:31 +00003266 return ObjCMethod;
Chris Lattnerda463fe2007-12-12 07:09:47 +00003267}
3268
Chris Lattner438e5012008-12-17 07:13:27 +00003269bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +00003270 // Following is also an error. But it is caused by a missing @end
3271 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003272 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003273 return false;
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003274
3275 // If we switched context to translation unit while we are still lexically in
3276 // an objc container, it means the parser missed emitting an error.
3277 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3278 return false;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003279
Anders Carlssona6b508a2008-11-04 16:57:32 +00003280 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3281 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003282
Anders Carlssona6b508a2008-11-04 16:57:32 +00003283 return true;
3284}
Chris Lattner438e5012008-12-17 07:13:27 +00003285
James Dennett634962f2012-06-14 21:40:34 +00003286/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattner438e5012008-12-17 07:13:27 +00003287/// instance variables of ClassName into Decls.
John McCall48871652010-08-21 09:40:31 +00003288void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00003289 IdentifierInfo *ClassName,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003290 SmallVectorImpl<Decl*> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00003291 // Check that ClassName is a valid class
Douglas Gregorb2ccf012010-04-15 22:33:43 +00003292 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattner438e5012008-12-17 07:13:27 +00003293 if (!Class) {
3294 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3295 return;
3296 }
John McCall5fb5df92012-06-20 06:18:46 +00003297 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00003298 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3299 return;
3300 }
Mike Stump11289f42009-09-09 15:08:12 +00003301
Chris Lattner438e5012008-12-17 07:13:27 +00003302 // Collect the instance variables
Jordy Rosea91768e2011-07-22 02:08:32 +00003303 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003304 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003305 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003306 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003307 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCall48871652010-08-21 09:40:31 +00003308 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaradff19302011-03-08 08:55:46 +00003309 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3310 /*FIXME: StartL=*/ID->getLocation(),
3311 ID->getLocation(),
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003312 ID->getIdentifier(), ID->getType(),
3313 ID->getBitWidth());
John McCall48871652010-08-21 09:40:31 +00003314 Decls.push_back(FD);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003315 }
Mike Stump11289f42009-09-09 15:08:12 +00003316
Chris Lattner438e5012008-12-17 07:13:27 +00003317 // Introduce all of these fields into the appropriate scope.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003318 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00003319 D != Decls.end(); ++D) {
John McCall48871652010-08-21 09:40:31 +00003320 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003321 if (getLangOpts().CPlusPlus)
Chris Lattner438e5012008-12-17 07:13:27 +00003322 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCall48871652010-08-21 09:40:31 +00003323 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003324 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00003325 }
3326}
3327
Douglas Gregorf3564192010-04-26 17:32:49 +00003328/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaradff19302011-03-08 08:55:46 +00003329VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3330 SourceLocation StartLoc,
3331 SourceLocation IdLoc,
3332 IdentifierInfo *Id,
Douglas Gregorf3564192010-04-26 17:32:49 +00003333 bool Invalid) {
3334 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3335 // duration shall not be qualified by an address-space qualifier."
3336 // Since all parameters have automatic store duration, they can not have
3337 // an address space.
3338 if (T.getAddressSpace() != 0) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003339 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregorf3564192010-04-26 17:32:49 +00003340 Invalid = true;
3341 }
3342
3343 // An @catch parameter must be an unqualified object pointer type;
3344 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3345 if (Invalid) {
3346 // Don't do any further checking.
Douglas Gregorf4e837f2010-04-26 17:57:08 +00003347 } else if (T->isDependentType()) {
3348 // Okay: we don't know what this type will instantiate to.
Douglas Gregorf3564192010-04-26 17:32:49 +00003349 } else if (!T->isObjCObjectPointerType()) {
3350 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003351 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregorf3564192010-04-26 17:32:49 +00003352 } else if (T->isObjCQualifiedIdType()) {
3353 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003354 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregorf3564192010-04-26 17:32:49 +00003355 }
3356
Abramo Bagnaradff19302011-03-08 08:55:46 +00003357 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003358 T, TInfo, SC_None);
Douglas Gregor3f324d562010-05-03 18:51:14 +00003359 New->setExceptionVariable(true);
3360
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003361 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003362 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003363 Invalid = true;
3364
Douglas Gregorf3564192010-04-26 17:32:49 +00003365 if (Invalid)
3366 New->setInvalidDecl();
3367 return New;
3368}
3369
John McCall48871652010-08-21 09:40:31 +00003370Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003371 const DeclSpec &DS = D.getDeclSpec();
3372
3373 // We allow the "register" storage class on exception variables because
3374 // GCC did, but we drop it completely. Any other storage class is an error.
3375 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3376 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3377 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithb4a9e862013-04-12 22:46:28 +00003378 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003379 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithb4a9e862013-04-12 22:46:28 +00003380 << DeclSpec::getSpecifierName(SCS);
3381 }
3382 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3383 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3384 diag::err_invalid_thread)
3385 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregorf3564192010-04-26 17:32:49 +00003386 D.getMutableDeclSpec().ClearStorageClassSpecs();
3387
Richard Smithb1402ae2013-03-18 22:52:47 +00003388 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregorf3564192010-04-26 17:32:49 +00003389
3390 // Check that there are no default arguments inside the type of this
3391 // exception object (C++ only).
David Blaikiebbafb8a2012-03-11 07:00:24 +00003392 if (getLangOpts().CPlusPlus)
Douglas Gregorf3564192010-04-26 17:32:49 +00003393 CheckExtraCXXDefaultArguments(D);
3394
Argyrios Kyrtzidisef7022f2011-06-28 03:01:15 +00003395 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCall8cb7bdf2010-06-04 23:28:52 +00003396 QualType ExceptionType = TInfo->getType();
Douglas Gregorf3564192010-04-26 17:32:49 +00003397
Abramo Bagnaradff19302011-03-08 08:55:46 +00003398 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3399 D.getSourceRange().getBegin(),
3400 D.getIdentifierLoc(),
3401 D.getIdentifier(),
Douglas Gregorf3564192010-04-26 17:32:49 +00003402 D.isInvalidType());
3403
3404 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3405 if (D.getCXXScopeSpec().isSet()) {
3406 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3407 << D.getCXXScopeSpec().getRange();
3408 New->setInvalidDecl();
3409 }
3410
3411 // Add the parameter declaration into this scope.
John McCall48871652010-08-21 09:40:31 +00003412 S->AddDecl(New);
Douglas Gregorf3564192010-04-26 17:32:49 +00003413 if (D.getIdentifier())
3414 IdResolver.AddDecl(New);
3415
3416 ProcessDeclAttributes(S, New, D);
3417
3418 if (New->hasAttr<BlocksAttr>())
3419 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCall48871652010-08-21 09:40:31 +00003420 return New;
Douglas Gregore11ee112010-04-23 23:01:43 +00003421}
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003422
3423/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003424/// initialization.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003425void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003426 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003427 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3428 Iv= Iv->getNextIvar()) {
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003429 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00003430 if (QT->isRecordType())
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003431 Ivars.push_back(Iv);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003432 }
3433}
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003434
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003435void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor72e357f2011-07-28 14:54:22 +00003436 // Load referenced selectors from the external source.
3437 if (ExternalSource) {
3438 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3439 ExternalSource->ReadReferencedSelectors(Sels);
3440 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3441 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3442 }
3443
Fariborz Jahanian42f89382013-05-30 21:48:58 +00003444 DiagnoseMismatchedMethodsInGlobalPool();
3445
Fariborz Jahanianc9b7c202011-02-04 23:19:27 +00003446 // Warning will be issued only when selector table is
3447 // generated (which means there is at lease one implementation
3448 // in the TU). This is to match gcc's behavior.
3449 if (ReferencedSelectors.empty() ||
3450 !Context.AnyObjCImplementation())
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003451 return;
3452 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3453 ReferencedSelectors.begin(),
3454 E = ReferencedSelectors.end(); S != E; ++S) {
3455 Selector Sel = (*S).first;
3456 if (!LookupImplementedMethodInGlobalPool(Sel))
3457 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3458 }
3459 return;
3460}
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003461
3462ObjCIvarDecl *
3463Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method,
3464 const ObjCPropertyDecl *&PDecl) const {
3465
3466 const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
3467 if (!IDecl)
3468 return 0;
3469 Method = IDecl->lookupMethod(Method->getSelector(), true);
3470 if (!Method || !Method->isPropertyAccessor())
3471 return 0;
Fariborz Jahanian617e49a2013-11-15 17:48:00 +00003472 if ((PDecl = Method->findPropertyDecl())) {
3473 if (!PDecl->getDeclContext())
3474 return 0;
3475 // Make sure property belongs to accessor's class and not to
3476 // one of its super classes.
3477 if (const ObjCInterfaceDecl *CID =
3478 dyn_cast<ObjCInterfaceDecl>(PDecl->getDeclContext()))
3479 if (CID != IDecl)
3480 return 0;
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003481 return PDecl->getPropertyIvarDecl();
Fariborz Jahanian617e49a2013-11-15 17:48:00 +00003482 }
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003483 return 0;
3484}
3485
3486void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S) {
3487 if (S->hasUnrecoverableErrorOccurred() || !S->isInObjcMethodScope())
3488 return;
3489
3490 const ObjCMethodDecl *CurMethod = getCurMethodDecl();
3491 if (!CurMethod)
3492 return;
3493 const ObjCPropertyDecl *PDecl;
3494 const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl);
3495 if (IV && !IV->getBackingIvarReferencedInAccessor()) {
3496 Diag(getCurMethodDecl()->getLocation(), diag::warn_unused_property_backing_ivar)
3497 << IV->getDeclName();
3498 Diag(PDecl->getLocation(), diag::note_property_declare);
3499 }
3500}