blob: 6de4e5bb21a32e993c1a6ea5f8232b43b90b22bd [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
Nico Weber1fb82662011-08-28 22:35:17 +0000394 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber715abaf2011-08-22 17:25:57 +0000395 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
396 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
397 // Only do this if the current class actually has a superclass.
Jordan Rosed03d99d2013-03-05 01:27:54 +0000398 if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) {
Jordan Rose2afd6612012-10-19 16:05:26 +0000399 ObjCMethodFamily Family = MDecl->getMethodFamily();
400 if (Family == OMF_dealloc) {
401 if (!(getLangOpts().ObjCAutoRefCount ||
402 getLangOpts().getGC() == LangOptions::GCOnly))
403 getCurFunction()->ObjCShouldCallSuper = true;
404
405 } else if (Family == OMF_finalize) {
406 if (Context.getLangOpts().getGC() != LangOptions::NonGC)
407 getCurFunction()->ObjCShouldCallSuper = true;
408
Fariborz Jahaniance4bbb22013-11-05 00:28:21 +0000409 } else {
Jordan Rose2afd6612012-10-19 16:05:26 +0000410 const ObjCMethodDecl *SuperMethod =
Jordan Rosed03d99d2013-03-05 01:27:54 +0000411 SuperClass->lookupMethod(MDecl->getSelector(),
412 MDecl->isInstanceMethod());
Jordan Rose2afd6612012-10-19 16:05:26 +0000413 getCurFunction()->ObjCShouldCallSuper =
414 (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahaniand6876b22012-09-10 18:04:25 +0000415 }
Nico Weber1fb82662011-08-28 22:35:17 +0000416 }
Nico Weber715abaf2011-08-22 17:25:57 +0000417 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000418}
419
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000420namespace {
421
422// Callback to only accept typo corrections that are Objective-C classes.
423// If an ObjCInterfaceDecl* is given to the constructor, then the validation
424// function will reject corrections to that class.
425class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
426 public:
427 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
428 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
429 : CurrentIDecl(IDecl) {}
430
431 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
432 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
433 return ID && !declaresSameEntity(ID, CurrentIDecl);
434 }
435
436 private:
437 ObjCInterfaceDecl *CurrentIDecl;
438};
439
440}
441
John McCall48871652010-08-21 09:40:31 +0000442Decl *Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000443ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
444 IdentifierInfo *ClassName, SourceLocation ClassLoc,
445 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCall48871652010-08-21 09:40:31 +0000446 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000447 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000448 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000449 assert(ClassName && "Missing class identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000450
Chris Lattnerda463fe2007-12-12 07:09:47 +0000451 // Check for another declaration kind with the same name.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000452 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000453 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor5101c242008-12-05 18:15:24 +0000454
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000455 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000456 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000457 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000458 }
Mike Stump11289f42009-09-09 15:08:12 +0000459
Douglas Gregordc9166c2011-12-15 20:29:51 +0000460 // Create a declaration to describe this @interface.
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000461 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +0000462
463 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
464 // A previous decl with a different name is because of
465 // @compatibility_alias, for example:
466 // \code
467 // @class NewImage;
468 // @compatibility_alias OldImage NewImage;
469 // \endcode
470 // A lookup for 'OldImage' will return the 'NewImage' decl.
471 //
472 // In such a case use the real declaration name, instead of the alias one,
473 // otherwise we will break IdentifierResolver and redecls-chain invariants.
474 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
475 // has been aliased.
476 ClassName = PrevIDecl->getIdentifier();
477 }
478
Douglas Gregordc9166c2011-12-15 20:29:51 +0000479 ObjCInterfaceDecl *IDecl
480 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000481 PrevIDecl, ClassLoc);
Douglas Gregordc9166c2011-12-15 20:29:51 +0000482
Douglas Gregordc9166c2011-12-15 20:29:51 +0000483 if (PrevIDecl) {
484 // Class already seen. Was it a definition?
485 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
486 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
487 << PrevIDecl->getDeclName();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000488 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregordc9166c2011-12-15 20:29:51 +0000489 IDecl->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000490 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000491 }
Douglas Gregordc9166c2011-12-15 20:29:51 +0000492
493 if (AttrList)
494 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
495 PushOnScopeChains(IDecl, TUScope);
Mike Stump11289f42009-09-09 15:08:12 +0000496
Douglas Gregordc9166c2011-12-15 20:29:51 +0000497 // Start the definition of this class. If we're in a redefinition case, there
498 // may already be a definition, so we'll end up adding to it.
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000499 if (!IDecl->hasDefinition())
500 IDecl->startDefinition();
501
Chris Lattnerda463fe2007-12-12 07:09:47 +0000502 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000503 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000504 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
505 LookupOrdinaryName);
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000506
507 if (!PrevDecl) {
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000508 // Try to correct for a typo in the superclass name without correcting
509 // to the class we're defining.
510 ObjCInterfaceValidatorCCC Validator(IDecl);
511 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000512 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain4e8942c2012-01-31 23:49:25 +0000513 NULL, Validator)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000514 diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest)
515 << SuperName << ClassName);
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000516 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000517 }
518 }
519
Douglas Gregor0b144e12011-12-15 00:29:59 +0000520 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000521 Diag(SuperLoc, diag::err_recursive_superclass)
522 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor16408322011-12-15 22:34:59 +0000523 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stump12b8ce12009-08-04 21:02:39 +0000524 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000525 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000526 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000527
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000528 // Diagnose classes that inherit from deprecated classes.
529 if (SuperClassDecl)
530 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000531
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000532 if (PrevDecl && SuperClassDecl == 0) {
533 // The previous declaration was not a class decl. Check if we have a
534 // typedef. If we do, get the underlying class type.
Richard Smithdda56e42011-04-15 14:24:37 +0000535 if (const TypedefNameDecl *TDecl =
536 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000537 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000538 if (T->isObjCObjectType()) {
Fariborz Jahanian83f1be12013-04-04 18:45:52 +0000539 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000540 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian83f1be12013-04-04 18:45:52 +0000541 // This handles the following case:
542 // @interface NewI @end
543 // typedef NewI DeprI __attribute__((deprecated("blah")))
544 // @interface SI : DeprI /* warn here */ @end
545 (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc);
546 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000547 }
548 }
Mike Stump11289f42009-09-09 15:08:12 +0000549
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000550 // This handles the following case:
551 //
552 // typedef int SuperClass;
553 // @interface MyClass : SuperClass {} @end
554 //
555 if (!SuperClassDecl) {
556 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
557 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff189d41f2009-02-04 17:14:05 +0000558 }
559 }
Mike Stump11289f42009-09-09 15:08:12 +0000560
Richard Smithdda56e42011-04-15 14:24:37 +0000561 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000562 if (!SuperClassDecl)
563 Diag(SuperLoc, diag::err_undef_superclass)
564 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor4123a862011-11-14 22:10:01 +0000565 else if (RequireCompleteType(SuperLoc,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000566 Context.getObjCInterfaceType(SuperClassDecl),
567 diag::err_forward_superclass,
568 SuperClassDecl->getDeclName(),
569 ClassName,
570 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahanian3ee91fa2011-06-23 23:16:19 +0000571 SuperClassDecl = 0;
572 }
Steve Naroff189d41f2009-02-04 17:14:05 +0000573 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000574 IDecl->setSuperClass(SuperClassDecl);
575 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor16408322011-12-15 22:34:59 +0000576 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000577 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000578 } else { // we have a root class.
Douglas Gregor16408322011-12-15 22:34:59 +0000579 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000580 }
Mike Stump11289f42009-09-09 15:08:12 +0000581
Sebastian Redle7c1fe62010-08-13 00:28:03 +0000582 // Check then save referenced protocols.
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000583 if (NumProtoRefs) {
Roman Divackye6377112012-09-06 15:59:27 +0000584 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000585 ProtoLocs, Context);
Douglas Gregor16408322011-12-15 22:34:59 +0000586 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000587 }
Mike Stump11289f42009-09-09 15:08:12 +0000588
Anders Carlssona6b508a2008-11-04 16:57:32 +0000589 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000590 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000591}
592
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000593/// ActOnTypedefedProtocols - this action finds protocol list as part of the
594/// typedef'ed use for a qualified super class and adds them to the list
595/// of the protocols.
596void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs,
597 IdentifierInfo *SuperName,
598 SourceLocation SuperLoc) {
599 if (!SuperName)
600 return;
601 NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
602 LookupOrdinaryName);
603 if (!IDecl)
604 return;
605
606 if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) {
607 QualType T = TDecl->getUnderlyingType();
608 if (T->isObjCObjectType())
609 if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>())
610 for (ObjCObjectType::qual_iterator I = OPT->qual_begin(),
611 E = OPT->qual_end(); I != E; ++I)
612 ProtocolRefs.push_back(*I);
613 }
614}
615
Richard Smithac4e36d2012-08-08 23:32:13 +0000616/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett634962f2012-06-14 21:40:34 +0000617/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithac4e36d2012-08-08 23:32:13 +0000618Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
619 IdentifierInfo *AliasName,
620 SourceLocation AliasLocation,
621 IdentifierInfo *ClassName,
622 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000623 // Look for previous declaration of alias name
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000624 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000625 LookupOrdinaryName, ForRedeclaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000626 if (ADecl) {
Eli Friedmanfd6b3f82013-06-21 01:49:53 +0000627 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000628 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +0000629 return 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000630 }
631 // Check for class declaration
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000632 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000633 LookupOrdinaryName, ForRedeclaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000634 if (const TypedefNameDecl *TDecl =
635 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000636 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000637 if (T->isObjCObjectType()) {
638 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000639 ClassName = IDecl->getIdentifier();
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000640 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000641 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000642 }
643 }
644 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000645 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
646 if (CDecl == 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000647 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000648 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000649 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +0000650 return 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000651 }
Mike Stump11289f42009-09-09 15:08:12 +0000652
Chris Lattner219b3e92008-03-16 21:17:37 +0000653 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000654 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000655 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000656
Anders Carlssona6b508a2008-11-04 16:57:32 +0000657 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000658 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000659
John McCall48871652010-08-21 09:40:31 +0000660 return AliasDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000661}
662
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000663bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff41d09ad2009-03-05 15:22:01 +0000664 IdentifierInfo *PName,
665 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000666 const ObjCList<ObjCProtocolDecl> &PList) {
667
668 bool res = false;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000669 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
670 E = PList.end(); I != E; ++I) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000671 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
672 Ploc)) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000673 if (PDecl->getIdentifier() == PName) {
674 Diag(Ploc, diag::err_protocol_has_circular_dependency);
675 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000676 res = true;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000677 }
Douglas Gregore6e48b12012-01-01 19:29:29 +0000678
679 if (!PDecl->hasDefinition())
680 continue;
681
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000682 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
683 PDecl->getLocation(), PDecl->getReferencedProtocols()))
684 res = true;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000685 }
686 }
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000687 return res;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000688}
689
John McCall48871652010-08-21 09:40:31 +0000690Decl *
Chris Lattner3bbae002008-07-26 04:03:38 +0000691Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
692 IdentifierInfo *ProtocolName,
693 SourceLocation ProtocolLoc,
John McCall48871652010-08-21 09:40:31 +0000694 Decl * const *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000695 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000696 const SourceLocation *ProtoLocs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000697 SourceLocation EndProtoLoc,
698 AttributeList *AttrList) {
Fariborz Jahaniancadf7c52011-05-12 22:04:39 +0000699 bool err = false;
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000700 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000701 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor32c17572012-01-01 20:30:41 +0000702 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
703 ForRedeclaration);
704 ObjCProtocolDecl *PDecl = 0;
705 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
706 // If we already have a definition, complain.
707 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
708 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump11289f42009-09-09 15:08:12 +0000709
Douglas Gregor32c17572012-01-01 20:30:41 +0000710 // Create a new protocol that is completely distinct from previous
711 // declarations, and do not make this protocol available for name lookup.
712 // That way, we'll end up completely ignoring the duplicate.
713 // FIXME: Can we turn this into an error?
714 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
715 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000716 /*PrevDecl=*/0);
Douglas Gregor32c17572012-01-01 20:30:41 +0000717 PDecl->startDefinition();
718 } else {
719 if (PrevDecl) {
720 // Check for circular dependencies among protocol declarations. This can
721 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis95dfc122011-11-13 22:08:30 +0000722 ObjCList<ObjCProtocolDecl> PList;
723 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
724 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor32c17572012-01-01 20:30:41 +0000725 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis95dfc122011-11-13 22:08:30 +0000726 }
Douglas Gregor32c17572012-01-01 20:30:41 +0000727
728 // Create the new declaration.
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +0000729 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +0000730 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000731 /*PrevDecl=*/PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000732
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000733 PushOnScopeChains(PDecl, TUScope);
Douglas Gregore6e48b12012-01-01 19:29:29 +0000734 PDecl->startDefinition();
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000735 }
Douglas Gregore6e48b12012-01-01 19:29:29 +0000736
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000737 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000738 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor32c17572012-01-01 20:30:41 +0000739
740 // Merge attributes from previous declarations.
741 if (PrevDecl)
742 mergeDeclAttributes(PDecl, PrevDecl);
743
Fariborz Jahaniancadf7c52011-05-12 22:04:39 +0000744 if (!err && NumProtoRefs ) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000745 /// Check then save referenced protocols.
Roman Divackye6377112012-09-06 15:59:27 +0000746 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000747 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000748 }
Mike Stump11289f42009-09-09 15:08:12 +0000749
750 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000751 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000752}
753
754/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000755/// issues an error if they are not declared. It returns list of
756/// protocol declarations in its 'Protocols' argument.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000757void
Chris Lattner3bbae002008-07-26 04:03:38 +0000758Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000759 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000760 unsigned NumProtocols,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000761 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000762 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000763 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
764 ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000765 if (!PDecl) {
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000766 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000767 TypoCorrection Corrected = CorrectTypo(
768 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain4e8942c2012-01-31 23:49:25 +0000769 LookupObjCProtocolName, TUScope, NULL, Validator);
Richard Smithf9b15102013-08-17 00:46:16 +0000770 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>()))
771 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest)
772 << ProtocolId[i].first);
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000773 }
774
775 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000776 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000777 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000778 continue;
779 }
Fariborz Jahanianada44a22013-04-09 17:52:29 +0000780 // If this is a forward protocol declaration, get its definition.
781 if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
782 PDecl = PDecl->getDefinition();
783
Douglas Gregor171c45a2009-02-18 21:56:37 +0000784 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000785
786 // If this is a forward declaration and we are supposed to warn in this
787 // case, do it.
Douglas Gregoreed49792013-01-17 00:38:46 +0000788 // FIXME: Recover nicely in the hidden case.
789 if (WarnOnDeclarations &&
790 (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()))
Chris Lattner3b054132008-11-19 05:08:23 +0000791 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000792 << ProtocolId[i].first;
John McCall48871652010-08-21 09:40:31 +0000793 Protocols.push_back(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000794 }
795}
796
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000797/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000798/// a class method in its extension.
799///
Mike Stump11289f42009-09-09 15:08:12 +0000800void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000801 ObjCInterfaceDecl *ID) {
802 if (!ID)
803 return; // Possibly due to previous error
804
805 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000806 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
807 e = ID->meth_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +0000808 ObjCMethodDecl *MD = *i;
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000809 MethodMap[MD->getSelector()] = MD;
810 }
811
812 if (MethodMap.empty())
813 return;
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000814 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
815 e = CAT->meth_end(); i != e; ++i) {
David Blaikie40ed2972012-06-06 20:45:41 +0000816 ObjCMethodDecl *Method = *i;
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000817 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
818 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
819 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
820 << Method->getDeclName();
821 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
822 }
823 }
824}
825
James Dennett634962f2012-06-14 21:40:34 +0000826/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorf6102672012-01-01 21:23:57 +0000827Sema::DeclGroupPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000828Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000829 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000830 unsigned NumElts,
831 AttributeList *attrList) {
Douglas Gregorf6102672012-01-01 21:23:57 +0000832 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000833 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000834 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor32c17572012-01-01 20:30:41 +0000835 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
836 ForRedeclaration);
837 ObjCProtocolDecl *PDecl
838 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
839 IdentList[i].second, AtProtocolLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000840 PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000841
842 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorf6102672012-01-01 21:23:57 +0000843 CheckObjCDeclScope(PDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000844
Douglas Gregor42ff1bb2012-01-01 20:33:24 +0000845 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000846 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor32c17572012-01-01 20:30:41 +0000847
848 if (PrevDecl)
849 mergeDeclAttributes(PDecl, PrevDecl);
850
Douglas Gregorf6102672012-01-01 21:23:57 +0000851 DeclsInGroup.push_back(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000852 }
Mike Stump11289f42009-09-09 15:08:12 +0000853
Rafael Espindolaab417692013-07-09 12:05:01 +0000854 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000855}
856
John McCall48871652010-08-21 09:40:31 +0000857Decl *Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000858ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
859 IdentifierInfo *ClassName, SourceLocation ClassLoc,
860 IdentifierInfo *CategoryName,
861 SourceLocation CategoryLoc,
John McCall48871652010-08-21 09:40:31 +0000862 Decl * const *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000863 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000864 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000865 SourceLocation EndProtoLoc) {
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000866 ObjCCategoryDecl *CDecl;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000867 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek514ff702010-02-23 19:39:46 +0000868
869 /// Check that class of this category is already completely declared.
Douglas Gregor4123a862011-11-14 22:10:01 +0000870
871 if (!IDecl
872 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000873 diag::err_category_forward_interface,
874 CategoryName == 0)) {
Ted Kremenek514ff702010-02-23 19:39:46 +0000875 // Create an invalid ObjCCategoryDecl to serve as context for
876 // the enclosing method declarations. We mark the decl invalid
877 // to make it clear that this isn't a valid AST.
878 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000879 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek514ff702010-02-23 19:39:46 +0000880 CDecl->setInvalidDecl();
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +0000881 CurContext->addDecl(CDecl);
Douglas Gregor4123a862011-11-14 22:10:01 +0000882
883 if (!IDecl)
884 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000885 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek514ff702010-02-23 19:39:46 +0000886 }
887
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000888 if (!CategoryName && IDecl->getImplementation()) {
889 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
890 Diag(IDecl->getImplementation()->getLocation(),
891 diag::note_implementation_declared);
Ted Kremenek514ff702010-02-23 19:39:46 +0000892 }
893
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000894 if (CategoryName) {
895 /// Check for duplicate interface declaration for this category
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000896 if (ObjCCategoryDecl *Previous
897 = IDecl->FindCategoryDeclaration(CategoryName)) {
898 // Class extensions can be declared multiple times, categories cannot.
899 Diag(CategoryLoc, diag::warn_dup_category_def)
900 << ClassName << CategoryName;
901 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner9018ca82009-02-16 21:26:43 +0000902 }
903 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000904
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000905 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
906 ClassLoc, CategoryLoc, CategoryName, IDecl);
907 // FIXME: PushOnScopeChains?
908 CurContext->addDecl(CDecl);
909
Chris Lattnerda463fe2007-12-12 07:09:47 +0000910 if (NumProtoRefs) {
Roman Divackye6377112012-09-06 15:59:27 +0000911 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000912 ProtoLocs, Context);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000913 // Protocols in the class extension belong to the class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000914 if (CDecl->IsClassExtension())
Roman Divackye6377112012-09-06 15:59:27 +0000915 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000916 NumProtoRefs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000917 }
Mike Stump11289f42009-09-09 15:08:12 +0000918
Anders Carlssona6b508a2008-11-04 16:57:32 +0000919 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000920 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000921}
922
923/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000924/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000925/// object.
John McCall48871652010-08-21 09:40:31 +0000926Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000927 SourceLocation AtCatImplLoc,
928 IdentifierInfo *ClassName, SourceLocation ClassLoc,
929 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000930 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000931 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +0000932 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000933 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
934 if (!CatIDecl) {
935 // Category @implementation with no corresponding @interface.
936 // Create and install one.
Argyrios Kyrtzidis41fc05c2011-11-23 20:27:26 +0000937 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
938 ClassLoc, CatLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000939 CatName, IDecl);
Argyrios Kyrtzidis41fc05c2011-11-23 20:27:26 +0000940 CatIDecl->setImplicit();
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000941 }
942 }
943
Mike Stump11289f42009-09-09 15:08:12 +0000944 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +0000945 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +0000946 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000947 /// Check that class of this category is already completely declared.
Douglas Gregor4123a862011-11-14 22:10:01 +0000948 if (!IDecl) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000949 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld2930c22011-07-22 02:45:48 +0000950 CDecl->setInvalidDecl();
Douglas Gregor4123a862011-11-14 22:10:01 +0000951 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
952 diag::err_undef_interface)) {
953 CDecl->setInvalidDecl();
John McCalld2930c22011-07-22 02:45:48 +0000954 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000955
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000956 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000957 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000958
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +0000959 // If the interface is deprecated/unavailable, warn/error about it.
960 if (IDecl)
961 DiagnoseUseOfDecl(IDecl, ClassLoc);
962
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000963 /// Check that CatName, category name, is not used in another implementation.
964 if (CatIDecl) {
965 if (CatIDecl->getImplementation()) {
966 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
967 << CatName;
968 Diag(CatIDecl->getImplementation()->getLocation(),
969 diag::note_previous_definition);
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000970 CDecl->setInvalidDecl();
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +0000971 } else {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000972 CatIDecl->setImplementation(CDecl);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +0000973 // Warn on implementating category of deprecated class under
974 // -Wdeprecated-implementations flag.
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000975 DiagnoseObjCImplementedDeprecations(*this,
976 dyn_cast<NamedDecl>(IDecl),
977 CDecl->getLocation(), 2);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +0000978 }
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000979 }
Mike Stump11289f42009-09-09 15:08:12 +0000980
Anders Carlssona6b508a2008-11-04 16:57:32 +0000981 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000982 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000983}
984
John McCall48871652010-08-21 09:40:31 +0000985Decl *Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000986 SourceLocation AtClassImplLoc,
987 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +0000988 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000989 SourceLocation SuperClassLoc) {
Richard Smithf9b15102013-08-17 00:46:16 +0000990 ObjCInterfaceDecl *IDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000991 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +0000992 NamedDecl *PrevDecl
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000993 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
994 ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000995 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000996 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000997 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor1c283312010-08-11 12:19:30 +0000998 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000999 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
1000 diag::warn_undef_interface);
Douglas Gregor40f7a002010-01-04 17:27:12 +00001001 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001002 // We did not find anything with the name ClassName; try to correct for
Douglas Gregor40f7a002010-01-04 17:27:12 +00001003 // typos in the class name.
Kaelyn Uhraine31b8882012-01-13 01:32:50 +00001004 ObjCInterfaceValidatorCCC Validator;
Richard Smithf9b15102013-08-17 00:46:16 +00001005 TypoCorrection Corrected =
1006 CorrectTypo(DeclarationNameInfo(ClassName, ClassLoc),
1007 LookupOrdinaryName, TUScope, NULL, Validator);
1008 if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) {
1009 // Suggest the (potentially) correct interface name. Don't provide a
1010 // code-modification hint or use the typo name for recovery, because
1011 // this is just a warning. The program may actually be correct.
1012 diagnoseTypo(Corrected,
1013 PDiag(diag::warn_undef_interface_suggest) << ClassName,
1014 /*ErrorRecovery*/false);
Douglas Gregor40f7a002010-01-04 17:27:12 +00001015 } else {
1016 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
1017 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001018 }
Mike Stump11289f42009-09-09 15:08:12 +00001019
Chris Lattnerda463fe2007-12-12 07:09:47 +00001020 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001021 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001022 if (SuperClassname) {
1023 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001024 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
1025 LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001026 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001027 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1028 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +00001029 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001030 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001031 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidis3b60cff2012-03-13 01:09:36 +00001032 if (SDecl && !SDecl->hasDefinition())
1033 SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001034 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001035 Diag(SuperClassLoc, diag::err_undef_superclass)
1036 << SuperClassname << ClassName;
Douglas Gregor0b144e12011-12-15 00:29:59 +00001037 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001038 // This implementation and its interface do not have the same
1039 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001040 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001041 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001042 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001043 }
1044 }
1045 }
Mike Stump11289f42009-09-09 15:08:12 +00001046
Chris Lattnerda463fe2007-12-12 07:09:47 +00001047 if (!IDecl) {
1048 // Legacy case of @implementation with no corresponding @interface.
1049 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +00001050
Mike Stump87c57ac2009-05-16 07:39:55 +00001051 // FIXME: Do we support attributes on the @implementation? If so we should
1052 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +00001053 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001054 ClassName, /*PrevDecl=*/0, ClassLoc,
1055 true);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001056 IDecl->startDefinition();
Douglas Gregor16408322011-12-15 22:34:59 +00001057 if (SDecl) {
1058 IDecl->setSuperClass(SDecl);
1059 IDecl->setSuperClassLoc(SuperClassLoc);
1060 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1061 } else {
1062 IDecl->setEndOfDefinitionLoc(ClassLoc);
1063 }
1064
Douglas Gregorac345a32009-04-24 00:16:12 +00001065 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor1c283312010-08-11 12:19:30 +00001066 } else {
1067 // Mark the interface as being completed, even if it was just as
1068 // @class ....;
1069 // declaration; the user cannot reopen it.
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001070 if (!IDecl->hasDefinition())
1071 IDecl->startDefinition();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001072 }
Mike Stump11289f42009-09-09 15:08:12 +00001073
1074 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001075 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001076 ClassLoc, AtClassImplLoc, SuperClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001077
Anders Carlssona6b508a2008-11-04 16:57:32 +00001078 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001079 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001080
Chris Lattnerda463fe2007-12-12 07:09:47 +00001081 // Check that there is no duplicate implementation of this class.
Douglas Gregor1c283312010-08-11 12:19:30 +00001082 if (IDecl->getImplementation()) {
1083 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001084 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +00001085 Diag(IDecl->getImplementation()->getLocation(),
1086 diag::note_previous_definition);
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +00001087 IMPDecl->setInvalidDecl();
Douglas Gregor1c283312010-08-11 12:19:30 +00001088 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001089 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +00001090 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +00001091 // Warn on implementating deprecated class under
1092 // -Wdeprecated-implementations flag.
Fariborz Jahaniand724a102011-02-15 17:49:58 +00001093 DiagnoseObjCImplementedDeprecations(*this,
1094 dyn_cast<NamedDecl>(IDecl),
1095 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001096 }
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001097 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001098}
1099
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001100Sema::DeclGroupPtrTy
1101Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1102 SmallVector<Decl *, 64> DeclsInGroup;
1103 DeclsInGroup.reserve(Decls.size() + 1);
1104
1105 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1106 Decl *Dcl = Decls[i];
1107 if (!Dcl)
1108 continue;
1109 if (Dcl->getDeclContext()->isFileContext())
1110 Dcl->setTopLevelDeclInObjCContainer();
1111 DeclsInGroup.push_back(Dcl);
1112 }
1113
1114 DeclsInGroup.push_back(ObjCImpDecl);
1115
Rafael Espindolaab417692013-07-09 12:05:01 +00001116 return BuildDeclaratorGroup(DeclsInGroup, false);
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001117}
1118
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001119void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1120 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001121 SourceLocation RBrace) {
1122 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001123 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001124 if (!IDecl)
1125 return;
James Dennett634962f2012-06-14 21:40:34 +00001126 /// Check case of non-existing \@interface decl.
1127 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattnerda463fe2007-12-12 07:09:47 +00001128 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +00001129 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor16408322011-12-15 22:34:59 +00001130 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian20912d62010-02-17 17:00:07 +00001131 // Add ivar's to class's DeclContext.
1132 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanianc0309cd2010-02-17 18:10:54 +00001133 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith05afe5e2012-03-13 03:12:56 +00001134 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanianaef66222010-02-19 00:31:17 +00001135 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian20912d62010-02-17 17:00:07 +00001136 }
1137
Chris Lattnerda463fe2007-12-12 07:09:47 +00001138 return;
1139 }
1140 // If implementation has empty ivar list, just return.
1141 if (numIvars == 0)
1142 return;
Mike Stump11289f42009-09-09 15:08:12 +00001143
Chris Lattnerda463fe2007-12-12 07:09:47 +00001144 assert(ivars && "missing @implementation ivars");
John McCall5fb5df92012-06-20 06:18:46 +00001145 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001146 if (ImpDecl->getSuperClass())
1147 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1148 for (unsigned i = 0; i < numIvars; i++) {
1149 ObjCIvarDecl* ImplIvar = ivars[i];
1150 if (const ObjCIvarDecl *ClsIvar =
1151 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1152 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1153 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1154 continue;
1155 }
Fariborz Jahaniane23f26b2013-06-26 22:10:27 +00001156 // Check class extensions (unnamed categories) for duplicate ivars.
1157 for (ObjCInterfaceDecl::visible_extensions_iterator
1158 Ext = IDecl->visible_extensions_begin(),
1159 ExtEnd = IDecl->visible_extensions_end();
1160 Ext != ExtEnd; ++Ext) {
1161 ObjCCategoryDecl *CDecl = *Ext;
1162 if (const ObjCIvarDecl *ClsExtIvar =
1163 CDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1164 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1165 Diag(ClsExtIvar->getLocation(), diag::note_previous_definition);
1166 continue;
1167 }
1168 }
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001169 // Instance ivar to Implementation's DeclContext.
1170 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith05afe5e2012-03-13 03:12:56 +00001171 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001172 ImpDecl->addDecl(ImplIvar);
1173 }
1174 return;
1175 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001176 // Check interface's Ivar list against those in the implementation.
1177 // names and types must match.
1178 //
Chris Lattnerda463fe2007-12-12 07:09:47 +00001179 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001180 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +00001181 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1182 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001183 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie40ed2972012-06-06 20:45:41 +00001184 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001185 assert (ImplIvar && "missing implementation ivar");
1186 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +00001187
Steve Naroff157599f2009-03-03 14:49:36 +00001188 // First, make sure the types match.
Richard Smithcaf33902011-10-10 18:28:20 +00001189 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +00001190 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001191 << ImplIvar->getIdentifier()
1192 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +00001193 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smithcaf33902011-10-10 18:28:20 +00001194 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1195 ImplIvar->getBitWidthValue(Context) !=
1196 ClsIvar->getBitWidthValue(Context)) {
1197 Diag(ImplIvar->getBitWidth()->getLocStart(),
1198 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1199 Diag(ClsIvar->getBitWidth()->getLocStart(),
1200 diag::note_previous_definition);
Mike Stump11289f42009-09-09 15:08:12 +00001201 }
Steve Naroff157599f2009-03-03 14:49:36 +00001202 // Make sure the names are identical.
1203 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001204 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001205 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +00001206 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001207 }
1208 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001209 }
Mike Stump11289f42009-09-09 15:08:12 +00001210
Chris Lattner0f29d982007-12-12 18:11:49 +00001211 if (numIvars > 0)
Chris Lattner83021e92007-12-12 18:19:52 +00001212 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +00001213 else if (IVI != IVE)
David Blaikie2d7c57e2012-04-30 02:36:29 +00001214 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001215}
1216
Steve Naroff15833ed2008-02-10 21:38:56 +00001217void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001218 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian9fc39c42011-06-24 20:31:37 +00001219 // No point warning no definition of method which is 'unavailable'.
Douglas Gregorc2e3d5c2012-12-11 18:53:07 +00001220 switch (method->getAvailability()) {
1221 case AR_Available:
1222 case AR_Deprecated:
1223 break;
1224
1225 // Don't warn about unavailable or not-yet-introduced methods.
1226 case AR_NotYetIntroduced:
1227 case AR_Unavailable:
Fariborz Jahanian9fc39c42011-06-24 20:31:37 +00001228 return;
Douglas Gregorc2e3d5c2012-12-11 18:53:07 +00001229 }
1230
Ted Kremenek65d63572013-03-27 00:02:21 +00001231 // FIXME: For now ignore 'IncompleteImpl'.
1232 // Previously we grouped all unimplemented methods under a single
1233 // warning, but some users strongly voiced that they would prefer
1234 // separate warnings. We will give that approach a try, as that
1235 // matches what we do with protocols.
1236
1237 Diag(ImpLoc, DiagID) << method->getDeclName();
1238
1239 // Issue a note to the original declaration.
1240 SourceLocation MethodLoc = method->getLocStart();
1241 if (MethodLoc.isValid())
1242 Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff15833ed2008-02-10 21:38:56 +00001243}
1244
David Chisnallb62d15c2010-10-25 17:23:52 +00001245/// Determines if type B can be substituted for type A. Returns true if we can
1246/// guarantee that anything that the user will do to an object of type A can
1247/// also be done to an object of type B. This is trivially true if the two
1248/// types are the same, or if B is a subclass of A. It becomes more complex
1249/// in cases where protocols are involved.
1250///
1251/// Object types in Objective-C describe the minimum requirements for an
1252/// object, rather than providing a complete description of a type. For
1253/// example, if A is a subclass of B, then B* may refer to an instance of A.
1254/// The principle of substitutability means that we may use an instance of A
1255/// anywhere that we may use an instance of B - it will implement all of the
1256/// ivars of B and all of the methods of B.
1257///
1258/// This substitutability is important when type checking methods, because
1259/// the implementation may have stricter type definitions than the interface.
1260/// The interface specifies minimum requirements, but the implementation may
1261/// have more accurate ones. For example, a method may privately accept
1262/// instances of B, but only publish that it accepts instances of A. Any
1263/// object passed to it will be type checked against B, and so will implicitly
1264/// by a valid A*. Similarly, a method may return a subclass of the class that
1265/// it is declared as returning.
1266///
1267/// This is most important when considering subclassing. A method in a
1268/// subclass must accept any object as an argument that its superclass's
1269/// implementation accepts. It may, however, accept a more general type
1270/// without breaking substitutability (i.e. you can still use the subclass
1271/// anywhere that you can use the superclass, but not vice versa). The
1272/// converse requirement applies to return types: the return type for a
1273/// subclass method must be a valid object of the kind that the superclass
1274/// advertises, but it may be specified more accurately. This avoids the need
1275/// for explicit down-casting by callers.
1276///
1277/// Note: This is a stricter requirement than for assignment.
John McCall071df462010-10-28 02:34:38 +00001278static bool isObjCTypeSubstitutable(ASTContext &Context,
1279 const ObjCObjectPointerType *A,
1280 const ObjCObjectPointerType *B,
1281 bool rejectId) {
1282 // Reject a protocol-unqualified id.
1283 if (rejectId && B->isObjCIdType()) return false;
David Chisnallb62d15c2010-10-25 17:23:52 +00001284
1285 // If B is a qualified id, then A must also be a qualified id and it must
1286 // implement all of the protocols in B. It may not be a qualified class.
1287 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1288 // stricter definition so it is not substitutable for id<A>.
1289 if (B->isObjCQualifiedIdType()) {
1290 return A->isObjCQualifiedIdType() &&
John McCall071df462010-10-28 02:34:38 +00001291 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1292 QualType(B,0),
1293 false);
David Chisnallb62d15c2010-10-25 17:23:52 +00001294 }
1295
1296 /*
1297 // id is a special type that bypasses type checking completely. We want a
1298 // warning when it is used in one place but not another.
1299 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1300
1301
1302 // If B is a qualified id, then A must also be a qualified id (which it isn't
1303 // if we've got this far)
1304 if (B->isObjCQualifiedIdType()) return false;
1305 */
1306
1307 // Now we know that A and B are (potentially-qualified) class types. The
1308 // normal rules for assignment apply.
John McCall071df462010-10-28 02:34:38 +00001309 return Context.canAssignObjCInterfaces(A, B);
David Chisnallb62d15c2010-10-25 17:23:52 +00001310}
1311
John McCall071df462010-10-28 02:34:38 +00001312static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1313 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1314}
1315
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001316static bool CheckMethodOverrideReturn(Sema &S,
John McCall071df462010-10-28 02:34:38 +00001317 ObjCMethodDecl *MethodImpl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001318 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001319 bool IsProtocolMethodDecl,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001320 bool IsOverridingMode,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001321 bool Warn) {
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001322 if (IsProtocolMethodDecl &&
1323 (MethodDecl->getObjCDeclQualifier() !=
1324 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001325 if (Warn) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001326 S.Diag(MethodImpl->getLocation(),
1327 (IsOverridingMode ?
1328 diag::warn_conflicting_overriding_ret_type_modifiers
1329 : diag::warn_conflicting_ret_type_modifiers))
1330 << MethodImpl->getDeclName()
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001331 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1332 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1333 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1334 }
1335 else
1336 return false;
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001337 }
1338
John McCall071df462010-10-28 02:34:38 +00001339 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001340 MethodDecl->getResultType()))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001341 return true;
1342 if (!Warn)
1343 return false;
John McCall071df462010-10-28 02:34:38 +00001344
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001345 unsigned DiagID =
1346 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1347 : diag::warn_conflicting_ret_types;
John McCall071df462010-10-28 02:34:38 +00001348
1349 // Mismatches between ObjC pointers go into a different warning
1350 // category, and sometimes they're even completely whitelisted.
1351 if (const ObjCObjectPointerType *ImplPtrTy =
1352 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1353 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001354 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall071df462010-10-28 02:34:38 +00001355 // Allow non-matching return types as long as they don't violate
1356 // the principle of substitutability. Specifically, we permit
1357 // return types that are subclasses of the declared return type,
1358 // or that are more-qualified versions of the declared type.
1359 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001360 return false;
John McCall071df462010-10-28 02:34:38 +00001361
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001362 DiagID =
1363 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1364 : diag::warn_non_covariant_ret_types;
John McCall071df462010-10-28 02:34:38 +00001365 }
1366 }
1367
1368 S.Diag(MethodImpl->getLocation(), DiagID)
1369 << MethodImpl->getDeclName()
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001370 << MethodDecl->getResultType()
John McCall071df462010-10-28 02:34:38 +00001371 << MethodImpl->getResultType()
1372 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001373 S.Diag(MethodDecl->getLocation(),
1374 IsOverridingMode ? diag::note_previous_declaration
1375 : diag::note_previous_definition)
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001376 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001377 return false;
John McCall071df462010-10-28 02:34:38 +00001378}
1379
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001380static bool CheckMethodOverrideParam(Sema &S,
John McCall071df462010-10-28 02:34:38 +00001381 ObjCMethodDecl *MethodImpl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001382 ObjCMethodDecl *MethodDecl,
John McCall071df462010-10-28 02:34:38 +00001383 ParmVarDecl *ImplVar,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001384 ParmVarDecl *IfaceVar,
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001385 bool IsProtocolMethodDecl,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001386 bool IsOverridingMode,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001387 bool Warn) {
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001388 if (IsProtocolMethodDecl &&
1389 (ImplVar->getObjCDeclQualifier() !=
1390 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001391 if (Warn) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001392 if (IsOverridingMode)
1393 S.Diag(ImplVar->getLocation(),
1394 diag::warn_conflicting_overriding_param_modifiers)
1395 << getTypeRange(ImplVar->getTypeSourceInfo())
1396 << MethodImpl->getDeclName();
1397 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001398 diag::warn_conflicting_param_modifiers)
1399 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001400 << MethodImpl->getDeclName();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001401 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1402 << getTypeRange(IfaceVar->getTypeSourceInfo());
1403 }
1404 else
1405 return false;
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001406 }
1407
John McCall071df462010-10-28 02:34:38 +00001408 QualType ImplTy = ImplVar->getType();
1409 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001410
John McCall071df462010-10-28 02:34:38 +00001411 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001412 return true;
1413
1414 if (!Warn)
1415 return false;
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001416 unsigned DiagID =
1417 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1418 : diag::warn_conflicting_param_types;
John McCall071df462010-10-28 02:34:38 +00001419
1420 // Mismatches between ObjC pointers go into a different warning
1421 // category, and sometimes they're even completely whitelisted.
1422 if (const ObjCObjectPointerType *ImplPtrTy =
1423 ImplTy->getAs<ObjCObjectPointerType>()) {
1424 if (const ObjCObjectPointerType *IfacePtrTy =
1425 IfaceTy->getAs<ObjCObjectPointerType>()) {
1426 // Allow non-matching argument types as long as they don't
1427 // violate the principle of substitutability. Specifically, the
1428 // implementation must accept any objects that the superclass
1429 // accepts, however it may also accept others.
1430 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001431 return false;
John McCall071df462010-10-28 02:34:38 +00001432
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001433 DiagID =
1434 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1435 : diag::warn_non_contravariant_param_types;
John McCall071df462010-10-28 02:34:38 +00001436 }
1437 }
1438
1439 S.Diag(ImplVar->getLocation(), DiagID)
1440 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001441 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1442 S.Diag(IfaceVar->getLocation(),
1443 (IsOverridingMode ? diag::note_previous_declaration
1444 : diag::note_previous_definition))
John McCall071df462010-10-28 02:34:38 +00001445 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001446 return false;
John McCall071df462010-10-28 02:34:38 +00001447}
John McCall31168b02011-06-15 23:02:42 +00001448
1449/// In ARC, check whether the conventional meanings of the two methods
1450/// match. If they don't, it's a hard error.
1451static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1452 ObjCMethodDecl *decl) {
1453 ObjCMethodFamily implFamily = impl->getMethodFamily();
1454 ObjCMethodFamily declFamily = decl->getMethodFamily();
1455 if (implFamily == declFamily) return false;
1456
1457 // Since conventions are sorted by selector, the only possibility is
1458 // that the types differ enough to cause one selector or the other
1459 // to fall out of the family.
1460 assert(implFamily == OMF_None || declFamily == OMF_None);
1461
1462 // No further diagnostics required on invalid declarations.
1463 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1464
1465 const ObjCMethodDecl *unmatched = impl;
1466 ObjCMethodFamily family = declFamily;
1467 unsigned errorID = diag::err_arc_lost_method_convention;
1468 unsigned noteID = diag::note_arc_lost_method_convention;
1469 if (declFamily == OMF_None) {
1470 unmatched = decl;
1471 family = implFamily;
1472 errorID = diag::err_arc_gained_method_convention;
1473 noteID = diag::note_arc_gained_method_convention;
1474 }
1475
1476 // Indexes into a %select clause in the diagnostic.
1477 enum FamilySelector {
1478 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1479 };
1480 FamilySelector familySelector = FamilySelector();
1481
1482 switch (family) {
1483 case OMF_None: llvm_unreachable("logic error, no method convention");
1484 case OMF_retain:
1485 case OMF_release:
1486 case OMF_autorelease:
1487 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00001488 case OMF_finalize:
John McCall31168b02011-06-15 23:02:42 +00001489 case OMF_retainCount:
1490 case OMF_self:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001491 case OMF_performSelector:
John McCall31168b02011-06-15 23:02:42 +00001492 // Mismatches for these methods don't change ownership
1493 // conventions, so we don't care.
1494 return false;
1495
1496 case OMF_init: familySelector = F_init; break;
1497 case OMF_alloc: familySelector = F_alloc; break;
1498 case OMF_copy: familySelector = F_copy; break;
1499 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1500 case OMF_new: familySelector = F_new; break;
1501 }
1502
1503 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1504 ReasonSelector reasonSelector;
1505
1506 // The only reason these methods don't fall within their families is
1507 // due to unusual result types.
1508 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1509 reasonSelector = R_UnrelatedReturn;
1510 } else {
1511 reasonSelector = R_NonObjectReturn;
1512 }
1513
Joerg Sonnenbergerffc6d492013-06-26 21:31:47 +00001514 S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
1515 S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
John McCall31168b02011-06-15 23:02:42 +00001516
1517 return true;
1518}
John McCall071df462010-10-28 02:34:38 +00001519
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +00001520void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001521 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001522 bool IsProtocolMethodDecl) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001523 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00001524 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1525 return;
1526
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001527 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001528 IsProtocolMethodDecl, false,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001529 true);
Mike Stump11289f42009-09-09 15:08:12 +00001530
Chris Lattner67f35b02009-04-11 19:58:42 +00001531 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001532 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1533 EF = MethodDecl->param_end();
1534 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001535 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001536 IsProtocolMethodDecl, false, true);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001537 }
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001538
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001539 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001540 Diag(ImpMethodDecl->getLocation(),
1541 diag::warn_conflicting_variadic);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001542 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001543 }
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001544}
1545
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001546void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1547 ObjCMethodDecl *Overridden,
1548 bool IsProtocolMethodDecl) {
1549
1550 CheckMethodOverrideReturn(*this, Method, Overridden,
1551 IsProtocolMethodDecl, true,
1552 true);
1553
1554 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001555 IF = Overridden->param_begin(), EM = Method->param_end(),
1556 EF = Overridden->param_end();
1557 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001558 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1559 IsProtocolMethodDecl, true, true);
1560 }
1561
1562 if (Method->isVariadic() != Overridden->isVariadic()) {
1563 Diag(Method->getLocation(),
1564 diag::warn_conflicting_overriding_variadic);
1565 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1566 }
1567}
1568
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001569/// WarnExactTypedMethods - This routine issues a warning if method
1570/// implementation declaration matches exactly that of its declaration.
1571void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1572 ObjCMethodDecl *MethodDecl,
1573 bool IsProtocolMethodDecl) {
1574 // don't issue warning when protocol method is optional because primary
1575 // class is not required to implement it and it is safe for protocol
1576 // to implement it.
1577 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1578 return;
1579 // don't issue warning when primary class's method is
1580 // depecated/unavailable.
1581 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1582 MethodDecl->hasAttr<DeprecatedAttr>())
1583 return;
1584
1585 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1586 IsProtocolMethodDecl, false, false);
1587 if (match)
1588 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001589 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1590 EF = MethodDecl->param_end();
1591 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001592 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1593 *IM, *IF,
1594 IsProtocolMethodDecl, false, false);
1595 if (!match)
1596 break;
1597 }
1598 if (match)
1599 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall92918512011-08-08 17:32:19 +00001600 if (match)
1601 match = !(MethodDecl->isClassMethod() &&
1602 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001603
1604 if (match) {
1605 Diag(ImpMethodDecl->getLocation(),
1606 diag::warn_category_method_impl_match);
Ted Kremenek59b10db2012-02-27 22:55:11 +00001607 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1608 << MethodDecl->getDeclName();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001609 }
1610}
1611
Mike Stump87c57ac2009-05-16 07:39:55 +00001612/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1613/// improve the efficiency of selector lookups and type checking by associating
1614/// with each protocol / interface / category the flattened instance tables. If
1615/// we used an immutable set to keep the table then it wouldn't add significant
1616/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001617
Steve Naroffa36992242008-02-08 22:06:17 +00001618/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +00001619/// Declared in protocol, and those referenced by it.
Steve Naroffa36992242008-02-08 22:06:17 +00001620void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1621 ObjCProtocolDecl *PDecl,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001622 bool& IncompleteImpl,
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001623 const SelectorSet &InsMap,
1624 const SelectorSet &ClsMap,
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +00001625 ObjCContainerDecl *CDecl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001626 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1627 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1628 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +00001629 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1630
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001631 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001632 ObjCInterfaceDecl *NSIDecl = 0;
John McCall5fb5df92012-06-20 06:18:46 +00001633 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump11289f42009-09-09 15:08:12 +00001634 // check to see if class implements forwardInvocation method and objects
1635 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001636 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +00001637 // Under such conditions, which means that every method possible is
1638 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001639 // found" warnings.
1640 // FIXME: Use a general GetUnarySelector method for this.
1641 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1642 Selector fISelector = Context.Selectors.getSelector(1, &II);
1643 if (InsMap.count(fISelector))
1644 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1645 // need be implemented in the implementation.
1646 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1647 }
Mike Stump11289f42009-09-09 15:08:12 +00001648
Fariborz Jahanianc41cf052013-01-07 19:21:03 +00001649 // If this is a forward protocol declaration, get its definition.
1650 if (!PDecl->isThisDeclarationADefinition() &&
1651 PDecl->getDefinition())
1652 PDecl = PDecl->getDefinition();
1653
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001654 // If a method lookup fails locally we still need to look and see if
1655 // the method was implemented by a base class or an inherited
1656 // protocol. This lookup is slow, but occurs rarely in correct code
1657 // and otherwise would terminate in a warning.
1658
Chris Lattnerda463fe2007-12-12 07:09:47 +00001659 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001660 if (!NSIDecl)
Mike Stump11289f42009-09-09 15:08:12 +00001661 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001662 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001663 ObjCMethodDecl *method = *I;
Mike Stump11289f42009-09-09 15:08:12 +00001664 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rosed01e83a2012-10-10 16:42:25 +00001665 !method->isPropertyAccessor() &&
1666 !InsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001667 (!Super || !Super->lookupMethod(method->getSelector(),
1668 true /* instance */,
1669 false /* shallowCategory */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001670 true /* followsSuper */,
1671 NULL /* category */,
1672 PDecl /* protocol */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001673 // If a method is not implemented in the category implementation but
1674 // has been declared in its primary class, superclass,
1675 // or in one of their protocols, no need to issue the warning.
1676 // This is because method will be implemented in the primary class
1677 // or one of its super class implementation.
1678
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001679 // Ugly, but necessary. Method declared in protcol might have
1680 // have been synthesized due to a property declared in the class which
1681 // uses the protocol.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001682 if (ObjCMethodDecl *MethodInClass =
Ted Kremenek00781502013-11-23 01:01:29 +00001683 IDecl->lookupMethod(method->getSelector(),
1684 true /* instance */,
1685 true /* shallowCategoryLookup */,
1686 false /* followSuper */))
Jordan Rosed01e83a2012-10-10 16:42:25 +00001687 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001688 continue;
1689 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1690 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1691 != DiagnosticsEngine::Ignored) {
1692 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001693 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1694 << PDecl->getDeclName();
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001695 }
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001696 }
1697 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001698 // check unimplemented class methods
Mike Stump11289f42009-09-09 15:08:12 +00001699 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001700 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregorbcced4e2009-04-09 21:40:53 +00001701 I != E; ++I) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001702 ObjCMethodDecl *method = *I;
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001703 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1704 !ClsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001705 (!Super || !Super->lookupMethod(method->getSelector(),
1706 false /* class method */,
1707 false /* shallowCategoryLookup */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001708 true /* followSuper */,
1709 NULL /* category */,
1710 PDecl /* protocol */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001711 // See above comment for instance method lookups.
Ted Kremenek00781502013-11-23 01:01:29 +00001712 if (C && IDecl->lookupMethod(method->getSelector(),
1713 false /* class */,
1714 true /* shallowCategoryLookup */,
1715 false /* followSuper */))
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001716 continue;
Ted Kremenek00781502013-11-23 01:01:29 +00001717
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001718 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikie9c902b52011-09-25 23:23:43 +00001719 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1720 DiagnosticsEngine::Ignored) {
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001721 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
1722 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1723 PDecl->getDeclName();
1724 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001725 }
Steve Naroff3ce37a62007-12-14 23:37:57 +00001726 }
Chris Lattner390d39a2008-07-21 21:32:27 +00001727 // Check on this protocols's referenced protocols, recursively.
1728 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1729 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001730 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001731}
1732
Fariborz Jahanianf9ae68a2011-07-16 00:08:33 +00001733/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001734/// or protocol against those declared in their implementations.
1735///
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001736void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1737 const SelectorSet &ClsMap,
1738 SelectorSet &InsMapSeen,
1739 SelectorSet &ClsMapSeen,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001740 ObjCImplDecl* IMPDecl,
1741 ObjCContainerDecl* CDecl,
1742 bool &IncompleteImpl,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001743 bool ImmediateClass,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001744 bool WarnCategoryMethodImpl) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001745 // Check and see if instance methods in class interface have been
1746 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001747 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1748 E = CDecl->instmeth_end(); I != E; ++I) {
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001749 if (!InsMapSeen.insert((*I)->getSelector()))
1750 continue;
Jordan Rosed01e83a2012-10-10 16:42:25 +00001751 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001752 !InsMap.count((*I)->getSelector())) {
1753 if (ImmediateClass)
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001754 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001755 diag::warn_undef_method_impl);
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001756 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +00001757 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001758 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001759 IMPDecl->getInstanceMethod((*I)->getSelector());
1760 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1761 "Expected to find the method through lookup as well");
1762 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001763 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001764 if (ImpMethodDecl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001765 if (!WarnCategoryMethodImpl)
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001766 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1767 isa<ObjCProtocolDecl>(CDecl));
Jordan Rosed01e83a2012-10-10 16:42:25 +00001768 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001769 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001770 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001771 }
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001772 }
1773 }
Mike Stump11289f42009-09-09 15:08:12 +00001774
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001775 // Check and see if class methods in class interface have been
1776 // implemented in the implementation class. If so, their types match.
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001777 for (ObjCInterfaceDecl::classmeth_iterator I = CDecl->classmeth_begin(),
1778 E = CDecl->classmeth_end();
1779 I != E; ++I) {
1780 if (!ClsMapSeen.insert((*I)->getSelector()))
1781 continue;
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001782 if (!ClsMap.count((*I)->getSelector())) {
1783 if (ImmediateClass)
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001784 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001785 diag::warn_undef_method_impl);
Mike Stump12b8ce12009-08-04 21:02:39 +00001786 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001787 ObjCMethodDecl *ImpMethodDecl =
1788 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001789 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1790 "Expected to find the method through lookup as well");
1791 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001792 if (!WarnCategoryMethodImpl)
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001793 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1794 isa<ObjCProtocolDecl>(CDecl));
1795 else
1796 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001797 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001798 }
1799 }
Fariborz Jahanian73853e52010-10-08 22:59:25 +00001800
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001801 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) {
1802 // Also, check for methods declared in protocols inherited by
1803 // this protocol.
1804 for (ObjCProtocolDecl::protocol_iterator
1805 PI = PD->protocol_begin(), E = PD->protocol_end(); PI != E; ++PI)
1806 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1807 IMPDecl, (*PI), IncompleteImpl, false,
1808 WarnCategoryMethodImpl);
1809 }
1810
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001811 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001812 // when checking that methods in implementation match their declaration,
1813 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1814 // extension; as well as those in categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001815 if (!WarnCategoryMethodImpl) {
1816 for (ObjCInterfaceDecl::visible_categories_iterator
1817 Cat = I->visible_categories_begin(),
1818 CatEnd = I->visible_categories_end();
1819 Cat != CatEnd; ++Cat) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001820 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001821 IMPDecl, *Cat, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001822 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001823 }
1824 } else {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001825 // Also methods in class extensions need be looked at next.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001826 for (ObjCInterfaceDecl::visible_extensions_iterator
1827 Ext = I->visible_extensions_begin(),
1828 ExtEnd = I->visible_extensions_end();
1829 Ext != ExtEnd; ++Ext) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001830 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001831 IMPDecl, *Ext, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001832 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001833 }
1834 }
1835
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001836 // Check for any implementation of a methods declared in protocol.
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001837 for (ObjCInterfaceDecl::all_protocol_iterator
1838 PI = I->all_referenced_protocol_begin(),
1839 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001840 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1841 IMPDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001842 (*PI), IncompleteImpl, false,
1843 WarnCategoryMethodImpl);
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001844
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001845 // FIXME. For now, we are not checking for extact match of methods
1846 // in category implementation and its primary class's super class.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001847 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001848 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +00001849 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001850 I->getSuperClass(), IncompleteImpl, false);
1851 }
1852}
1853
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001854/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1855/// category matches with those implemented in its primary class and
1856/// warns each time an exact match is found.
1857void Sema::CheckCategoryVsClassMethodMatches(
1858 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001859 SelectorSet InsMap, ClsMap;
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001860
1861 for (ObjCImplementationDecl::instmeth_iterator
1862 I = CatIMPDecl->instmeth_begin(),
1863 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1864 InsMap.insert((*I)->getSelector());
1865
1866 for (ObjCImplementationDecl::classmeth_iterator
1867 I = CatIMPDecl->classmeth_begin(),
1868 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1869 ClsMap.insert((*I)->getSelector());
1870 if (InsMap.empty() && ClsMap.empty())
1871 return;
1872
1873 // Get category's primary class.
1874 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1875 if (!CatDecl)
1876 return;
1877 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1878 if (!IDecl)
1879 return;
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001880 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001881 bool IncompleteImpl = false;
1882 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1883 CatIMPDecl, IDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001884 IncompleteImpl, false,
1885 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001886}
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001887
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001888void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump11289f42009-09-09 15:08:12 +00001889 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001890 bool IncompleteImpl) {
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001891 SelectorSet InsMap;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001892 // Check and see if instance methods in class interface have been
1893 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001894 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001895 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001896 InsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001897
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001898 // Check and see if properties declared in the interface have either 1)
1899 // an implementation or 2) there is a @synthesize/@dynamic implementation
1900 // of the property in the @implementation.
Fariborz Jahanian3c9707b2012-01-03 19:46:00 +00001901 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall5fb5df92012-06-20 06:18:46 +00001902 if (!(LangOpts.ObjCDefaultSynthProperties &&
1903 LangOpts.ObjCRuntime.isNonFragile()) ||
1904 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001905 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian98609b32010-01-20 01:51:55 +00001906
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001907 SelectorSet ClsMap;
Mike Stump11289f42009-09-09 15:08:12 +00001908 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001909 I = IMPDecl->classmeth_begin(),
1910 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner061227a2007-12-12 17:58:05 +00001911 ClsMap.insert((*I)->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001912
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001913 // Check for type conflict of methods declared in a class/protocol and
1914 // its implementation; if any.
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001915 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +00001916 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1917 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001918 IncompleteImpl, true);
Fariborz Jahanian2bda1b62011-08-03 18:21:12 +00001919
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001920 // check all methods implemented in category against those declared
1921 // in its primary class.
1922 if (ObjCCategoryImplDecl *CatDecl =
1923 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1924 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001925
Chris Lattnerda463fe2007-12-12 07:09:47 +00001926 // Check the protocol list for unimplemented methods in the @implementation
1927 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001928 // Check and see if class methods in class interface have been
1929 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001930
Chris Lattner9ef10f42009-03-01 00:56:52 +00001931 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek0ef508d2010-09-01 01:21:15 +00001932 for (ObjCInterfaceDecl::all_protocol_iterator
1933 PI = I->all_referenced_protocol_begin(),
1934 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump11289f42009-09-09 15:08:12 +00001935 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001936 InsMap, ClsMap, I);
1937 // Check class extensions (unnamed categories)
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001938 for (ObjCInterfaceDecl::visible_extensions_iterator
1939 Ext = I->visible_extensions_begin(),
1940 ExtEnd = I->visible_extensions_end();
1941 Ext != ExtEnd; ++Ext) {
1942 ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
1943 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001944 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001945 // For extended class, unimplemented methods in its protocols will
1946 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +00001947 if (!C->IsClassExtension()) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001948 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1949 E = C->protocol_end(); PI != E; ++PI)
1950 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +00001951 InsMap, ClsMap, CDecl);
Fariborz Jahanianeb3f1002013-04-24 17:06:38 +00001952 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00001953 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00001954 } else
David Blaikie83d382b2011-09-23 05:06:16 +00001955 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00001956}
1957
Mike Stump11289f42009-09-09 15:08:12 +00001958/// ActOnForwardClassDeclaration -
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00001959Sema::DeclGroupPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00001960Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00001961 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00001962 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00001963 unsigned NumElts) {
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00001964 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001965 for (unsigned i = 0; i != NumElts; ++i) {
1966 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001967 NamedDecl *PrevDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001968 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001969 LookupOrdinaryName, ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001970 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00001971 // GCC apparently allows the following idiom:
1972 //
1973 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1974 // @class XCElementToggler;
1975 //
Fariborz Jahanian04c44552012-01-24 00:40:15 +00001976 // Here we have chosen to ignore the forward class declaration
1977 // with a warning. Since this is the implied behavior.
Richard Smithdda56e42011-04-15 14:24:37 +00001978 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCall8b07ec22010-05-15 11:32:37 +00001979 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001980 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00001981 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCall8b07ec22010-05-15 11:32:37 +00001982 } else {
Mike Stump12b8ce12009-08-04 21:02:39 +00001983 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahanian04c44552012-01-24 00:40:15 +00001984 // to the underlying class. Just ignore the forward class with a warning
1985 // as this will force the intended behavior which is to lookup the typedef
1986 // name.
1987 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1988 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1989 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1990 continue;
1991 }
Fariborz Jahanian0d451812009-05-07 21:49:26 +00001992 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001993 }
Douglas Gregordc9166c2011-12-15 20:29:51 +00001994
1995 // Create a declaration to describe this forward declaration.
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001996 ObjCInterfaceDecl *PrevIDecl
1997 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00001998
1999 IdentifierInfo *ClassName = IdentList[i];
2000 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
2001 // A previous decl with a different name is because of
2002 // @compatibility_alias, for example:
2003 // \code
2004 // @class NewImage;
2005 // @compatibility_alias OldImage NewImage;
2006 // \endcode
2007 // A lookup for 'OldImage' will return the 'NewImage' decl.
2008 //
2009 // In such a case use the real declaration name, instead of the alias one,
2010 // otherwise we will break IdentifierResolver and redecls-chain invariants.
2011 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
2012 // has been aliased.
2013 ClassName = PrevIDecl->getIdentifier();
2014 }
2015
Douglas Gregordc9166c2011-12-15 20:29:51 +00002016 ObjCInterfaceDecl *IDecl
2017 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002018 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002019 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002020
Douglas Gregordc9166c2011-12-15 20:29:51 +00002021 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeafd0b2011-12-27 22:43:10 +00002022 CheckObjCDeclScope(IDecl);
2023 DeclsInGroup.push_back(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002024 }
Rafael Espindolaab417692013-07-09 12:05:01 +00002025
2026 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002027}
2028
John McCall54507ab2011-06-16 01:15:19 +00002029static bool tryMatchRecordTypes(ASTContext &Context,
2030 Sema::MethodMatchStrategy strategy,
2031 const Type *left, const Type *right);
2032
John McCall31168b02011-06-15 23:02:42 +00002033static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
2034 QualType leftQT, QualType rightQT) {
2035 const Type *left =
2036 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2037 const Type *right =
2038 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2039
2040 if (left == right) return true;
2041
2042 // If we're doing a strict match, the types have to match exactly.
2043 if (strategy == Sema::MMS_strict) return false;
2044
2045 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2046
2047 // Otherwise, use this absurdly complicated algorithm to try to
2048 // validate the basic, low-level compatibility of the two types.
2049
2050 // As a minimum, require the sizes and alignments to match.
2051 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
2052 return false;
2053
2054 // Consider all the kinds of non-dependent canonical types:
2055 // - functions and arrays aren't possible as return and parameter types
2056
2057 // - vector types of equal size can be arbitrarily mixed
2058 if (isa<VectorType>(left)) return isa<VectorType>(right);
2059 if (isa<VectorType>(right)) return false;
2060
2061 // - references should only match references of identical type
John McCall54507ab2011-06-16 01:15:19 +00002062 // - structs, unions, and Objective-C objects must match more-or-less
2063 // exactly
John McCall31168b02011-06-15 23:02:42 +00002064 // - everything else should be a scalar
2065 if (!left->isScalarType() || !right->isScalarType())
John McCall54507ab2011-06-16 01:15:19 +00002066 return tryMatchRecordTypes(Context, strategy, left, right);
John McCall31168b02011-06-15 23:02:42 +00002067
John McCall9320b872011-09-09 05:25:32 +00002068 // Make scalars agree in kind, except count bools as chars, and group
2069 // all non-member pointers together.
John McCall31168b02011-06-15 23:02:42 +00002070 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2071 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2072 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2073 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall9320b872011-09-09 05:25:32 +00002074 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2075 leftSK = Type::STK_ObjCObjectPointer;
2076 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2077 rightSK = Type::STK_ObjCObjectPointer;
John McCall31168b02011-06-15 23:02:42 +00002078
2079 // Note that data member pointers and function member pointers don't
2080 // intermix because of the size differences.
2081
2082 return (leftSK == rightSK);
2083}
Chris Lattnerda463fe2007-12-12 07:09:47 +00002084
John McCall54507ab2011-06-16 01:15:19 +00002085static bool tryMatchRecordTypes(ASTContext &Context,
2086 Sema::MethodMatchStrategy strategy,
2087 const Type *lt, const Type *rt) {
2088 assert(lt && rt && lt != rt);
2089
2090 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2091 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2092 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2093
2094 // Require union-hood to match.
2095 if (left->isUnion() != right->isUnion()) return false;
2096
2097 // Require an exact match if either is non-POD.
2098 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2099 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2100 return false;
2101
2102 // Require size and alignment to match.
2103 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2104
2105 // Require fields to match.
2106 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2107 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2108 for (; li != le && ri != re; ++li, ++ri) {
2109 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2110 return false;
2111 }
2112 return (li == le && ri == re);
2113}
2114
Chris Lattnerda463fe2007-12-12 07:09:47 +00002115/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2116/// returns true, or false, accordingly.
2117/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCall31168b02011-06-15 23:02:42 +00002118bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2119 const ObjCMethodDecl *right,
2120 MethodMatchStrategy strategy) {
2121 if (!matchTypes(Context, strategy,
2122 left->getResultType(), right->getResultType()))
2123 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002124
Douglas Gregor560b7fa2013-02-07 19:13:24 +00002125 // If either is hidden, it is not considered to match.
2126 if (left->isHidden() || right->isHidden())
2127 return false;
2128
David Blaikiebbafb8a2012-03-11 07:00:24 +00002129 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002130 (left->hasAttr<NSReturnsRetainedAttr>()
2131 != right->hasAttr<NSReturnsRetainedAttr>() ||
2132 left->hasAttr<NSConsumesSelfAttr>()
2133 != right->hasAttr<NSConsumesSelfAttr>()))
2134 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002135
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002136 ObjCMethodDecl::param_const_iterator
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002137 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2138 re = right->param_end();
Mike Stump11289f42009-09-09 15:08:12 +00002139
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002140 for (; li != le && ri != re; ++li, ++ri) {
John McCall31168b02011-06-15 23:02:42 +00002141 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002142 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCall31168b02011-06-15 23:02:42 +00002143
2144 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2145 return false;
2146
David Blaikiebbafb8a2012-03-11 07:00:24 +00002147 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002148 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2149 return false;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002150 }
2151 return true;
2152}
2153
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002154void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002155 // Record at the head of the list whether there were 0, 1, or >= 2 methods
2156 // inside categories.
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00002157 if (ObjCCategoryDecl *
2158 CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
2159 if (!CD->IsClassExtension() && List->getBits() < 2)
2160 List->setBits(List->getBits()+1);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002161
Douglas Gregorc454afe2012-01-25 00:19:56 +00002162 // If the list is empty, make it a singleton list.
2163 if (List->Method == 0) {
2164 List->Method = Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002165 List->setNext(0);
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002166 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002167 }
2168
2169 // We've seen a method with this name, see if we have already seen this type
2170 // signature.
2171 ObjCMethodList *Previous = List;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002172 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregor600a2f52013-06-21 00:20:25 +00002173 // If we are building a module, keep all of the methods.
2174 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2175 continue;
2176
Douglas Gregore1716012012-01-25 00:49:42 +00002177 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregorc454afe2012-01-25 00:19:56 +00002178 continue;
2179
2180 ObjCMethodDecl *PrevObjCMethod = List->Method;
2181
2182 // Propagate the 'defined' bit.
2183 if (Method->isDefined())
2184 PrevObjCMethod->setDefined(true);
2185
2186 // If a method is deprecated, push it in the global pool.
2187 // This is used for better diagnostics.
2188 if (Method->isDeprecated()) {
2189 if (!PrevObjCMethod->isDeprecated())
2190 List->Method = Method;
2191 }
2192 // If new method is unavailable, push it into global pool
2193 // unless previous one is deprecated.
2194 if (Method->isUnavailable()) {
2195 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2196 List->Method = Method;
2197 }
2198
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002199 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002200 }
2201
2202 // We have a new signature for an existing method - add it.
2203 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregore1716012012-01-25 00:49:42 +00002204 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002205 Previous->setNext(new (Mem) ObjCMethodList(Method, 0));
Douglas Gregorc454afe2012-01-25 00:19:56 +00002206}
2207
Sebastian Redl75d8a322010-08-02 23:18:59 +00002208/// \brief Read the contents of the method pool for a given selector from
2209/// external storage.
Douglas Gregore1716012012-01-25 00:49:42 +00002210void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002211 assert(ExternalSource && "We need an external AST source");
Douglas Gregore1716012012-01-25 00:49:42 +00002212 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002213}
2214
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002215void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002216 bool instance) {
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002217 // Ignore methods of invalid containers.
2218 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002219 return;
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002220
Douglas Gregor70f449b2012-01-25 00:59:09 +00002221 if (ExternalSource)
2222 ReadMethodPool(Method->getSelector());
2223
Sebastian Redl75d8a322010-08-02 23:18:59 +00002224 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor70f449b2012-01-25 00:59:09 +00002225 if (Pos == MethodPool.end())
2226 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2227 GlobalMethods())).first;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002228
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002229 Method->setDefined(impl);
Douglas Gregorc454afe2012-01-25 00:19:56 +00002230
Sebastian Redl75d8a322010-08-02 23:18:59 +00002231 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002232 addMethodToGlobalList(&Entry, Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002233}
2234
John McCall31168b02011-06-15 23:02:42 +00002235/// Determines if this is an "acceptable" loose mismatch in the global
2236/// method pool. This exists mostly as a hack to get around certain
2237/// global mismatches which we can't afford to make warnings / errors.
2238/// Really, what we want is a way to take a method out of the global
2239/// method pool.
2240static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2241 ObjCMethodDecl *other) {
2242 if (!chosen->isInstanceMethod())
2243 return false;
2244
2245 Selector sel = chosen->getSelector();
2246 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2247 return false;
2248
2249 // Don't complain about mismatches for -length if the method we
2250 // chose has an integral result type.
2251 return (chosen->getResultType()->isIntegerType());
2252}
2253
Sebastian Redl75d8a322010-08-02 23:18:59 +00002254ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00002255 bool receiverIdOrClass,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002256 bool warn, bool instance) {
Douglas Gregor70f449b2012-01-25 00:59:09 +00002257 if (ExternalSource)
2258 ReadMethodPool(Sel);
2259
Sebastian Redl75d8a322010-08-02 23:18:59 +00002260 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor70f449b2012-01-25 00:59:09 +00002261 if (Pos == MethodPool.end())
2262 return 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002263
Douglas Gregor77f49a42013-01-16 18:47:38 +00002264 // Gather the non-hidden methods.
Sebastian Redl75d8a322010-08-02 23:18:59 +00002265 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Robert Wilhelmb869a8f2013-08-10 12:33:24 +00002266 SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002267 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Douglas Gregor77f49a42013-01-16 18:47:38 +00002268 if (M->Method && !M->Method->isHidden()) {
2269 // If we're not supposed to warn about mismatches, we're done.
2270 if (!warn)
2271 return M->Method;
Mike Stump11289f42009-09-09 15:08:12 +00002272
Douglas Gregor77f49a42013-01-16 18:47:38 +00002273 Methods.push_back(M->Method);
Sebastian Redl75d8a322010-08-02 23:18:59 +00002274 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00002275 }
Douglas Gregor77f49a42013-01-16 18:47:38 +00002276
2277 // If there aren't any visible methods, we're done.
2278 // FIXME: Recover if there are any known-but-hidden methods?
2279 if (Methods.empty())
2280 return 0;
2281
2282 if (Methods.size() == 1)
2283 return Methods[0];
2284
2285 // We found multiple methods, so we may have to complain.
2286 bool issueDiagnostic = false, issueError = false;
2287
2288 // We support a warning which complains about *any* difference in
2289 // method signature.
2290 bool strictSelectorMatch =
2291 (receiverIdOrClass && warn &&
2292 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2293 R.getBegin())
2294 != DiagnosticsEngine::Ignored));
2295 if (strictSelectorMatch) {
2296 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2297 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2298 issueDiagnostic = true;
2299 break;
2300 }
2301 }
2302 }
2303
2304 // If we didn't see any strict differences, we won't see any loose
2305 // differences. In ARC, however, we also need to check for loose
2306 // mismatches, because most of them are errors.
2307 if (!strictSelectorMatch ||
2308 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2309 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2310 // This checks if the methods differ in type mismatch.
2311 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2312 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2313 issueDiagnostic = true;
2314 if (getLangOpts().ObjCAutoRefCount)
2315 issueError = true;
2316 break;
2317 }
2318 }
2319
2320 if (issueDiagnostic) {
2321 if (issueError)
2322 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2323 else if (strictSelectorMatch)
2324 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2325 else
2326 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2327
2328 Diag(Methods[0]->getLocStart(),
2329 issueError ? diag::note_possibility : diag::note_using)
2330 << Methods[0]->getSourceRange();
2331 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2332 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2333 << Methods[I]->getSourceRange();
2334 }
2335 }
2336 return Methods[0];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002337}
2338
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002339ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redl75d8a322010-08-02 23:18:59 +00002340 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2341 if (Pos == MethodPool.end())
2342 return 0;
2343
2344 GlobalMethods &Methods = Pos->second;
2345
2346 if (Methods.first.Method && Methods.first.Method->isDefined())
2347 return Methods.first.Method;
2348 if (Methods.second.Method && Methods.second.Method->isDefined())
2349 return Methods.second.Method;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002350 return 0;
2351}
2352
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002353static void
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002354HelperSelectorsForTypoCorrection(
2355 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2356 StringRef Typo, const ObjCMethodDecl * Method) {
2357 const unsigned MaxEditDistance = 1;
2358 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieuea8d3702013-06-06 02:22:29 +00002359 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002360
2361 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2362 if (MinPossibleEditDistance > 0 &&
2363 Typo.size() / MinPossibleEditDistance < 1)
2364 return;
2365 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2366 if (EditDistance > MaxEditDistance)
2367 return;
2368 if (EditDistance == BestEditDistance)
2369 BestMethod.push_back(Method);
2370 else if (EditDistance < BestEditDistance) {
2371 BestMethod.clear();
2372 BestMethod.push_back(Method);
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002373 }
2374}
2375
Fariborz Jahanian75481672013-06-17 17:10:54 +00002376static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2377 QualType ObjectType) {
2378 if (ObjectType.isNull())
2379 return true;
2380 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2381 return true;
2382 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != 0;
2383}
2384
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002385const ObjCMethodDecl *
Fariborz Jahanian75481672013-06-17 17:10:54 +00002386Sema::SelectorsForTypoCorrection(Selector Sel,
2387 QualType ObjectType) {
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002388 unsigned NumArgs = Sel.getNumArgs();
2389 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002390 bool ObjectIsId = true, ObjectIsClass = true;
2391 if (ObjectType.isNull())
2392 ObjectIsId = ObjectIsClass = false;
2393 else if (!ObjectType->isObjCObjectPointerType())
2394 return 0;
2395 else if (const ObjCObjectPointerType *ObjCPtr =
2396 ObjectType->getAsObjCInterfacePointerType()) {
2397 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2398 ObjectIsId = ObjectIsClass = false;
2399 }
2400 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2401 ObjectIsClass = false;
2402 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2403 ObjectIsId = false;
2404 else
2405 return 0;
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002406
2407 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2408 e = MethodPool.end(); b != e; b++) {
2409 // instance methods
2410 for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
2411 if (M->Method &&
Fariborz Jahanian06499232013-06-18 17:10:58 +00002412 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2413 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002414 if (ObjectIsId)
2415 Methods.push_back(M->Method);
2416 else if (!ObjectIsClass &&
2417 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2418 Methods.push_back(M->Method);
2419 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002420 // class methods
2421 for (ObjCMethodList *M = &b->second.second; 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 (ObjectIsClass)
2426 Methods.push_back(M->Method);
2427 else if (!ObjectIsId &&
2428 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2429 Methods.push_back(M->Method);
2430 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002431 }
2432
2433 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2434 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2435 HelperSelectorsForTypoCorrection(SelectedMethods,
2436 Sel.getAsString(), Methods[i]);
2437 }
2438 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : NULL;
2439}
2440
2441static void
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002442HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S,
2443 ObjCMethodList &MethList) {
2444 ObjCMethodList *M = &MethList;
2445 ObjCMethodDecl *TargetMethod = M->Method;
2446 while (TargetMethod &&
2447 isa<ObjCImplDecl>(TargetMethod->getDeclContext())) {
2448 M = M->getNext();
2449 TargetMethod = M ? M->Method : 0;
2450 }
2451 if (!TargetMethod)
2452 return;
2453 bool FirstTime = true;
2454 for (M = M->getNext(); M; M=M->getNext()) {
2455 ObjCMethodDecl *MatchingMethodDecl = M->Method;
2456 if (isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext()))
2457 continue;
2458 if (!S.MatchTwoMethodDeclarations(TargetMethod,
2459 MatchingMethodDecl, Sema::MMS_loose)) {
2460 if (FirstTime) {
2461 FirstTime = false;
2462 S.Diag(TargetMethod->getLocation(), diag::warning_multiple_selectors)
2463 << TargetMethod->getSelector();
2464 }
2465 S.Diag(MatchingMethodDecl->getLocation(), diag::note_also_found);
2466 }
2467 }
2468}
2469
2470void Sema::DiagnoseMismatchedMethodsInGlobalPool() {
2471 unsigned DIAG = diag::warning_multiple_selectors;
2472 if (Diags.getDiagnosticLevel(DIAG, SourceLocation())
2473 == DiagnosticsEngine::Ignored)
2474 return;
2475 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2476 e = MethodPool.end(); b != e; b++) {
2477 // first, instance methods
2478 ObjCMethodList &InstMethList = b->second.first;
2479 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, InstMethList);
Fariborz Jahanianc07e8932013-05-30 21:52:50 +00002480 // second, class methods
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002481 ObjCMethodList &ClsMethList = b->second.second;
2482 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, ClsMethList);
2483 }
2484}
2485
2486/// DiagnoseDuplicateIvars -
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002487/// Check for duplicate ivars in the entire class at the start of
James Dennett634962f2012-06-14 21:40:34 +00002488/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002489/// add ivars to a class in random order which will not be known until
James Dennett634962f2012-06-14 21:40:34 +00002490/// class's \@implementation is seen.
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002491void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2492 ObjCInterfaceDecl *SID) {
2493 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2494 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie40ed2972012-06-06 20:45:41 +00002495 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002496 if (Ivar->isInvalidDecl())
2497 continue;
2498 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2499 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2500 if (prevIvar) {
2501 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2502 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2503 Ivar->setInvalidDecl();
2504 }
2505 }
2506 }
2507}
2508
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002509Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2510 switch (CurContext->getDeclKind()) {
2511 case Decl::ObjCInterface:
2512 return Sema::OCK_Interface;
2513 case Decl::ObjCProtocol:
2514 return Sema::OCK_Protocol;
2515 case Decl::ObjCCategory:
2516 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2517 return Sema::OCK_ClassExtension;
2518 else
2519 return Sema::OCK_Category;
2520 case Decl::ObjCImplementation:
2521 return Sema::OCK_Implementation;
2522 case Decl::ObjCCategoryImpl:
2523 return Sema::OCK_CategoryImplementation;
2524
2525 default:
2526 return Sema::OCK_None;
2527 }
2528}
2529
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002530// Note: For class/category implementations, allMethods is always null.
Robert Wilhelm57c67112013-07-17 21:14:35 +00002531Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods,
Fariborz Jahaniandfb76872013-07-17 00:05:08 +00002532 ArrayRef<DeclGroupPtrTy> allTUVars) {
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002533 if (getObjCContainerKind() == Sema::OCK_None)
2534 return 0;
2535
2536 assert(AtEnd.isValid() && "Invalid location for '@end'");
2537
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002538 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2539 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002540
Mike Stump11289f42009-09-09 15:08:12 +00002541 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00002542 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2543 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002544 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00002545
Steve Naroff35c62ae2009-01-08 17:28:14 +00002546 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2547 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2548 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2549
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002550 for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002551 ObjCMethodDecl *Method =
John McCall48871652010-08-21 09:40:31 +00002552 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002553
2554 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00002555 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002556 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002557 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002558 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002559 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002560 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002561 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002562 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002563 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002564 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002565 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002566 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002567 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002568 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002569 if (!Context.getSourceManager().isInSystemHeader(
2570 Method->getLocation()))
2571 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2572 << Method->getDeclName();
2573 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2574 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002575 InsMap[Method->getSelector()] = Method;
2576 /// The following allows us to typecheck messages to "id".
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002577 AddInstanceMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002578 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002579 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002580 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002581 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002582 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002583 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002584 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002585 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002586 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002587 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002588 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002589 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002590 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002591 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002592 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002593 if (!Context.getSourceManager().isInSystemHeader(
2594 Method->getLocation()))
2595 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2596 << Method->getDeclName();
2597 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2598 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002599 ClsMap[Method->getSelector()] = Method;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002600 AddFactoryMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002601 }
2602 }
2603 }
Douglas Gregorb8982092013-01-21 19:42:21 +00002604 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2605 // Nothing to do here.
Steve Naroffb3a87982009-01-09 15:36:25 +00002606 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002607 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00002608 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002609 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00002610
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002611 if (C->IsClassExtension()) {
2612 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2613 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002614 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002615 }
Steve Naroffb3a87982009-01-09 15:36:25 +00002616 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002617 if (CDecl->getIdentifier())
2618 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2619 // user-defined setter/getter. It also synthesizes setter/getter methods
2620 // and adds them to the DeclContext and global method pools.
2621 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2622 E = CDecl->prop_end();
2623 I != E; ++I)
David Blaikie40ed2972012-06-06 20:45:41 +00002624 ProcessPropertyDecl(*I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00002625 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00002626 }
2627 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002628 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002629 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002630 // Any property declared in a class extension might have user
2631 // declared setter or getter in current class extension or one
2632 // of the other class extensions. Mark them as synthesized as
2633 // property will be synthesized when property with same name is
2634 // seen in the @implementation.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002635 for (ObjCInterfaceDecl::visible_extensions_iterator
2636 Ext = IDecl->visible_extensions_begin(),
2637 ExtEnd = IDecl->visible_extensions_end();
2638 Ext != ExtEnd; ++Ext) {
2639 for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
2640 E = Ext->prop_end(); I != E; ++I) {
David Blaikie40ed2972012-06-06 20:45:41 +00002641 ObjCPropertyDecl *Property = *I;
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002642 // Skip over properties declared @dynamic
2643 if (const ObjCPropertyImplDecl *PIDecl
2644 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2645 if (PIDecl->getPropertyImplementation()
2646 == ObjCPropertyImplDecl::Dynamic)
2647 continue;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002648
2649 for (ObjCInterfaceDecl::visible_extensions_iterator
2650 Ext = IDecl->visible_extensions_begin(),
2651 ExtEnd = IDecl->visible_extensions_end();
2652 Ext != ExtEnd; ++Ext) {
2653 if (ObjCMethodDecl *GetterMethod
2654 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002655 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002656 if (!Property->isReadOnly())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002657 if (ObjCMethodDecl *SetterMethod
2658 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002659 SetterMethod->setPropertyAccessor(true);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002660 }
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002661 }
2662 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00002663 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002664 AtomicPropertySetterGetterRules(IC, IDecl);
John McCall31168b02011-06-15 23:02:42 +00002665 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00002666
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002667 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2668 if (IDecl->getSuperClass() == NULL) {
2669 // This class has no superclass, so check that it has been marked with
2670 // __attribute((objc_root_class)).
2671 if (!HasRootClassAttr) {
2672 SourceLocation DeclLoc(IDecl->getLocation());
2673 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2674 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2675 << IDecl->getIdentifier();
2676 // See if NSObject is in the current scope, and if it is, suggest
2677 // adding " : NSObject " to the class declaration.
2678 NamedDecl *IF = LookupSingleName(TUScope,
2679 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2680 DeclLoc, LookupOrdinaryName);
2681 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2682 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2683 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2684 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2685 } else {
2686 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2687 }
2688 }
2689 } else if (HasRootClassAttr) {
2690 // Complain that only root classes may have this attribute.
2691 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2692 }
2693
John McCall5fb5df92012-06-20 06:18:46 +00002694 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002695 while (IDecl->getSuperClass()) {
2696 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2697 IDecl = IDecl->getSuperClass();
2698 }
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002699 }
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002700 }
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00002701 SetIvarInitializers(IC);
Mike Stump11289f42009-09-09 15:08:12 +00002702 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00002703 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002704 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00002705
Chris Lattnerda463fe2007-12-12 07:09:47 +00002706 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00002707 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00002708 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002709 if (ObjCCategoryDecl *Cat
2710 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2711 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002712 }
2713 }
2714 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002715 if (isInterfaceDeclKind) {
2716 // Reject invalid vardecls.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002717 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002718 DeclGroupRef DG = allTUVars[i].get();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002719 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2720 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00002721 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00002722 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00002723 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002724 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00002725 }
Fariborz Jahanian4327b322011-08-29 17:33:12 +00002726 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002727
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();
Argyrios Kyrtzidis8ad3bab2011-11-23 20:27:36 +00002730 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2731 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002732 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2733 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002734
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +00002735 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002736 return ClassDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002737}
2738
2739
2740/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2741/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00002742static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002743CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCallca872902011-05-01 03:04:29 +00002744 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002745}
2746
Douglas Gregor33823722011-06-11 01:09:30 +00002747/// \brief Check whether the declared result type of the given Objective-C
2748/// method declaration is compatible with the method's class.
2749///
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002750static Sema::ResultTypeCompatibilityKind
Douglas Gregor33823722011-06-11 01:09:30 +00002751CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2752 ObjCInterfaceDecl *CurrentClass) {
2753 QualType ResultType = Method->getResultType();
Douglas Gregor33823722011-06-11 01:09:30 +00002754
2755 // If an Objective-C method inherits its related result type, then its
2756 // declared result type must be compatible with its own class type. The
2757 // declared result type is compatible if:
2758 if (const ObjCObjectPointerType *ResultObjectType
2759 = ResultType->getAs<ObjCObjectPointerType>()) {
2760 // - it is id or qualified id, or
2761 if (ResultObjectType->isObjCIdType() ||
2762 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002763 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002764
2765 if (CurrentClass) {
2766 if (ObjCInterfaceDecl *ResultClass
2767 = ResultObjectType->getInterfaceDecl()) {
2768 // - it is the same as the method's class type, or
Douglas Gregor0b144e12011-12-15 00:29:59 +00002769 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002770 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002771
2772 // - it is a superclass of the method's class type
2773 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002774 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002775 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00002776 } else {
2777 // Any Objective-C pointer type might be acceptable for a protocol
2778 // method; we just don't know.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002779 return Sema::RTC_Unknown;
Douglas Gregor33823722011-06-11 01:09:30 +00002780 }
2781 }
2782
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002783 return Sema::RTC_Incompatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002784}
2785
John McCalld2930c22011-07-22 02:45:48 +00002786namespace {
2787/// A helper class for searching for methods which a particular method
2788/// overrides.
2789class OverrideSearch {
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002790public:
John McCalld2930c22011-07-22 02:45:48 +00002791 Sema &S;
2792 ObjCMethodDecl *Method;
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002793 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCalld2930c22011-07-22 02:45:48 +00002794 bool Recursive;
2795
2796public:
2797 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2798 Selector selector = method->getSelector();
2799
2800 // Bypass this search if we've never seen an instance/class method
2801 // with this selector before.
2802 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2803 if (it == S.MethodPool.end()) {
Axel Naumanndd433f02012-10-18 19:05:02 +00002804 if (!S.getExternalSource()) return;
Douglas Gregore1716012012-01-25 00:49:42 +00002805 S.ReadMethodPool(selector);
2806
2807 it = S.MethodPool.find(selector);
2808 if (it == S.MethodPool.end())
2809 return;
John McCalld2930c22011-07-22 02:45:48 +00002810 }
2811 ObjCMethodList &list =
2812 method->isInstanceMethod() ? it->second.first : it->second.second;
2813 if (!list.Method) return;
2814
2815 ObjCContainerDecl *container
2816 = cast<ObjCContainerDecl>(method->getDeclContext());
2817
2818 // Prevent the search from reaching this container again. This is
2819 // important with categories, which override methods from the
2820 // interface and each other.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002821 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2822 searchFromContainer(container);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002823 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2824 searchFromContainer(Interface);
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002825 } else {
2826 searchFromContainer(container);
2827 }
Douglas Gregor33823722011-06-11 01:09:30 +00002828 }
John McCalld2930c22011-07-22 02:45:48 +00002829
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002830 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCalld2930c22011-07-22 02:45:48 +00002831 iterator begin() const { return Overridden.begin(); }
2832 iterator end() const { return Overridden.end(); }
2833
2834private:
2835 void searchFromContainer(ObjCContainerDecl *container) {
2836 if (container->isInvalidDecl()) return;
2837
2838 switch (container->getDeclKind()) {
2839#define OBJCCONTAINER(type, base) \
2840 case Decl::type: \
2841 searchFrom(cast<type##Decl>(container)); \
2842 break;
2843#define ABSTRACT_DECL(expansion)
2844#define DECL(type, base) \
2845 case Decl::type:
2846#include "clang/AST/DeclNodes.inc"
2847 llvm_unreachable("not an ObjC container!");
2848 }
2849 }
2850
2851 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00002852 if (!protocol->hasDefinition())
2853 return;
2854
John McCalld2930c22011-07-22 02:45:48 +00002855 // A method in a protocol declaration overrides declarations from
2856 // referenced ("parent") protocols.
2857 search(protocol->getReferencedProtocols());
2858 }
2859
2860 void searchFrom(ObjCCategoryDecl *category) {
2861 // A method in a category declaration overrides declarations from
2862 // the main class and from protocols the category references.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002863 // The main class is handled in the constructor.
John McCalld2930c22011-07-22 02:45:48 +00002864 search(category->getReferencedProtocols());
2865 }
2866
2867 void searchFrom(ObjCCategoryImplDecl *impl) {
2868 // A method in a category definition that has a category
2869 // declaration overrides declarations from the category
2870 // declaration.
2871 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2872 search(category);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002873 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2874 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002875
2876 // Otherwise it overrides declarations from the class.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002877 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2878 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002879 }
2880 }
2881
2882 void searchFrom(ObjCInterfaceDecl *iface) {
2883 // A method in a class declaration overrides declarations from
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00002884 if (!iface->hasDefinition())
2885 return;
2886
John McCalld2930c22011-07-22 02:45:48 +00002887 // - categories,
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00002888 for (ObjCInterfaceDecl::known_categories_iterator
2889 cat = iface->known_categories_begin(),
2890 catEnd = iface->known_categories_end();
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002891 cat != catEnd; ++cat) {
2892 search(*cat);
2893 }
John McCalld2930c22011-07-22 02:45:48 +00002894
2895 // - the super class, and
2896 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2897 search(super);
2898
2899 // - any referenced protocols.
2900 search(iface->getReferencedProtocols());
2901 }
2902
2903 void searchFrom(ObjCImplementationDecl *impl) {
2904 // A method in a class implementation overrides declarations from
2905 // the class interface.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002906 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2907 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002908 }
2909
2910
2911 void search(const ObjCProtocolList &protocols) {
2912 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2913 i != e; ++i)
2914 search(*i);
2915 }
2916
2917 void search(ObjCContainerDecl *container) {
John McCalld2930c22011-07-22 02:45:48 +00002918 // Check for a method in this container which matches this selector.
2919 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00002920 Method->isInstanceMethod(),
2921 /*AllowHidden=*/true);
John McCalld2930c22011-07-22 02:45:48 +00002922
2923 // If we find one, record it and bail out.
2924 if (meth) {
2925 Overridden.insert(meth);
2926 return;
2927 }
2928
2929 // Otherwise, search for methods that a hypothetical method here
2930 // would have overridden.
2931
2932 // Note that we're now in a recursive case.
2933 Recursive = true;
2934
2935 searchFromContainer(container);
2936 }
2937};
Douglas Gregor33823722011-06-11 01:09:30 +00002938}
2939
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002940void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2941 ObjCInterfaceDecl *CurrentClass,
2942 ResultTypeCompatibilityKind RTC) {
2943 // Search for overridden methods and merge information down from them.
2944 OverrideSearch overrides(*this, ObjCMethod);
2945 // Keep track if the method overrides any method in the class's base classes,
2946 // its protocols, or its categories' protocols; we will keep that info
2947 // in the ObjCMethodDecl.
2948 // For this info, a method in an implementation is not considered as
2949 // overriding the same method in the interface or its categories.
2950 bool hasOverriddenMethodsInBaseOrProtocol = false;
2951 for (OverrideSearch::iterator
2952 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2953 ObjCMethodDecl *overridden = *i;
2954
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00002955 if (!hasOverriddenMethodsInBaseOrProtocol) {
2956 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2957 CurrentClass != overridden->getClassInterface() ||
2958 overridden->isOverriding()) {
2959 hasOverriddenMethodsInBaseOrProtocol = true;
2960
2961 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
2962 // OverrideSearch will return as "overridden" the same method in the
2963 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
2964 // check whether a category of a base class introduced a method with the
2965 // same selector, after the interface method declaration.
2966 // To avoid unnecessary lookups in the majority of cases, we use the
2967 // extra info bits in GlobalMethodPool to check whether there were any
2968 // category methods with this selector.
2969 GlobalMethodPool::iterator It =
2970 MethodPool.find(ObjCMethod->getSelector());
2971 if (It != MethodPool.end()) {
2972 ObjCMethodList &List =
2973 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
2974 unsigned CategCount = List.getBits();
2975 if (CategCount > 0) {
2976 // If the method is in a category we'll do lookup if there were at
2977 // least 2 category methods recorded, otherwise only one will do.
2978 if (CategCount > 1 ||
2979 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
2980 OverrideSearch overrides(*this, overridden);
2981 for (OverrideSearch::iterator
2982 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
2983 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00002984 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
2985 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00002986 hasOverriddenMethodsInBaseOrProtocol = true;
2987 overridden->setOverriding(true);
2988 break;
2989 }
2990 }
2991 }
2992 }
2993 }
2994 }
2995 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002996
2997 // Propagate down the 'related result type' bit from overridden methods.
2998 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2999 ObjCMethod->SetRelatedResultType();
3000
3001 // Then merge the declarations.
3002 mergeObjCMethodDecls(ObjCMethod, overridden);
3003
3004 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3005 continue; // Conflicting properties are detected elsewhere.
3006
3007 // Check for overriding methods
3008 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3009 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3010 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3011 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3012
3013 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanian31a25682012-07-05 22:26:07 +00003014 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3015 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003016 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3017 E = ObjCMethod->param_end();
Douglas Gregor0bf70f42012-05-17 23:13:29 +00003018 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3019 PrevE = overridden->param_end();
3020 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003021 assert(PrevI != overridden->param_end() && "Param mismatch");
3022 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3023 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3024 // If type of argument of method in this class does not match its
3025 // respective argument type in the super class method, issue warning;
3026 if (!Context.typesAreCompatible(T1, T2)) {
3027 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3028 << T1 << T2;
3029 Diag(overridden->getLocation(), diag::note_previous_declaration);
3030 break;
3031 }
3032 }
3033 }
3034 }
3035
3036 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3037}
3038
John McCall48871652010-08-21 09:40:31 +00003039Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003040 Scope *S,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003041 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003042 tok::TokenKind MethodType,
John McCallba7bf592010-08-24 05:47:05 +00003043 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003044 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003045 Selector Sel,
3046 // optional arguments. The number of types/arguments is obtained
3047 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003048 ObjCArgInfo *ArgInfo,
Fariborz Jahanian60462092010-04-08 00:30:06 +00003049 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattnerda463fe2007-12-12 07:09:47 +00003050 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003051 bool isVariadic, bool MethodDefinition) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003052 // Make sure we can establish a context for the method.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003053 if (!CurContext->isObjCContainer()) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003054 Diag(MethodLoc, diag::error_missing_method_context);
John McCall48871652010-08-21 09:40:31 +00003055 return 0;
Steve Naroff83777fe2008-02-29 21:48:07 +00003056 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003057 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3058 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003059 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00003060
Douglas Gregorbab8a962011-09-08 01:46:34 +00003061 bool HasRelatedResultType = false;
Douglas Gregor12852d92010-03-08 14:59:44 +00003062 TypeSourceInfo *ResultTInfo = 0;
Steve Naroff32606412009-02-20 22:59:16 +00003063 if (ReturnType) {
Douglas Gregor12852d92010-03-08 14:59:44 +00003064 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump11289f42009-09-09 15:08:12 +00003065
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003066 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
John McCall48871652010-08-21 09:40:31 +00003067 return 0;
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003068
Douglas Gregorbab8a962011-09-08 01:46:34 +00003069 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003070 } else { // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003071 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianb21138f2011-07-21 17:38:14 +00003072 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003073 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003074 }
Mike Stump11289f42009-09-09 15:08:12 +00003075
3076 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003077 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003078 resultDeclType,
Douglas Gregor12852d92010-03-08 14:59:44 +00003079 ResultTInfo,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003080 CurContext,
Chris Lattner8d8829e2008-03-16 00:49:28 +00003081 MethodType == tok::minus, isVariadic,
Jordan Rosed01e83a2012-10-10 16:42:25 +00003082 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis004df6e2011-08-17 19:25:08 +00003083 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor33823722011-06-11 01:09:30 +00003084 MethodDeclKind == tok::objc_optional
3085 ? ObjCMethodDecl::Optional
3086 : ObjCMethodDecl::Required,
Douglas Gregorbab8a962011-09-08 01:46:34 +00003087 HasRelatedResultType);
Mike Stump11289f42009-09-09 15:08:12 +00003088
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003089 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00003090
Chris Lattner23b0faf2009-04-11 19:42:43 +00003091 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00003092 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00003093 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00003094
David Blaikie7d170102013-05-15 07:37:26 +00003095 if (!ArgInfo[i].Type) {
John McCall856bbea2009-10-23 21:48:59 +00003096 ArgType = Context.getObjCIdType();
3097 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003098 } else {
John McCall856bbea2009-10-23 21:48:59 +00003099 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003100 }
Mike Stump11289f42009-09-09 15:08:12 +00003101
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003102 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3103 LookupOrdinaryName, ForRedeclaration);
3104 LookupName(R, S);
3105 if (R.isSingleResult()) {
3106 NamedDecl *PrevDecl = R.getFoundDecl();
3107 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003108 Diag(ArgInfo[i].NameLoc,
3109 (MethodDefinition ? diag::warn_method_param_redefinition
3110 : diag::warn_method_param_declaration))
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003111 << ArgInfo[i].Name;
3112 Diag(PrevDecl->getLocation(),
3113 diag::note_previous_declaration);
3114 }
3115 }
3116
Abramo Bagnaradff19302011-03-08 08:55:46 +00003117 SourceLocation StartLoc = DI
3118 ? DI->getTypeLoc().getBeginLoc()
3119 : ArgInfo[i].NameLoc;
3120
John McCalld44f4d72011-04-23 02:46:06 +00003121 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3122 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003123 ArgType, DI, SC_None);
Mike Stump11289f42009-09-09 15:08:12 +00003124
John McCall82490832011-05-02 00:30:12 +00003125 Param->setObjCMethodScopeInfo(i);
3126
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003127 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003128 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00003129
Chris Lattner9713a1c2009-04-11 19:34:56 +00003130 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00003131 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00003132
Fariborz Jahanian52d02f62012-01-14 18:44:35 +00003133 if (Param->hasAttr<BlocksAttr>()) {
3134 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3135 Param->setInvalidDecl();
3136 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003137 S->AddDecl(Param);
3138 IdResolver.AddDecl(Param);
3139
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003140 Params.push_back(Param);
3141 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003142
Fariborz Jahanian60462092010-04-08 00:30:06 +00003143 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCall48871652010-08-21 09:40:31 +00003144 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003145 QualType ArgType = Param->getType();
3146 if (ArgType.isNull())
3147 ArgType = Context.getObjCIdType();
3148 else
3149 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor84280642011-07-12 04:42:08 +00003150 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003151
Fariborz Jahanian60462092010-04-08 00:30:06 +00003152 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003153 Params.push_back(Param);
3154 }
3155
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003156 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003157 ObjCMethod->setObjCDeclQualifier(
3158 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00003159
3160 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00003161 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00003162
Douglas Gregor87e92752010-12-21 17:34:17 +00003163 // Add the method now.
John McCalld2930c22011-07-22 02:45:48 +00003164 const ObjCMethodDecl *PrevMethod = 0;
3165 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00003166 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003167 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3168 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003169 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003170 PrevMethod = ImpDecl->getClassMethod(Sel);
3171 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003172 }
Douglas Gregor33823722011-06-11 01:09:30 +00003173
Fariborz Jahanian512a4cc92011-10-22 01:21:15 +00003174 ObjCMethodDecl *IMD = 0;
3175 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3176 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3177 ObjCMethod->isInstanceMethod());
Fariborz Jahaniandb4fc282013-07-09 22:02:20 +00003178 if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
3179 !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
3180 // merge the attribute into implementation.
3181 ObjCMethod->addAttr(
3182 new (Context) ObjCRequiresSuperAttr(ObjCMethod->getLocation(), Context));
3183 }
Douglas Gregor87e92752010-12-21 17:34:17 +00003184 } else {
3185 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003186 }
John McCalld2930c22011-07-22 02:45:48 +00003187
Chris Lattnerda463fe2007-12-12 07:09:47 +00003188 if (PrevMethod) {
3189 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00003190 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00003191 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00003192 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian096f7c12013-05-13 17:27:00 +00003193 ObjCMethod->setInvalidDecl();
3194 return ObjCMethod;
Mike Stump11289f42009-09-09 15:08:12 +00003195 }
John McCall28a6aea2009-11-04 02:18:39 +00003196
Douglas Gregor33823722011-06-11 01:09:30 +00003197 // If this Objective-C method does not have a related result type, but we
3198 // are allowed to infer related result types, try to do so based on the
3199 // method family.
3200 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3201 if (!CurrentClass) {
3202 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3203 CurrentClass = Cat->getClassInterface();
3204 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3205 CurrentClass = Impl->getClassInterface();
3206 else if (ObjCCategoryImplDecl *CatImpl
3207 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3208 CurrentClass = CatImpl->getClassInterface();
3209 }
John McCalld2930c22011-07-22 02:45:48 +00003210
Douglas Gregorbab8a962011-09-08 01:46:34 +00003211 ResultTypeCompatibilityKind RTC
3212 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCalld2930c22011-07-22 02:45:48 +00003213
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003214 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCalld2930c22011-07-22 02:45:48 +00003215
John McCall31168b02011-06-15 23:02:42 +00003216 bool ARCError = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003217 if (getLangOpts().ObjCAutoRefCount)
John McCalle48f3892013-04-04 01:38:37 +00003218 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCall31168b02011-06-15 23:02:42 +00003219
Douglas Gregorbab8a962011-09-08 01:46:34 +00003220 // Infer the related result type when possible.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003221 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregorbab8a962011-09-08 01:46:34 +00003222 !ObjCMethod->hasRelatedResultType() &&
3223 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor33823722011-06-11 01:09:30 +00003224 bool InferRelatedResultType = false;
3225 switch (ObjCMethod->getMethodFamily()) {
3226 case OMF_None:
3227 case OMF_copy:
3228 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00003229 case OMF_finalize:
Douglas Gregor33823722011-06-11 01:09:30 +00003230 case OMF_mutableCopy:
3231 case OMF_release:
3232 case OMF_retainCount:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00003233 case OMF_performSelector:
Douglas Gregor33823722011-06-11 01:09:30 +00003234 break;
3235
3236 case OMF_alloc:
3237 case OMF_new:
3238 InferRelatedResultType = ObjCMethod->isClassMethod();
3239 break;
3240
3241 case OMF_init:
3242 case OMF_autorelease:
3243 case OMF_retain:
3244 case OMF_self:
3245 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3246 break;
3247 }
3248
John McCalld2930c22011-07-22 02:45:48 +00003249 if (InferRelatedResultType)
Douglas Gregor33823722011-06-11 01:09:30 +00003250 ObjCMethod->SetRelatedResultType();
Douglas Gregor33823722011-06-11 01:09:30 +00003251 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00003252
3253 ActOnDocumentableDecl(ObjCMethod);
3254
John McCall48871652010-08-21 09:40:31 +00003255 return ObjCMethod;
Chris Lattnerda463fe2007-12-12 07:09:47 +00003256}
3257
Chris Lattner438e5012008-12-17 07:13:27 +00003258bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +00003259 // Following is also an error. But it is caused by a missing @end
3260 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003261 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003262 return false;
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003263
3264 // If we switched context to translation unit while we are still lexically in
3265 // an objc container, it means the parser missed emitting an error.
3266 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3267 return false;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003268
Anders Carlssona6b508a2008-11-04 16:57:32 +00003269 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3270 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003271
Anders Carlssona6b508a2008-11-04 16:57:32 +00003272 return true;
3273}
Chris Lattner438e5012008-12-17 07:13:27 +00003274
James Dennett634962f2012-06-14 21:40:34 +00003275/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattner438e5012008-12-17 07:13:27 +00003276/// instance variables of ClassName into Decls.
John McCall48871652010-08-21 09:40:31 +00003277void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00003278 IdentifierInfo *ClassName,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003279 SmallVectorImpl<Decl*> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00003280 // Check that ClassName is a valid class
Douglas Gregorb2ccf012010-04-15 22:33:43 +00003281 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattner438e5012008-12-17 07:13:27 +00003282 if (!Class) {
3283 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3284 return;
3285 }
John McCall5fb5df92012-06-20 06:18:46 +00003286 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00003287 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3288 return;
3289 }
Mike Stump11289f42009-09-09 15:08:12 +00003290
Chris Lattner438e5012008-12-17 07:13:27 +00003291 // Collect the instance variables
Jordy Rosea91768e2011-07-22 02:08:32 +00003292 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003293 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003294 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003295 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003296 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCall48871652010-08-21 09:40:31 +00003297 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaradff19302011-03-08 08:55:46 +00003298 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3299 /*FIXME: StartL=*/ID->getLocation(),
3300 ID->getLocation(),
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003301 ID->getIdentifier(), ID->getType(),
3302 ID->getBitWidth());
John McCall48871652010-08-21 09:40:31 +00003303 Decls.push_back(FD);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003304 }
Mike Stump11289f42009-09-09 15:08:12 +00003305
Chris Lattner438e5012008-12-17 07:13:27 +00003306 // Introduce all of these fields into the appropriate scope.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003307 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00003308 D != Decls.end(); ++D) {
John McCall48871652010-08-21 09:40:31 +00003309 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003310 if (getLangOpts().CPlusPlus)
Chris Lattner438e5012008-12-17 07:13:27 +00003311 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCall48871652010-08-21 09:40:31 +00003312 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003313 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00003314 }
3315}
3316
Douglas Gregorf3564192010-04-26 17:32:49 +00003317/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaradff19302011-03-08 08:55:46 +00003318VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3319 SourceLocation StartLoc,
3320 SourceLocation IdLoc,
3321 IdentifierInfo *Id,
Douglas Gregorf3564192010-04-26 17:32:49 +00003322 bool Invalid) {
3323 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3324 // duration shall not be qualified by an address-space qualifier."
3325 // Since all parameters have automatic store duration, they can not have
3326 // an address space.
3327 if (T.getAddressSpace() != 0) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003328 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregorf3564192010-04-26 17:32:49 +00003329 Invalid = true;
3330 }
3331
3332 // An @catch parameter must be an unqualified object pointer type;
3333 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3334 if (Invalid) {
3335 // Don't do any further checking.
Douglas Gregorf4e837f2010-04-26 17:57:08 +00003336 } else if (T->isDependentType()) {
3337 // Okay: we don't know what this type will instantiate to.
Douglas Gregorf3564192010-04-26 17:32:49 +00003338 } else if (!T->isObjCObjectPointerType()) {
3339 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003340 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregorf3564192010-04-26 17:32:49 +00003341 } else if (T->isObjCQualifiedIdType()) {
3342 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003343 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregorf3564192010-04-26 17:32:49 +00003344 }
3345
Abramo Bagnaradff19302011-03-08 08:55:46 +00003346 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003347 T, TInfo, SC_None);
Douglas Gregor3f324d562010-05-03 18:51:14 +00003348 New->setExceptionVariable(true);
3349
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003350 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003351 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003352 Invalid = true;
3353
Douglas Gregorf3564192010-04-26 17:32:49 +00003354 if (Invalid)
3355 New->setInvalidDecl();
3356 return New;
3357}
3358
John McCall48871652010-08-21 09:40:31 +00003359Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003360 const DeclSpec &DS = D.getDeclSpec();
3361
3362 // We allow the "register" storage class on exception variables because
3363 // GCC did, but we drop it completely. Any other storage class is an error.
3364 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3365 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3366 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithb4a9e862013-04-12 22:46:28 +00003367 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003368 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithb4a9e862013-04-12 22:46:28 +00003369 << DeclSpec::getSpecifierName(SCS);
3370 }
3371 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3372 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3373 diag::err_invalid_thread)
3374 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregorf3564192010-04-26 17:32:49 +00003375 D.getMutableDeclSpec().ClearStorageClassSpecs();
3376
Richard Smithb1402ae2013-03-18 22:52:47 +00003377 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregorf3564192010-04-26 17:32:49 +00003378
3379 // Check that there are no default arguments inside the type of this
3380 // exception object (C++ only).
David Blaikiebbafb8a2012-03-11 07:00:24 +00003381 if (getLangOpts().CPlusPlus)
Douglas Gregorf3564192010-04-26 17:32:49 +00003382 CheckExtraCXXDefaultArguments(D);
3383
Argyrios Kyrtzidisef7022f2011-06-28 03:01:15 +00003384 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCall8cb7bdf2010-06-04 23:28:52 +00003385 QualType ExceptionType = TInfo->getType();
Douglas Gregorf3564192010-04-26 17:32:49 +00003386
Abramo Bagnaradff19302011-03-08 08:55:46 +00003387 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3388 D.getSourceRange().getBegin(),
3389 D.getIdentifierLoc(),
3390 D.getIdentifier(),
Douglas Gregorf3564192010-04-26 17:32:49 +00003391 D.isInvalidType());
3392
3393 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3394 if (D.getCXXScopeSpec().isSet()) {
3395 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3396 << D.getCXXScopeSpec().getRange();
3397 New->setInvalidDecl();
3398 }
3399
3400 // Add the parameter declaration into this scope.
John McCall48871652010-08-21 09:40:31 +00003401 S->AddDecl(New);
Douglas Gregorf3564192010-04-26 17:32:49 +00003402 if (D.getIdentifier())
3403 IdResolver.AddDecl(New);
3404
3405 ProcessDeclAttributes(S, New, D);
3406
3407 if (New->hasAttr<BlocksAttr>())
3408 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCall48871652010-08-21 09:40:31 +00003409 return New;
Douglas Gregore11ee112010-04-23 23:01:43 +00003410}
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003411
3412/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003413/// initialization.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003414void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003415 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003416 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3417 Iv= Iv->getNextIvar()) {
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003418 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00003419 if (QT->isRecordType())
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003420 Ivars.push_back(Iv);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003421 }
3422}
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003423
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003424void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor72e357f2011-07-28 14:54:22 +00003425 // Load referenced selectors from the external source.
3426 if (ExternalSource) {
3427 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3428 ExternalSource->ReadReferencedSelectors(Sels);
3429 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3430 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3431 }
3432
Fariborz Jahanian42f89382013-05-30 21:48:58 +00003433 DiagnoseMismatchedMethodsInGlobalPool();
3434
Fariborz Jahanianc9b7c202011-02-04 23:19:27 +00003435 // Warning will be issued only when selector table is
3436 // generated (which means there is at lease one implementation
3437 // in the TU). This is to match gcc's behavior.
3438 if (ReferencedSelectors.empty() ||
3439 !Context.AnyObjCImplementation())
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003440 return;
3441 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3442 ReferencedSelectors.begin(),
3443 E = ReferencedSelectors.end(); S != E; ++S) {
3444 Selector Sel = (*S).first;
3445 if (!LookupImplementedMethodInGlobalPool(Sel))
3446 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3447 }
3448 return;
3449}
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003450
3451ObjCIvarDecl *
3452Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method,
3453 const ObjCPropertyDecl *&PDecl) const {
3454
3455 const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
3456 if (!IDecl)
3457 return 0;
3458 Method = IDecl->lookupMethod(Method->getSelector(), true);
3459 if (!Method || !Method->isPropertyAccessor())
3460 return 0;
Fariborz Jahanian617e49a2013-11-15 17:48:00 +00003461 if ((PDecl = Method->findPropertyDecl())) {
3462 if (!PDecl->getDeclContext())
3463 return 0;
3464 // Make sure property belongs to accessor's class and not to
3465 // one of its super classes.
3466 if (const ObjCInterfaceDecl *CID =
3467 dyn_cast<ObjCInterfaceDecl>(PDecl->getDeclContext()))
3468 if (CID != IDecl)
3469 return 0;
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003470 return PDecl->getPropertyIvarDecl();
Fariborz Jahanian617e49a2013-11-15 17:48:00 +00003471 }
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003472 return 0;
3473}
3474
3475void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S) {
3476 if (S->hasUnrecoverableErrorOccurred() || !S->isInObjcMethodScope())
3477 return;
3478
3479 const ObjCMethodDecl *CurMethod = getCurMethodDecl();
3480 if (!CurMethod)
3481 return;
3482 const ObjCPropertyDecl *PDecl;
3483 const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl);
3484 if (IV && !IV->getBackingIvarReferencedInAccessor()) {
3485 Diag(getCurMethodDecl()->getLocation(), diag::warn_unused_property_backing_ivar)
3486 << IV->getDeclName();
3487 Diag(PDecl->getLocation(), diag::note_property_declare);
3488 }
3489}