blob: 38318791fd774e42ccc844f0dad49fd82fda174d [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"
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +000018#include "clang/AST/DataRecursiveASTVisitor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000019#include "clang/AST/DeclObjC.h"
Steve Naroff157599f2009-03-03 14:49:36 +000020#include "clang/AST/Expr.h"
John McCall31168b02011-06-15 23:02:42 +000021#include "clang/AST/ExprObjC.h"
John McCall31168b02011-06-15 23:02:42 +000022#include "clang/Basic/SourceManager.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
Alp Toker314cc812014-01-25 16:55:45 +000051 const ObjCObjectType *result =
52 method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType();
John McCall31168b02011-06-15 23:02:42 +000053
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.
Craig Topperc3ec1492014-05-26 06:22:03 +000073 const ObjCInterfaceDecl *receiverClass = nullptr;
John McCall31168b02011-06-15 23:02:42 +000074 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)) {
Aaron Ballman36a53502014-01-16 13:03:14 +0000100 method->addAttr(UnavailableAttr::CreateImplicit(Context,
101 "init method returns a type unrelated to its receiver type",
102 loc));
John McCall31168b02011-06-15 23:02:42 +0000103 return true;
104 }
105
106 // Otherwise, it's an error.
107 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
108 method->setInvalidDecl();
109 return true;
110}
111
Fariborz Jahanianac8dbf02011-09-27 22:35:36 +0000112void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregor66a8ca02013-01-15 22:43:08 +0000113 const ObjCMethodDecl *Overridden) {
Douglas Gregor33823722011-06-11 01:09:30 +0000114 if (Overridden->hasRelatedResultType() &&
115 !NewMethod->hasRelatedResultType()) {
116 // This can only happen when the method follows a naming convention that
117 // implies a related result type, and the original (overridden) method has
118 // a suitable return type, but the new (overriding) method does not have
119 // a suitable return type.
Alp Toker314cc812014-01-25 16:55:45 +0000120 QualType ResultType = NewMethod->getReturnType();
Aaron Ballman41b10ac2014-08-01 13:20:09 +0000121 SourceRange ResultTypeRange = NewMethod->getReturnTypeSourceRange();
Douglas Gregor33823722011-06-11 01:09:30 +0000122
123 // Figure out which class this method is part of, if any.
124 ObjCInterfaceDecl *CurrentClass
125 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
126 if (!CurrentClass) {
127 DeclContext *DC = NewMethod->getDeclContext();
128 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
129 CurrentClass = Cat->getClassInterface();
130 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
131 CurrentClass = Impl->getClassInterface();
132 else if (ObjCCategoryImplDecl *CatImpl
133 = dyn_cast<ObjCCategoryImplDecl>(DC))
134 CurrentClass = CatImpl->getClassInterface();
135 }
136
137 if (CurrentClass) {
138 Diag(NewMethod->getLocation(),
139 diag::warn_related_result_type_compatibility_class)
140 << Context.getObjCInterfaceType(CurrentClass)
141 << ResultType
142 << ResultTypeRange;
143 } else {
144 Diag(NewMethod->getLocation(),
145 diag::warn_related_result_type_compatibility_protocol)
146 << ResultType
147 << ResultTypeRange;
148 }
149
Douglas Gregorbab8a962011-09-08 01:46:34 +0000150 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
151 Diag(Overridden->getLocation(),
John McCall5ec7e7d2013-03-19 07:04:25 +0000152 diag::note_related_result_type_family)
153 << /*overridden method*/ 0
Douglas Gregorbab8a962011-09-08 01:46:34 +0000154 << Family;
155 else
156 Diag(Overridden->getLocation(),
157 diag::note_related_result_type_overridden);
Douglas Gregor33823722011-06-11 01:09:30 +0000158 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000159 if (getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanianac8dbf02011-09-27 22:35:36 +0000160 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
161 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
162 Diag(NewMethod->getLocation(),
163 diag::err_nsreturns_retained_attribute_mismatch) << 1;
164 Diag(Overridden->getLocation(), diag::note_previous_decl)
165 << "method";
166 }
167 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
168 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
169 Diag(NewMethod->getLocation(),
170 diag::err_nsreturns_retained_attribute_mismatch) << 0;
171 Diag(Overridden->getLocation(), diag::note_previous_decl)
172 << "method";
173 }
Douglas Gregor0bf70f42012-05-17 23:13:29 +0000174 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
175 oe = Overridden->param_end();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000176 for (ObjCMethodDecl::param_iterator
177 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Douglas Gregor0bf70f42012-05-17 23:13:29 +0000178 ni != ne && oi != oe; ++ni, ++oi) {
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000179 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanianac8dbf02011-09-27 22:35:36 +0000180 ParmVarDecl *newDecl = (*ni);
181 if (newDecl->hasAttr<NSConsumedAttr>() !=
182 oldDecl->hasAttr<NSConsumedAttr>()) {
183 Diag(newDecl->getLocation(),
184 diag::err_nsconsumed_attribute_mismatch);
185 Diag(oldDecl->getLocation(), diag::note_previous_decl)
186 << "parameter";
187 }
188 }
189 }
Douglas Gregor33823722011-06-11 01:09:30 +0000190}
191
John McCall31168b02011-06-15 23:02:42 +0000192/// \brief Check a method declaration for compatibility with the Objective-C
193/// ARC conventions.
John McCalle48f3892013-04-04 01:38:37 +0000194bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) {
John McCall31168b02011-06-15 23:02:42 +0000195 ObjCMethodFamily family = method->getMethodFamily();
196 switch (family) {
197 case OMF_None:
Nico Weber1fb82662011-08-28 22:35:17 +0000198 case OMF_finalize:
John McCall31168b02011-06-15 23:02:42 +0000199 case OMF_retain:
200 case OMF_release:
201 case OMF_autorelease:
202 case OMF_retainCount:
203 case OMF_self:
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000204 case OMF_initialize:
John McCalld2930c22011-07-22 02:45:48 +0000205 case OMF_performSelector:
John McCall31168b02011-06-15 23:02:42 +0000206 return false;
207
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000208 case OMF_dealloc:
Alp Toker314cc812014-01-25 16:55:45 +0000209 if (!Context.hasSameType(method->getReturnType(), Context.VoidTy)) {
Aaron Ballman41b10ac2014-08-01 13:20:09 +0000210 SourceRange ResultTypeRange = method->getReturnTypeSourceRange();
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000211 if (ResultTypeRange.isInvalid())
Alp Toker314cc812014-01-25 16:55:45 +0000212 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
213 << method->getReturnType()
214 << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000215 else
Alp Toker314cc812014-01-25 16:55:45 +0000216 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
217 << method->getReturnType()
218 << FixItHint::CreateReplacement(ResultTypeRange, "void");
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000219 return true;
220 }
221 return false;
222
John McCall31168b02011-06-15 23:02:42 +0000223 case OMF_init:
224 // If the method doesn't obey the init rules, don't bother annotating it.
John McCalle48f3892013-04-04 01:38:37 +0000225 if (checkInitMethod(method, QualType()))
John McCall31168b02011-06-15 23:02:42 +0000226 return true;
227
Aaron Ballman36a53502014-01-16 13:03:14 +0000228 method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context));
John McCall31168b02011-06-15 23:02:42 +0000229
230 // Don't add a second copy of this attribute, but otherwise don't
231 // let it be suppressed.
232 if (method->hasAttr<NSReturnsRetainedAttr>())
233 return false;
234 break;
235
236 case OMF_alloc:
237 case OMF_copy:
238 case OMF_mutableCopy:
239 case OMF_new:
240 if (method->hasAttr<NSReturnsRetainedAttr>() ||
241 method->hasAttr<NSReturnsNotRetainedAttr>() ||
242 method->hasAttr<NSReturnsAutoreleasedAttr>())
243 return false;
244 break;
245 }
246
Aaron Ballman36a53502014-01-16 13:03:14 +0000247 method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context));
John McCall31168b02011-06-15 23:02:42 +0000248 return false;
249}
250
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000251static void DiagnoseObjCImplementedDeprecations(Sema &S,
252 NamedDecl *ND,
253 SourceLocation ImplLoc,
254 int select) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000255 if (ND && ND->isDeprecated()) {
Fariborz Jahanian6fd94352011-02-16 00:30:31 +0000256 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000257 if (select == 0)
Ted Kremenek59b10db2012-02-27 22:55:11 +0000258 S.Diag(ND->getLocation(), diag::note_method_declared_at)
259 << ND->getDeclName();
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000260 else
261 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
262 }
263}
264
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +0000265/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
266/// pool.
267void Sema::AddAnyMethodToGlobalPool(Decl *D) {
268 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
269
270 // If we don't have a valid method decl, simply return.
271 if (!MDecl)
272 return;
273 if (MDecl->isInstanceMethod())
274 AddInstanceMethodToGlobalPool(MDecl, true);
275 else
276 AddFactoryMethodToGlobalPool(MDecl, true);
277}
278
Fariborz Jahanian1dfeace2012-09-13 18:53:14 +0000279/// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer
280/// has explicit ownership attribute; false otherwise.
281static bool
282HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) {
283 QualType T = Param->getType();
284
Fariborz Jahanian1dfeace2012-09-13 18:53:14 +0000285 if (const PointerType *PT = T->getAs<PointerType>()) {
286 T = PT->getPointeeType();
287 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
288 T = RT->getPointeeType();
289 } else {
290 return true;
291 }
292
293 // If we have a lifetime qualifier, but it's local, we must have
294 // inferred it. So, it is implicit.
295 return !T.getLocalQualifiers().hasObjCLifetime();
296}
297
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +0000298/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
299/// and user declared, in the method definition's AST.
300void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000301 assert((getCurMethodDecl() == nullptr) && "Methodparsing confused");
John McCall48871652010-08-21 09:40:31 +0000302 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian577574a2012-07-02 23:37:09 +0000303
Steve Naroff542cd5d2008-07-25 17:57:26 +0000304 // If we don't have a valid method decl, simply return.
305 if (!MDecl)
306 return;
Steve Naroff1d2538c2007-12-18 01:30:32 +0000307
Chris Lattnerda463fe2007-12-12 07:09:47 +0000308 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor91f84212008-12-11 16:49:14 +0000309 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9a28e842010-03-01 23:15:13 +0000310 PushFunctionScope();
311
Chris Lattnerda463fe2007-12-12 07:09:47 +0000312 // Create Decl objects for each parameter, entrring them in the scope for
313 // binding to their use.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000314
315 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000316 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump11289f42009-09-09 15:08:12 +0000317
Daniel Dunbar279d1cc2008-08-26 06:07:48 +0000318 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
319 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000320
Reid Kleckner5a115802013-06-24 14:38:26 +0000321 // The ObjC parser requires parameter names so there's no need to check.
322 CheckParmsForFunctionDef(MDecl->param_begin(), MDecl->param_end(),
323 /*CheckParameterNames=*/false);
324
Chris Lattner58258242008-04-10 02:22:51 +0000325 // Introduce all of the other parameters into this scope.
Aaron Ballman43b68be2014-03-07 17:50:17 +0000326 for (auto *Param : MDecl->params()) {
Fariborz Jahanianb3e87122010-09-17 22:07:07 +0000327 if (!Param->isInvalidDecl() &&
Fariborz Jahanian1dfeace2012-09-13 18:53:14 +0000328 getLangOpts().ObjCAutoRefCount &&
329 !HasExplicitOwnershipAttr(*this, Param))
330 Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
331 Param->getType();
Fariborz Jahaniancd278ff2012-08-30 23:56:02 +0000332
Aaron Ballman43b68be2014-03-07 17:50:17 +0000333 if (Param->getIdentifier())
334 PushOnScopeChains(Param, FnBodyScope);
Fariborz Jahanianb3e87122010-09-17 22:07:07 +0000335 }
John McCall31168b02011-06-15 23:02:42 +0000336
337 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikiebbafb8a2012-03-11 07:00:24 +0000338 if (getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +0000339 switch (MDecl->getMethodFamily()) {
340 case OMF_retain:
341 case OMF_retainCount:
342 case OMF_release:
343 case OMF_autorelease:
344 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
Fariborz Jahanian39d1c422013-05-16 19:08:44 +0000345 << 0 << MDecl->getSelector();
John McCall31168b02011-06-15 23:02:42 +0000346 break;
347
348 case OMF_None:
349 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000350 case OMF_finalize:
John McCall31168b02011-06-15 23:02:42 +0000351 case OMF_alloc:
352 case OMF_init:
353 case OMF_mutableCopy:
354 case OMF_copy:
355 case OMF_new:
356 case OMF_self:
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +0000357 case OMF_initialize:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000358 case OMF_performSelector:
John McCall31168b02011-06-15 23:02:42 +0000359 break;
360 }
361 }
362
Nico Weber715abaf2011-08-22 17:25:57 +0000363 // Warn on deprecated methods under -Wdeprecated-implementations,
364 // and prepare for warning on missing super calls.
365 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +0000366 ObjCMethodDecl *IMD =
367 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
368
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000369 if (IMD) {
370 ObjCImplDecl *ImplDeclOfMethodDef =
371 dyn_cast<ObjCImplDecl>(MDecl->getDeclContext());
372 ObjCContainerDecl *ContDeclOfMethodDecl =
373 dyn_cast<ObjCContainerDecl>(IMD->getDeclContext());
Craig Topperc3ec1492014-05-26 06:22:03 +0000374 ObjCImplDecl *ImplDeclOfMethodDecl = nullptr;
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000375 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
376 ImplDeclOfMethodDecl = OID->getImplementation();
Fariborz Jahanianed39e7c2014-03-18 16:25:22 +0000377 else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl)) {
378 if (CD->IsClassExtension()) {
379 if (ObjCInterfaceDecl *OID = CD->getClassInterface())
380 ImplDeclOfMethodDecl = OID->getImplementation();
381 } else
382 ImplDeclOfMethodDecl = CD->getImplementation();
Fariborz Jahanian19a08bb2014-03-18 00:10:37 +0000383 }
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000384 // No need to issue deprecated warning if deprecated mehod in class/category
385 // is being implemented in its own implementation (no overriding is involved).
Fariborz Jahanianed39e7c2014-03-18 16:25:22 +0000386 if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef)
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000387 DiagnoseObjCImplementedDeprecations(*this,
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000388 dyn_cast<NamedDecl>(IMD),
389 MDecl->getLocation(), 0);
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000390 }
Nico Weber715abaf2011-08-22 17:25:57 +0000391
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +0000392 if (MDecl->getMethodFamily() == OMF_init) {
393 if (MDecl->isDesignatedInitializerForTheInterface()) {
394 getCurFunction()->ObjCIsDesignatedInit = true;
Fariborz Jahaniane3b5c992014-03-14 23:30:18 +0000395 getCurFunction()->ObjCWarnForNoDesignatedInitChain =
Craig Topperc3ec1492014-05-26 06:22:03 +0000396 IC->getSuperClass() != nullptr;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +0000397 } else if (IC->hasDesignatedInitializers()) {
398 getCurFunction()->ObjCIsSecondaryInit = true;
Fariborz Jahaniane3b5c992014-03-14 23:30:18 +0000399 getCurFunction()->ObjCWarnForNoInitDelegation = true;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +0000400 }
401 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000402
Nico Weber1fb82662011-08-28 22:35:17 +0000403 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber715abaf2011-08-22 17:25:57 +0000404 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
405 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
406 // Only do this if the current class actually has a superclass.
Jordan Rosed03d99d2013-03-05 01:27:54 +0000407 if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) {
Jordan Rose2afd6612012-10-19 16:05:26 +0000408 ObjCMethodFamily Family = MDecl->getMethodFamily();
409 if (Family == OMF_dealloc) {
410 if (!(getLangOpts().ObjCAutoRefCount ||
411 getLangOpts().getGC() == LangOptions::GCOnly))
412 getCurFunction()->ObjCShouldCallSuper = true;
413
414 } else if (Family == OMF_finalize) {
415 if (Context.getLangOpts().getGC() != LangOptions::NonGC)
416 getCurFunction()->ObjCShouldCallSuper = true;
417
Fariborz Jahaniance4bbb22013-11-05 00:28:21 +0000418 } else {
Jordan Rose2afd6612012-10-19 16:05:26 +0000419 const ObjCMethodDecl *SuperMethod =
Jordan Rosed03d99d2013-03-05 01:27:54 +0000420 SuperClass->lookupMethod(MDecl->getSelector(),
421 MDecl->isInstanceMethod());
Jordan Rose2afd6612012-10-19 16:05:26 +0000422 getCurFunction()->ObjCShouldCallSuper =
423 (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahaniand6876b22012-09-10 18:04:25 +0000424 }
Nico Weber1fb82662011-08-28 22:35:17 +0000425 }
Nico Weber715abaf2011-08-22 17:25:57 +0000426 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000427}
428
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000429namespace {
430
431// Callback to only accept typo corrections that are Objective-C classes.
432// If an ObjCInterfaceDecl* is given to the constructor, then the validation
433// function will reject corrections to that class.
434class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
435 public:
Craig Topperc3ec1492014-05-26 06:22:03 +0000436 ObjCInterfaceValidatorCCC() : CurrentIDecl(nullptr) {}
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000437 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
438 : CurrentIDecl(IDecl) {}
439
Craig Toppere14c0f82014-03-12 04:55:44 +0000440 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000441 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
442 return ID && !declaresSameEntity(ID, CurrentIDecl);
443 }
444
445 private:
446 ObjCInterfaceDecl *CurrentIDecl;
447};
448
449}
450
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000451static void diagnoseUseOfProtocols(Sema &TheSema,
452 ObjCContainerDecl *CD,
453 ObjCProtocolDecl *const *ProtoRefs,
454 unsigned NumProtoRefs,
455 const SourceLocation *ProtoLocs) {
456 assert(ProtoRefs);
457 // Diagnose availability in the context of the ObjC container.
458 Sema::ContextRAII SavedContext(TheSema, CD);
459 for (unsigned i = 0; i < NumProtoRefs; ++i) {
460 (void)TheSema.DiagnoseUseOfDecl(ProtoRefs[i], ProtoLocs[i]);
461 }
462}
463
John McCall48871652010-08-21 09:40:31 +0000464Decl *Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000465ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
466 IdentifierInfo *ClassName, SourceLocation ClassLoc,
467 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCall48871652010-08-21 09:40:31 +0000468 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000469 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000470 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000471 assert(ClassName && "Missing class identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000472
Chris Lattnerda463fe2007-12-12 07:09:47 +0000473 // Check for another declaration kind with the same name.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000474 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000475 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor5101c242008-12-05 18:15:24 +0000476
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000477 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000478 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000479 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000480 }
Mike Stump11289f42009-09-09 15:08:12 +0000481
Douglas Gregordc9166c2011-12-15 20:29:51 +0000482 // Create a declaration to describe this @interface.
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000483 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +0000484
485 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
486 // A previous decl with a different name is because of
487 // @compatibility_alias, for example:
488 // \code
489 // @class NewImage;
490 // @compatibility_alias OldImage NewImage;
491 // \endcode
492 // A lookup for 'OldImage' will return the 'NewImage' decl.
493 //
494 // In such a case use the real declaration name, instead of the alias one,
495 // otherwise we will break IdentifierResolver and redecls-chain invariants.
496 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
497 // has been aliased.
498 ClassName = PrevIDecl->getIdentifier();
499 }
500
Douglas Gregordc9166c2011-12-15 20:29:51 +0000501 ObjCInterfaceDecl *IDecl
502 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000503 PrevIDecl, ClassLoc);
Douglas Gregordc9166c2011-12-15 20:29:51 +0000504
Douglas Gregordc9166c2011-12-15 20:29:51 +0000505 if (PrevIDecl) {
506 // Class already seen. Was it a definition?
507 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
508 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
509 << PrevIDecl->getDeclName();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000510 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregordc9166c2011-12-15 20:29:51 +0000511 IDecl->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000512 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000513 }
Douglas Gregordc9166c2011-12-15 20:29:51 +0000514
515 if (AttrList)
516 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
517 PushOnScopeChains(IDecl, TUScope);
Mike Stump11289f42009-09-09 15:08:12 +0000518
Douglas Gregordc9166c2011-12-15 20:29:51 +0000519 // Start the definition of this class. If we're in a redefinition case, there
520 // may already be a definition, so we'll end up adding to it.
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000521 if (!IDecl->hasDefinition())
522 IDecl->startDefinition();
523
Chris Lattnerda463fe2007-12-12 07:09:47 +0000524 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000525 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000526 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
527 LookupOrdinaryName);
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000528
529 if (!PrevDecl) {
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000530 // Try to correct for a typo in the superclass name without correcting
531 // to the class we're defining.
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000532 if (TypoCorrection Corrected =
533 CorrectTypo(DeclarationNameInfo(SuperName, SuperLoc),
534 LookupOrdinaryName, TUScope, nullptr,
535 llvm::make_unique<ObjCInterfaceValidatorCCC>(IDecl),
536 CTK_ErrorRecovery)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000537 diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest)
538 << SuperName << ClassName);
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000539 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000540 }
541 }
542
Douglas Gregor0b144e12011-12-15 00:29:59 +0000543 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000544 Diag(SuperLoc, diag::err_recursive_superclass)
545 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor16408322011-12-15 22:34:59 +0000546 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stump12b8ce12009-08-04 21:02:39 +0000547 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000548 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000549 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000550
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000551 // Diagnose availability in the context of the @interface.
552 ContextRAII SavedContext(*this, IDecl);
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000553 // Diagnose classes that inherit from deprecated classes.
554 if (SuperClassDecl)
555 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000556
Craig Topperc3ec1492014-05-26 06:22:03 +0000557 if (PrevDecl && !SuperClassDecl) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000558 // The previous declaration was not a class decl. Check if we have a
559 // typedef. If we do, get the underlying class type.
Richard Smithdda56e42011-04-15 14:24:37 +0000560 if (const TypedefNameDecl *TDecl =
561 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000562 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000563 if (T->isObjCObjectType()) {
Fariborz Jahanian83f1be12013-04-04 18:45:52 +0000564 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000565 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian83f1be12013-04-04 18:45:52 +0000566 // This handles the following case:
567 // @interface NewI @end
568 // typedef NewI DeprI __attribute__((deprecated("blah")))
569 // @interface SI : DeprI /* warn here */ @end
570 (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc);
571 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000572 }
573 }
Mike Stump11289f42009-09-09 15:08:12 +0000574
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000575 // This handles the following case:
576 //
577 // typedef int SuperClass;
578 // @interface MyClass : SuperClass {} @end
579 //
580 if (!SuperClassDecl) {
581 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
582 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff189d41f2009-02-04 17:14:05 +0000583 }
584 }
Mike Stump11289f42009-09-09 15:08:12 +0000585
Richard Smithdda56e42011-04-15 14:24:37 +0000586 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000587 if (!SuperClassDecl)
588 Diag(SuperLoc, diag::err_undef_superclass)
589 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor4123a862011-11-14 22:10:01 +0000590 else if (RequireCompleteType(SuperLoc,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000591 Context.getObjCInterfaceType(SuperClassDecl),
592 diag::err_forward_superclass,
593 SuperClassDecl->getDeclName(),
594 ClassName,
595 SourceRange(AtInterfaceLoc, ClassLoc))) {
Craig Topperc3ec1492014-05-26 06:22:03 +0000596 SuperClassDecl = nullptr;
Fariborz Jahanian3ee91fa2011-06-23 23:16:19 +0000597 }
Steve Naroff189d41f2009-02-04 17:14:05 +0000598 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000599 IDecl->setSuperClass(SuperClassDecl);
600 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor16408322011-12-15 22:34:59 +0000601 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000602 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000603 } else { // we have a root class.
Douglas Gregor16408322011-12-15 22:34:59 +0000604 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000605 }
Mike Stump11289f42009-09-09 15:08:12 +0000606
Sebastian Redle7c1fe62010-08-13 00:28:03 +0000607 // Check then save referenced protocols.
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000608 if (NumProtoRefs) {
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000609 diagnoseUseOfProtocols(*this, IDecl, (ObjCProtocolDecl*const*)ProtoRefs,
610 NumProtoRefs, ProtoLocs);
Roman Divackye6377112012-09-06 15:59:27 +0000611 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000612 ProtoLocs, Context);
Douglas Gregor16408322011-12-15 22:34:59 +0000613 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000614 }
Mike Stump11289f42009-09-09 15:08:12 +0000615
Anders Carlssona6b508a2008-11-04 16:57:32 +0000616 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000617 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000618}
619
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000620/// ActOnTypedefedProtocols - this action finds protocol list as part of the
621/// typedef'ed use for a qualified super class and adds them to the list
622/// of the protocols.
623void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs,
624 IdentifierInfo *SuperName,
625 SourceLocation SuperLoc) {
626 if (!SuperName)
627 return;
628 NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
629 LookupOrdinaryName);
630 if (!IDecl)
631 return;
632
633 if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) {
634 QualType T = TDecl->getUnderlyingType();
635 if (T->isObjCObjectType())
636 if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>())
Benjamin Kramerf9890422015-02-17 16:48:30 +0000637 ProtocolRefs.append(OPT->qual_begin(), OPT->qual_end());
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000638 }
639}
640
Richard Smithac4e36d2012-08-08 23:32:13 +0000641/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett634962f2012-06-14 21:40:34 +0000642/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithac4e36d2012-08-08 23:32:13 +0000643Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
644 IdentifierInfo *AliasName,
645 SourceLocation AliasLocation,
646 IdentifierInfo *ClassName,
647 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000648 // Look for previous declaration of alias name
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000649 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000650 LookupOrdinaryName, ForRedeclaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000651 if (ADecl) {
Eli Friedmanfd6b3f82013-06-21 01:49:53 +0000652 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000653 Diag(ADecl->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +0000654 return nullptr;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000655 }
656 // Check for class declaration
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000657 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000658 LookupOrdinaryName, ForRedeclaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000659 if (const TypedefNameDecl *TDecl =
660 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000661 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000662 if (T->isObjCObjectType()) {
663 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000664 ClassName = IDecl->getIdentifier();
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000665 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000666 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000667 }
668 }
669 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000670 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
Craig Topperc3ec1492014-05-26 06:22:03 +0000671 if (!CDecl) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000672 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000673 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000674 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
Craig Topperc3ec1492014-05-26 06:22:03 +0000675 return nullptr;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000676 }
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattner219b3e92008-03-16 21:17:37 +0000678 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000679 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000680 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000681
Anders Carlssona6b508a2008-11-04 16:57:32 +0000682 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000683 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000684
John McCall48871652010-08-21 09:40:31 +0000685 return AliasDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000686}
687
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000688bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff41d09ad2009-03-05 15:22:01 +0000689 IdentifierInfo *PName,
690 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000691 const ObjCList<ObjCProtocolDecl> &PList) {
692
693 bool res = false;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000694 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
695 E = PList.end(); I != E; ++I) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000696 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
697 Ploc)) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000698 if (PDecl->getIdentifier() == PName) {
699 Diag(Ploc, diag::err_protocol_has_circular_dependency);
700 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000701 res = true;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000702 }
Douglas Gregore6e48b12012-01-01 19:29:29 +0000703
704 if (!PDecl->hasDefinition())
705 continue;
706
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000707 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
708 PDecl->getLocation(), PDecl->getReferencedProtocols()))
709 res = true;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000710 }
711 }
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000712 return res;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000713}
714
John McCall48871652010-08-21 09:40:31 +0000715Decl *
Chris Lattner3bbae002008-07-26 04:03:38 +0000716Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
717 IdentifierInfo *ProtocolName,
718 SourceLocation ProtocolLoc,
John McCall48871652010-08-21 09:40:31 +0000719 Decl * const *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000720 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000721 const SourceLocation *ProtoLocs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000722 SourceLocation EndProtoLoc,
723 AttributeList *AttrList) {
Fariborz Jahaniancadf7c52011-05-12 22:04:39 +0000724 bool err = false;
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000725 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000726 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor32c17572012-01-01 20:30:41 +0000727 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
728 ForRedeclaration);
Craig Topperc3ec1492014-05-26 06:22:03 +0000729 ObjCProtocolDecl *PDecl = nullptr;
730 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : nullptr) {
Douglas Gregor32c17572012-01-01 20:30:41 +0000731 // If we already have a definition, complain.
732 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
733 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump11289f42009-09-09 15:08:12 +0000734
Douglas Gregor32c17572012-01-01 20:30:41 +0000735 // Create a new protocol that is completely distinct from previous
736 // declarations, and do not make this protocol available for name lookup.
737 // That way, we'll end up completely ignoring the duplicate.
738 // FIXME: Can we turn this into an error?
739 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
740 ProtocolLoc, AtProtoInterfaceLoc,
Craig Topperc3ec1492014-05-26 06:22:03 +0000741 /*PrevDecl=*/nullptr);
Douglas Gregor32c17572012-01-01 20:30:41 +0000742 PDecl->startDefinition();
743 } else {
744 if (PrevDecl) {
745 // Check for circular dependencies among protocol declarations. This can
746 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis95dfc122011-11-13 22:08:30 +0000747 ObjCList<ObjCProtocolDecl> PList;
748 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
749 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor32c17572012-01-01 20:30:41 +0000750 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis95dfc122011-11-13 22:08:30 +0000751 }
Douglas Gregor32c17572012-01-01 20:30:41 +0000752
753 // Create the new declaration.
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +0000754 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +0000755 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000756 /*PrevDecl=*/PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000757
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000758 PushOnScopeChains(PDecl, TUScope);
Douglas Gregore6e48b12012-01-01 19:29:29 +0000759 PDecl->startDefinition();
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000760 }
Douglas Gregore6e48b12012-01-01 19:29:29 +0000761
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000762 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000763 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor32c17572012-01-01 20:30:41 +0000764
765 // Merge attributes from previous declarations.
766 if (PrevDecl)
767 mergeDeclAttributes(PDecl, PrevDecl);
768
Fariborz Jahaniancadf7c52011-05-12 22:04:39 +0000769 if (!err && NumProtoRefs ) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000770 /// Check then save referenced protocols.
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000771 diagnoseUseOfProtocols(*this, PDecl, (ObjCProtocolDecl*const*)ProtoRefs,
772 NumProtoRefs, ProtoLocs);
Roman Divackye6377112012-09-06 15:59:27 +0000773 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000774 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000775 }
Mike Stump11289f42009-09-09 15:08:12 +0000776
777 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000778 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000779}
780
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000781static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl,
782 ObjCProtocolDecl *&UndefinedProtocol) {
783 if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) {
784 UndefinedProtocol = PDecl;
785 return true;
786 }
787
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000788 for (auto *PI : PDecl->protocols())
789 if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) {
790 UndefinedProtocol = PI;
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000791 return true;
792 }
793 return false;
794}
795
Chris Lattnerda463fe2007-12-12 07:09:47 +0000796/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000797/// issues an error if they are not declared. It returns list of
798/// protocol declarations in its 'Protocols' argument.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000799void
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000800Sema::FindProtocolDeclaration(bool WarnOnDeclarations, bool ForObjCContainer,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000801 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000802 unsigned NumProtocols,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000803 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000804 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000805 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
806 ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000807 if (!PDecl) {
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000808 TypoCorrection Corrected = CorrectTypo(
809 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Takata89c881b2014-10-27 18:07:29 +0000810 LookupObjCProtocolName, TUScope, nullptr,
811 llvm::make_unique<DeclFilterCCC<ObjCProtocolDecl>>(),
Craig Topperc3ec1492014-05-26 06:22:03 +0000812 CTK_ErrorRecovery);
Richard Smithf9b15102013-08-17 00:46:16 +0000813 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>()))
814 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest)
815 << ProtocolId[i].first);
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000816 }
817
818 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000819 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000820 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000821 continue;
822 }
Fariborz Jahanianada44a22013-04-09 17:52:29 +0000823 // If this is a forward protocol declaration, get its definition.
824 if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
825 PDecl = PDecl->getDefinition();
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000826
827 // For an objc container, delay protocol reference checking until after we
828 // can set the objc decl as the availability context, otherwise check now.
829 if (!ForObjCContainer) {
830 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
831 }
Chris Lattner9c1842b2008-07-26 03:47:43 +0000832
833 // If this is a forward declaration and we are supposed to warn in this
834 // case, do it.
Douglas Gregoreed49792013-01-17 00:38:46 +0000835 // FIXME: Recover nicely in the hidden case.
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000836 ObjCProtocolDecl *UndefinedProtocol;
837
Douglas Gregoreed49792013-01-17 00:38:46 +0000838 if (WarnOnDeclarations &&
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000839 NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000840 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000841 << ProtocolId[i].first;
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000842 Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined)
843 << UndefinedProtocol;
844 }
John McCall48871652010-08-21 09:40:31 +0000845 Protocols.push_back(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000846 }
847}
848
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000849/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000850/// a class method in its extension.
851///
Mike Stump11289f42009-09-09 15:08:12 +0000852void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000853 ObjCInterfaceDecl *ID) {
854 if (!ID)
855 return; // Possibly due to previous error
856
857 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Aaron Ballmanaff18c02014-03-13 19:03:34 +0000858 for (auto *MD : ID->methods())
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000859 MethodMap[MD->getSelector()] = MD;
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000860
861 if (MethodMap.empty())
862 return;
Aaron Ballmanaff18c02014-03-13 19:03:34 +0000863 for (const auto *Method : CAT->methods()) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000864 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
Fariborz Jahanian83d674e2014-03-17 17:46:10 +0000865 if (PrevMethod &&
866 (PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) &&
867 !MatchTwoMethodDeclarations(Method, PrevMethod)) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000868 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
869 << Method->getDeclName();
870 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
871 }
872 }
873}
874
James Dennett634962f2012-06-14 21:40:34 +0000875/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorf6102672012-01-01 21:23:57 +0000876Sema::DeclGroupPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000877Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000878 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000879 unsigned NumElts,
880 AttributeList *attrList) {
Douglas Gregorf6102672012-01-01 21:23:57 +0000881 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000882 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000883 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor32c17572012-01-01 20:30:41 +0000884 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
885 ForRedeclaration);
886 ObjCProtocolDecl *PDecl
887 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
888 IdentList[i].second, AtProtocolLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000889 PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000890
891 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorf6102672012-01-01 21:23:57 +0000892 CheckObjCDeclScope(PDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000893
Douglas Gregor42ff1bb2012-01-01 20:33:24 +0000894 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000895 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor32c17572012-01-01 20:30:41 +0000896
897 if (PrevDecl)
898 mergeDeclAttributes(PDecl, PrevDecl);
899
Douglas Gregorf6102672012-01-01 21:23:57 +0000900 DeclsInGroup.push_back(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000901 }
Mike Stump11289f42009-09-09 15:08:12 +0000902
Rafael Espindolaab417692013-07-09 12:05:01 +0000903 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000904}
905
John McCall48871652010-08-21 09:40:31 +0000906Decl *Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000907ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
908 IdentifierInfo *ClassName, SourceLocation ClassLoc,
909 IdentifierInfo *CategoryName,
910 SourceLocation CategoryLoc,
John McCall48871652010-08-21 09:40:31 +0000911 Decl * const *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000912 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000913 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000914 SourceLocation EndProtoLoc) {
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000915 ObjCCategoryDecl *CDecl;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000916 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek514ff702010-02-23 19:39:46 +0000917
918 /// Check that class of this category is already completely declared.
Douglas Gregor4123a862011-11-14 22:10:01 +0000919
920 if (!IDecl
921 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000922 diag::err_category_forward_interface,
Craig Topperc3ec1492014-05-26 06:22:03 +0000923 CategoryName == nullptr)) {
Ted Kremenek514ff702010-02-23 19:39:46 +0000924 // Create an invalid ObjCCategoryDecl to serve as context for
925 // the enclosing method declarations. We mark the decl invalid
926 // to make it clear that this isn't a valid AST.
927 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000928 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek514ff702010-02-23 19:39:46 +0000929 CDecl->setInvalidDecl();
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +0000930 CurContext->addDecl(CDecl);
Douglas Gregor4123a862011-11-14 22:10:01 +0000931
932 if (!IDecl)
933 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000934 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek514ff702010-02-23 19:39:46 +0000935 }
936
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000937 if (!CategoryName && IDecl->getImplementation()) {
938 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
939 Diag(IDecl->getImplementation()->getLocation(),
940 diag::note_implementation_declared);
Ted Kremenek514ff702010-02-23 19:39:46 +0000941 }
942
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000943 if (CategoryName) {
944 /// Check for duplicate interface declaration for this category
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000945 if (ObjCCategoryDecl *Previous
946 = IDecl->FindCategoryDeclaration(CategoryName)) {
947 // Class extensions can be declared multiple times, categories cannot.
948 Diag(CategoryLoc, diag::warn_dup_category_def)
949 << ClassName << CategoryName;
950 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner9018ca82009-02-16 21:26:43 +0000951 }
952 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000953
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000954 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
955 ClassLoc, CategoryLoc, CategoryName, IDecl);
956 // FIXME: PushOnScopeChains?
957 CurContext->addDecl(CDecl);
958
Chris Lattnerda463fe2007-12-12 07:09:47 +0000959 if (NumProtoRefs) {
Argyrios Kyrtzidis4ecdd2c2015-04-19 20:15:55 +0000960 diagnoseUseOfProtocols(*this, CDecl, (ObjCProtocolDecl*const*)ProtoRefs,
961 NumProtoRefs, ProtoLocs);
962 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000963 ProtoLocs, Context);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000964 // Protocols in the class extension belong to the class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000965 if (CDecl->IsClassExtension())
Roman Divackye6377112012-09-06 15:59:27 +0000966 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000967 NumProtoRefs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000968 }
Mike Stump11289f42009-09-09 15:08:12 +0000969
Anders Carlssona6b508a2008-11-04 16:57:32 +0000970 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000971 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000972}
973
974/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000975/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000976/// object.
John McCall48871652010-08-21 09:40:31 +0000977Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000978 SourceLocation AtCatImplLoc,
979 IdentifierInfo *ClassName, SourceLocation ClassLoc,
980 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000981 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Craig Topperc3ec1492014-05-26 06:22:03 +0000982 ObjCCategoryDecl *CatIDecl = nullptr;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +0000983 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000984 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
985 if (!CatIDecl) {
986 // Category @implementation with no corresponding @interface.
987 // Create and install one.
Argyrios Kyrtzidis41fc05c2011-11-23 20:27:26 +0000988 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
989 ClassLoc, CatLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000990 CatName, IDecl);
Argyrios Kyrtzidis41fc05c2011-11-23 20:27:26 +0000991 CatIDecl->setImplicit();
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000992 }
993 }
994
Mike Stump11289f42009-09-09 15:08:12 +0000995 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +0000996 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +0000997 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000998 /// Check that class of this category is already completely declared.
Douglas Gregor4123a862011-11-14 22:10:01 +0000999 if (!IDecl) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001000 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld2930c22011-07-22 02:45:48 +00001001 CDecl->setInvalidDecl();
Douglas Gregor4123a862011-11-14 22:10:01 +00001002 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
1003 diag::err_undef_interface)) {
1004 CDecl->setInvalidDecl();
John McCalld2930c22011-07-22 02:45:48 +00001005 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001006
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001007 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001008 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +00001009
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +00001010 // If the interface is deprecated/unavailable, warn/error about it.
1011 if (IDecl)
1012 DiagnoseUseOfDecl(IDecl, ClassLoc);
1013
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001014 /// Check that CatName, category name, is not used in another implementation.
1015 if (CatIDecl) {
1016 if (CatIDecl->getImplementation()) {
1017 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
1018 << CatName;
1019 Diag(CatIDecl->getImplementation()->getLocation(),
1020 diag::note_previous_definition);
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +00001021 CDecl->setInvalidDecl();
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +00001022 } else {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001023 CatIDecl->setImplementation(CDecl);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +00001024 // Warn on implementating category of deprecated class under
1025 // -Wdeprecated-implementations flag.
Fariborz Jahaniand724a102011-02-15 17:49:58 +00001026 DiagnoseObjCImplementedDeprecations(*this,
1027 dyn_cast<NamedDecl>(IDecl),
1028 CDecl->getLocation(), 2);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +00001029 }
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001030 }
Mike Stump11289f42009-09-09 15:08:12 +00001031
Anders Carlssona6b508a2008-11-04 16:57:32 +00001032 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001033 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001034}
1035
John McCall48871652010-08-21 09:40:31 +00001036Decl *Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001037 SourceLocation AtClassImplLoc,
1038 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001039 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001040 SourceLocation SuperClassLoc) {
Craig Topperc3ec1492014-05-26 06:22:03 +00001041 ObjCInterfaceDecl *IDecl = nullptr;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001042 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001043 NamedDecl *PrevDecl
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001044 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
1045 ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001046 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001047 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +00001048 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor1c283312010-08-11 12:19:30 +00001049 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001050 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
1051 diag::warn_undef_interface);
Douglas Gregor40f7a002010-01-04 17:27:12 +00001052 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001053 // We did not find anything with the name ClassName; try to correct for
Douglas Gregor40f7a002010-01-04 17:27:12 +00001054 // typos in the class name.
Kaelyn Takata89c881b2014-10-27 18:07:29 +00001055 TypoCorrection Corrected = CorrectTypo(
1056 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
1057 nullptr, llvm::make_unique<ObjCInterfaceValidatorCCC>(), CTK_NonError);
Richard Smithf9b15102013-08-17 00:46:16 +00001058 if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) {
1059 // Suggest the (potentially) correct interface name. Don't provide a
1060 // code-modification hint or use the typo name for recovery, because
1061 // this is just a warning. The program may actually be correct.
1062 diagnoseTypo(Corrected,
1063 PDiag(diag::warn_undef_interface_suggest) << ClassName,
1064 /*ErrorRecovery*/false);
Douglas Gregor40f7a002010-01-04 17:27:12 +00001065 } else {
1066 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
1067 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001068 }
Mike Stump11289f42009-09-09 15:08:12 +00001069
Chris Lattnerda463fe2007-12-12 07:09:47 +00001070 // Check that super class name is valid class name
Craig Topperc3ec1492014-05-26 06:22:03 +00001071 ObjCInterfaceDecl *SDecl = nullptr;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001072 if (SuperClassname) {
1073 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001074 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
1075 LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001076 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001077 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1078 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +00001079 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001080 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001081 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidis3b60cff2012-03-13 01:09:36 +00001082 if (SDecl && !SDecl->hasDefinition())
Craig Topperc3ec1492014-05-26 06:22:03 +00001083 SDecl = nullptr;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001084 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001085 Diag(SuperClassLoc, diag::err_undef_superclass)
1086 << SuperClassname << ClassName;
Douglas Gregor0b144e12011-12-15 00:29:59 +00001087 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001088 // This implementation and its interface do not have the same
1089 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001090 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001091 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001092 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001093 }
1094 }
1095 }
Mike Stump11289f42009-09-09 15:08:12 +00001096
Chris Lattnerda463fe2007-12-12 07:09:47 +00001097 if (!IDecl) {
1098 // Legacy case of @implementation with no corresponding @interface.
1099 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +00001100
Mike Stump87c57ac2009-05-16 07:39:55 +00001101 // FIXME: Do we support attributes on the @implementation? If so we should
1102 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +00001103 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Craig Topperc3ec1492014-05-26 06:22:03 +00001104 ClassName, /*PrevDecl=*/nullptr, ClassLoc,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001105 true);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001106 IDecl->startDefinition();
Douglas Gregor16408322011-12-15 22:34:59 +00001107 if (SDecl) {
1108 IDecl->setSuperClass(SDecl);
1109 IDecl->setSuperClassLoc(SuperClassLoc);
1110 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1111 } else {
1112 IDecl->setEndOfDefinitionLoc(ClassLoc);
1113 }
1114
Douglas Gregorac345a32009-04-24 00:16:12 +00001115 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor1c283312010-08-11 12:19:30 +00001116 } else {
1117 // Mark the interface as being completed, even if it was just as
1118 // @class ....;
1119 // declaration; the user cannot reopen it.
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001120 if (!IDecl->hasDefinition())
1121 IDecl->startDefinition();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001122 }
Mike Stump11289f42009-09-09 15:08:12 +00001123
1124 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001125 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001126 ClassLoc, AtClassImplLoc, SuperClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001127
Anders Carlssona6b508a2008-11-04 16:57:32 +00001128 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001129 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001130
Chris Lattnerda463fe2007-12-12 07:09:47 +00001131 // Check that there is no duplicate implementation of this class.
Douglas Gregor1c283312010-08-11 12:19:30 +00001132 if (IDecl->getImplementation()) {
1133 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001134 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +00001135 Diag(IDecl->getImplementation()->getLocation(),
1136 diag::note_previous_definition);
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +00001137 IMPDecl->setInvalidDecl();
Douglas Gregor1c283312010-08-11 12:19:30 +00001138 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001139 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +00001140 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +00001141 // Warn on implementating deprecated class under
1142 // -Wdeprecated-implementations flag.
Fariborz Jahaniand724a102011-02-15 17:49:58 +00001143 DiagnoseObjCImplementedDeprecations(*this,
1144 dyn_cast<NamedDecl>(IDecl),
1145 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001146 }
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001147 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001148}
1149
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001150Sema::DeclGroupPtrTy
1151Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1152 SmallVector<Decl *, 64> DeclsInGroup;
1153 DeclsInGroup.reserve(Decls.size() + 1);
1154
1155 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1156 Decl *Dcl = Decls[i];
1157 if (!Dcl)
1158 continue;
1159 if (Dcl->getDeclContext()->isFileContext())
1160 Dcl->setTopLevelDeclInObjCContainer();
1161 DeclsInGroup.push_back(Dcl);
1162 }
1163
1164 DeclsInGroup.push_back(ObjCImpDecl);
1165
Rafael Espindolaab417692013-07-09 12:05:01 +00001166 return BuildDeclaratorGroup(DeclsInGroup, false);
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001167}
1168
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001169void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1170 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001171 SourceLocation RBrace) {
1172 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001173 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001174 if (!IDecl)
1175 return;
James Dennett634962f2012-06-14 21:40:34 +00001176 /// Check case of non-existing \@interface decl.
1177 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattnerda463fe2007-12-12 07:09:47 +00001178 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +00001179 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor16408322011-12-15 22:34:59 +00001180 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian20912d62010-02-17 17:00:07 +00001181 // Add ivar's to class's DeclContext.
1182 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanianc0309cd2010-02-17 18:10:54 +00001183 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith05afe5e2012-03-13 03:12:56 +00001184 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanianaef66222010-02-19 00:31:17 +00001185 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian20912d62010-02-17 17:00:07 +00001186 }
1187
Chris Lattnerda463fe2007-12-12 07:09:47 +00001188 return;
1189 }
1190 // If implementation has empty ivar list, just return.
1191 if (numIvars == 0)
1192 return;
Mike Stump11289f42009-09-09 15:08:12 +00001193
Chris Lattnerda463fe2007-12-12 07:09:47 +00001194 assert(ivars && "missing @implementation ivars");
John McCall5fb5df92012-06-20 06:18:46 +00001195 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001196 if (ImpDecl->getSuperClass())
1197 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1198 for (unsigned i = 0; i < numIvars; i++) {
1199 ObjCIvarDecl* ImplIvar = ivars[i];
1200 if (const ObjCIvarDecl *ClsIvar =
1201 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1202 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1203 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1204 continue;
1205 }
Fariborz Jahaniane23f26b2013-06-26 22:10:27 +00001206 // Check class extensions (unnamed categories) for duplicate ivars.
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001207 for (const auto *CDecl : IDecl->visible_extensions()) {
Fariborz Jahaniane23f26b2013-06-26 22:10:27 +00001208 if (const ObjCIvarDecl *ClsExtIvar =
1209 CDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1210 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1211 Diag(ClsExtIvar->getLocation(), diag::note_previous_definition);
1212 continue;
1213 }
1214 }
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001215 // Instance ivar to Implementation's DeclContext.
1216 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith05afe5e2012-03-13 03:12:56 +00001217 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001218 ImpDecl->addDecl(ImplIvar);
1219 }
1220 return;
1221 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001222 // Check interface's Ivar list against those in the implementation.
1223 // names and types must match.
1224 //
Chris Lattnerda463fe2007-12-12 07:09:47 +00001225 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001226 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +00001227 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1228 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001229 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie40ed2972012-06-06 20:45:41 +00001230 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001231 assert (ImplIvar && "missing implementation ivar");
1232 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +00001233
Steve Naroff157599f2009-03-03 14:49:36 +00001234 // First, make sure the types match.
Richard Smithcaf33902011-10-10 18:28:20 +00001235 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +00001236 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001237 << ImplIvar->getIdentifier()
1238 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +00001239 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smithcaf33902011-10-10 18:28:20 +00001240 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1241 ImplIvar->getBitWidthValue(Context) !=
1242 ClsIvar->getBitWidthValue(Context)) {
1243 Diag(ImplIvar->getBitWidth()->getLocStart(),
1244 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1245 Diag(ClsIvar->getBitWidth()->getLocStart(),
1246 diag::note_previous_definition);
Mike Stump11289f42009-09-09 15:08:12 +00001247 }
Steve Naroff157599f2009-03-03 14:49:36 +00001248 // Make sure the names are identical.
1249 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001250 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001251 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +00001252 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001253 }
1254 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001255 }
Mike Stump11289f42009-09-09 15:08:12 +00001256
Chris Lattner0f29d982007-12-12 18:11:49 +00001257 if (numIvars > 0)
Alp Tokerfff06742013-12-02 03:50:21 +00001258 Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +00001259 else if (IVI != IVE)
Alp Tokerfff06742013-12-02 03:50:21 +00001260 Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001261}
1262
Ted Kremenekf87decd2013-12-13 05:58:44 +00001263static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc,
1264 ObjCMethodDecl *method,
1265 bool &IncompleteImpl,
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001266 unsigned DiagID,
Craig Topperc3ec1492014-05-26 06:22:03 +00001267 NamedDecl *NeededFor = nullptr) {
Fariborz Jahanian9fc39c42011-06-24 20:31:37 +00001268 // No point warning no definition of method which is 'unavailable'.
Douglas Gregorc2e3d5c2012-12-11 18:53:07 +00001269 switch (method->getAvailability()) {
1270 case AR_Available:
1271 case AR_Deprecated:
1272 break;
1273
1274 // Don't warn about unavailable or not-yet-introduced methods.
1275 case AR_NotYetIntroduced:
1276 case AR_Unavailable:
Fariborz Jahanian9fc39c42011-06-24 20:31:37 +00001277 return;
Douglas Gregorc2e3d5c2012-12-11 18:53:07 +00001278 }
1279
Ted Kremenek65d63572013-03-27 00:02:21 +00001280 // FIXME: For now ignore 'IncompleteImpl'.
1281 // Previously we grouped all unimplemented methods under a single
1282 // warning, but some users strongly voiced that they would prefer
1283 // separate warnings. We will give that approach a try, as that
1284 // matches what we do with protocols.
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001285 {
1286 const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID);
1287 B << method;
1288 if (NeededFor)
1289 B << NeededFor;
1290 }
Ted Kremenek65d63572013-03-27 00:02:21 +00001291
1292 // Issue a note to the original declaration.
1293 SourceLocation MethodLoc = method->getLocStart();
1294 if (MethodLoc.isValid())
Ted Kremenekf87decd2013-12-13 05:58:44 +00001295 S.Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff15833ed2008-02-10 21:38:56 +00001296}
1297
David Chisnallb62d15c2010-10-25 17:23:52 +00001298/// Determines if type B can be substituted for type A. Returns true if we can
1299/// guarantee that anything that the user will do to an object of type A can
1300/// also be done to an object of type B. This is trivially true if the two
1301/// types are the same, or if B is a subclass of A. It becomes more complex
1302/// in cases where protocols are involved.
1303///
1304/// Object types in Objective-C describe the minimum requirements for an
1305/// object, rather than providing a complete description of a type. For
1306/// example, if A is a subclass of B, then B* may refer to an instance of A.
1307/// The principle of substitutability means that we may use an instance of A
1308/// anywhere that we may use an instance of B - it will implement all of the
1309/// ivars of B and all of the methods of B.
1310///
1311/// This substitutability is important when type checking methods, because
1312/// the implementation may have stricter type definitions than the interface.
1313/// The interface specifies minimum requirements, but the implementation may
1314/// have more accurate ones. For example, a method may privately accept
1315/// instances of B, but only publish that it accepts instances of A. Any
1316/// object passed to it will be type checked against B, and so will implicitly
1317/// by a valid A*. Similarly, a method may return a subclass of the class that
1318/// it is declared as returning.
1319///
1320/// This is most important when considering subclassing. A method in a
1321/// subclass must accept any object as an argument that its superclass's
1322/// implementation accepts. It may, however, accept a more general type
1323/// without breaking substitutability (i.e. you can still use the subclass
1324/// anywhere that you can use the superclass, but not vice versa). The
1325/// converse requirement applies to return types: the return type for a
1326/// subclass method must be a valid object of the kind that the superclass
1327/// advertises, but it may be specified more accurately. This avoids the need
1328/// for explicit down-casting by callers.
1329///
1330/// Note: This is a stricter requirement than for assignment.
John McCall071df462010-10-28 02:34:38 +00001331static bool isObjCTypeSubstitutable(ASTContext &Context,
1332 const ObjCObjectPointerType *A,
1333 const ObjCObjectPointerType *B,
1334 bool rejectId) {
1335 // Reject a protocol-unqualified id.
1336 if (rejectId && B->isObjCIdType()) return false;
David Chisnallb62d15c2010-10-25 17:23:52 +00001337
1338 // If B is a qualified id, then A must also be a qualified id and it must
1339 // implement all of the protocols in B. It may not be a qualified class.
1340 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1341 // stricter definition so it is not substitutable for id<A>.
1342 if (B->isObjCQualifiedIdType()) {
1343 return A->isObjCQualifiedIdType() &&
John McCall071df462010-10-28 02:34:38 +00001344 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1345 QualType(B,0),
1346 false);
David Chisnallb62d15c2010-10-25 17:23:52 +00001347 }
1348
1349 /*
1350 // id is a special type that bypasses type checking completely. We want a
1351 // warning when it is used in one place but not another.
1352 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1353
1354
1355 // If B is a qualified id, then A must also be a qualified id (which it isn't
1356 // if we've got this far)
1357 if (B->isObjCQualifiedIdType()) return false;
1358 */
1359
1360 // Now we know that A and B are (potentially-qualified) class types. The
1361 // normal rules for assignment apply.
John McCall071df462010-10-28 02:34:38 +00001362 return Context.canAssignObjCInterfaces(A, B);
David Chisnallb62d15c2010-10-25 17:23:52 +00001363}
1364
John McCall071df462010-10-28 02:34:38 +00001365static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1366 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1367}
1368
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001369static bool CheckMethodOverrideReturn(Sema &S,
John McCall071df462010-10-28 02:34:38 +00001370 ObjCMethodDecl *MethodImpl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001371 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001372 bool IsProtocolMethodDecl,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001373 bool IsOverridingMode,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001374 bool Warn) {
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001375 if (IsProtocolMethodDecl &&
1376 (MethodDecl->getObjCDeclQualifier() !=
1377 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001378 if (Warn) {
Alp Toker314cc812014-01-25 16:55:45 +00001379 S.Diag(MethodImpl->getLocation(),
1380 (IsOverridingMode
1381 ? diag::warn_conflicting_overriding_ret_type_modifiers
1382 : diag::warn_conflicting_ret_type_modifiers))
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001383 << MethodImpl->getDeclName()
Aaron Ballman41b10ac2014-08-01 13:20:09 +00001384 << MethodImpl->getReturnTypeSourceRange();
Alp Toker314cc812014-01-25 16:55:45 +00001385 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
Aaron Ballman41b10ac2014-08-01 13:20:09 +00001386 << MethodDecl->getReturnTypeSourceRange();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001387 }
1388 else
1389 return false;
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001390 }
Alp Toker314cc812014-01-25 16:55:45 +00001391
1392 if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(),
1393 MethodDecl->getReturnType()))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001394 return true;
1395 if (!Warn)
1396 return false;
John McCall071df462010-10-28 02:34:38 +00001397
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001398 unsigned DiagID =
1399 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1400 : diag::warn_conflicting_ret_types;
John McCall071df462010-10-28 02:34:38 +00001401
1402 // Mismatches between ObjC pointers go into a different warning
1403 // category, and sometimes they're even completely whitelisted.
1404 if (const ObjCObjectPointerType *ImplPtrTy =
Alp Toker314cc812014-01-25 16:55:45 +00001405 MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) {
John McCall071df462010-10-28 02:34:38 +00001406 if (const ObjCObjectPointerType *IfacePtrTy =
Alp Toker314cc812014-01-25 16:55:45 +00001407 MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) {
John McCall071df462010-10-28 02:34:38 +00001408 // Allow non-matching return types as long as they don't violate
1409 // the principle of substitutability. Specifically, we permit
1410 // return types that are subclasses of the declared return type,
1411 // or that are more-qualified versions of the declared type.
1412 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001413 return false;
John McCall071df462010-10-28 02:34:38 +00001414
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001415 DiagID =
1416 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1417 : diag::warn_non_covariant_ret_types;
John McCall071df462010-10-28 02:34:38 +00001418 }
1419 }
1420
1421 S.Diag(MethodImpl->getLocation(), DiagID)
Alp Toker314cc812014-01-25 16:55:45 +00001422 << MethodImpl->getDeclName() << MethodDecl->getReturnType()
1423 << MethodImpl->getReturnType()
Aaron Ballman41b10ac2014-08-01 13:20:09 +00001424 << MethodImpl->getReturnTypeSourceRange();
Alp Toker314cc812014-01-25 16:55:45 +00001425 S.Diag(MethodDecl->getLocation(), IsOverridingMode
1426 ? diag::note_previous_declaration
1427 : diag::note_previous_definition)
Aaron Ballman41b10ac2014-08-01 13:20:09 +00001428 << MethodDecl->getReturnTypeSourceRange();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001429 return false;
John McCall071df462010-10-28 02:34:38 +00001430}
1431
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001432static bool CheckMethodOverrideParam(Sema &S,
John McCall071df462010-10-28 02:34:38 +00001433 ObjCMethodDecl *MethodImpl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001434 ObjCMethodDecl *MethodDecl,
John McCall071df462010-10-28 02:34:38 +00001435 ParmVarDecl *ImplVar,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001436 ParmVarDecl *IfaceVar,
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001437 bool IsProtocolMethodDecl,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001438 bool IsOverridingMode,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001439 bool Warn) {
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001440 if (IsProtocolMethodDecl &&
1441 (ImplVar->getObjCDeclQualifier() !=
1442 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001443 if (Warn) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001444 if (IsOverridingMode)
1445 S.Diag(ImplVar->getLocation(),
1446 diag::warn_conflicting_overriding_param_modifiers)
1447 << getTypeRange(ImplVar->getTypeSourceInfo())
1448 << MethodImpl->getDeclName();
1449 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001450 diag::warn_conflicting_param_modifiers)
1451 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001452 << MethodImpl->getDeclName();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001453 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1454 << getTypeRange(IfaceVar->getTypeSourceInfo());
1455 }
1456 else
1457 return false;
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001458 }
1459
John McCall071df462010-10-28 02:34:38 +00001460 QualType ImplTy = ImplVar->getType();
1461 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001462
John McCall071df462010-10-28 02:34:38 +00001463 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001464 return true;
1465
1466 if (!Warn)
1467 return false;
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001468 unsigned DiagID =
1469 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1470 : diag::warn_conflicting_param_types;
John McCall071df462010-10-28 02:34:38 +00001471
1472 // Mismatches between ObjC pointers go into a different warning
1473 // category, and sometimes they're even completely whitelisted.
1474 if (const ObjCObjectPointerType *ImplPtrTy =
1475 ImplTy->getAs<ObjCObjectPointerType>()) {
1476 if (const ObjCObjectPointerType *IfacePtrTy =
1477 IfaceTy->getAs<ObjCObjectPointerType>()) {
1478 // Allow non-matching argument types as long as they don't
1479 // violate the principle of substitutability. Specifically, the
1480 // implementation must accept any objects that the superclass
1481 // accepts, however it may also accept others.
1482 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001483 return false;
John McCall071df462010-10-28 02:34:38 +00001484
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001485 DiagID =
1486 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1487 : diag::warn_non_contravariant_param_types;
John McCall071df462010-10-28 02:34:38 +00001488 }
1489 }
1490
1491 S.Diag(ImplVar->getLocation(), DiagID)
1492 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001493 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1494 S.Diag(IfaceVar->getLocation(),
1495 (IsOverridingMode ? diag::note_previous_declaration
1496 : diag::note_previous_definition))
John McCall071df462010-10-28 02:34:38 +00001497 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001498 return false;
John McCall071df462010-10-28 02:34:38 +00001499}
John McCall31168b02011-06-15 23:02:42 +00001500
1501/// In ARC, check whether the conventional meanings of the two methods
1502/// match. If they don't, it's a hard error.
1503static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1504 ObjCMethodDecl *decl) {
1505 ObjCMethodFamily implFamily = impl->getMethodFamily();
1506 ObjCMethodFamily declFamily = decl->getMethodFamily();
1507 if (implFamily == declFamily) return false;
1508
1509 // Since conventions are sorted by selector, the only possibility is
1510 // that the types differ enough to cause one selector or the other
1511 // to fall out of the family.
1512 assert(implFamily == OMF_None || declFamily == OMF_None);
1513
1514 // No further diagnostics required on invalid declarations.
1515 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1516
1517 const ObjCMethodDecl *unmatched = impl;
1518 ObjCMethodFamily family = declFamily;
1519 unsigned errorID = diag::err_arc_lost_method_convention;
1520 unsigned noteID = diag::note_arc_lost_method_convention;
1521 if (declFamily == OMF_None) {
1522 unmatched = decl;
1523 family = implFamily;
1524 errorID = diag::err_arc_gained_method_convention;
1525 noteID = diag::note_arc_gained_method_convention;
1526 }
1527
1528 // Indexes into a %select clause in the diagnostic.
1529 enum FamilySelector {
1530 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1531 };
1532 FamilySelector familySelector = FamilySelector();
1533
1534 switch (family) {
1535 case OMF_None: llvm_unreachable("logic error, no method convention");
1536 case OMF_retain:
1537 case OMF_release:
1538 case OMF_autorelease:
1539 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00001540 case OMF_finalize:
John McCall31168b02011-06-15 23:02:42 +00001541 case OMF_retainCount:
1542 case OMF_self:
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +00001543 case OMF_initialize:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001544 case OMF_performSelector:
John McCall31168b02011-06-15 23:02:42 +00001545 // Mismatches for these methods don't change ownership
1546 // conventions, so we don't care.
1547 return false;
1548
1549 case OMF_init: familySelector = F_init; break;
1550 case OMF_alloc: familySelector = F_alloc; break;
1551 case OMF_copy: familySelector = F_copy; break;
1552 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1553 case OMF_new: familySelector = F_new; break;
1554 }
1555
1556 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1557 ReasonSelector reasonSelector;
1558
1559 // The only reason these methods don't fall within their families is
1560 // due to unusual result types.
Alp Toker314cc812014-01-25 16:55:45 +00001561 if (unmatched->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00001562 reasonSelector = R_UnrelatedReturn;
1563 } else {
1564 reasonSelector = R_NonObjectReturn;
1565 }
1566
Joerg Sonnenbergerffc6d492013-06-26 21:31:47 +00001567 S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
1568 S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
John McCall31168b02011-06-15 23:02:42 +00001569
1570 return true;
1571}
John McCall071df462010-10-28 02:34:38 +00001572
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +00001573void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001574 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001575 bool IsProtocolMethodDecl) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001576 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00001577 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1578 return;
1579
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001580 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001581 IsProtocolMethodDecl, false,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001582 true);
Mike Stump11289f42009-09-09 15:08:12 +00001583
Chris Lattner67f35b02009-04-11 19:58:42 +00001584 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001585 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1586 EF = MethodDecl->param_end();
1587 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001588 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001589 IsProtocolMethodDecl, false, true);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001590 }
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001591
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001592 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001593 Diag(ImpMethodDecl->getLocation(),
1594 diag::warn_conflicting_variadic);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001595 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001596 }
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001597}
1598
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001599void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1600 ObjCMethodDecl *Overridden,
1601 bool IsProtocolMethodDecl) {
1602
1603 CheckMethodOverrideReturn(*this, Method, Overridden,
1604 IsProtocolMethodDecl, true,
1605 true);
1606
1607 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001608 IF = Overridden->param_begin(), EM = Method->param_end(),
1609 EF = Overridden->param_end();
1610 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001611 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1612 IsProtocolMethodDecl, true, true);
1613 }
1614
1615 if (Method->isVariadic() != Overridden->isVariadic()) {
1616 Diag(Method->getLocation(),
1617 diag::warn_conflicting_overriding_variadic);
1618 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1619 }
1620}
1621
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001622/// WarnExactTypedMethods - This routine issues a warning if method
1623/// implementation declaration matches exactly that of its declaration.
1624void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1625 ObjCMethodDecl *MethodDecl,
1626 bool IsProtocolMethodDecl) {
1627 // don't issue warning when protocol method is optional because primary
1628 // class is not required to implement it and it is safe for protocol
1629 // to implement it.
1630 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1631 return;
1632 // don't issue warning when primary class's method is
1633 // depecated/unavailable.
1634 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1635 MethodDecl->hasAttr<DeprecatedAttr>())
1636 return;
1637
1638 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1639 IsProtocolMethodDecl, false, false);
1640 if (match)
1641 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001642 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1643 EF = MethodDecl->param_end();
1644 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001645 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1646 *IM, *IF,
1647 IsProtocolMethodDecl, false, false);
1648 if (!match)
1649 break;
1650 }
1651 if (match)
1652 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall92918512011-08-08 17:32:19 +00001653 if (match)
1654 match = !(MethodDecl->isClassMethod() &&
1655 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001656
1657 if (match) {
1658 Diag(ImpMethodDecl->getLocation(),
1659 diag::warn_category_method_impl_match);
Ted Kremenek59b10db2012-02-27 22:55:11 +00001660 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1661 << MethodDecl->getDeclName();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001662 }
1663}
1664
Mike Stump87c57ac2009-05-16 07:39:55 +00001665/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1666/// improve the efficiency of selector lookups and type checking by associating
1667/// with each protocol / interface / category the flattened instance tables. If
1668/// we used an immutable set to keep the table then it wouldn't add significant
1669/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001670
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001671typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet;
Ahmed Charlesaf94d562014-03-09 11:34:25 +00001672typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet;
Ted Kremenek760a2ac2014-03-05 23:18:22 +00001673
1674static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl,
1675 ProtocolNameSet &PNS) {
1676 if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1677 PNS.insert(PDecl->getIdentifier());
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001678 for (const auto *PI : PDecl->protocols())
1679 findProtocolsWithExplicitImpls(PI, PNS);
Ted Kremenek760a2ac2014-03-05 23:18:22 +00001680}
1681
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001682/// Recursively populates a set with all conformed protocols in a class
1683/// hierarchy that have the 'objc_protocol_requires_explicit_implementation'
1684/// attribute.
1685static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super,
1686 ProtocolNameSet &PNS) {
1687 if (!Super)
1688 return;
1689
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001690 for (const auto *I : Super->all_referenced_protocols())
1691 findProtocolsWithExplicitImpls(I, PNS);
Ted Kremenek760a2ac2014-03-05 23:18:22 +00001692
1693 findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS);
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001694}
1695
Steve Naroffa36992242008-02-08 22:06:17 +00001696/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +00001697/// Declared in protocol, and those referenced by it.
Ted Kremenek285ee852013-12-13 06:26:10 +00001698static void CheckProtocolMethodDefs(Sema &S,
1699 SourceLocation ImpLoc,
1700 ObjCProtocolDecl *PDecl,
1701 bool& IncompleteImpl,
1702 const Sema::SelectorSet &InsMap,
1703 const Sema::SelectorSet &ClsMap,
Ted Kremenek33e430f2013-12-13 06:26:14 +00001704 ObjCContainerDecl *CDecl,
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001705 LazyProtocolNameSet &ProtocolsExplictImpl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001706 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1707 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1708 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +00001709 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1710
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001711 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Craig Topperc3ec1492014-05-26 06:22:03 +00001712 ObjCInterfaceDecl *NSIDecl = nullptr;
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001713
1714 // If this protocol is marked 'objc_protocol_requires_explicit_implementation'
1715 // then we should check if any class in the super class hierarchy also
1716 // conforms to this protocol, either directly or via protocol inheritance.
1717 // If so, we can skip checking this protocol completely because we
1718 // know that a parent class already satisfies this protocol.
1719 //
1720 // Note: we could generalize this logic for all protocols, and merely
1721 // add the limit on looking at the super class chain for just
1722 // specially marked protocols. This may be a good optimization. This
1723 // change is restricted to 'objc_protocol_requires_explicit_implementation'
1724 // protocols for now for controlled evaluation.
1725 if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) {
Ahmed Charlesaf94d562014-03-09 11:34:25 +00001726 if (!ProtocolsExplictImpl) {
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001727 ProtocolsExplictImpl.reset(new ProtocolNameSet);
1728 findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl);
1729 }
1730 if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) !=
1731 ProtocolsExplictImpl->end())
1732 return;
1733
1734 // If no super class conforms to the protocol, we should not search
1735 // for methods in the super class to implicitly satisfy the protocol.
Craig Topperc3ec1492014-05-26 06:22:03 +00001736 Super = nullptr;
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001737 }
1738
Ted Kremenek285ee852013-12-13 06:26:10 +00001739 if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump11289f42009-09-09 15:08:12 +00001740 // check to see if class implements forwardInvocation method and objects
1741 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001742 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +00001743 // Under such conditions, which means that every method possible is
1744 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001745 // found" warnings.
1746 // FIXME: Use a general GetUnarySelector method for this.
Ted Kremenek285ee852013-12-13 06:26:10 +00001747 IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation");
1748 Selector fISelector = S.Context.Selectors.getSelector(1, &II);
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001749 if (InsMap.count(fISelector))
1750 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1751 // need be implemented in the implementation.
Ted Kremenek285ee852013-12-13 06:26:10 +00001752 NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy"));
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001753 }
Mike Stump11289f42009-09-09 15:08:12 +00001754
Fariborz Jahanianc41cf052013-01-07 19:21:03 +00001755 // If this is a forward protocol declaration, get its definition.
1756 if (!PDecl->isThisDeclarationADefinition() &&
1757 PDecl->getDefinition())
1758 PDecl = PDecl->getDefinition();
1759
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001760 // If a method lookup fails locally we still need to look and see if
1761 // the method was implemented by a base class or an inherited
1762 // protocol. This lookup is slow, but occurs rarely in correct code
1763 // and otherwise would terminate in a warning.
1764
Chris Lattnerda463fe2007-12-12 07:09:47 +00001765 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001766 if (!NSIDecl)
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001767 for (auto *method : PDecl->instance_methods()) {
Mike Stump11289f42009-09-09 15:08:12 +00001768 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rosed01e83a2012-10-10 16:42:25 +00001769 !method->isPropertyAccessor() &&
1770 !InsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001771 (!Super || !Super->lookupMethod(method->getSelector(),
1772 true /* instance */,
1773 false /* shallowCategory */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001774 true /* followsSuper */,
Craig Topperc3ec1492014-05-26 06:22:03 +00001775 nullptr /* category */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001776 // If a method is not implemented in the category implementation but
1777 // has been declared in its primary class, superclass,
1778 // or in one of their protocols, no need to issue the warning.
1779 // This is because method will be implemented in the primary class
1780 // or one of its super class implementation.
1781
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001782 // Ugly, but necessary. Method declared in protcol might have
1783 // have been synthesized due to a property declared in the class which
1784 // uses the protocol.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001785 if (ObjCMethodDecl *MethodInClass =
Ted Kremenek00781502013-11-23 01:01:29 +00001786 IDecl->lookupMethod(method->getSelector(),
1787 true /* instance */,
1788 true /* shallowCategoryLookup */,
1789 false /* followSuper */))
Jordan Rosed01e83a2012-10-10 16:42:25 +00001790 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001791 continue;
1792 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001793 if (!S.Diags.isIgnored(DIAG, ImpLoc)) {
Ted Kremenek285ee852013-12-13 06:26:10 +00001794 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG,
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001795 PDecl);
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001796 }
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001797 }
1798 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001799 // check unimplemented class methods
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001800 for (auto *method : PDecl->class_methods()) {
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001801 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1802 !ClsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001803 (!Super || !Super->lookupMethod(method->getSelector(),
1804 false /* class method */,
1805 false /* shallowCategoryLookup */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001806 true /* followSuper */,
Craig Topperc3ec1492014-05-26 06:22:03 +00001807 nullptr /* category */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001808 // See above comment for instance method lookups.
Ted Kremenek00781502013-11-23 01:01:29 +00001809 if (C && IDecl->lookupMethod(method->getSelector(),
1810 false /* class */,
1811 true /* shallowCategoryLookup */,
1812 false /* followSuper */))
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001813 continue;
Ted Kremenek00781502013-11-23 01:01:29 +00001814
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001815 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00001816 if (!S.Diags.isIgnored(DIAG, ImpLoc)) {
Ted Kremenek285ee852013-12-13 06:26:10 +00001817 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl);
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001818 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001819 }
Steve Naroff3ce37a62007-12-14 23:37:57 +00001820 }
Chris Lattner390d39a2008-07-21 21:32:27 +00001821 // Check on this protocols's referenced protocols, recursively.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001822 for (auto *PI : PDecl->protocols())
1823 CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap,
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001824 CDecl, ProtocolsExplictImpl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001825}
1826
Fariborz Jahanianf9ae68a2011-07-16 00:08:33 +00001827/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001828/// or protocol against those declared in their implementations.
1829///
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001830void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1831 const SelectorSet &ClsMap,
1832 SelectorSet &InsMapSeen,
1833 SelectorSet &ClsMapSeen,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001834 ObjCImplDecl* IMPDecl,
1835 ObjCContainerDecl* CDecl,
1836 bool &IncompleteImpl,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001837 bool ImmediateClass,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001838 bool WarnCategoryMethodImpl) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001839 // Check and see if instance methods in class interface have been
1840 // implemented in the implementation class. If so, their types match.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001841 for (auto *I : CDecl->instance_methods()) {
David Blaikie82e95a32014-11-19 07:49:47 +00001842 if (!InsMapSeen.insert(I->getSelector()).second)
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001843 continue;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001844 if (!I->isPropertyAccessor() &&
1845 !InsMap.count(I->getSelector())) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001846 if (ImmediateClass)
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001847 WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001848 diag::warn_undef_method_impl);
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001849 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +00001850 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001851 ObjCMethodDecl *ImpMethodDecl =
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001852 IMPDecl->getInstanceMethod(I->getSelector());
1853 assert(CDecl->getInstanceMethod(I->getSelector()) &&
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001854 "Expected to find the method through lookup as well");
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001855 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001856 if (ImpMethodDecl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001857 if (!WarnCategoryMethodImpl)
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001858 WarnConflictingTypedMethods(ImpMethodDecl, I,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001859 isa<ObjCProtocolDecl>(CDecl));
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001860 else if (!I->isPropertyAccessor())
1861 WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001862 }
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001863 }
1864 }
Mike Stump11289f42009-09-09 15:08:12 +00001865
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001866 // Check and see if class methods in class interface have been
1867 // implemented in the implementation class. If so, their types match.
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001868 for (auto *I : CDecl->class_methods()) {
David Blaikie82e95a32014-11-19 07:49:47 +00001869 if (!ClsMapSeen.insert(I->getSelector()).second)
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001870 continue;
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001871 if (!ClsMap.count(I->getSelector())) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001872 if (ImmediateClass)
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001873 WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001874 diag::warn_undef_method_impl);
Mike Stump12b8ce12009-08-04 21:02:39 +00001875 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001876 ObjCMethodDecl *ImpMethodDecl =
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001877 IMPDecl->getClassMethod(I->getSelector());
1878 assert(CDecl->getClassMethod(I->getSelector()) &&
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001879 "Expected to find the method through lookup as well");
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001880 if (!WarnCategoryMethodImpl)
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001881 WarnConflictingTypedMethods(ImpMethodDecl, I,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001882 isa<ObjCProtocolDecl>(CDecl));
1883 else
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001884 WarnExactTypedMethods(ImpMethodDecl, I,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001885 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001886 }
1887 }
Fariborz Jahanian73853e52010-10-08 22:59:25 +00001888
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001889 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) {
1890 // Also, check for methods declared in protocols inherited by
1891 // this protocol.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001892 for (auto *PI : PD->protocols())
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001893 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001894 IMPDecl, PI, IncompleteImpl, false,
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001895 WarnCategoryMethodImpl);
1896 }
1897
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001898 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001899 // when checking that methods in implementation match their declaration,
1900 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1901 // extension; as well as those in categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001902 if (!WarnCategoryMethodImpl) {
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001903 for (auto *Cat : I->visible_categories())
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001904 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001905 IMPDecl, Cat, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001906 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001907 } else {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001908 // Also methods in class extensions need be looked at next.
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001909 for (auto *Ext : I->visible_extensions())
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001910 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001911 IMPDecl, Ext, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001912 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001913 }
1914
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001915 // Check for any implementation of a methods declared in protocol.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001916 for (auto *PI : I->all_referenced_protocols())
Mike Stump11289f42009-09-09 15:08:12 +00001917 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001918 IMPDecl, PI, IncompleteImpl, false,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001919 WarnCategoryMethodImpl);
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001920
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001921 // FIXME. For now, we are not checking for extact match of methods
1922 // in category implementation and its primary class's super class.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001923 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001924 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +00001925 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001926 I->getSuperClass(), IncompleteImpl, false);
1927 }
1928}
1929
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001930/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1931/// category matches with those implemented in its primary class and
1932/// warns each time an exact match is found.
1933void Sema::CheckCategoryVsClassMethodMatches(
1934 ObjCCategoryImplDecl *CatIMPDecl) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001935 // Get category's primary class.
1936 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1937 if (!CatDecl)
1938 return;
1939 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1940 if (!IDecl)
1941 return;
Fariborz Jahanianf3077a22013-12-05 20:52:31 +00001942 ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass();
1943 SelectorSet InsMap, ClsMap;
1944
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001945 for (const auto *I : CatIMPDecl->instance_methods()) {
1946 Selector Sel = I->getSelector();
Fariborz Jahanianf3077a22013-12-05 20:52:31 +00001947 // When checking for methods implemented in the category, skip over
1948 // those declared in category class's super class. This is because
1949 // the super class must implement the method.
1950 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true))
1951 continue;
1952 InsMap.insert(Sel);
1953 }
1954
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001955 for (const auto *I : CatIMPDecl->class_methods()) {
1956 Selector Sel = I->getSelector();
Fariborz Jahanianf3077a22013-12-05 20:52:31 +00001957 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false))
1958 continue;
1959 ClsMap.insert(Sel);
1960 }
1961 if (InsMap.empty() && ClsMap.empty())
1962 return;
1963
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001964 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001965 bool IncompleteImpl = false;
1966 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1967 CatIMPDecl, IDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001968 IncompleteImpl, false,
1969 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001970}
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001971
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001972void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump11289f42009-09-09 15:08:12 +00001973 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001974 bool IncompleteImpl) {
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001975 SelectorSet InsMap;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001976 // Check and see if instance methods in class interface have been
1977 // implemented in the implementation class.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001978 for (const auto *I : IMPDecl->instance_methods())
1979 InsMap.insert(I->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001980
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001981 // Check and see if properties declared in the interface have either 1)
1982 // an implementation or 2) there is a @synthesize/@dynamic implementation
1983 // of the property in the @implementation.
Ted Kremenek348e88c2014-02-21 19:41:34 +00001984 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1985 bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties &&
1986 LangOpts.ObjCRuntime.isNonFragile() &&
1987 !IDecl->isObjCRequiresPropertyDefs();
1988 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties);
1989 }
1990
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001991 SelectorSet ClsMap;
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001992 for (const auto *I : IMPDecl->class_methods())
1993 ClsMap.insert(I->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001994
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001995 // Check for type conflict of methods declared in a class/protocol and
1996 // its implementation; if any.
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001997 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +00001998 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1999 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00002000 IncompleteImpl, true);
Fariborz Jahanian2bda1b62011-08-03 18:21:12 +00002001
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00002002 // check all methods implemented in category against those declared
2003 // in its primary class.
2004 if (ObjCCategoryImplDecl *CatDecl =
2005 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
2006 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump11289f42009-09-09 15:08:12 +00002007
Chris Lattnerda463fe2007-12-12 07:09:47 +00002008 // Check the protocol list for unimplemented methods in the @implementation
2009 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +00002010 // Check and see if class methods in class interface have been
2011 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00002012
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00002013 LazyProtocolNameSet ExplicitImplProtocols;
2014
Chris Lattner9ef10f42009-03-01 00:56:52 +00002015 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +00002016 for (auto *PI : I->all_referenced_protocols())
2017 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl,
2018 InsMap, ClsMap, I, ExplicitImplProtocols);
Chris Lattner9ef10f42009-03-01 00:56:52 +00002019 // Check class extensions (unnamed categories)
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00002020 for (auto *Ext : I->visible_extensions())
2021 ImplMethodsVsClassMethods(S, IMPDecl, Ext, IncompleteImpl);
Chris Lattner9ef10f42009-03-01 00:56:52 +00002022 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00002023 // For extended class, unimplemented methods in its protocols will
2024 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002025 if (!C->IsClassExtension()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00002026 for (auto *P : C->protocols())
2027 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P,
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00002028 IncompleteImpl, InsMap, ClsMap, CDecl,
2029 ExplicitImplProtocols);
Ted Kremenek348e88c2014-02-21 19:41:34 +00002030 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl,
Nico Weber2e0c8f72014-12-27 03:58:08 +00002031 /*SynthesizeProperties=*/false);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00002032 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00002033 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002034 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00002035}
2036
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00002037Sema::DeclGroupPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00002038Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00002039 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00002040 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00002041 unsigned NumElts) {
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00002042 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002043 for (unsigned i = 0; i != NumElts; ++i) {
2044 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00002045 NamedDecl *PrevDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002046 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorb8eaf292010-04-15 23:40:53 +00002047 LookupOrdinaryName, ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002048 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00002049 // GCC apparently allows the following idiom:
2050 //
2051 // typedef NSObject < XCElementTogglerP > XCElementToggler;
2052 // @class XCElementToggler;
2053 //
Fariborz Jahanian04c44552012-01-24 00:40:15 +00002054 // Here we have chosen to ignore the forward class declaration
2055 // with a warning. Since this is the implied behavior.
Richard Smithdda56e42011-04-15 14:24:37 +00002056 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCall8b07ec22010-05-15 11:32:37 +00002057 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002058 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00002059 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCall8b07ec22010-05-15 11:32:37 +00002060 } else {
Mike Stump12b8ce12009-08-04 21:02:39 +00002061 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahanian04c44552012-01-24 00:40:15 +00002062 // to the underlying class. Just ignore the forward class with a warning
Nico Weber2e0c8f72014-12-27 03:58:08 +00002063 // as this will force the intended behavior which is to lookup the
2064 // typedef name.
Fariborz Jahanian04c44552012-01-24 00:40:15 +00002065 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
Nico Weber2e0c8f72014-12-27 03:58:08 +00002066 Diag(AtClassLoc, diag::warn_forward_class_redefinition)
2067 << IdentList[i];
Fariborz Jahanian04c44552012-01-24 00:40:15 +00002068 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
2069 continue;
2070 }
Fariborz Jahanian0d451812009-05-07 21:49:26 +00002071 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002072 }
Douglas Gregordc9166c2011-12-15 20:29:51 +00002073
2074 // Create a declaration to describe this forward declaration.
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00002075 ObjCInterfaceDecl *PrevIDecl
2076 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002077
2078 IdentifierInfo *ClassName = IdentList[i];
2079 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
2080 // A previous decl with a different name is because of
2081 // @compatibility_alias, for example:
2082 // \code
2083 // @class NewImage;
2084 // @compatibility_alias OldImage NewImage;
2085 // \endcode
2086 // A lookup for 'OldImage' will return the 'NewImage' decl.
2087 //
2088 // In such a case use the real declaration name, instead of the alias one,
2089 // otherwise we will break IdentifierResolver and redecls-chain invariants.
2090 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
2091 // has been aliased.
2092 ClassName = PrevIDecl->getIdentifier();
2093 }
2094
Douglas Gregordc9166c2011-12-15 20:29:51 +00002095 ObjCInterfaceDecl *IDecl
2096 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002097 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002098 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002099
Douglas Gregordc9166c2011-12-15 20:29:51 +00002100 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeafd0b2011-12-27 22:43:10 +00002101 CheckObjCDeclScope(IDecl);
2102 DeclsInGroup.push_back(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002103 }
Rafael Espindolaab417692013-07-09 12:05:01 +00002104
2105 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002106}
2107
John McCall54507ab2011-06-16 01:15:19 +00002108static bool tryMatchRecordTypes(ASTContext &Context,
2109 Sema::MethodMatchStrategy strategy,
2110 const Type *left, const Type *right);
2111
John McCall31168b02011-06-15 23:02:42 +00002112static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
2113 QualType leftQT, QualType rightQT) {
2114 const Type *left =
2115 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2116 const Type *right =
2117 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2118
2119 if (left == right) return true;
2120
2121 // If we're doing a strict match, the types have to match exactly.
2122 if (strategy == Sema::MMS_strict) return false;
2123
2124 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2125
2126 // Otherwise, use this absurdly complicated algorithm to try to
2127 // validate the basic, low-level compatibility of the two types.
2128
2129 // As a minimum, require the sizes and alignments to match.
David Majnemer34b57492014-07-30 01:30:47 +00002130 TypeInfo LeftTI = Context.getTypeInfo(left);
2131 TypeInfo RightTI = Context.getTypeInfo(right);
2132 if (LeftTI.Width != RightTI.Width)
2133 return false;
2134
2135 if (LeftTI.Align != RightTI.Align)
John McCall31168b02011-06-15 23:02:42 +00002136 return false;
2137
2138 // Consider all the kinds of non-dependent canonical types:
2139 // - functions and arrays aren't possible as return and parameter types
2140
2141 // - vector types of equal size can be arbitrarily mixed
2142 if (isa<VectorType>(left)) return isa<VectorType>(right);
2143 if (isa<VectorType>(right)) return false;
2144
2145 // - references should only match references of identical type
John McCall54507ab2011-06-16 01:15:19 +00002146 // - structs, unions, and Objective-C objects must match more-or-less
2147 // exactly
John McCall31168b02011-06-15 23:02:42 +00002148 // - everything else should be a scalar
2149 if (!left->isScalarType() || !right->isScalarType())
John McCall54507ab2011-06-16 01:15:19 +00002150 return tryMatchRecordTypes(Context, strategy, left, right);
John McCall31168b02011-06-15 23:02:42 +00002151
John McCall9320b872011-09-09 05:25:32 +00002152 // Make scalars agree in kind, except count bools as chars, and group
2153 // all non-member pointers together.
John McCall31168b02011-06-15 23:02:42 +00002154 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2155 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2156 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2157 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall9320b872011-09-09 05:25:32 +00002158 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2159 leftSK = Type::STK_ObjCObjectPointer;
2160 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2161 rightSK = Type::STK_ObjCObjectPointer;
John McCall31168b02011-06-15 23:02:42 +00002162
2163 // Note that data member pointers and function member pointers don't
2164 // intermix because of the size differences.
2165
2166 return (leftSK == rightSK);
2167}
Chris Lattnerda463fe2007-12-12 07:09:47 +00002168
John McCall54507ab2011-06-16 01:15:19 +00002169static bool tryMatchRecordTypes(ASTContext &Context,
2170 Sema::MethodMatchStrategy strategy,
2171 const Type *lt, const Type *rt) {
2172 assert(lt && rt && lt != rt);
2173
2174 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2175 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2176 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2177
2178 // Require union-hood to match.
2179 if (left->isUnion() != right->isUnion()) return false;
2180
2181 // Require an exact match if either is non-POD.
2182 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2183 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2184 return false;
2185
2186 // Require size and alignment to match.
David Majnemer34b57492014-07-30 01:30:47 +00002187 TypeInfo LeftTI = Context.getTypeInfo(lt);
2188 TypeInfo RightTI = Context.getTypeInfo(rt);
2189 if (LeftTI.Width != RightTI.Width)
2190 return false;
2191
2192 if (LeftTI.Align != RightTI.Align)
2193 return false;
John McCall54507ab2011-06-16 01:15:19 +00002194
2195 // Require fields to match.
2196 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2197 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2198 for (; li != le && ri != re; ++li, ++ri) {
2199 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2200 return false;
2201 }
2202 return (li == le && ri == re);
2203}
2204
Chris Lattnerda463fe2007-12-12 07:09:47 +00002205/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2206/// returns true, or false, accordingly.
2207/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCall31168b02011-06-15 23:02:42 +00002208bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2209 const ObjCMethodDecl *right,
2210 MethodMatchStrategy strategy) {
Alp Toker314cc812014-01-25 16:55:45 +00002211 if (!matchTypes(Context, strategy, left->getReturnType(),
2212 right->getReturnType()))
John McCall31168b02011-06-15 23:02:42 +00002213 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002214
Douglas Gregor560b7fa2013-02-07 19:13:24 +00002215 // If either is hidden, it is not considered to match.
2216 if (left->isHidden() || right->isHidden())
2217 return false;
2218
David Blaikiebbafb8a2012-03-11 07:00:24 +00002219 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002220 (left->hasAttr<NSReturnsRetainedAttr>()
2221 != right->hasAttr<NSReturnsRetainedAttr>() ||
2222 left->hasAttr<NSConsumesSelfAttr>()
2223 != right->hasAttr<NSConsumesSelfAttr>()))
2224 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002225
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002226 ObjCMethodDecl::param_const_iterator
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002227 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2228 re = right->param_end();
Mike Stump11289f42009-09-09 15:08:12 +00002229
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002230 for (; li != le && ri != re; ++li, ++ri) {
John McCall31168b02011-06-15 23:02:42 +00002231 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002232 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCall31168b02011-06-15 23:02:42 +00002233
2234 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2235 return false;
2236
David Blaikiebbafb8a2012-03-11 07:00:24 +00002237 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002238 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2239 return false;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002240 }
2241 return true;
2242}
2243
Nico Weber2e0c8f72014-12-27 03:58:08 +00002244void Sema::addMethodToGlobalList(ObjCMethodList *List,
2245 ObjCMethodDecl *Method) {
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002246 // Record at the head of the list whether there were 0, 1, or >= 2 methods
2247 // inside categories.
Nico Weber2e0c8f72014-12-27 03:58:08 +00002248 if (ObjCCategoryDecl *CD =
2249 dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00002250 if (!CD->IsClassExtension() && List->getBits() < 2)
Nico Weber2e0c8f72014-12-27 03:58:08 +00002251 List->setBits(List->getBits() + 1);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002252
Douglas Gregorc454afe2012-01-25 00:19:56 +00002253 // If the list is empty, make it a singleton list.
Nico Weber2e0c8f72014-12-27 03:58:08 +00002254 if (List->getMethod() == nullptr) {
2255 List->setMethod(Method);
Craig Topperc3ec1492014-05-26 06:22:03 +00002256 List->setNext(nullptr);
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002257 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002258 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00002259
Douglas Gregorc454afe2012-01-25 00:19:56 +00002260 // We've seen a method with this name, see if we have already seen this type
2261 // signature.
2262 ObjCMethodList *Previous = List;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002263 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregor600a2f52013-06-21 00:20:25 +00002264 // If we are building a module, keep all of the methods.
2265 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2266 continue;
2267
Fariborz Jahaniand436b2a2015-04-07 16:56:27 +00002268 if (!MatchTwoMethodDeclarations(Method, List->getMethod())) {
2269 // Even if two method types do not match, we would like to say
2270 // there is more than one declaration so unavailability/deprecated
2271 // warning is not too noisy.
2272 if (!Method->isDefined())
2273 List->setHasMoreThanOneDecl(true);
Douglas Gregorc454afe2012-01-25 00:19:56 +00002274 continue;
Fariborz Jahaniand436b2a2015-04-07 16:56:27 +00002275 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00002276
2277 ObjCMethodDecl *PrevObjCMethod = List->getMethod();
Douglas Gregorc454afe2012-01-25 00:19:56 +00002278
2279 // Propagate the 'defined' bit.
2280 if (Method->isDefined())
2281 PrevObjCMethod->setDefined(true);
Nico Webere3b11042014-12-27 07:09:37 +00002282 else {
Nico Weber2e0c8f72014-12-27 03:58:08 +00002283 // Objective-C doesn't allow an @interface for a class after its
2284 // @implementation. So if Method is not defined and there already is
2285 // an entry for this type signature, Method has to be for a different
2286 // class than PrevObjCMethod.
2287 List->setHasMoreThanOneDecl(true);
2288 }
2289
Douglas Gregorc454afe2012-01-25 00:19:56 +00002290 // If a method is deprecated, push it in the global pool.
2291 // This is used for better diagnostics.
2292 if (Method->isDeprecated()) {
2293 if (!PrevObjCMethod->isDeprecated())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002294 List->setMethod(Method);
Douglas Gregorc454afe2012-01-25 00:19:56 +00002295 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00002296 // If the new method is unavailable, push it into global pool
Douglas Gregorc454afe2012-01-25 00:19:56 +00002297 // unless previous one is deprecated.
2298 if (Method->isUnavailable()) {
2299 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Nico Weber2e0c8f72014-12-27 03:58:08 +00002300 List->setMethod(Method);
Douglas Gregorc454afe2012-01-25 00:19:56 +00002301 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00002302
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002303 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002304 }
Nico Weber2e0c8f72014-12-27 03:58:08 +00002305
Douglas Gregorc454afe2012-01-25 00:19:56 +00002306 // We have a new signature for an existing method - add it.
2307 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregore1716012012-01-25 00:49:42 +00002308 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Nico Weber2e0c8f72014-12-27 03:58:08 +00002309 Previous->setNext(new (Mem) ObjCMethodList(Method));
Douglas Gregorc454afe2012-01-25 00:19:56 +00002310}
2311
Sebastian Redl75d8a322010-08-02 23:18:59 +00002312/// \brief Read the contents of the method pool for a given selector from
2313/// external storage.
Douglas Gregore1716012012-01-25 00:49:42 +00002314void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002315 assert(ExternalSource && "We need an external AST source");
Douglas Gregore1716012012-01-25 00:49:42 +00002316 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002317}
2318
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002319void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002320 bool instance) {
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002321 // Ignore methods of invalid containers.
2322 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002323 return;
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002324
Douglas Gregor70f449b2012-01-25 00:59:09 +00002325 if (ExternalSource)
2326 ReadMethodPool(Method->getSelector());
2327
Sebastian Redl75d8a322010-08-02 23:18:59 +00002328 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor70f449b2012-01-25 00:59:09 +00002329 if (Pos == MethodPool.end())
2330 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2331 GlobalMethods())).first;
Nico Weber2e0c8f72014-12-27 03:58:08 +00002332
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002333 Method->setDefined(impl);
Douglas Gregorc454afe2012-01-25 00:19:56 +00002334
Sebastian Redl75d8a322010-08-02 23:18:59 +00002335 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002336 addMethodToGlobalList(&Entry, Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002337}
2338
John McCall31168b02011-06-15 23:02:42 +00002339/// Determines if this is an "acceptable" loose mismatch in the global
2340/// method pool. This exists mostly as a hack to get around certain
2341/// global mismatches which we can't afford to make warnings / errors.
2342/// Really, what we want is a way to take a method out of the global
2343/// method pool.
2344static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2345 ObjCMethodDecl *other) {
2346 if (!chosen->isInstanceMethod())
2347 return false;
2348
2349 Selector sel = chosen->getSelector();
2350 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2351 return false;
2352
2353 // Don't complain about mismatches for -length if the method we
2354 // chose has an integral result type.
Alp Toker314cc812014-01-25 16:55:45 +00002355 return (chosen->getReturnType()->isIntegerType());
John McCall31168b02011-06-15 23:02:42 +00002356}
2357
Nico Weber2e0c8f72014-12-27 03:58:08 +00002358bool Sema::CollectMultipleMethodsInGlobalPool(
2359 Selector Sel, SmallVectorImpl<ObjCMethodDecl *> &Methods, bool instance) {
Fariborz Jahanian30ae8d42014-08-13 21:07:35 +00002360 if (ExternalSource)
2361 ReadMethodPool(Sel);
2362
2363 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2364 if (Pos == MethodPool.end())
2365 return false;
2366 // Gather the non-hidden methods.
2367 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
2368 for (ObjCMethodList *M = &MethList; M; M = M->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002369 if (M->getMethod() && !M->getMethod()->isHidden())
2370 Methods.push_back(M->getMethod());
2371 return Methods.size() > 1;
Fariborz Jahanian30ae8d42014-08-13 21:07:35 +00002372}
2373
Fariborz Jahanian890803f2015-04-15 17:26:21 +00002374bool Sema::AreMultipleMethodsInGlobalPool(Selector Sel, ObjCMethodDecl *BestMethod,
2375 SourceRange R,
2376 bool receiverIdOrClass) {
Fariborz Jahanianc62d16f2014-11-13 22:27:05 +00002377 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Nico Weber2e0c8f72014-12-27 03:58:08 +00002378 // Test for no method in the pool which should not trigger any warning by
2379 // caller.
Fariborz Jahanianc62d16f2014-11-13 22:27:05 +00002380 if (Pos == MethodPool.end())
2381 return true;
Fariborz Jahanian890803f2015-04-15 17:26:21 +00002382 ObjCMethodList &MethList =
2383 BestMethod->isInstanceMethod() ? Pos->second.first : Pos->second.second;
2384
2385 // Diagnose finding more than one method in global pool
2386 SmallVector<ObjCMethodDecl *, 4> Methods;
2387 Methods.push_back(BestMethod);
Jonathan Roelofs74411362015-04-28 18:04:44 +00002388 for (ObjCMethodList *ML = &MethList; ML; ML = ML->getNext())
2389 if (ObjCMethodDecl *M = ML->getMethod())
2390 if (!M->isHidden() && M != BestMethod && !M->hasAttr<UnavailableAttr>())
2391 Methods.push_back(M);
Fariborz Jahanian890803f2015-04-15 17:26:21 +00002392 if (Methods.size() > 1)
2393 DiagnoseMultipleMethodInGlobalPool(Methods, Sel, R, receiverIdOrClass);
2394
Nico Weber2e0c8f72014-12-27 03:58:08 +00002395 return MethList.hasMoreThanOneDecl();
Fariborz Jahanianc62d16f2014-11-13 22:27:05 +00002396}
2397
Sebastian Redl75d8a322010-08-02 23:18:59 +00002398ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00002399 bool receiverIdOrClass,
Fariborz Jahanian890803f2015-04-15 17:26:21 +00002400 bool instance) {
Douglas Gregor70f449b2012-01-25 00:59:09 +00002401 if (ExternalSource)
2402 ReadMethodPool(Sel);
2403
Sebastian Redl75d8a322010-08-02 23:18:59 +00002404 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor70f449b2012-01-25 00:59:09 +00002405 if (Pos == MethodPool.end())
Craig Topperc3ec1492014-05-26 06:22:03 +00002406 return nullptr;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002407
Douglas Gregor77f49a42013-01-16 18:47:38 +00002408 // Gather the non-hidden methods.
Sebastian Redl75d8a322010-08-02 23:18:59 +00002409 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Robert Wilhelmb869a8f2013-08-10 12:33:24 +00002410 SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002411 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Fariborz Jahanian890803f2015-04-15 17:26:21 +00002412 if (M->getMethod() && !M->getMethod()->isHidden())
2413 return M->getMethod();
Douglas Gregorc78d3462009-04-24 21:10:55 +00002414 }
Fariborz Jahanian890803f2015-04-15 17:26:21 +00002415 return nullptr;
2416}
Douglas Gregor77f49a42013-01-16 18:47:38 +00002417
Fariborz Jahanian890803f2015-04-15 17:26:21 +00002418void Sema::DiagnoseMultipleMethodInGlobalPool(SmallVectorImpl<ObjCMethodDecl*> &Methods,
2419 Selector Sel, SourceRange R,
2420 bool receiverIdOrClass) {
Douglas Gregor77f49a42013-01-16 18:47:38 +00002421 // We found multiple methods, so we may have to complain.
2422 bool issueDiagnostic = false, issueError = false;
Jonathan Roelofs74411362015-04-28 18:04:44 +00002423
Douglas Gregor77f49a42013-01-16 18:47:38 +00002424 // We support a warning which complains about *any* difference in
2425 // method signature.
2426 bool strictSelectorMatch =
Fariborz Jahanian890803f2015-04-15 17:26:21 +00002427 receiverIdOrClass &&
2428 !Diags.isIgnored(diag::warn_strict_multiple_method_decl, R.getBegin());
Douglas Gregor77f49a42013-01-16 18:47:38 +00002429 if (strictSelectorMatch) {
2430 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2431 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2432 issueDiagnostic = true;
2433 break;
2434 }
2435 }
2436 }
Jonathan Roelofs74411362015-04-28 18:04:44 +00002437
Douglas Gregor77f49a42013-01-16 18:47:38 +00002438 // If we didn't see any strict differences, we won't see any loose
2439 // differences. In ARC, however, we also need to check for loose
2440 // mismatches, because most of them are errors.
2441 if (!strictSelectorMatch ||
2442 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2443 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2444 // This checks if the methods differ in type mismatch.
2445 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2446 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2447 issueDiagnostic = true;
2448 if (getLangOpts().ObjCAutoRefCount)
2449 issueError = true;
2450 break;
2451 }
2452 }
Fariborz Jahanian890803f2015-04-15 17:26:21 +00002453
Douglas Gregor77f49a42013-01-16 18:47:38 +00002454 if (issueDiagnostic) {
2455 if (issueError)
2456 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2457 else if (strictSelectorMatch)
2458 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2459 else
2460 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
Fariborz Jahanian890803f2015-04-15 17:26:21 +00002461
Douglas Gregor77f49a42013-01-16 18:47:38 +00002462 Diag(Methods[0]->getLocStart(),
2463 issueError ? diag::note_possibility : diag::note_using)
Fariborz Jahanian890803f2015-04-15 17:26:21 +00002464 << Methods[0]->getSourceRange();
Douglas Gregor77f49a42013-01-16 18:47:38 +00002465 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2466 Diag(Methods[I]->getLocStart(), diag::note_also_found)
Fariborz Jahanian890803f2015-04-15 17:26:21 +00002467 << Methods[I]->getSourceRange();
2468 }
Douglas Gregor77f49a42013-01-16 18:47:38 +00002469 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00002470}
2471
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002472ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redl75d8a322010-08-02 23:18:59 +00002473 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2474 if (Pos == MethodPool.end())
Craig Topperc3ec1492014-05-26 06:22:03 +00002475 return nullptr;
Sebastian Redl75d8a322010-08-02 23:18:59 +00002476
2477 GlobalMethods &Methods = Pos->second;
Fariborz Jahanianec762bd2014-03-26 20:59:26 +00002478 for (const ObjCMethodList *Method = &Methods.first; Method;
2479 Method = Method->getNext())
Fariborz Jahanian4019c7f2015-02-19 21:52:41 +00002480 if (Method->getMethod() &&
2481 (Method->getMethod()->isDefined() ||
2482 Method->getMethod()->isPropertyAccessor()))
Nico Weber2e0c8f72014-12-27 03:58:08 +00002483 return Method->getMethod();
Fariborz Jahanianec762bd2014-03-26 20:59:26 +00002484
2485 for (const ObjCMethodList *Method = &Methods.second; Method;
2486 Method = Method->getNext())
Fariborz Jahanian4019c7f2015-02-19 21:52:41 +00002487 if (Method->getMethod() &&
2488 (Method->getMethod()->isDefined() ||
2489 Method->getMethod()->isPropertyAccessor()))
Nico Weber2e0c8f72014-12-27 03:58:08 +00002490 return Method->getMethod();
Craig Topperc3ec1492014-05-26 06:22:03 +00002491 return nullptr;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002492}
2493
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002494static void
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002495HelperSelectorsForTypoCorrection(
2496 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2497 StringRef Typo, const ObjCMethodDecl * Method) {
2498 const unsigned MaxEditDistance = 1;
2499 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieuea8d3702013-06-06 02:22:29 +00002500 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002501
2502 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2503 if (MinPossibleEditDistance > 0 &&
2504 Typo.size() / MinPossibleEditDistance < 1)
2505 return;
2506 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2507 if (EditDistance > MaxEditDistance)
2508 return;
2509 if (EditDistance == BestEditDistance)
2510 BestMethod.push_back(Method);
2511 else if (EditDistance < BestEditDistance) {
2512 BestMethod.clear();
2513 BestMethod.push_back(Method);
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002514 }
2515}
2516
Fariborz Jahanian75481672013-06-17 17:10:54 +00002517static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2518 QualType ObjectType) {
2519 if (ObjectType.isNull())
2520 return true;
2521 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2522 return true;
Craig Topperc3ec1492014-05-26 06:22:03 +00002523 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) !=
2524 nullptr;
Fariborz Jahanian75481672013-06-17 17:10:54 +00002525}
2526
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002527const ObjCMethodDecl *
Fariborz Jahanian75481672013-06-17 17:10:54 +00002528Sema::SelectorsForTypoCorrection(Selector Sel,
2529 QualType ObjectType) {
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002530 unsigned NumArgs = Sel.getNumArgs();
2531 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002532 bool ObjectIsId = true, ObjectIsClass = true;
2533 if (ObjectType.isNull())
2534 ObjectIsId = ObjectIsClass = false;
2535 else if (!ObjectType->isObjCObjectPointerType())
Craig Topperc3ec1492014-05-26 06:22:03 +00002536 return nullptr;
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002537 else if (const ObjCObjectPointerType *ObjCPtr =
2538 ObjectType->getAsObjCInterfacePointerType()) {
2539 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2540 ObjectIsId = ObjectIsClass = false;
2541 }
2542 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2543 ObjectIsClass = false;
2544 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2545 ObjectIsId = false;
2546 else
Craig Topperc3ec1492014-05-26 06:22:03 +00002547 return nullptr;
2548
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002549 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2550 e = MethodPool.end(); b != e; b++) {
2551 // instance methods
2552 for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002553 if (M->getMethod() &&
2554 (M->getMethod()->getSelector().getNumArgs() == NumArgs) &&
2555 (M->getMethod()->getSelector() != Sel)) {
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002556 if (ObjectIsId)
Nico Weber2e0c8f72014-12-27 03:58:08 +00002557 Methods.push_back(M->getMethod());
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002558 else if (!ObjectIsClass &&
Nico Weber2e0c8f72014-12-27 03:58:08 +00002559 HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(),
2560 ObjectType))
2561 Methods.push_back(M->getMethod());
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002562 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002563 // class methods
2564 for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
Nico Weber2e0c8f72014-12-27 03:58:08 +00002565 if (M->getMethod() &&
2566 (M->getMethod()->getSelector().getNumArgs() == NumArgs) &&
2567 (M->getMethod()->getSelector() != Sel)) {
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002568 if (ObjectIsClass)
Nico Weber2e0c8f72014-12-27 03:58:08 +00002569 Methods.push_back(M->getMethod());
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002570 else if (!ObjectIsId &&
Nico Weber2e0c8f72014-12-27 03:58:08 +00002571 HelperIsMethodInObjCType(*this, M->getMethod()->getSelector(),
2572 ObjectType))
2573 Methods.push_back(M->getMethod());
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002574 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002575 }
2576
2577 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2578 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2579 HelperSelectorsForTypoCorrection(SelectedMethods,
2580 Sel.getAsString(), Methods[i]);
2581 }
Craig Topperc3ec1492014-05-26 06:22:03 +00002582 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : nullptr;
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002583}
2584
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002585/// DiagnoseDuplicateIvars -
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002586/// Check for duplicate ivars in the entire class at the start of
James Dennett634962f2012-06-14 21:40:34 +00002587/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002588/// add ivars to a class in random order which will not be known until
James Dennett634962f2012-06-14 21:40:34 +00002589/// class's \@implementation is seen.
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002590void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2591 ObjCInterfaceDecl *SID) {
Aaron Ballman59abbd42014-03-13 21:09:43 +00002592 for (auto *Ivar : ID->ivars()) {
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002593 if (Ivar->isInvalidDecl())
2594 continue;
2595 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2596 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2597 if (prevIvar) {
2598 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2599 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2600 Ivar->setInvalidDecl();
2601 }
2602 }
2603 }
2604}
2605
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002606Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2607 switch (CurContext->getDeclKind()) {
2608 case Decl::ObjCInterface:
2609 return Sema::OCK_Interface;
2610 case Decl::ObjCProtocol:
2611 return Sema::OCK_Protocol;
2612 case Decl::ObjCCategory:
Benjamin Kramera008d3a2015-04-10 11:37:55 +00002613 if (cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002614 return Sema::OCK_ClassExtension;
Benjamin Kramera008d3a2015-04-10 11:37:55 +00002615 return Sema::OCK_Category;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002616 case Decl::ObjCImplementation:
2617 return Sema::OCK_Implementation;
2618 case Decl::ObjCCategoryImpl:
2619 return Sema::OCK_CategoryImplementation;
2620
2621 default:
2622 return Sema::OCK_None;
2623 }
2624}
2625
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002626// Note: For class/category implementations, allMethods is always null.
Robert Wilhelm57c67112013-07-17 21:14:35 +00002627Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods,
Fariborz Jahaniandfb76872013-07-17 00:05:08 +00002628 ArrayRef<DeclGroupPtrTy> allTUVars) {
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002629 if (getObjCContainerKind() == Sema::OCK_None)
Craig Topperc3ec1492014-05-26 06:22:03 +00002630 return nullptr;
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002631
2632 assert(AtEnd.isValid() && "Invalid location for '@end'");
2633
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002634 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2635 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002636
Mike Stump11289f42009-09-09 15:08:12 +00002637 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00002638 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2639 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002640 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00002641
Steve Naroff35c62ae2009-01-08 17:28:14 +00002642 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2643 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2644 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2645
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002646 for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002647 ObjCMethodDecl *Method =
John McCall48871652010-08-21 09:40:31 +00002648 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002649
2650 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00002651 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002652 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002653 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002654 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002655 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002656 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002657 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002658 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002659 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002660 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002661 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002662 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002663 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002664 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002665 if (!Context.getSourceManager().isInSystemHeader(
2666 Method->getLocation()))
2667 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2668 << Method->getDeclName();
2669 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2670 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002671 InsMap[Method->getSelector()] = Method;
2672 /// The following allows us to typecheck messages to "id".
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002673 AddInstanceMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002674 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002675 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002676 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002677 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002678 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002679 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002680 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002681 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002682 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002683 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002684 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002685 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002686 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002687 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002688 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002689 if (!Context.getSourceManager().isInSystemHeader(
2690 Method->getLocation()))
2691 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2692 << Method->getDeclName();
2693 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2694 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002695 ClsMap[Method->getSelector()] = Method;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002696 AddFactoryMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002697 }
2698 }
2699 }
Douglas Gregorb8982092013-01-21 19:42:21 +00002700 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2701 // Nothing to do here.
Steve Naroffb3a87982009-01-09 15:36:25 +00002702 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002703 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00002704 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002705 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00002706
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002707 if (C->IsClassExtension()) {
2708 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2709 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002710 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002711 }
Steve Naroffb3a87982009-01-09 15:36:25 +00002712 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002713 if (CDecl->getIdentifier())
2714 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2715 // user-defined setter/getter. It also synthesizes setter/getter methods
2716 // and adds them to the DeclContext and global method pools.
Aaron Ballmand174edf2014-03-13 19:11:50 +00002717 for (auto *I : CDecl->properties())
Aaron Ballmandc4bea42014-03-13 18:47:37 +00002718 ProcessPropertyDecl(I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00002719 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00002720 }
2721 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002722 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002723 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002724 // Any property declared in a class extension might have user
2725 // declared setter or getter in current class extension or one
2726 // of the other class extensions. Mark them as synthesized as
2727 // property will be synthesized when property with same name is
2728 // seen in the @implementation.
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00002729 for (const auto *Ext : IDecl->visible_extensions()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00002730 for (const auto *Property : Ext->properties()) {
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002731 // Skip over properties declared @dynamic
2732 if (const ObjCPropertyImplDecl *PIDecl
2733 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2734 if (PIDecl->getPropertyImplementation()
2735 == ObjCPropertyImplDecl::Dynamic)
2736 continue;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002737
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00002738 for (const auto *Ext : IDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002739 if (ObjCMethodDecl *GetterMethod
2740 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002741 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002742 if (!Property->isReadOnly())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002743 if (ObjCMethodDecl *SetterMethod
2744 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002745 SetterMethod->setPropertyAccessor(true);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002746 }
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002747 }
2748 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00002749 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002750 AtomicPropertySetterGetterRules(IC, IDecl);
John McCall31168b02011-06-15 23:02:42 +00002751 DiagnoseOwningPropertyGetterSynthesis(IC);
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002752 DiagnoseUnusedBackingIvarInAccessor(S, IC);
Fariborz Jahanian20cfff32015-03-11 16:59:48 +00002753 if (IDecl->hasDesignatedInitializers())
2754 DiagnoseMissingDesignatedInitOverrides(IC, IDecl);
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002755
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002756 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
Craig Topperc3ec1492014-05-26 06:22:03 +00002757 if (IDecl->getSuperClass() == nullptr) {
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002758 // This class has no superclass, so check that it has been marked with
2759 // __attribute((objc_root_class)).
2760 if (!HasRootClassAttr) {
2761 SourceLocation DeclLoc(IDecl->getLocation());
Alp Tokerb6cc5922014-05-03 03:45:55 +00002762 SourceLocation SuperClassLoc(getLocForEndOfToken(DeclLoc));
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002763 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2764 << IDecl->getIdentifier();
2765 // See if NSObject is in the current scope, and if it is, suggest
2766 // adding " : NSObject " to the class declaration.
2767 NamedDecl *IF = LookupSingleName(TUScope,
2768 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2769 DeclLoc, LookupOrdinaryName);
2770 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2771 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2772 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2773 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2774 } else {
2775 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2776 }
2777 }
2778 } else if (HasRootClassAttr) {
2779 // Complain that only root classes may have this attribute.
2780 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2781 }
2782
John McCall5fb5df92012-06-20 06:18:46 +00002783 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002784 while (IDecl->getSuperClass()) {
2785 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2786 IDecl = IDecl->getSuperClass();
2787 }
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002788 }
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002789 }
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00002790 SetIvarInitializers(IC);
Mike Stump11289f42009-09-09 15:08:12 +00002791 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00002792 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002793 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00002794
Chris Lattnerda463fe2007-12-12 07:09:47 +00002795 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00002796 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00002797 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002798 if (ObjCCategoryDecl *Cat
2799 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2800 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002801 }
2802 }
2803 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002804 if (isInterfaceDeclKind) {
2805 // Reject invalid vardecls.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002806 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002807 DeclGroupRef DG = allTUVars[i].get();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002808 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2809 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00002810 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00002811 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00002812 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002813 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00002814 }
Fariborz Jahanian4327b322011-08-29 17:33:12 +00002815 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002816
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002817 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002818 DeclGroupRef DG = allTUVars[i].get();
Argyrios Kyrtzidis8ad3bab2011-11-23 20:27:36 +00002819 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2820 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002821 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2822 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002823
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +00002824 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002825 return ClassDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002826}
2827
2828
2829/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2830/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00002831static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002832CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCallca872902011-05-01 03:04:29 +00002833 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002834}
2835
Douglas Gregor33823722011-06-11 01:09:30 +00002836/// \brief Check whether the declared result type of the given Objective-C
2837/// method declaration is compatible with the method's class.
2838///
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002839static Sema::ResultTypeCompatibilityKind
Douglas Gregor33823722011-06-11 01:09:30 +00002840CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2841 ObjCInterfaceDecl *CurrentClass) {
Alp Toker314cc812014-01-25 16:55:45 +00002842 QualType ResultType = Method->getReturnType();
2843
Douglas Gregor33823722011-06-11 01:09:30 +00002844 // If an Objective-C method inherits its related result type, then its
2845 // declared result type must be compatible with its own class type. The
2846 // declared result type is compatible if:
2847 if (const ObjCObjectPointerType *ResultObjectType
2848 = ResultType->getAs<ObjCObjectPointerType>()) {
2849 // - it is id or qualified id, or
2850 if (ResultObjectType->isObjCIdType() ||
2851 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002852 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002853
2854 if (CurrentClass) {
2855 if (ObjCInterfaceDecl *ResultClass
2856 = ResultObjectType->getInterfaceDecl()) {
2857 // - it is the same as the method's class type, or
Douglas Gregor0b144e12011-12-15 00:29:59 +00002858 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002859 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002860
2861 // - it is a superclass of the method's class type
2862 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002863 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002864 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00002865 } else {
2866 // Any Objective-C pointer type might be acceptable for a protocol
2867 // method; we just don't know.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002868 return Sema::RTC_Unknown;
Douglas Gregor33823722011-06-11 01:09:30 +00002869 }
2870 }
2871
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002872 return Sema::RTC_Incompatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002873}
2874
John McCalld2930c22011-07-22 02:45:48 +00002875namespace {
2876/// A helper class for searching for methods which a particular method
2877/// overrides.
2878class OverrideSearch {
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002879public:
John McCalld2930c22011-07-22 02:45:48 +00002880 Sema &S;
2881 ObjCMethodDecl *Method;
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002882 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCalld2930c22011-07-22 02:45:48 +00002883 bool Recursive;
2884
2885public:
2886 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2887 Selector selector = method->getSelector();
2888
2889 // Bypass this search if we've never seen an instance/class method
2890 // with this selector before.
2891 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2892 if (it == S.MethodPool.end()) {
Axel Naumanndd433f02012-10-18 19:05:02 +00002893 if (!S.getExternalSource()) return;
Douglas Gregore1716012012-01-25 00:49:42 +00002894 S.ReadMethodPool(selector);
2895
2896 it = S.MethodPool.find(selector);
2897 if (it == S.MethodPool.end())
2898 return;
John McCalld2930c22011-07-22 02:45:48 +00002899 }
2900 ObjCMethodList &list =
2901 method->isInstanceMethod() ? it->second.first : it->second.second;
Nico Weber2e0c8f72014-12-27 03:58:08 +00002902 if (!list.getMethod()) return;
John McCalld2930c22011-07-22 02:45:48 +00002903
2904 ObjCContainerDecl *container
2905 = cast<ObjCContainerDecl>(method->getDeclContext());
2906
2907 // Prevent the search from reaching this container again. This is
2908 // important with categories, which override methods from the
2909 // interface and each other.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002910 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2911 searchFromContainer(container);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002912 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2913 searchFromContainer(Interface);
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002914 } else {
2915 searchFromContainer(container);
2916 }
Douglas Gregor33823722011-06-11 01:09:30 +00002917 }
John McCalld2930c22011-07-22 02:45:48 +00002918
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002919 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCalld2930c22011-07-22 02:45:48 +00002920 iterator begin() const { return Overridden.begin(); }
2921 iterator end() const { return Overridden.end(); }
2922
2923private:
2924 void searchFromContainer(ObjCContainerDecl *container) {
2925 if (container->isInvalidDecl()) return;
2926
2927 switch (container->getDeclKind()) {
2928#define OBJCCONTAINER(type, base) \
2929 case Decl::type: \
2930 searchFrom(cast<type##Decl>(container)); \
2931 break;
2932#define ABSTRACT_DECL(expansion)
2933#define DECL(type, base) \
2934 case Decl::type:
2935#include "clang/AST/DeclNodes.inc"
2936 llvm_unreachable("not an ObjC container!");
2937 }
2938 }
2939
2940 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00002941 if (!protocol->hasDefinition())
2942 return;
2943
John McCalld2930c22011-07-22 02:45:48 +00002944 // A method in a protocol declaration overrides declarations from
2945 // referenced ("parent") protocols.
2946 search(protocol->getReferencedProtocols());
2947 }
2948
2949 void searchFrom(ObjCCategoryDecl *category) {
2950 // A method in a category declaration overrides declarations from
2951 // the main class and from protocols the category references.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002952 // The main class is handled in the constructor.
John McCalld2930c22011-07-22 02:45:48 +00002953 search(category->getReferencedProtocols());
2954 }
2955
2956 void searchFrom(ObjCCategoryImplDecl *impl) {
2957 // A method in a category definition that has a category
2958 // declaration overrides declarations from the category
2959 // declaration.
2960 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2961 search(category);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002962 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2963 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002964
2965 // Otherwise it overrides declarations from the class.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002966 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2967 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002968 }
2969 }
2970
2971 void searchFrom(ObjCInterfaceDecl *iface) {
2972 // A method in a class declaration overrides declarations from
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00002973 if (!iface->hasDefinition())
2974 return;
2975
John McCalld2930c22011-07-22 02:45:48 +00002976 // - categories,
Aaron Ballman15063e12014-03-13 21:35:02 +00002977 for (auto *Cat : iface->known_categories())
2978 search(Cat);
John McCalld2930c22011-07-22 02:45:48 +00002979
2980 // - the super class, and
2981 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2982 search(super);
2983
2984 // - any referenced protocols.
2985 search(iface->getReferencedProtocols());
2986 }
2987
2988 void searchFrom(ObjCImplementationDecl *impl) {
2989 // A method in a class implementation overrides declarations from
2990 // the class interface.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002991 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2992 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002993 }
2994
2995
2996 void search(const ObjCProtocolList &protocols) {
2997 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2998 i != e; ++i)
2999 search(*i);
3000 }
3001
3002 void search(ObjCContainerDecl *container) {
John McCalld2930c22011-07-22 02:45:48 +00003003 // Check for a method in this container which matches this selector.
3004 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00003005 Method->isInstanceMethod(),
3006 /*AllowHidden=*/true);
John McCalld2930c22011-07-22 02:45:48 +00003007
3008 // If we find one, record it and bail out.
3009 if (meth) {
3010 Overridden.insert(meth);
3011 return;
3012 }
3013
3014 // Otherwise, search for methods that a hypothetical method here
3015 // would have overridden.
3016
3017 // Note that we're now in a recursive case.
3018 Recursive = true;
3019
3020 searchFromContainer(container);
3021 }
3022};
Douglas Gregor33823722011-06-11 01:09:30 +00003023}
3024
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003025void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
3026 ObjCInterfaceDecl *CurrentClass,
3027 ResultTypeCompatibilityKind RTC) {
3028 // Search for overridden methods and merge information down from them.
3029 OverrideSearch overrides(*this, ObjCMethod);
3030 // Keep track if the method overrides any method in the class's base classes,
3031 // its protocols, or its categories' protocols; we will keep that info
3032 // in the ObjCMethodDecl.
3033 // For this info, a method in an implementation is not considered as
3034 // overriding the same method in the interface or its categories.
3035 bool hasOverriddenMethodsInBaseOrProtocol = false;
3036 for (OverrideSearch::iterator
3037 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
3038 ObjCMethodDecl *overridden = *i;
3039
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00003040 if (!hasOverriddenMethodsInBaseOrProtocol) {
3041 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
3042 CurrentClass != overridden->getClassInterface() ||
3043 overridden->isOverriding()) {
3044 hasOverriddenMethodsInBaseOrProtocol = true;
3045
3046 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
3047 // OverrideSearch will return as "overridden" the same method in the
3048 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
3049 // check whether a category of a base class introduced a method with the
3050 // same selector, after the interface method declaration.
3051 // To avoid unnecessary lookups in the majority of cases, we use the
3052 // extra info bits in GlobalMethodPool to check whether there were any
3053 // category methods with this selector.
3054 GlobalMethodPool::iterator It =
3055 MethodPool.find(ObjCMethod->getSelector());
3056 if (It != MethodPool.end()) {
3057 ObjCMethodList &List =
3058 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
3059 unsigned CategCount = List.getBits();
3060 if (CategCount > 0) {
3061 // If the method is in a category we'll do lookup if there were at
3062 // least 2 category methods recorded, otherwise only one will do.
3063 if (CategCount > 1 ||
3064 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
3065 OverrideSearch overrides(*this, overridden);
3066 for (OverrideSearch::iterator
3067 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
3068 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00003069 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
3070 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00003071 hasOverriddenMethodsInBaseOrProtocol = true;
3072 overridden->setOverriding(true);
3073 break;
3074 }
3075 }
3076 }
3077 }
3078 }
3079 }
3080 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003081
3082 // Propagate down the 'related result type' bit from overridden methods.
3083 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
3084 ObjCMethod->SetRelatedResultType();
3085
3086 // Then merge the declarations.
3087 mergeObjCMethodDecls(ObjCMethod, overridden);
3088
3089 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3090 continue; // Conflicting properties are detected elsewhere.
3091
3092 // Check for overriding methods
3093 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3094 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3095 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3096 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3097
3098 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanian31a25682012-07-05 22:26:07 +00003099 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3100 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003101 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3102 E = ObjCMethod->param_end();
Douglas Gregor0bf70f42012-05-17 23:13:29 +00003103 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3104 PrevE = overridden->param_end();
3105 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003106 assert(PrevI != overridden->param_end() && "Param mismatch");
3107 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3108 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3109 // If type of argument of method in this class does not match its
3110 // respective argument type in the super class method, issue warning;
3111 if (!Context.typesAreCompatible(T1, T2)) {
3112 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3113 << T1 << T2;
3114 Diag(overridden->getLocation(), diag::note_previous_declaration);
3115 break;
3116 }
3117 }
3118 }
3119 }
3120
3121 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3122}
3123
John McCall48871652010-08-21 09:40:31 +00003124Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003125 Scope *S,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003126 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003127 tok::TokenKind MethodType,
John McCallba7bf592010-08-24 05:47:05 +00003128 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003129 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003130 Selector Sel,
3131 // optional arguments. The number of types/arguments is obtained
3132 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003133 ObjCArgInfo *ArgInfo,
Fariborz Jahanian60462092010-04-08 00:30:06 +00003134 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattnerda463fe2007-12-12 07:09:47 +00003135 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003136 bool isVariadic, bool MethodDefinition) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003137 // Make sure we can establish a context for the method.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003138 if (!CurContext->isObjCContainer()) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003139 Diag(MethodLoc, diag::error_missing_method_context);
Craig Topperc3ec1492014-05-26 06:22:03 +00003140 return nullptr;
Steve Naroff83777fe2008-02-29 21:48:07 +00003141 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003142 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3143 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003144 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00003145
Douglas Gregorbab8a962011-09-08 01:46:34 +00003146 bool HasRelatedResultType = false;
Craig Topperc3ec1492014-05-26 06:22:03 +00003147 TypeSourceInfo *ReturnTInfo = nullptr;
Steve Naroff32606412009-02-20 22:59:16 +00003148 if (ReturnType) {
Alp Toker314cc812014-01-25 16:55:45 +00003149 resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo);
Mike Stump11289f42009-09-09 15:08:12 +00003150
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003151 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
Craig Topperc3ec1492014-05-26 06:22:03 +00003152 return nullptr;
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003153
Douglas Gregorbab8a962011-09-08 01:46:34 +00003154 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003155 } else { // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003156 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianb21138f2011-07-21 17:38:14 +00003157 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003158 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003159 }
Mike Stump11289f42009-09-09 15:08:12 +00003160
Alp Toker314cc812014-01-25 16:55:45 +00003161 ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create(
3162 Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext,
3163 MethodType == tok::minus, isVariadic,
3164 /*isPropertyAccessor=*/false,
3165 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
3166 MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional
3167 : ObjCMethodDecl::Required,
3168 HasRelatedResultType);
Mike Stump11289f42009-09-09 15:08:12 +00003169
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003170 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00003171
Chris Lattner23b0faf2009-04-11 19:42:43 +00003172 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00003173 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00003174 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00003175
David Blaikie7d170102013-05-15 07:37:26 +00003176 if (!ArgInfo[i].Type) {
John McCall856bbea2009-10-23 21:48:59 +00003177 ArgType = Context.getObjCIdType();
Craig Topperc3ec1492014-05-26 06:22:03 +00003178 DI = nullptr;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003179 } else {
John McCall856bbea2009-10-23 21:48:59 +00003180 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003181 }
Mike Stump11289f42009-09-09 15:08:12 +00003182
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003183 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3184 LookupOrdinaryName, ForRedeclaration);
3185 LookupName(R, S);
3186 if (R.isSingleResult()) {
3187 NamedDecl *PrevDecl = R.getFoundDecl();
3188 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003189 Diag(ArgInfo[i].NameLoc,
3190 (MethodDefinition ? diag::warn_method_param_redefinition
3191 : diag::warn_method_param_declaration))
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003192 << ArgInfo[i].Name;
3193 Diag(PrevDecl->getLocation(),
3194 diag::note_previous_declaration);
3195 }
3196 }
3197
Abramo Bagnaradff19302011-03-08 08:55:46 +00003198 SourceLocation StartLoc = DI
3199 ? DI->getTypeLoc().getBeginLoc()
3200 : ArgInfo[i].NameLoc;
3201
John McCalld44f4d72011-04-23 02:46:06 +00003202 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3203 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003204 ArgType, DI, SC_None);
Mike Stump11289f42009-09-09 15:08:12 +00003205
John McCall82490832011-05-02 00:30:12 +00003206 Param->setObjCMethodScopeInfo(i);
3207
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003208 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003209 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00003210
Chris Lattner9713a1c2009-04-11 19:34:56 +00003211 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00003212 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00003213
Fariborz Jahanian52d02f62012-01-14 18:44:35 +00003214 if (Param->hasAttr<BlocksAttr>()) {
3215 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3216 Param->setInvalidDecl();
3217 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003218 S->AddDecl(Param);
3219 IdResolver.AddDecl(Param);
3220
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003221 Params.push_back(Param);
3222 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003223
Fariborz Jahanian60462092010-04-08 00:30:06 +00003224 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCall48871652010-08-21 09:40:31 +00003225 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003226 QualType ArgType = Param->getType();
3227 if (ArgType.isNull())
3228 ArgType = Context.getObjCIdType();
3229 else
3230 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor84280642011-07-12 04:42:08 +00003231 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003232
Fariborz Jahanian60462092010-04-08 00:30:06 +00003233 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003234 Params.push_back(Param);
3235 }
3236
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003237 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003238 ObjCMethod->setObjCDeclQualifier(
3239 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00003240
3241 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00003242 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00003243
Douglas Gregor87e92752010-12-21 17:34:17 +00003244 // Add the method now.
Craig Topperc3ec1492014-05-26 06:22:03 +00003245 const ObjCMethodDecl *PrevMethod = nullptr;
John McCalld2930c22011-07-22 02:45:48 +00003246 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00003247 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003248 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3249 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003250 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003251 PrevMethod = ImpDecl->getClassMethod(Sel);
3252 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003253 }
Douglas Gregor33823722011-06-11 01:09:30 +00003254
Craig Topperc3ec1492014-05-26 06:22:03 +00003255 ObjCMethodDecl *IMD = nullptr;
Fariborz Jahanian512a4cc92011-10-22 01:21:15 +00003256 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3257 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3258 ObjCMethod->isInstanceMethod());
Fariborz Jahaniandb4fc282013-07-09 22:02:20 +00003259 if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
3260 !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
3261 // merge the attribute into implementation.
Aaron Ballman36a53502014-01-16 13:03:14 +00003262 ObjCMethod->addAttr(ObjCRequiresSuperAttr::CreateImplicit(Context,
3263 ObjCMethod->getLocation()));
Fariborz Jahaniandb4fc282013-07-09 22:02:20 +00003264 }
Fariborz Jahanian7e350d22013-12-17 22:44:28 +00003265 if (isa<ObjCCategoryImplDecl>(ImpDecl)) {
Fariborz Jahanianf40ef452014-01-28 22:46:29 +00003266 ObjCMethodFamily family =
3267 ObjCMethod->getSelector().getMethodFamily();
Fariborz Jahanian1b30b592013-12-18 00:52:54 +00003268 if (family == OMF_dealloc && IMD && IMD->isOverriding())
Fariborz Jahanian7e350d22013-12-17 22:44:28 +00003269 Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category)
3270 << ObjCMethod->getDeclName();
Fariborz Jahanian7e350d22013-12-17 22:44:28 +00003271 }
Douglas Gregor87e92752010-12-21 17:34:17 +00003272 } else {
3273 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003274 }
John McCalld2930c22011-07-22 02:45:48 +00003275
Chris Lattnerda463fe2007-12-12 07:09:47 +00003276 if (PrevMethod) {
3277 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00003278 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00003279 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00003280 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian096f7c12013-05-13 17:27:00 +00003281 ObjCMethod->setInvalidDecl();
3282 return ObjCMethod;
Mike Stump11289f42009-09-09 15:08:12 +00003283 }
John McCall28a6aea2009-11-04 02:18:39 +00003284
Douglas Gregor33823722011-06-11 01:09:30 +00003285 // If this Objective-C method does not have a related result type, but we
3286 // are allowed to infer related result types, try to do so based on the
3287 // method family.
3288 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3289 if (!CurrentClass) {
3290 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3291 CurrentClass = Cat->getClassInterface();
3292 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3293 CurrentClass = Impl->getClassInterface();
3294 else if (ObjCCategoryImplDecl *CatImpl
3295 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3296 CurrentClass = CatImpl->getClassInterface();
3297 }
John McCalld2930c22011-07-22 02:45:48 +00003298
Douglas Gregorbab8a962011-09-08 01:46:34 +00003299 ResultTypeCompatibilityKind RTC
3300 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCalld2930c22011-07-22 02:45:48 +00003301
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003302 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCalld2930c22011-07-22 02:45:48 +00003303
John McCall31168b02011-06-15 23:02:42 +00003304 bool ARCError = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003305 if (getLangOpts().ObjCAutoRefCount)
John McCalle48f3892013-04-04 01:38:37 +00003306 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCall31168b02011-06-15 23:02:42 +00003307
Douglas Gregorbab8a962011-09-08 01:46:34 +00003308 // Infer the related result type when possible.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003309 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregorbab8a962011-09-08 01:46:34 +00003310 !ObjCMethod->hasRelatedResultType() &&
3311 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor33823722011-06-11 01:09:30 +00003312 bool InferRelatedResultType = false;
3313 switch (ObjCMethod->getMethodFamily()) {
3314 case OMF_None:
3315 case OMF_copy:
3316 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00003317 case OMF_finalize:
Douglas Gregor33823722011-06-11 01:09:30 +00003318 case OMF_mutableCopy:
3319 case OMF_release:
3320 case OMF_retainCount:
Fariborz Jahanian78e9deb2014-08-22 16:57:26 +00003321 case OMF_initialize:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00003322 case OMF_performSelector:
Douglas Gregor33823722011-06-11 01:09:30 +00003323 break;
3324
3325 case OMF_alloc:
3326 case OMF_new:
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00003327 InferRelatedResultType = ObjCMethod->isClassMethod();
Douglas Gregor33823722011-06-11 01:09:30 +00003328 break;
3329
3330 case OMF_init:
3331 case OMF_autorelease:
3332 case OMF_retain:
3333 case OMF_self:
3334 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3335 break;
3336 }
3337
Fariborz Jahanian7a60b6d2015-04-16 18:38:44 +00003338 if (InferRelatedResultType &&
3339 !ObjCMethod->getReturnType()->isObjCIndependentClassType())
Douglas Gregor33823722011-06-11 01:09:30 +00003340 ObjCMethod->SetRelatedResultType();
Douglas Gregor33823722011-06-11 01:09:30 +00003341 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00003342
3343 ActOnDocumentableDecl(ObjCMethod);
3344
John McCall48871652010-08-21 09:40:31 +00003345 return ObjCMethod;
Chris Lattnerda463fe2007-12-12 07:09:47 +00003346}
3347
Chris Lattner438e5012008-12-17 07:13:27 +00003348bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +00003349 // Following is also an error. But it is caused by a missing @end
3350 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003351 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003352 return false;
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003353
3354 // If we switched context to translation unit while we are still lexically in
3355 // an objc container, it means the parser missed emitting an error.
3356 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3357 return false;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003358
Anders Carlssona6b508a2008-11-04 16:57:32 +00003359 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3360 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003361
Anders Carlssona6b508a2008-11-04 16:57:32 +00003362 return true;
3363}
Chris Lattner438e5012008-12-17 07:13:27 +00003364
James Dennett634962f2012-06-14 21:40:34 +00003365/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattner438e5012008-12-17 07:13:27 +00003366/// instance variables of ClassName into Decls.
John McCall48871652010-08-21 09:40:31 +00003367void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00003368 IdentifierInfo *ClassName,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003369 SmallVectorImpl<Decl*> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00003370 // Check that ClassName is a valid class
Douglas Gregorb2ccf012010-04-15 22:33:43 +00003371 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattner438e5012008-12-17 07:13:27 +00003372 if (!Class) {
3373 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3374 return;
3375 }
John McCall5fb5df92012-06-20 06:18:46 +00003376 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00003377 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3378 return;
3379 }
Mike Stump11289f42009-09-09 15:08:12 +00003380
Chris Lattner438e5012008-12-17 07:13:27 +00003381 // Collect the instance variables
Jordy Rosea91768e2011-07-22 02:08:32 +00003382 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003383 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003384 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003385 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003386 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCall48871652010-08-21 09:40:31 +00003387 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaradff19302011-03-08 08:55:46 +00003388 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3389 /*FIXME: StartL=*/ID->getLocation(),
3390 ID->getLocation(),
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003391 ID->getIdentifier(), ID->getType(),
3392 ID->getBitWidth());
John McCall48871652010-08-21 09:40:31 +00003393 Decls.push_back(FD);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003394 }
Mike Stump11289f42009-09-09 15:08:12 +00003395
Chris Lattner438e5012008-12-17 07:13:27 +00003396 // Introduce all of these fields into the appropriate scope.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003397 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00003398 D != Decls.end(); ++D) {
John McCall48871652010-08-21 09:40:31 +00003399 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003400 if (getLangOpts().CPlusPlus)
Chris Lattner438e5012008-12-17 07:13:27 +00003401 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCall48871652010-08-21 09:40:31 +00003402 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003403 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00003404 }
3405}
3406
Douglas Gregorf3564192010-04-26 17:32:49 +00003407/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaradff19302011-03-08 08:55:46 +00003408VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3409 SourceLocation StartLoc,
3410 SourceLocation IdLoc,
3411 IdentifierInfo *Id,
Douglas Gregorf3564192010-04-26 17:32:49 +00003412 bool Invalid) {
3413 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3414 // duration shall not be qualified by an address-space qualifier."
3415 // Since all parameters have automatic store duration, they can not have
3416 // an address space.
3417 if (T.getAddressSpace() != 0) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003418 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregorf3564192010-04-26 17:32:49 +00003419 Invalid = true;
3420 }
3421
3422 // An @catch parameter must be an unqualified object pointer type;
3423 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3424 if (Invalid) {
3425 // Don't do any further checking.
Douglas Gregorf4e837f2010-04-26 17:57:08 +00003426 } else if (T->isDependentType()) {
3427 // Okay: we don't know what this type will instantiate to.
Douglas Gregorf3564192010-04-26 17:32:49 +00003428 } else if (!T->isObjCObjectPointerType()) {
3429 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003430 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregorf3564192010-04-26 17:32:49 +00003431 } else if (T->isObjCQualifiedIdType()) {
3432 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003433 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregorf3564192010-04-26 17:32:49 +00003434 }
3435
Abramo Bagnaradff19302011-03-08 08:55:46 +00003436 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003437 T, TInfo, SC_None);
Douglas Gregor3f324d562010-05-03 18:51:14 +00003438 New->setExceptionVariable(true);
3439
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003440 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003441 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003442 Invalid = true;
3443
Douglas Gregorf3564192010-04-26 17:32:49 +00003444 if (Invalid)
3445 New->setInvalidDecl();
3446 return New;
3447}
3448
John McCall48871652010-08-21 09:40:31 +00003449Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003450 const DeclSpec &DS = D.getDeclSpec();
3451
3452 // We allow the "register" storage class on exception variables because
3453 // GCC did, but we drop it completely. Any other storage class is an error.
3454 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3455 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3456 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithb4a9e862013-04-12 22:46:28 +00003457 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003458 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithb4a9e862013-04-12 22:46:28 +00003459 << DeclSpec::getSpecifierName(SCS);
3460 }
3461 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3462 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3463 diag::err_invalid_thread)
3464 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregorf3564192010-04-26 17:32:49 +00003465 D.getMutableDeclSpec().ClearStorageClassSpecs();
3466
Richard Smithb1402ae2013-03-18 22:52:47 +00003467 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregorf3564192010-04-26 17:32:49 +00003468
3469 // Check that there are no default arguments inside the type of this
3470 // exception object (C++ only).
David Blaikiebbafb8a2012-03-11 07:00:24 +00003471 if (getLangOpts().CPlusPlus)
Douglas Gregorf3564192010-04-26 17:32:49 +00003472 CheckExtraCXXDefaultArguments(D);
3473
Argyrios Kyrtzidisef7022f2011-06-28 03:01:15 +00003474 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCall8cb7bdf2010-06-04 23:28:52 +00003475 QualType ExceptionType = TInfo->getType();
Douglas Gregorf3564192010-04-26 17:32:49 +00003476
Abramo Bagnaradff19302011-03-08 08:55:46 +00003477 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3478 D.getSourceRange().getBegin(),
3479 D.getIdentifierLoc(),
3480 D.getIdentifier(),
Douglas Gregorf3564192010-04-26 17:32:49 +00003481 D.isInvalidType());
3482
3483 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3484 if (D.getCXXScopeSpec().isSet()) {
3485 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3486 << D.getCXXScopeSpec().getRange();
3487 New->setInvalidDecl();
3488 }
3489
3490 // Add the parameter declaration into this scope.
John McCall48871652010-08-21 09:40:31 +00003491 S->AddDecl(New);
Douglas Gregorf3564192010-04-26 17:32:49 +00003492 if (D.getIdentifier())
3493 IdResolver.AddDecl(New);
3494
3495 ProcessDeclAttributes(S, New, D);
3496
3497 if (New->hasAttr<BlocksAttr>())
3498 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCall48871652010-08-21 09:40:31 +00003499 return New;
Douglas Gregore11ee112010-04-23 23:01:43 +00003500}
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003501
3502/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003503/// initialization.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003504void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003505 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003506 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3507 Iv= Iv->getNextIvar()) {
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003508 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00003509 if (QT->isRecordType())
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003510 Ivars.push_back(Iv);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003511 }
3512}
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003513
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003514void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor72e357f2011-07-28 14:54:22 +00003515 // Load referenced selectors from the external source.
3516 if (ExternalSource) {
3517 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3518 ExternalSource->ReadReferencedSelectors(Sels);
3519 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3520 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3521 }
3522
Fariborz Jahanianc9b7c202011-02-04 23:19:27 +00003523 // Warning will be issued only when selector table is
3524 // generated (which means there is at lease one implementation
3525 // in the TU). This is to match gcc's behavior.
3526 if (ReferencedSelectors.empty() ||
3527 !Context.AnyObjCImplementation())
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003528 return;
Chandler Carruth12c8f652015-03-27 00:55:05 +00003529 for (auto &SelectorAndLocation : ReferencedSelectors) {
3530 Selector Sel = SelectorAndLocation.first;
3531 SourceLocation Loc = SelectorAndLocation.second;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003532 if (!LookupImplementedMethodInGlobalPool(Sel))
Chandler Carruth12c8f652015-03-27 00:55:05 +00003533 Diag(Loc, diag::warn_unimplemented_selector) << Sel;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003534 }
3535 return;
3536}
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003537
3538ObjCIvarDecl *
3539Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method,
3540 const ObjCPropertyDecl *&PDecl) const {
Fariborz Jahanian1cc7ae12014-01-02 17:24:32 +00003541 if (Method->isClassMethod())
Craig Topperc3ec1492014-05-26 06:22:03 +00003542 return nullptr;
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003543 const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
3544 if (!IDecl)
Craig Topperc3ec1492014-05-26 06:22:03 +00003545 return nullptr;
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003546 Method = IDecl->lookupMethod(Method->getSelector(), /*isInstance=*/true,
3547 /*shallowCategoryLookup=*/false,
3548 /*followSuper=*/false);
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003549 if (!Method || !Method->isPropertyAccessor())
Craig Topperc3ec1492014-05-26 06:22:03 +00003550 return nullptr;
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003551 if ((PDecl = Method->findPropertyDecl()))
Fariborz Jahanian122d94f2014-01-27 22:27:43 +00003552 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) {
3553 // property backing ivar must belong to property's class
3554 // or be a private ivar in class's implementation.
3555 // FIXME. fix the const-ness issue.
3556 IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable(
3557 IV->getIdentifier());
3558 return IV;
3559 }
Craig Topperc3ec1492014-05-26 06:22:03 +00003560 return nullptr;
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003561}
3562
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003563namespace {
3564 /// Used by Sema::DiagnoseUnusedBackingIvarInAccessor to check if a property
3565 /// accessor references the backing ivar.
Argyrios Kyrtzidis98045c12014-01-03 19:53:09 +00003566 class UnusedBackingIvarChecker :
3567 public DataRecursiveASTVisitor<UnusedBackingIvarChecker> {
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003568 public:
3569 Sema &S;
3570 const ObjCMethodDecl *Method;
3571 const ObjCIvarDecl *IvarD;
3572 bool AccessedIvar;
3573 bool InvokedSelfMethod;
3574
3575 UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method,
3576 const ObjCIvarDecl *IvarD)
3577 : S(S), Method(Method), IvarD(IvarD),
3578 AccessedIvar(false), InvokedSelfMethod(false) {
3579 assert(IvarD);
3580 }
3581
3582 bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
3583 if (E->getDecl() == IvarD) {
3584 AccessedIvar = true;
3585 return false;
3586 }
3587 return true;
3588 }
3589
3590 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
3591 if (E->getReceiverKind() == ObjCMessageExpr::Instance &&
3592 S.isSelfExpr(E->getInstanceReceiver(), Method)) {
3593 InvokedSelfMethod = true;
3594 }
3595 return true;
3596 }
3597 };
3598}
3599
3600void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S,
3601 const ObjCImplementationDecl *ImplD) {
3602 if (S->hasUnrecoverableErrorOccurred())
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003603 return;
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003604
Aaron Ballmanf26acce2014-03-13 19:50:17 +00003605 for (const auto *CurMethod : ImplD->instance_methods()) {
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003606 unsigned DIAG = diag::warn_unused_property_backing_ivar;
3607 SourceLocation Loc = CurMethod->getLocation();
Alp Tokerd4a3f0e2014-06-15 23:30:39 +00003608 if (Diags.isIgnored(DIAG, Loc))
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003609 continue;
3610
3611 const ObjCPropertyDecl *PDecl;
3612 const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl);
3613 if (!IV)
3614 continue;
3615
3616 UnusedBackingIvarChecker Checker(*this, CurMethod, IV);
3617 Checker.TraverseStmt(CurMethod->getBody());
3618 if (Checker.AccessedIvar)
3619 continue;
3620
Fariborz Jahanian5b3105d2014-01-02 22:42:09 +00003621 // Do not issue this warning if backing ivar is used somewhere and accessor
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003622 // implementation makes a self call. This is to prevent false positive in
3623 // cases where the ivar is accessed by another method that the accessor
3624 // delegates to.
3625 if (!IV->isReferenced() || !Checker.InvokedSelfMethod) {
Argyrios Kyrtzidisd8a35322014-01-03 19:39:23 +00003626 Diag(Loc, DIAG) << IV;
Fariborz Jahanian5b3105d2014-01-02 22:42:09 +00003627 Diag(PDecl->getLocation(), diag::note_property_declare);
3628 }
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003629 }
3630}