blob: 3198b01949d3ceb48472e48ddf887eb2d21cdaef [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"
Patrick Beardacfbe9e2012-04-06 18:12:22 +000023#include "clang/Lex/Preprocessor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000024#include "clang/Sema/DeclSpec.h"
25#include "clang/Sema/ExternalSemaSource.h"
26#include "clang/Sema/Lookup.h"
27#include "clang/Sema/Scope.h"
28#include "clang/Sema/ScopeInfo.h"
John McCalla1e130b2010-08-25 07:03:20 +000029#include "llvm/ADT/DenseSet.h"
30
Chris Lattnerda463fe2007-12-12 07:09:47 +000031using namespace clang;
32
John McCall31168b02011-06-15 23:02:42 +000033/// Check whether the given method, which must be in the 'init'
34/// family, is a valid member of that family.
35///
36/// \param receiverTypeIfCall - if null, check this as if declaring it;
37/// if non-null, check this as if making a call to it with the given
38/// receiver type
39///
40/// \return true to indicate that there was an error and appropriate
41/// actions were taken
42bool Sema::checkInitMethod(ObjCMethodDecl *method,
43 QualType receiverTypeIfCall) {
44 if (method->isInvalidDecl()) return true;
45
46 // This castAs is safe: methods that don't return an object
47 // pointer won't be inferred as inits and will reject an explicit
48 // objc_method_family(init).
49
50 // We ignore protocols here. Should we? What about Class?
51
Alp Toker314cc812014-01-25 16:55:45 +000052 const ObjCObjectType *result =
53 method->getReturnType()->castAs<ObjCObjectPointerType>()->getObjectType();
John McCall31168b02011-06-15 23:02:42 +000054
55 if (result->isObjCId()) {
56 return false;
57 } else if (result->isObjCClass()) {
58 // fall through: always an error
59 } else {
60 ObjCInterfaceDecl *resultClass = result->getInterface();
61 assert(resultClass && "unexpected object type!");
62
63 // It's okay for the result type to still be a forward declaration
64 // if we're checking an interface declaration.
Douglas Gregordc9166c2011-12-15 20:29:51 +000065 if (!resultClass->hasDefinition()) {
John McCall31168b02011-06-15 23:02:42 +000066 if (receiverTypeIfCall.isNull() &&
67 !isa<ObjCImplementationDecl>(method->getDeclContext()))
68 return false;
69
70 // Otherwise, we try to compare class types.
71 } else {
72 // If this method was declared in a protocol, we can't check
73 // anything unless we have a receiver type that's an interface.
74 const ObjCInterfaceDecl *receiverClass = 0;
75 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
76 if (receiverTypeIfCall.isNull())
77 return false;
78
79 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
80 ->getInterfaceDecl();
81
82 // This can be null for calls to e.g. id<Foo>.
83 if (!receiverClass) return false;
84 } else {
85 receiverClass = method->getClassInterface();
86 assert(receiverClass && "method not associated with a class!");
87 }
88
89 // If either class is a subclass of the other, it's fine.
90 if (receiverClass->isSuperClassOf(resultClass) ||
91 resultClass->isSuperClassOf(receiverClass))
92 return false;
93 }
94 }
95
96 SourceLocation loc = method->getLocation();
97
98 // If we're in a system header, and this is not a call, just make
99 // the method unusable.
100 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
Aaron Ballman36a53502014-01-16 13:03:14 +0000101 method->addAttr(UnavailableAttr::CreateImplicit(Context,
102 "init method returns a type unrelated to its receiver type",
103 loc));
John McCall31168b02011-06-15 23:02:42 +0000104 return true;
105 }
106
107 // Otherwise, it's an error.
108 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
109 method->setInvalidDecl();
110 return true;
111}
112
Fariborz Jahanianac8dbf02011-09-27 22:35:36 +0000113void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregor66a8ca02013-01-15 22:43:08 +0000114 const ObjCMethodDecl *Overridden) {
Douglas Gregor33823722011-06-11 01:09:30 +0000115 if (Overridden->hasRelatedResultType() &&
116 !NewMethod->hasRelatedResultType()) {
117 // This can only happen when the method follows a naming convention that
118 // implies a related result type, and the original (overridden) method has
119 // a suitable return type, but the new (overriding) method does not have
120 // a suitable return type.
Alp Toker314cc812014-01-25 16:55:45 +0000121 QualType ResultType = NewMethod->getReturnType();
Douglas Gregor33823722011-06-11 01:09:30 +0000122 SourceRange ResultTypeRange;
Alp Toker314cc812014-01-25 16:55:45 +0000123 if (const TypeSourceInfo *ResultTypeInfo =
124 NewMethod->getReturnTypeSourceInfo())
Douglas Gregor33823722011-06-11 01:09:30 +0000125 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
126
127 // Figure out which class this method is part of, if any.
128 ObjCInterfaceDecl *CurrentClass
129 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
130 if (!CurrentClass) {
131 DeclContext *DC = NewMethod->getDeclContext();
132 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
133 CurrentClass = Cat->getClassInterface();
134 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
135 CurrentClass = Impl->getClassInterface();
136 else if (ObjCCategoryImplDecl *CatImpl
137 = dyn_cast<ObjCCategoryImplDecl>(DC))
138 CurrentClass = CatImpl->getClassInterface();
139 }
140
141 if (CurrentClass) {
142 Diag(NewMethod->getLocation(),
143 diag::warn_related_result_type_compatibility_class)
144 << Context.getObjCInterfaceType(CurrentClass)
145 << ResultType
146 << ResultTypeRange;
147 } else {
148 Diag(NewMethod->getLocation(),
149 diag::warn_related_result_type_compatibility_protocol)
150 << ResultType
151 << ResultTypeRange;
152 }
153
Douglas Gregorbab8a962011-09-08 01:46:34 +0000154 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
155 Diag(Overridden->getLocation(),
John McCall5ec7e7d2013-03-19 07:04:25 +0000156 diag::note_related_result_type_family)
157 << /*overridden method*/ 0
Douglas Gregorbab8a962011-09-08 01:46:34 +0000158 << Family;
159 else
160 Diag(Overridden->getLocation(),
161 diag::note_related_result_type_overridden);
Douglas Gregor33823722011-06-11 01:09:30 +0000162 }
David Blaikiebbafb8a2012-03-11 07:00:24 +0000163 if (getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanianac8dbf02011-09-27 22:35:36 +0000164 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
165 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
166 Diag(NewMethod->getLocation(),
167 diag::err_nsreturns_retained_attribute_mismatch) << 1;
168 Diag(Overridden->getLocation(), diag::note_previous_decl)
169 << "method";
170 }
171 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
172 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
173 Diag(NewMethod->getLocation(),
174 diag::err_nsreturns_retained_attribute_mismatch) << 0;
175 Diag(Overridden->getLocation(), diag::note_previous_decl)
176 << "method";
177 }
Douglas Gregor0bf70f42012-05-17 23:13:29 +0000178 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
179 oe = Overridden->param_end();
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000180 for (ObjCMethodDecl::param_iterator
181 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Douglas Gregor0bf70f42012-05-17 23:13:29 +0000182 ni != ne && oi != oe; ++ni, ++oi) {
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +0000183 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanianac8dbf02011-09-27 22:35:36 +0000184 ParmVarDecl *newDecl = (*ni);
185 if (newDecl->hasAttr<NSConsumedAttr>() !=
186 oldDecl->hasAttr<NSConsumedAttr>()) {
187 Diag(newDecl->getLocation(),
188 diag::err_nsconsumed_attribute_mismatch);
189 Diag(oldDecl->getLocation(), diag::note_previous_decl)
190 << "parameter";
191 }
192 }
193 }
Douglas Gregor33823722011-06-11 01:09:30 +0000194}
195
John McCall31168b02011-06-15 23:02:42 +0000196/// \brief Check a method declaration for compatibility with the Objective-C
197/// ARC conventions.
John McCalle48f3892013-04-04 01:38:37 +0000198bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) {
John McCall31168b02011-06-15 23:02:42 +0000199 ObjCMethodFamily family = method->getMethodFamily();
200 switch (family) {
201 case OMF_None:
Nico Weber1fb82662011-08-28 22:35:17 +0000202 case OMF_finalize:
John McCall31168b02011-06-15 23:02:42 +0000203 case OMF_retain:
204 case OMF_release:
205 case OMF_autorelease:
206 case OMF_retainCount:
207 case OMF_self:
John McCalld2930c22011-07-22 02:45:48 +0000208 case OMF_performSelector:
John McCall31168b02011-06-15 23:02:42 +0000209 return false;
210
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000211 case OMF_dealloc:
Alp Toker314cc812014-01-25 16:55:45 +0000212 if (!Context.hasSameType(method->getReturnType(), Context.VoidTy)) {
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000213 SourceRange ResultTypeRange;
Alp Toker314cc812014-01-25 16:55:45 +0000214 if (const TypeSourceInfo *ResultTypeInfo =
215 method->getReturnTypeSourceInfo())
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000216 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
217 if (ResultTypeRange.isInvalid())
Alp Toker314cc812014-01-25 16:55:45 +0000218 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
219 << method->getReturnType()
220 << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000221 else
Alp Toker314cc812014-01-25 16:55:45 +0000222 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
223 << method->getReturnType()
224 << FixItHint::CreateReplacement(ResultTypeRange, "void");
Fariborz Jahanianb7f03c12012-07-30 20:52:48 +0000225 return true;
226 }
227 return false;
228
John McCall31168b02011-06-15 23:02:42 +0000229 case OMF_init:
230 // If the method doesn't obey the init rules, don't bother annotating it.
John McCalle48f3892013-04-04 01:38:37 +0000231 if (checkInitMethod(method, QualType()))
John McCall31168b02011-06-15 23:02:42 +0000232 return true;
233
Aaron Ballman36a53502014-01-16 13:03:14 +0000234 method->addAttr(NSConsumesSelfAttr::CreateImplicit(Context));
John McCall31168b02011-06-15 23:02:42 +0000235
236 // Don't add a second copy of this attribute, but otherwise don't
237 // let it be suppressed.
238 if (method->hasAttr<NSReturnsRetainedAttr>())
239 return false;
240 break;
241
242 case OMF_alloc:
243 case OMF_copy:
244 case OMF_mutableCopy:
245 case OMF_new:
246 if (method->hasAttr<NSReturnsRetainedAttr>() ||
247 method->hasAttr<NSReturnsNotRetainedAttr>() ||
248 method->hasAttr<NSReturnsAutoreleasedAttr>())
249 return false;
250 break;
251 }
252
Aaron Ballman36a53502014-01-16 13:03:14 +0000253 method->addAttr(NSReturnsRetainedAttr::CreateImplicit(Context));
John McCall31168b02011-06-15 23:02:42 +0000254 return false;
255}
256
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000257static void DiagnoseObjCImplementedDeprecations(Sema &S,
258 NamedDecl *ND,
259 SourceLocation ImplLoc,
260 int select) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000261 if (ND && ND->isDeprecated()) {
Fariborz Jahanian6fd94352011-02-16 00:30:31 +0000262 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000263 if (select == 0)
Ted Kremenek59b10db2012-02-27 22:55:11 +0000264 S.Diag(ND->getLocation(), diag::note_method_declared_at)
265 << ND->getDeclName();
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000266 else
267 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
268 }
269}
270
Fariborz Jahanianbd0642f2011-08-31 17:37:55 +0000271/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
272/// pool.
273void Sema::AddAnyMethodToGlobalPool(Decl *D) {
274 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
275
276 // If we don't have a valid method decl, simply return.
277 if (!MDecl)
278 return;
279 if (MDecl->isInstanceMethod())
280 AddInstanceMethodToGlobalPool(MDecl, true);
281 else
282 AddFactoryMethodToGlobalPool(MDecl, true);
283}
284
Fariborz Jahanian1dfeace2012-09-13 18:53:14 +0000285/// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer
286/// has explicit ownership attribute; false otherwise.
287static bool
288HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) {
289 QualType T = Param->getType();
290
Fariborz Jahanian1dfeace2012-09-13 18:53:14 +0000291 if (const PointerType *PT = T->getAs<PointerType>()) {
292 T = PT->getPointeeType();
293 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
294 T = RT->getPointeeType();
295 } else {
296 return true;
297 }
298
299 // If we have a lifetime qualifier, but it's local, we must have
300 // inferred it. So, it is implicit.
301 return !T.getLocalQualifiers().hasObjCLifetime();
302}
303
Fariborz Jahanian18d0a5d2012-08-08 23:41:08 +0000304/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
305/// and user declared, in the method definition's AST.
306void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
307 assert((getCurMethodDecl() == 0) && "Methodparsing confused");
John McCall48871652010-08-21 09:40:31 +0000308 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian577574a2012-07-02 23:37:09 +0000309
Steve Naroff542cd5d2008-07-25 17:57:26 +0000310 // If we don't have a valid method decl, simply return.
311 if (!MDecl)
312 return;
Steve Naroff1d2538c2007-12-18 01:30:32 +0000313
Chris Lattnerda463fe2007-12-12 07:09:47 +0000314 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor91f84212008-12-11 16:49:14 +0000315 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9a28e842010-03-01 23:15:13 +0000316 PushFunctionScope();
317
Chris Lattnerda463fe2007-12-12 07:09:47 +0000318 // Create Decl objects for each parameter, entrring them in the scope for
319 // binding to their use.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000320
321 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanian3d8552a2008-12-09 20:23:04 +0000322 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump11289f42009-09-09 15:08:12 +0000323
Daniel Dunbar279d1cc2008-08-26 06:07:48 +0000324 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
325 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +0000326
Reid Kleckner5a115802013-06-24 14:38:26 +0000327 // The ObjC parser requires parameter names so there's no need to check.
328 CheckParmsForFunctionDef(MDecl->param_begin(), MDecl->param_end(),
329 /*CheckParameterNames=*/false);
330
Chris Lattner58258242008-04-10 02:22:51 +0000331 // Introduce all of the other parameters into this scope.
Aaron Ballman43b68be2014-03-07 17:50:17 +0000332 for (auto *Param : MDecl->params()) {
Fariborz Jahanianb3e87122010-09-17 22:07:07 +0000333 if (!Param->isInvalidDecl() &&
Fariborz Jahanian1dfeace2012-09-13 18:53:14 +0000334 getLangOpts().ObjCAutoRefCount &&
335 !HasExplicitOwnershipAttr(*this, Param))
336 Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
337 Param->getType();
Fariborz Jahaniancd278ff2012-08-30 23:56:02 +0000338
Aaron Ballman43b68be2014-03-07 17:50:17 +0000339 if (Param->getIdentifier())
340 PushOnScopeChains(Param, FnBodyScope);
Fariborz Jahanianb3e87122010-09-17 22:07:07 +0000341 }
John McCall31168b02011-06-15 23:02:42 +0000342
343 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikiebbafb8a2012-03-11 07:00:24 +0000344 if (getLangOpts().ObjCAutoRefCount) {
John McCall31168b02011-06-15 23:02:42 +0000345 switch (MDecl->getMethodFamily()) {
346 case OMF_retain:
347 case OMF_retainCount:
348 case OMF_release:
349 case OMF_autorelease:
350 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
Fariborz Jahanian39d1c422013-05-16 19:08:44 +0000351 << 0 << MDecl->getSelector();
John McCall31168b02011-06-15 23:02:42 +0000352 break;
353
354 case OMF_None:
355 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +0000356 case OMF_finalize:
John McCall31168b02011-06-15 23:02:42 +0000357 case OMF_alloc:
358 case OMF_init:
359 case OMF_mutableCopy:
360 case OMF_copy:
361 case OMF_new:
362 case OMF_self:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +0000363 case OMF_performSelector:
John McCall31168b02011-06-15 23:02:42 +0000364 break;
365 }
366 }
367
Nico Weber715abaf2011-08-22 17:25:57 +0000368 // Warn on deprecated methods under -Wdeprecated-implementations,
369 // and prepare for warning on missing super calls.
370 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian566fff02012-09-07 23:46:23 +0000371 ObjCMethodDecl *IMD =
372 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
373
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000374 if (IMD) {
375 ObjCImplDecl *ImplDeclOfMethodDef =
376 dyn_cast<ObjCImplDecl>(MDecl->getDeclContext());
377 ObjCContainerDecl *ContDeclOfMethodDecl =
378 dyn_cast<ObjCContainerDecl>(IMD->getDeclContext());
379 ObjCImplDecl *ImplDeclOfMethodDecl = 0;
380 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
381 ImplDeclOfMethodDecl = OID->getImplementation();
382 else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl))
383 ImplDeclOfMethodDecl = CD->getImplementation();
384 // No need to issue deprecated warning if deprecated mehod in class/category
385 // is being implemented in its own implementation (no overriding is involved).
386 if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef)
387 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 =
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +0000396 IC->getSuperClass() != 0;
397 } 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:
436 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
437 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
John McCall48871652010-08-21 09:40:31 +0000451Decl *Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000452ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
453 IdentifierInfo *ClassName, SourceLocation ClassLoc,
454 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCall48871652010-08-21 09:40:31 +0000455 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000456 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000457 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000458 assert(ClassName && "Missing class identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000459
Chris Lattnerda463fe2007-12-12 07:09:47 +0000460 // Check for another declaration kind with the same name.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000461 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000462 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor5101c242008-12-05 18:15:24 +0000463
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000464 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000465 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000466 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000467 }
Mike Stump11289f42009-09-09 15:08:12 +0000468
Douglas Gregordc9166c2011-12-15 20:29:51 +0000469 // Create a declaration to describe this @interface.
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000470 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +0000471
472 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
473 // A previous decl with a different name is because of
474 // @compatibility_alias, for example:
475 // \code
476 // @class NewImage;
477 // @compatibility_alias OldImage NewImage;
478 // \endcode
479 // A lookup for 'OldImage' will return the 'NewImage' decl.
480 //
481 // In such a case use the real declaration name, instead of the alias one,
482 // otherwise we will break IdentifierResolver and redecls-chain invariants.
483 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
484 // has been aliased.
485 ClassName = PrevIDecl->getIdentifier();
486 }
487
Douglas Gregordc9166c2011-12-15 20:29:51 +0000488 ObjCInterfaceDecl *IDecl
489 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000490 PrevIDecl, ClassLoc);
Douglas Gregordc9166c2011-12-15 20:29:51 +0000491
Douglas Gregordc9166c2011-12-15 20:29:51 +0000492 if (PrevIDecl) {
493 // Class already seen. Was it a definition?
494 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
495 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
496 << PrevIDecl->getDeclName();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000497 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregordc9166c2011-12-15 20:29:51 +0000498 IDecl->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000499 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000500 }
Douglas Gregordc9166c2011-12-15 20:29:51 +0000501
502 if (AttrList)
503 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
504 PushOnScopeChains(IDecl, TUScope);
Mike Stump11289f42009-09-09 15:08:12 +0000505
Douglas Gregordc9166c2011-12-15 20:29:51 +0000506 // Start the definition of this class. If we're in a redefinition case, there
507 // may already be a definition, so we'll end up adding to it.
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000508 if (!IDecl->hasDefinition())
509 IDecl->startDefinition();
510
Chris Lattnerda463fe2007-12-12 07:09:47 +0000511 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000512 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000513 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
514 LookupOrdinaryName);
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000515
516 if (!PrevDecl) {
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000517 // Try to correct for a typo in the superclass name without correcting
518 // to the class we're defining.
519 ObjCInterfaceValidatorCCC Validator(IDecl);
520 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000521 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain4e8942c2012-01-31 23:49:25 +0000522 NULL, Validator)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000523 diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest)
524 << SuperName << ClassName);
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000525 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000526 }
527 }
528
Douglas Gregor0b144e12011-12-15 00:29:59 +0000529 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000530 Diag(SuperLoc, diag::err_recursive_superclass)
531 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor16408322011-12-15 22:34:59 +0000532 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stump12b8ce12009-08-04 21:02:39 +0000533 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000534 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000535 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000536
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000537 // Diagnose classes that inherit from deprecated classes.
538 if (SuperClassDecl)
539 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000540
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000541 if (PrevDecl && SuperClassDecl == 0) {
542 // The previous declaration was not a class decl. Check if we have a
543 // typedef. If we do, get the underlying class type.
Richard Smithdda56e42011-04-15 14:24:37 +0000544 if (const TypedefNameDecl *TDecl =
545 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000546 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000547 if (T->isObjCObjectType()) {
Fariborz Jahanian83f1be12013-04-04 18:45:52 +0000548 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000549 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian83f1be12013-04-04 18:45:52 +0000550 // This handles the following case:
551 // @interface NewI @end
552 // typedef NewI DeprI __attribute__((deprecated("blah")))
553 // @interface SI : DeprI /* warn here */ @end
554 (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc);
555 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000556 }
557 }
Mike Stump11289f42009-09-09 15:08:12 +0000558
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000559 // This handles the following case:
560 //
561 // typedef int SuperClass;
562 // @interface MyClass : SuperClass {} @end
563 //
564 if (!SuperClassDecl) {
565 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
566 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff189d41f2009-02-04 17:14:05 +0000567 }
568 }
Mike Stump11289f42009-09-09 15:08:12 +0000569
Richard Smithdda56e42011-04-15 14:24:37 +0000570 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000571 if (!SuperClassDecl)
572 Diag(SuperLoc, diag::err_undef_superclass)
573 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor4123a862011-11-14 22:10:01 +0000574 else if (RequireCompleteType(SuperLoc,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000575 Context.getObjCInterfaceType(SuperClassDecl),
576 diag::err_forward_superclass,
577 SuperClassDecl->getDeclName(),
578 ClassName,
579 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahanian3ee91fa2011-06-23 23:16:19 +0000580 SuperClassDecl = 0;
581 }
Steve Naroff189d41f2009-02-04 17:14:05 +0000582 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000583 IDecl->setSuperClass(SuperClassDecl);
584 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor16408322011-12-15 22:34:59 +0000585 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000586 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000587 } else { // we have a root class.
Douglas Gregor16408322011-12-15 22:34:59 +0000588 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000589 }
Mike Stump11289f42009-09-09 15:08:12 +0000590
Sebastian Redle7c1fe62010-08-13 00:28:03 +0000591 // Check then save referenced protocols.
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000592 if (NumProtoRefs) {
Roman Divackye6377112012-09-06 15:59:27 +0000593 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000594 ProtoLocs, Context);
Douglas Gregor16408322011-12-15 22:34:59 +0000595 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000596 }
Mike Stump11289f42009-09-09 15:08:12 +0000597
Anders Carlssona6b508a2008-11-04 16:57:32 +0000598 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000599 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000600}
601
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000602/// ActOnTypedefedProtocols - this action finds protocol list as part of the
603/// typedef'ed use for a qualified super class and adds them to the list
604/// of the protocols.
605void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs,
606 IdentifierInfo *SuperName,
607 SourceLocation SuperLoc) {
608 if (!SuperName)
609 return;
610 NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
611 LookupOrdinaryName);
612 if (!IDecl)
613 return;
614
615 if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) {
616 QualType T = TDecl->getUnderlyingType();
617 if (T->isObjCObjectType())
618 if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>())
619 for (ObjCObjectType::qual_iterator I = OPT->qual_begin(),
620 E = OPT->qual_end(); I != E; ++I)
621 ProtocolRefs.push_back(*I);
622 }
623}
624
Richard Smithac4e36d2012-08-08 23:32:13 +0000625/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett634962f2012-06-14 21:40:34 +0000626/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithac4e36d2012-08-08 23:32:13 +0000627Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
628 IdentifierInfo *AliasName,
629 SourceLocation AliasLocation,
630 IdentifierInfo *ClassName,
631 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000632 // Look for previous declaration of alias name
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000633 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000634 LookupOrdinaryName, ForRedeclaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000635 if (ADecl) {
Eli Friedmanfd6b3f82013-06-21 01:49:53 +0000636 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000637 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +0000638 return 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000639 }
640 // Check for class declaration
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000641 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000642 LookupOrdinaryName, ForRedeclaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000643 if (const TypedefNameDecl *TDecl =
644 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000645 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000646 if (T->isObjCObjectType()) {
647 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000648 ClassName = IDecl->getIdentifier();
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000649 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000650 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000651 }
652 }
653 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000654 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
655 if (CDecl == 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000656 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000657 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000658 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +0000659 return 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000660 }
Mike Stump11289f42009-09-09 15:08:12 +0000661
Chris Lattner219b3e92008-03-16 21:17:37 +0000662 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000663 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000664 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000665
Anders Carlssona6b508a2008-11-04 16:57:32 +0000666 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000667 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000668
John McCall48871652010-08-21 09:40:31 +0000669 return AliasDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000670}
671
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000672bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff41d09ad2009-03-05 15:22:01 +0000673 IdentifierInfo *PName,
674 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000675 const ObjCList<ObjCProtocolDecl> &PList) {
676
677 bool res = false;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000678 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
679 E = PList.end(); I != E; ++I) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000680 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
681 Ploc)) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000682 if (PDecl->getIdentifier() == PName) {
683 Diag(Ploc, diag::err_protocol_has_circular_dependency);
684 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000685 res = true;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000686 }
Douglas Gregore6e48b12012-01-01 19:29:29 +0000687
688 if (!PDecl->hasDefinition())
689 continue;
690
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000691 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
692 PDecl->getLocation(), PDecl->getReferencedProtocols()))
693 res = true;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000694 }
695 }
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000696 return res;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000697}
698
John McCall48871652010-08-21 09:40:31 +0000699Decl *
Chris Lattner3bbae002008-07-26 04:03:38 +0000700Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
701 IdentifierInfo *ProtocolName,
702 SourceLocation ProtocolLoc,
John McCall48871652010-08-21 09:40:31 +0000703 Decl * const *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000704 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000705 const SourceLocation *ProtoLocs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000706 SourceLocation EndProtoLoc,
707 AttributeList *AttrList) {
Fariborz Jahaniancadf7c52011-05-12 22:04:39 +0000708 bool err = false;
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000709 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000710 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor32c17572012-01-01 20:30:41 +0000711 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
712 ForRedeclaration);
713 ObjCProtocolDecl *PDecl = 0;
714 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
715 // If we already have a definition, complain.
716 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
717 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump11289f42009-09-09 15:08:12 +0000718
Douglas Gregor32c17572012-01-01 20:30:41 +0000719 // Create a new protocol that is completely distinct from previous
720 // declarations, and do not make this protocol available for name lookup.
721 // That way, we'll end up completely ignoring the duplicate.
722 // FIXME: Can we turn this into an error?
723 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
724 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000725 /*PrevDecl=*/0);
Douglas Gregor32c17572012-01-01 20:30:41 +0000726 PDecl->startDefinition();
727 } else {
728 if (PrevDecl) {
729 // Check for circular dependencies among protocol declarations. This can
730 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis95dfc122011-11-13 22:08:30 +0000731 ObjCList<ObjCProtocolDecl> PList;
732 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
733 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor32c17572012-01-01 20:30:41 +0000734 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis95dfc122011-11-13 22:08:30 +0000735 }
Douglas Gregor32c17572012-01-01 20:30:41 +0000736
737 // Create the new declaration.
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +0000738 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +0000739 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000740 /*PrevDecl=*/PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000741
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000742 PushOnScopeChains(PDecl, TUScope);
Douglas Gregore6e48b12012-01-01 19:29:29 +0000743 PDecl->startDefinition();
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000744 }
Douglas Gregore6e48b12012-01-01 19:29:29 +0000745
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000746 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000747 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor32c17572012-01-01 20:30:41 +0000748
749 // Merge attributes from previous declarations.
750 if (PrevDecl)
751 mergeDeclAttributes(PDecl, PrevDecl);
752
Fariborz Jahaniancadf7c52011-05-12 22:04:39 +0000753 if (!err && NumProtoRefs ) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000754 /// Check then save referenced protocols.
Roman Divackye6377112012-09-06 15:59:27 +0000755 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000756 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000757 }
Mike Stump11289f42009-09-09 15:08:12 +0000758
759 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000760 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000761}
762
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000763static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl,
764 ObjCProtocolDecl *&UndefinedProtocol) {
765 if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) {
766 UndefinedProtocol = PDecl;
767 return true;
768 }
769
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000770 for (auto *PI : PDecl->protocols())
771 if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) {
772 UndefinedProtocol = PI;
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000773 return true;
774 }
775 return false;
776}
777
Chris Lattnerda463fe2007-12-12 07:09:47 +0000778/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000779/// issues an error if they are not declared. It returns list of
780/// protocol declarations in its 'Protocols' argument.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000781void
Chris Lattner3bbae002008-07-26 04:03:38 +0000782Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000783 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000784 unsigned NumProtocols,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000785 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000786 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000787 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
788 ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000789 if (!PDecl) {
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000790 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000791 TypoCorrection Corrected = CorrectTypo(
792 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain4e8942c2012-01-31 23:49:25 +0000793 LookupObjCProtocolName, TUScope, NULL, Validator);
Richard Smithf9b15102013-08-17 00:46:16 +0000794 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>()))
795 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest)
796 << ProtocolId[i].first);
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000797 }
798
799 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000800 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000801 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000802 continue;
803 }
Fariborz Jahanianada44a22013-04-09 17:52:29 +0000804 // If this is a forward protocol declaration, get its definition.
805 if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
806 PDecl = PDecl->getDefinition();
807
Douglas Gregor171c45a2009-02-18 21:56:37 +0000808 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000809
810 // If this is a forward declaration and we are supposed to warn in this
811 // case, do it.
Douglas Gregoreed49792013-01-17 00:38:46 +0000812 // FIXME: Recover nicely in the hidden case.
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000813 ObjCProtocolDecl *UndefinedProtocol;
814
Douglas Gregoreed49792013-01-17 00:38:46 +0000815 if (WarnOnDeclarations &&
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000816 NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000817 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000818 << ProtocolId[i].first;
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000819 Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined)
820 << UndefinedProtocol;
821 }
John McCall48871652010-08-21 09:40:31 +0000822 Protocols.push_back(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000823 }
824}
825
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000826/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000827/// a class method in its extension.
828///
Mike Stump11289f42009-09-09 15:08:12 +0000829void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000830 ObjCInterfaceDecl *ID) {
831 if (!ID)
832 return; // Possibly due to previous error
833
834 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Aaron Ballmanaff18c02014-03-13 19:03:34 +0000835 for (auto *MD : ID->methods())
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000836 MethodMap[MD->getSelector()] = MD;
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000837
838 if (MethodMap.empty())
839 return;
Aaron Ballmanaff18c02014-03-13 19:03:34 +0000840 for (const auto *Method : CAT->methods()) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000841 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
842 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
843 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
844 << Method->getDeclName();
845 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
846 }
847 }
848}
849
James Dennett634962f2012-06-14 21:40:34 +0000850/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorf6102672012-01-01 21:23:57 +0000851Sema::DeclGroupPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000852Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000853 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000854 unsigned NumElts,
855 AttributeList *attrList) {
Douglas Gregorf6102672012-01-01 21:23:57 +0000856 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000857 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000858 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor32c17572012-01-01 20:30:41 +0000859 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
860 ForRedeclaration);
861 ObjCProtocolDecl *PDecl
862 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
863 IdentList[i].second, AtProtocolLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000864 PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000865
866 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorf6102672012-01-01 21:23:57 +0000867 CheckObjCDeclScope(PDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000868
Douglas Gregor42ff1bb2012-01-01 20:33:24 +0000869 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000870 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor32c17572012-01-01 20:30:41 +0000871
872 if (PrevDecl)
873 mergeDeclAttributes(PDecl, PrevDecl);
874
Douglas Gregorf6102672012-01-01 21:23:57 +0000875 DeclsInGroup.push_back(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000876 }
Mike Stump11289f42009-09-09 15:08:12 +0000877
Rafael Espindolaab417692013-07-09 12:05:01 +0000878 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000879}
880
John McCall48871652010-08-21 09:40:31 +0000881Decl *Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000882ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
883 IdentifierInfo *ClassName, SourceLocation ClassLoc,
884 IdentifierInfo *CategoryName,
885 SourceLocation CategoryLoc,
John McCall48871652010-08-21 09:40:31 +0000886 Decl * const *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000887 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000888 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000889 SourceLocation EndProtoLoc) {
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000890 ObjCCategoryDecl *CDecl;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000891 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek514ff702010-02-23 19:39:46 +0000892
893 /// Check that class of this category is already completely declared.
Douglas Gregor4123a862011-11-14 22:10:01 +0000894
895 if (!IDecl
896 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000897 diag::err_category_forward_interface,
898 CategoryName == 0)) {
Ted Kremenek514ff702010-02-23 19:39:46 +0000899 // Create an invalid ObjCCategoryDecl to serve as context for
900 // the enclosing method declarations. We mark the decl invalid
901 // to make it clear that this isn't a valid AST.
902 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000903 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek514ff702010-02-23 19:39:46 +0000904 CDecl->setInvalidDecl();
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +0000905 CurContext->addDecl(CDecl);
Douglas Gregor4123a862011-11-14 22:10:01 +0000906
907 if (!IDecl)
908 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000909 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek514ff702010-02-23 19:39:46 +0000910 }
911
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000912 if (!CategoryName && IDecl->getImplementation()) {
913 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
914 Diag(IDecl->getImplementation()->getLocation(),
915 diag::note_implementation_declared);
Ted Kremenek514ff702010-02-23 19:39:46 +0000916 }
917
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000918 if (CategoryName) {
919 /// Check for duplicate interface declaration for this category
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000920 if (ObjCCategoryDecl *Previous
921 = IDecl->FindCategoryDeclaration(CategoryName)) {
922 // Class extensions can be declared multiple times, categories cannot.
923 Diag(CategoryLoc, diag::warn_dup_category_def)
924 << ClassName << CategoryName;
925 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner9018ca82009-02-16 21:26:43 +0000926 }
927 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000928
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000929 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
930 ClassLoc, CategoryLoc, CategoryName, IDecl);
931 // FIXME: PushOnScopeChains?
932 CurContext->addDecl(CDecl);
933
Chris Lattnerda463fe2007-12-12 07:09:47 +0000934 if (NumProtoRefs) {
Roman Divackye6377112012-09-06 15:59:27 +0000935 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000936 ProtoLocs, Context);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000937 // Protocols in the class extension belong to the class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000938 if (CDecl->IsClassExtension())
Roman Divackye6377112012-09-06 15:59:27 +0000939 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000940 NumProtoRefs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000941 }
Mike Stump11289f42009-09-09 15:08:12 +0000942
Anders Carlssona6b508a2008-11-04 16:57:32 +0000943 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000944 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000945}
946
947/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000948/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000949/// object.
John McCall48871652010-08-21 09:40:31 +0000950Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000951 SourceLocation AtCatImplLoc,
952 IdentifierInfo *ClassName, SourceLocation ClassLoc,
953 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000954 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000955 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +0000956 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000957 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
958 if (!CatIDecl) {
959 // Category @implementation with no corresponding @interface.
960 // Create and install one.
Argyrios Kyrtzidis41fc05c2011-11-23 20:27:26 +0000961 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
962 ClassLoc, CatLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000963 CatName, IDecl);
Argyrios Kyrtzidis41fc05c2011-11-23 20:27:26 +0000964 CatIDecl->setImplicit();
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000965 }
966 }
967
Mike Stump11289f42009-09-09 15:08:12 +0000968 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +0000969 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +0000970 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000971 /// Check that class of this category is already completely declared.
Douglas Gregor4123a862011-11-14 22:10:01 +0000972 if (!IDecl) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000973 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld2930c22011-07-22 02:45:48 +0000974 CDecl->setInvalidDecl();
Douglas Gregor4123a862011-11-14 22:10:01 +0000975 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
976 diag::err_undef_interface)) {
977 CDecl->setInvalidDecl();
John McCalld2930c22011-07-22 02:45:48 +0000978 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000979
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000980 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000981 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000982
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +0000983 // If the interface is deprecated/unavailable, warn/error about it.
984 if (IDecl)
985 DiagnoseUseOfDecl(IDecl, ClassLoc);
986
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000987 /// Check that CatName, category name, is not used in another implementation.
988 if (CatIDecl) {
989 if (CatIDecl->getImplementation()) {
990 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
991 << CatName;
992 Diag(CatIDecl->getImplementation()->getLocation(),
993 diag::note_previous_definition);
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +0000994 CDecl->setInvalidDecl();
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +0000995 } else {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000996 CatIDecl->setImplementation(CDecl);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +0000997 // Warn on implementating category of deprecated class under
998 // -Wdeprecated-implementations flag.
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000999 DiagnoseObjCImplementedDeprecations(*this,
1000 dyn_cast<NamedDecl>(IDecl),
1001 CDecl->getLocation(), 2);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +00001002 }
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001003 }
Mike Stump11289f42009-09-09 15:08:12 +00001004
Anders Carlssona6b508a2008-11-04 16:57:32 +00001005 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001006 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001007}
1008
John McCall48871652010-08-21 09:40:31 +00001009Decl *Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001010 SourceLocation AtClassImplLoc,
1011 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001012 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001013 SourceLocation SuperClassLoc) {
Richard Smithf9b15102013-08-17 00:46:16 +00001014 ObjCInterfaceDecl *IDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001015 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001016 NamedDecl *PrevDecl
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001017 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
1018 ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001019 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001020 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +00001021 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor1c283312010-08-11 12:19:30 +00001022 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001023 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
1024 diag::warn_undef_interface);
Douglas Gregor40f7a002010-01-04 17:27:12 +00001025 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001026 // We did not find anything with the name ClassName; try to correct for
Douglas Gregor40f7a002010-01-04 17:27:12 +00001027 // typos in the class name.
Kaelyn Uhraine31b8882012-01-13 01:32:50 +00001028 ObjCInterfaceValidatorCCC Validator;
Richard Smithf9b15102013-08-17 00:46:16 +00001029 TypoCorrection Corrected =
1030 CorrectTypo(DeclarationNameInfo(ClassName, ClassLoc),
1031 LookupOrdinaryName, TUScope, NULL, Validator);
1032 if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) {
1033 // Suggest the (potentially) correct interface name. Don't provide a
1034 // code-modification hint or use the typo name for recovery, because
1035 // this is just a warning. The program may actually be correct.
1036 diagnoseTypo(Corrected,
1037 PDiag(diag::warn_undef_interface_suggest) << ClassName,
1038 /*ErrorRecovery*/false);
Douglas Gregor40f7a002010-01-04 17:27:12 +00001039 } else {
1040 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
1041 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001042 }
Mike Stump11289f42009-09-09 15:08:12 +00001043
Chris Lattnerda463fe2007-12-12 07:09:47 +00001044 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001045 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001046 if (SuperClassname) {
1047 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001048 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
1049 LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001050 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001051 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1052 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +00001053 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001054 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001055 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidis3b60cff2012-03-13 01:09:36 +00001056 if (SDecl && !SDecl->hasDefinition())
1057 SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001058 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001059 Diag(SuperClassLoc, diag::err_undef_superclass)
1060 << SuperClassname << ClassName;
Douglas Gregor0b144e12011-12-15 00:29:59 +00001061 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001062 // This implementation and its interface do not have the same
1063 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001064 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001065 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001066 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001067 }
1068 }
1069 }
Mike Stump11289f42009-09-09 15:08:12 +00001070
Chris Lattnerda463fe2007-12-12 07:09:47 +00001071 if (!IDecl) {
1072 // Legacy case of @implementation with no corresponding @interface.
1073 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +00001074
Mike Stump87c57ac2009-05-16 07:39:55 +00001075 // FIXME: Do we support attributes on the @implementation? If so we should
1076 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +00001077 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001078 ClassName, /*PrevDecl=*/0, ClassLoc,
1079 true);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001080 IDecl->startDefinition();
Douglas Gregor16408322011-12-15 22:34:59 +00001081 if (SDecl) {
1082 IDecl->setSuperClass(SDecl);
1083 IDecl->setSuperClassLoc(SuperClassLoc);
1084 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1085 } else {
1086 IDecl->setEndOfDefinitionLoc(ClassLoc);
1087 }
1088
Douglas Gregorac345a32009-04-24 00:16:12 +00001089 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor1c283312010-08-11 12:19:30 +00001090 } else {
1091 // Mark the interface as being completed, even if it was just as
1092 // @class ....;
1093 // declaration; the user cannot reopen it.
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001094 if (!IDecl->hasDefinition())
1095 IDecl->startDefinition();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001096 }
Mike Stump11289f42009-09-09 15:08:12 +00001097
1098 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001099 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001100 ClassLoc, AtClassImplLoc, SuperClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001101
Anders Carlssona6b508a2008-11-04 16:57:32 +00001102 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001103 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001104
Chris Lattnerda463fe2007-12-12 07:09:47 +00001105 // Check that there is no duplicate implementation of this class.
Douglas Gregor1c283312010-08-11 12:19:30 +00001106 if (IDecl->getImplementation()) {
1107 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001108 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +00001109 Diag(IDecl->getImplementation()->getLocation(),
1110 diag::note_previous_definition);
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +00001111 IMPDecl->setInvalidDecl();
Douglas Gregor1c283312010-08-11 12:19:30 +00001112 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001113 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +00001114 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +00001115 // Warn on implementating deprecated class under
1116 // -Wdeprecated-implementations flag.
Fariborz Jahaniand724a102011-02-15 17:49:58 +00001117 DiagnoseObjCImplementedDeprecations(*this,
1118 dyn_cast<NamedDecl>(IDecl),
1119 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001120 }
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001121 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001122}
1123
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001124Sema::DeclGroupPtrTy
1125Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1126 SmallVector<Decl *, 64> DeclsInGroup;
1127 DeclsInGroup.reserve(Decls.size() + 1);
1128
1129 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1130 Decl *Dcl = Decls[i];
1131 if (!Dcl)
1132 continue;
1133 if (Dcl->getDeclContext()->isFileContext())
1134 Dcl->setTopLevelDeclInObjCContainer();
1135 DeclsInGroup.push_back(Dcl);
1136 }
1137
1138 DeclsInGroup.push_back(ObjCImpDecl);
1139
Rafael Espindolaab417692013-07-09 12:05:01 +00001140 return BuildDeclaratorGroup(DeclsInGroup, false);
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001141}
1142
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001143void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1144 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001145 SourceLocation RBrace) {
1146 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001147 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001148 if (!IDecl)
1149 return;
James Dennett634962f2012-06-14 21:40:34 +00001150 /// Check case of non-existing \@interface decl.
1151 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattnerda463fe2007-12-12 07:09:47 +00001152 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +00001153 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor16408322011-12-15 22:34:59 +00001154 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian20912d62010-02-17 17:00:07 +00001155 // Add ivar's to class's DeclContext.
1156 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanianc0309cd2010-02-17 18:10:54 +00001157 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith05afe5e2012-03-13 03:12:56 +00001158 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanianaef66222010-02-19 00:31:17 +00001159 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian20912d62010-02-17 17:00:07 +00001160 }
1161
Chris Lattnerda463fe2007-12-12 07:09:47 +00001162 return;
1163 }
1164 // If implementation has empty ivar list, just return.
1165 if (numIvars == 0)
1166 return;
Mike Stump11289f42009-09-09 15:08:12 +00001167
Chris Lattnerda463fe2007-12-12 07:09:47 +00001168 assert(ivars && "missing @implementation ivars");
John McCall5fb5df92012-06-20 06:18:46 +00001169 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001170 if (ImpDecl->getSuperClass())
1171 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1172 for (unsigned i = 0; i < numIvars; i++) {
1173 ObjCIvarDecl* ImplIvar = ivars[i];
1174 if (const ObjCIvarDecl *ClsIvar =
1175 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1176 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1177 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1178 continue;
1179 }
Fariborz Jahaniane23f26b2013-06-26 22:10:27 +00001180 // Check class extensions (unnamed categories) for duplicate ivars.
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001181 for (const auto *CDecl : IDecl->visible_extensions()) {
Fariborz Jahaniane23f26b2013-06-26 22:10:27 +00001182 if (const ObjCIvarDecl *ClsExtIvar =
1183 CDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1184 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1185 Diag(ClsExtIvar->getLocation(), diag::note_previous_definition);
1186 continue;
1187 }
1188 }
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001189 // Instance ivar to Implementation's DeclContext.
1190 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith05afe5e2012-03-13 03:12:56 +00001191 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001192 ImpDecl->addDecl(ImplIvar);
1193 }
1194 return;
1195 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001196 // Check interface's Ivar list against those in the implementation.
1197 // names and types must match.
1198 //
Chris Lattnerda463fe2007-12-12 07:09:47 +00001199 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001200 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +00001201 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1202 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001203 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie40ed2972012-06-06 20:45:41 +00001204 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001205 assert (ImplIvar && "missing implementation ivar");
1206 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +00001207
Steve Naroff157599f2009-03-03 14:49:36 +00001208 // First, make sure the types match.
Richard Smithcaf33902011-10-10 18:28:20 +00001209 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +00001210 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001211 << ImplIvar->getIdentifier()
1212 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +00001213 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smithcaf33902011-10-10 18:28:20 +00001214 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1215 ImplIvar->getBitWidthValue(Context) !=
1216 ClsIvar->getBitWidthValue(Context)) {
1217 Diag(ImplIvar->getBitWidth()->getLocStart(),
1218 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1219 Diag(ClsIvar->getBitWidth()->getLocStart(),
1220 diag::note_previous_definition);
Mike Stump11289f42009-09-09 15:08:12 +00001221 }
Steve Naroff157599f2009-03-03 14:49:36 +00001222 // Make sure the names are identical.
1223 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001224 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001225 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +00001226 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001227 }
1228 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001229 }
Mike Stump11289f42009-09-09 15:08:12 +00001230
Chris Lattner0f29d982007-12-12 18:11:49 +00001231 if (numIvars > 0)
Alp Tokerfff06742013-12-02 03:50:21 +00001232 Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +00001233 else if (IVI != IVE)
Alp Tokerfff06742013-12-02 03:50:21 +00001234 Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001235}
1236
Ted Kremenekf87decd2013-12-13 05:58:44 +00001237static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc,
1238 ObjCMethodDecl *method,
1239 bool &IncompleteImpl,
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001240 unsigned DiagID,
1241 NamedDecl *NeededFor = 0) {
Fariborz Jahanian9fc39c42011-06-24 20:31:37 +00001242 // No point warning no definition of method which is 'unavailable'.
Douglas Gregorc2e3d5c2012-12-11 18:53:07 +00001243 switch (method->getAvailability()) {
1244 case AR_Available:
1245 case AR_Deprecated:
1246 break;
1247
1248 // Don't warn about unavailable or not-yet-introduced methods.
1249 case AR_NotYetIntroduced:
1250 case AR_Unavailable:
Fariborz Jahanian9fc39c42011-06-24 20:31:37 +00001251 return;
Douglas Gregorc2e3d5c2012-12-11 18:53:07 +00001252 }
1253
Ted Kremenek65d63572013-03-27 00:02:21 +00001254 // FIXME: For now ignore 'IncompleteImpl'.
1255 // Previously we grouped all unimplemented methods under a single
1256 // warning, but some users strongly voiced that they would prefer
1257 // separate warnings. We will give that approach a try, as that
1258 // matches what we do with protocols.
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001259 {
1260 const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID);
1261 B << method;
1262 if (NeededFor)
1263 B << NeededFor;
1264 }
Ted Kremenek65d63572013-03-27 00:02:21 +00001265
1266 // Issue a note to the original declaration.
1267 SourceLocation MethodLoc = method->getLocStart();
1268 if (MethodLoc.isValid())
Ted Kremenekf87decd2013-12-13 05:58:44 +00001269 S.Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff15833ed2008-02-10 21:38:56 +00001270}
1271
David Chisnallb62d15c2010-10-25 17:23:52 +00001272/// Determines if type B can be substituted for type A. Returns true if we can
1273/// guarantee that anything that the user will do to an object of type A can
1274/// also be done to an object of type B. This is trivially true if the two
1275/// types are the same, or if B is a subclass of A. It becomes more complex
1276/// in cases where protocols are involved.
1277///
1278/// Object types in Objective-C describe the minimum requirements for an
1279/// object, rather than providing a complete description of a type. For
1280/// example, if A is a subclass of B, then B* may refer to an instance of A.
1281/// The principle of substitutability means that we may use an instance of A
1282/// anywhere that we may use an instance of B - it will implement all of the
1283/// ivars of B and all of the methods of B.
1284///
1285/// This substitutability is important when type checking methods, because
1286/// the implementation may have stricter type definitions than the interface.
1287/// The interface specifies minimum requirements, but the implementation may
1288/// have more accurate ones. For example, a method may privately accept
1289/// instances of B, but only publish that it accepts instances of A. Any
1290/// object passed to it will be type checked against B, and so will implicitly
1291/// by a valid A*. Similarly, a method may return a subclass of the class that
1292/// it is declared as returning.
1293///
1294/// This is most important when considering subclassing. A method in a
1295/// subclass must accept any object as an argument that its superclass's
1296/// implementation accepts. It may, however, accept a more general type
1297/// without breaking substitutability (i.e. you can still use the subclass
1298/// anywhere that you can use the superclass, but not vice versa). The
1299/// converse requirement applies to return types: the return type for a
1300/// subclass method must be a valid object of the kind that the superclass
1301/// advertises, but it may be specified more accurately. This avoids the need
1302/// for explicit down-casting by callers.
1303///
1304/// Note: This is a stricter requirement than for assignment.
John McCall071df462010-10-28 02:34:38 +00001305static bool isObjCTypeSubstitutable(ASTContext &Context,
1306 const ObjCObjectPointerType *A,
1307 const ObjCObjectPointerType *B,
1308 bool rejectId) {
1309 // Reject a protocol-unqualified id.
1310 if (rejectId && B->isObjCIdType()) return false;
David Chisnallb62d15c2010-10-25 17:23:52 +00001311
1312 // If B is a qualified id, then A must also be a qualified id and it must
1313 // implement all of the protocols in B. It may not be a qualified class.
1314 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1315 // stricter definition so it is not substitutable for id<A>.
1316 if (B->isObjCQualifiedIdType()) {
1317 return A->isObjCQualifiedIdType() &&
John McCall071df462010-10-28 02:34:38 +00001318 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1319 QualType(B,0),
1320 false);
David Chisnallb62d15c2010-10-25 17:23:52 +00001321 }
1322
1323 /*
1324 // id is a special type that bypasses type checking completely. We want a
1325 // warning when it is used in one place but not another.
1326 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1327
1328
1329 // If B is a qualified id, then A must also be a qualified id (which it isn't
1330 // if we've got this far)
1331 if (B->isObjCQualifiedIdType()) return false;
1332 */
1333
1334 // Now we know that A and B are (potentially-qualified) class types. The
1335 // normal rules for assignment apply.
John McCall071df462010-10-28 02:34:38 +00001336 return Context.canAssignObjCInterfaces(A, B);
David Chisnallb62d15c2010-10-25 17:23:52 +00001337}
1338
John McCall071df462010-10-28 02:34:38 +00001339static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1340 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1341}
1342
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001343static bool CheckMethodOverrideReturn(Sema &S,
John McCall071df462010-10-28 02:34:38 +00001344 ObjCMethodDecl *MethodImpl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001345 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001346 bool IsProtocolMethodDecl,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001347 bool IsOverridingMode,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001348 bool Warn) {
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001349 if (IsProtocolMethodDecl &&
1350 (MethodDecl->getObjCDeclQualifier() !=
1351 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001352 if (Warn) {
Alp Toker314cc812014-01-25 16:55:45 +00001353 S.Diag(MethodImpl->getLocation(),
1354 (IsOverridingMode
1355 ? diag::warn_conflicting_overriding_ret_type_modifiers
1356 : diag::warn_conflicting_ret_type_modifiers))
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001357 << MethodImpl->getDeclName()
Alp Toker314cc812014-01-25 16:55:45 +00001358 << getTypeRange(MethodImpl->getReturnTypeSourceInfo());
1359 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1360 << getTypeRange(MethodDecl->getReturnTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001361 }
1362 else
1363 return false;
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001364 }
Alp Toker314cc812014-01-25 16:55:45 +00001365
1366 if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(),
1367 MethodDecl->getReturnType()))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001368 return true;
1369 if (!Warn)
1370 return false;
John McCall071df462010-10-28 02:34:38 +00001371
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001372 unsigned DiagID =
1373 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1374 : diag::warn_conflicting_ret_types;
John McCall071df462010-10-28 02:34:38 +00001375
1376 // Mismatches between ObjC pointers go into a different warning
1377 // category, and sometimes they're even completely whitelisted.
1378 if (const ObjCObjectPointerType *ImplPtrTy =
Alp Toker314cc812014-01-25 16:55:45 +00001379 MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) {
John McCall071df462010-10-28 02:34:38 +00001380 if (const ObjCObjectPointerType *IfacePtrTy =
Alp Toker314cc812014-01-25 16:55:45 +00001381 MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) {
John McCall071df462010-10-28 02:34:38 +00001382 // Allow non-matching return types as long as they don't violate
1383 // the principle of substitutability. Specifically, we permit
1384 // return types that are subclasses of the declared return type,
1385 // or that are more-qualified versions of the declared type.
1386 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001387 return false;
John McCall071df462010-10-28 02:34:38 +00001388
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001389 DiagID =
1390 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1391 : diag::warn_non_covariant_ret_types;
John McCall071df462010-10-28 02:34:38 +00001392 }
1393 }
1394
1395 S.Diag(MethodImpl->getLocation(), DiagID)
Alp Toker314cc812014-01-25 16:55:45 +00001396 << MethodImpl->getDeclName() << MethodDecl->getReturnType()
1397 << MethodImpl->getReturnType()
1398 << getTypeRange(MethodImpl->getReturnTypeSourceInfo());
1399 S.Diag(MethodDecl->getLocation(), IsOverridingMode
1400 ? diag::note_previous_declaration
1401 : diag::note_previous_definition)
1402 << getTypeRange(MethodDecl->getReturnTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001403 return false;
John McCall071df462010-10-28 02:34:38 +00001404}
1405
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001406static bool CheckMethodOverrideParam(Sema &S,
John McCall071df462010-10-28 02:34:38 +00001407 ObjCMethodDecl *MethodImpl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001408 ObjCMethodDecl *MethodDecl,
John McCall071df462010-10-28 02:34:38 +00001409 ParmVarDecl *ImplVar,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001410 ParmVarDecl *IfaceVar,
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001411 bool IsProtocolMethodDecl,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001412 bool IsOverridingMode,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001413 bool Warn) {
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001414 if (IsProtocolMethodDecl &&
1415 (ImplVar->getObjCDeclQualifier() !=
1416 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001417 if (Warn) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001418 if (IsOverridingMode)
1419 S.Diag(ImplVar->getLocation(),
1420 diag::warn_conflicting_overriding_param_modifiers)
1421 << getTypeRange(ImplVar->getTypeSourceInfo())
1422 << MethodImpl->getDeclName();
1423 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001424 diag::warn_conflicting_param_modifiers)
1425 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001426 << MethodImpl->getDeclName();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001427 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1428 << getTypeRange(IfaceVar->getTypeSourceInfo());
1429 }
1430 else
1431 return false;
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001432 }
1433
John McCall071df462010-10-28 02:34:38 +00001434 QualType ImplTy = ImplVar->getType();
1435 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001436
John McCall071df462010-10-28 02:34:38 +00001437 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001438 return true;
1439
1440 if (!Warn)
1441 return false;
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001442 unsigned DiagID =
1443 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1444 : diag::warn_conflicting_param_types;
John McCall071df462010-10-28 02:34:38 +00001445
1446 // Mismatches between ObjC pointers go into a different warning
1447 // category, and sometimes they're even completely whitelisted.
1448 if (const ObjCObjectPointerType *ImplPtrTy =
1449 ImplTy->getAs<ObjCObjectPointerType>()) {
1450 if (const ObjCObjectPointerType *IfacePtrTy =
1451 IfaceTy->getAs<ObjCObjectPointerType>()) {
1452 // Allow non-matching argument types as long as they don't
1453 // violate the principle of substitutability. Specifically, the
1454 // implementation must accept any objects that the superclass
1455 // accepts, however it may also accept others.
1456 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001457 return false;
John McCall071df462010-10-28 02:34:38 +00001458
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001459 DiagID =
1460 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1461 : diag::warn_non_contravariant_param_types;
John McCall071df462010-10-28 02:34:38 +00001462 }
1463 }
1464
1465 S.Diag(ImplVar->getLocation(), DiagID)
1466 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001467 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1468 S.Diag(IfaceVar->getLocation(),
1469 (IsOverridingMode ? diag::note_previous_declaration
1470 : diag::note_previous_definition))
John McCall071df462010-10-28 02:34:38 +00001471 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001472 return false;
John McCall071df462010-10-28 02:34:38 +00001473}
John McCall31168b02011-06-15 23:02:42 +00001474
1475/// In ARC, check whether the conventional meanings of the two methods
1476/// match. If they don't, it's a hard error.
1477static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1478 ObjCMethodDecl *decl) {
1479 ObjCMethodFamily implFamily = impl->getMethodFamily();
1480 ObjCMethodFamily declFamily = decl->getMethodFamily();
1481 if (implFamily == declFamily) return false;
1482
1483 // Since conventions are sorted by selector, the only possibility is
1484 // that the types differ enough to cause one selector or the other
1485 // to fall out of the family.
1486 assert(implFamily == OMF_None || declFamily == OMF_None);
1487
1488 // No further diagnostics required on invalid declarations.
1489 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1490
1491 const ObjCMethodDecl *unmatched = impl;
1492 ObjCMethodFamily family = declFamily;
1493 unsigned errorID = diag::err_arc_lost_method_convention;
1494 unsigned noteID = diag::note_arc_lost_method_convention;
1495 if (declFamily == OMF_None) {
1496 unmatched = decl;
1497 family = implFamily;
1498 errorID = diag::err_arc_gained_method_convention;
1499 noteID = diag::note_arc_gained_method_convention;
1500 }
1501
1502 // Indexes into a %select clause in the diagnostic.
1503 enum FamilySelector {
1504 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1505 };
1506 FamilySelector familySelector = FamilySelector();
1507
1508 switch (family) {
1509 case OMF_None: llvm_unreachable("logic error, no method convention");
1510 case OMF_retain:
1511 case OMF_release:
1512 case OMF_autorelease:
1513 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00001514 case OMF_finalize:
John McCall31168b02011-06-15 23:02:42 +00001515 case OMF_retainCount:
1516 case OMF_self:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001517 case OMF_performSelector:
John McCall31168b02011-06-15 23:02:42 +00001518 // Mismatches for these methods don't change ownership
1519 // conventions, so we don't care.
1520 return false;
1521
1522 case OMF_init: familySelector = F_init; break;
1523 case OMF_alloc: familySelector = F_alloc; break;
1524 case OMF_copy: familySelector = F_copy; break;
1525 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1526 case OMF_new: familySelector = F_new; break;
1527 }
1528
1529 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1530 ReasonSelector reasonSelector;
1531
1532 // The only reason these methods don't fall within their families is
1533 // due to unusual result types.
Alp Toker314cc812014-01-25 16:55:45 +00001534 if (unmatched->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00001535 reasonSelector = R_UnrelatedReturn;
1536 } else {
1537 reasonSelector = R_NonObjectReturn;
1538 }
1539
Joerg Sonnenbergerffc6d492013-06-26 21:31:47 +00001540 S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
1541 S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
John McCall31168b02011-06-15 23:02:42 +00001542
1543 return true;
1544}
John McCall071df462010-10-28 02:34:38 +00001545
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +00001546void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001547 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001548 bool IsProtocolMethodDecl) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001549 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00001550 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1551 return;
1552
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001553 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001554 IsProtocolMethodDecl, false,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001555 true);
Mike Stump11289f42009-09-09 15:08:12 +00001556
Chris Lattner67f35b02009-04-11 19:58:42 +00001557 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001558 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1559 EF = MethodDecl->param_end();
1560 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001561 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001562 IsProtocolMethodDecl, false, true);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001563 }
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001564
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001565 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001566 Diag(ImpMethodDecl->getLocation(),
1567 diag::warn_conflicting_variadic);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001568 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001569 }
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001570}
1571
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001572void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1573 ObjCMethodDecl *Overridden,
1574 bool IsProtocolMethodDecl) {
1575
1576 CheckMethodOverrideReturn(*this, Method, Overridden,
1577 IsProtocolMethodDecl, true,
1578 true);
1579
1580 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001581 IF = Overridden->param_begin(), EM = Method->param_end(),
1582 EF = Overridden->param_end();
1583 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001584 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1585 IsProtocolMethodDecl, true, true);
1586 }
1587
1588 if (Method->isVariadic() != Overridden->isVariadic()) {
1589 Diag(Method->getLocation(),
1590 diag::warn_conflicting_overriding_variadic);
1591 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1592 }
1593}
1594
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001595/// WarnExactTypedMethods - This routine issues a warning if method
1596/// implementation declaration matches exactly that of its declaration.
1597void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1598 ObjCMethodDecl *MethodDecl,
1599 bool IsProtocolMethodDecl) {
1600 // don't issue warning when protocol method is optional because primary
1601 // class is not required to implement it and it is safe for protocol
1602 // to implement it.
1603 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1604 return;
1605 // don't issue warning when primary class's method is
1606 // depecated/unavailable.
1607 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1608 MethodDecl->hasAttr<DeprecatedAttr>())
1609 return;
1610
1611 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1612 IsProtocolMethodDecl, false, false);
1613 if (match)
1614 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001615 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1616 EF = MethodDecl->param_end();
1617 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001618 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1619 *IM, *IF,
1620 IsProtocolMethodDecl, false, false);
1621 if (!match)
1622 break;
1623 }
1624 if (match)
1625 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall92918512011-08-08 17:32:19 +00001626 if (match)
1627 match = !(MethodDecl->isClassMethod() &&
1628 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001629
1630 if (match) {
1631 Diag(ImpMethodDecl->getLocation(),
1632 diag::warn_category_method_impl_match);
Ted Kremenek59b10db2012-02-27 22:55:11 +00001633 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1634 << MethodDecl->getDeclName();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001635 }
1636}
1637
Mike Stump87c57ac2009-05-16 07:39:55 +00001638/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1639/// improve the efficiency of selector lookups and type checking by associating
1640/// with each protocol / interface / category the flattened instance tables. If
1641/// we used an immutable set to keep the table then it wouldn't add significant
1642/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001643
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001644typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet;
Ahmed Charlesaf94d562014-03-09 11:34:25 +00001645typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet;
Ted Kremenek760a2ac2014-03-05 23:18:22 +00001646
1647static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl,
1648 ProtocolNameSet &PNS) {
1649 if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1650 PNS.insert(PDecl->getIdentifier());
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001651 for (const auto *PI : PDecl->protocols())
1652 findProtocolsWithExplicitImpls(PI, PNS);
Ted Kremenek760a2ac2014-03-05 23:18:22 +00001653}
1654
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001655/// Recursively populates a set with all conformed protocols in a class
1656/// hierarchy that have the 'objc_protocol_requires_explicit_implementation'
1657/// attribute.
1658static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super,
1659 ProtocolNameSet &PNS) {
1660 if (!Super)
1661 return;
1662
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001663 for (const auto *I : Super->all_referenced_protocols())
1664 findProtocolsWithExplicitImpls(I, PNS);
Ted Kremenek760a2ac2014-03-05 23:18:22 +00001665
1666 findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS);
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001667}
1668
Steve Naroffa36992242008-02-08 22:06:17 +00001669/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +00001670/// Declared in protocol, and those referenced by it.
Ted Kremenek285ee852013-12-13 06:26:10 +00001671static void CheckProtocolMethodDefs(Sema &S,
1672 SourceLocation ImpLoc,
1673 ObjCProtocolDecl *PDecl,
1674 bool& IncompleteImpl,
1675 const Sema::SelectorSet &InsMap,
1676 const Sema::SelectorSet &ClsMap,
Ted Kremenek33e430f2013-12-13 06:26:14 +00001677 ObjCContainerDecl *CDecl,
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001678 LazyProtocolNameSet &ProtocolsExplictImpl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001679 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1680 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1681 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +00001682 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1683
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001684 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001685 ObjCInterfaceDecl *NSIDecl = 0;
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001686
1687 // If this protocol is marked 'objc_protocol_requires_explicit_implementation'
1688 // then we should check if any class in the super class hierarchy also
1689 // conforms to this protocol, either directly or via protocol inheritance.
1690 // If so, we can skip checking this protocol completely because we
1691 // know that a parent class already satisfies this protocol.
1692 //
1693 // Note: we could generalize this logic for all protocols, and merely
1694 // add the limit on looking at the super class chain for just
1695 // specially marked protocols. This may be a good optimization. This
1696 // change is restricted to 'objc_protocol_requires_explicit_implementation'
1697 // protocols for now for controlled evaluation.
1698 if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) {
Ahmed Charlesaf94d562014-03-09 11:34:25 +00001699 if (!ProtocolsExplictImpl) {
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001700 ProtocolsExplictImpl.reset(new ProtocolNameSet);
1701 findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl);
1702 }
1703 if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) !=
1704 ProtocolsExplictImpl->end())
1705 return;
1706
1707 // If no super class conforms to the protocol, we should not search
1708 // for methods in the super class to implicitly satisfy the protocol.
1709 Super = NULL;
1710 }
1711
Ted Kremenek285ee852013-12-13 06:26:10 +00001712 if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump11289f42009-09-09 15:08:12 +00001713 // check to see if class implements forwardInvocation method and objects
1714 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001715 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +00001716 // Under such conditions, which means that every method possible is
1717 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001718 // found" warnings.
1719 // FIXME: Use a general GetUnarySelector method for this.
Ted Kremenek285ee852013-12-13 06:26:10 +00001720 IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation");
1721 Selector fISelector = S.Context.Selectors.getSelector(1, &II);
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001722 if (InsMap.count(fISelector))
1723 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1724 // need be implemented in the implementation.
Ted Kremenek285ee852013-12-13 06:26:10 +00001725 NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy"));
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001726 }
Mike Stump11289f42009-09-09 15:08:12 +00001727
Fariborz Jahanianc41cf052013-01-07 19:21:03 +00001728 // If this is a forward protocol declaration, get its definition.
1729 if (!PDecl->isThisDeclarationADefinition() &&
1730 PDecl->getDefinition())
1731 PDecl = PDecl->getDefinition();
1732
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001733 // If a method lookup fails locally we still need to look and see if
1734 // the method was implemented by a base class or an inherited
1735 // protocol. This lookup is slow, but occurs rarely in correct code
1736 // and otherwise would terminate in a warning.
1737
Chris Lattnerda463fe2007-12-12 07:09:47 +00001738 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001739 if (!NSIDecl)
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001740 for (auto *method : PDecl->instance_methods()) {
Mike Stump11289f42009-09-09 15:08:12 +00001741 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rosed01e83a2012-10-10 16:42:25 +00001742 !method->isPropertyAccessor() &&
1743 !InsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001744 (!Super || !Super->lookupMethod(method->getSelector(),
1745 true /* instance */,
1746 false /* shallowCategory */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001747 true /* followsSuper */,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001748 NULL /* category */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001749 // If a method is not implemented in the category implementation but
1750 // has been declared in its primary class, superclass,
1751 // or in one of their protocols, no need to issue the warning.
1752 // This is because method will be implemented in the primary class
1753 // or one of its super class implementation.
1754
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001755 // Ugly, but necessary. Method declared in protcol might have
1756 // have been synthesized due to a property declared in the class which
1757 // uses the protocol.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001758 if (ObjCMethodDecl *MethodInClass =
Ted Kremenek00781502013-11-23 01:01:29 +00001759 IDecl->lookupMethod(method->getSelector(),
1760 true /* instance */,
1761 true /* shallowCategoryLookup */,
1762 false /* followSuper */))
Jordan Rosed01e83a2012-10-10 16:42:25 +00001763 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001764 continue;
1765 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Ted Kremenek285ee852013-12-13 06:26:10 +00001766 if (S.Diags.getDiagnosticLevel(DIAG, ImpLoc)
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001767 != DiagnosticsEngine::Ignored) {
Ted Kremenek285ee852013-12-13 06:26:10 +00001768 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG,
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001769 PDecl);
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001770 }
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001771 }
1772 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001773 // check unimplemented class methods
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001774 for (auto *method : PDecl->class_methods()) {
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001775 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1776 !ClsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001777 (!Super || !Super->lookupMethod(method->getSelector(),
1778 false /* class method */,
1779 false /* shallowCategoryLookup */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001780 true /* followSuper */,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001781 NULL /* category */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001782 // See above comment for instance method lookups.
Ted Kremenek00781502013-11-23 01:01:29 +00001783 if (C && IDecl->lookupMethod(method->getSelector(),
1784 false /* class */,
1785 true /* shallowCategoryLookup */,
1786 false /* followSuper */))
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001787 continue;
Ted Kremenek00781502013-11-23 01:01:29 +00001788
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001789 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Ted Kremenek285ee852013-12-13 06:26:10 +00001790 if (S.Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
David Blaikie9c902b52011-09-25 23:23:43 +00001791 DiagnosticsEngine::Ignored) {
Ted Kremenek285ee852013-12-13 06:26:10 +00001792 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl);
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001793 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001794 }
Steve Naroff3ce37a62007-12-14 23:37:57 +00001795 }
Chris Lattner390d39a2008-07-21 21:32:27 +00001796 // Check on this protocols's referenced protocols, recursively.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001797 for (auto *PI : PDecl->protocols())
1798 CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap,
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001799 CDecl, ProtocolsExplictImpl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001800}
1801
Fariborz Jahanianf9ae68a2011-07-16 00:08:33 +00001802/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001803/// or protocol against those declared in their implementations.
1804///
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001805void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1806 const SelectorSet &ClsMap,
1807 SelectorSet &InsMapSeen,
1808 SelectorSet &ClsMapSeen,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001809 ObjCImplDecl* IMPDecl,
1810 ObjCContainerDecl* CDecl,
1811 bool &IncompleteImpl,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001812 bool ImmediateClass,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001813 bool WarnCategoryMethodImpl) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001814 // Check and see if instance methods in class interface have been
1815 // implemented in the implementation class. If so, their types match.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001816 for (auto *I : CDecl->instance_methods()) {
1817 if (!InsMapSeen.insert(I->getSelector()))
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001818 continue;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001819 if (!I->isPropertyAccessor() &&
1820 !InsMap.count(I->getSelector())) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001821 if (ImmediateClass)
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001822 WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001823 diag::warn_undef_method_impl);
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001824 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +00001825 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001826 ObjCMethodDecl *ImpMethodDecl =
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001827 IMPDecl->getInstanceMethod(I->getSelector());
1828 assert(CDecl->getInstanceMethod(I->getSelector()) &&
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001829 "Expected to find the method through lookup as well");
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001830 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001831 if (ImpMethodDecl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001832 if (!WarnCategoryMethodImpl)
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001833 WarnConflictingTypedMethods(ImpMethodDecl, I,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001834 isa<ObjCProtocolDecl>(CDecl));
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001835 else if (!I->isPropertyAccessor())
1836 WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001837 }
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001838 }
1839 }
Mike Stump11289f42009-09-09 15:08:12 +00001840
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001841 // Check and see if class methods in class interface have been
1842 // implemented in the implementation class. If so, their types match.
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001843 for (auto *I : CDecl->class_methods()) {
1844 if (!ClsMapSeen.insert(I->getSelector()))
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001845 continue;
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001846 if (!ClsMap.count(I->getSelector())) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001847 if (ImmediateClass)
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001848 WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001849 diag::warn_undef_method_impl);
Mike Stump12b8ce12009-08-04 21:02:39 +00001850 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001851 ObjCMethodDecl *ImpMethodDecl =
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001852 IMPDecl->getClassMethod(I->getSelector());
1853 assert(CDecl->getClassMethod(I->getSelector()) &&
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001854 "Expected to find the method through lookup as well");
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001855 if (!WarnCategoryMethodImpl)
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001856 WarnConflictingTypedMethods(ImpMethodDecl, I,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001857 isa<ObjCProtocolDecl>(CDecl));
1858 else
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001859 WarnExactTypedMethods(ImpMethodDecl, I,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001860 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001861 }
1862 }
Fariborz Jahanian73853e52010-10-08 22:59:25 +00001863
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001864 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) {
1865 // Also, check for methods declared in protocols inherited by
1866 // this protocol.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001867 for (auto *PI : PD->protocols())
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001868 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001869 IMPDecl, PI, IncompleteImpl, false,
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001870 WarnCategoryMethodImpl);
1871 }
1872
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001873 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001874 // when checking that methods in implementation match their declaration,
1875 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1876 // extension; as well as those in categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001877 if (!WarnCategoryMethodImpl) {
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001878 for (auto *Cat : I->visible_categories())
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001879 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001880 IMPDecl, Cat, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001881 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001882 } else {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001883 // Also methods in class extensions need be looked at next.
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001884 for (auto *Ext : I->visible_extensions())
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001885 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001886 IMPDecl, Ext, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001887 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001888 }
1889
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001890 // Check for any implementation of a methods declared in protocol.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001891 for (auto *PI : I->all_referenced_protocols())
Mike Stump11289f42009-09-09 15:08:12 +00001892 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001893 IMPDecl, PI, IncompleteImpl, false,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001894 WarnCategoryMethodImpl);
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001895
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001896 // FIXME. For now, we are not checking for extact match of methods
1897 // in category implementation and its primary class's super class.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001898 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001899 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +00001900 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001901 I->getSuperClass(), IncompleteImpl, false);
1902 }
1903}
1904
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001905/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1906/// category matches with those implemented in its primary class and
1907/// warns each time an exact match is found.
1908void Sema::CheckCategoryVsClassMethodMatches(
1909 ObjCCategoryImplDecl *CatIMPDecl) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001910 // Get category's primary class.
1911 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1912 if (!CatDecl)
1913 return;
1914 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1915 if (!IDecl)
1916 return;
Fariborz Jahanianf3077a22013-12-05 20:52:31 +00001917 ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass();
1918 SelectorSet InsMap, ClsMap;
1919
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001920 for (const auto *I : CatIMPDecl->instance_methods()) {
1921 Selector Sel = I->getSelector();
Fariborz Jahanianf3077a22013-12-05 20:52:31 +00001922 // When checking for methods implemented in the category, skip over
1923 // those declared in category class's super class. This is because
1924 // the super class must implement the method.
1925 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true))
1926 continue;
1927 InsMap.insert(Sel);
1928 }
1929
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001930 for (const auto *I : CatIMPDecl->class_methods()) {
1931 Selector Sel = I->getSelector();
Fariborz Jahanianf3077a22013-12-05 20:52:31 +00001932 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false))
1933 continue;
1934 ClsMap.insert(Sel);
1935 }
1936 if (InsMap.empty() && ClsMap.empty())
1937 return;
1938
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001939 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001940 bool IncompleteImpl = false;
1941 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1942 CatIMPDecl, IDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001943 IncompleteImpl, false,
1944 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001945}
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001946
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001947void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump11289f42009-09-09 15:08:12 +00001948 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001949 bool IncompleteImpl) {
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001950 SelectorSet InsMap;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001951 // Check and see if instance methods in class interface have been
1952 // implemented in the implementation class.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001953 for (const auto *I : IMPDecl->instance_methods())
1954 InsMap.insert(I->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001955
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001956 // Check and see if properties declared in the interface have either 1)
1957 // an implementation or 2) there is a @synthesize/@dynamic implementation
1958 // of the property in the @implementation.
Ted Kremenek348e88c2014-02-21 19:41:34 +00001959 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1960 bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties &&
1961 LangOpts.ObjCRuntime.isNonFragile() &&
1962 !IDecl->isObjCRequiresPropertyDefs();
1963 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties);
1964 }
1965
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001966 SelectorSet ClsMap;
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001967 for (const auto *I : IMPDecl->class_methods())
1968 ClsMap.insert(I->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001969
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001970 // Check for type conflict of methods declared in a class/protocol and
1971 // its implementation; if any.
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001972 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +00001973 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1974 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001975 IncompleteImpl, true);
Fariborz Jahanian2bda1b62011-08-03 18:21:12 +00001976
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001977 // check all methods implemented in category against those declared
1978 // in its primary class.
1979 if (ObjCCategoryImplDecl *CatDecl =
1980 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1981 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001982
Chris Lattnerda463fe2007-12-12 07:09:47 +00001983 // Check the protocol list for unimplemented methods in the @implementation
1984 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001985 // Check and see if class methods in class interface have been
1986 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001987
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001988 LazyProtocolNameSet ExplicitImplProtocols;
1989
Chris Lattner9ef10f42009-03-01 00:56:52 +00001990 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001991 for (auto *PI : I->all_referenced_protocols())
1992 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl,
1993 InsMap, ClsMap, I, ExplicitImplProtocols);
Chris Lattner9ef10f42009-03-01 00:56:52 +00001994 // Check class extensions (unnamed categories)
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001995 for (auto *Ext : I->visible_extensions())
1996 ImplMethodsVsClassMethods(S, IMPDecl, Ext, IncompleteImpl);
Chris Lattner9ef10f42009-03-01 00:56:52 +00001997 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00001998 // For extended class, unimplemented methods in its protocols will
1999 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002000 if (!C->IsClassExtension()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00002001 for (auto *P : C->protocols())
2002 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P,
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00002003 IncompleteImpl, InsMap, ClsMap, CDecl,
2004 ExplicitImplProtocols);
Ted Kremenek348e88c2014-02-21 19:41:34 +00002005 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl,
2006 /* SynthesizeProperties */ false);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00002007 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00002008 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002009 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00002010}
2011
Mike Stump11289f42009-09-09 15:08:12 +00002012/// ActOnForwardClassDeclaration -
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00002013Sema::DeclGroupPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00002014Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00002015 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00002016 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00002017 unsigned NumElts) {
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00002018 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002019 for (unsigned i = 0; i != NumElts; ++i) {
2020 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00002021 NamedDecl *PrevDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002022 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorb8eaf292010-04-15 23:40:53 +00002023 LookupOrdinaryName, ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002024 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00002025 // GCC apparently allows the following idiom:
2026 //
2027 // typedef NSObject < XCElementTogglerP > XCElementToggler;
2028 // @class XCElementToggler;
2029 //
Fariborz Jahanian04c44552012-01-24 00:40:15 +00002030 // Here we have chosen to ignore the forward class declaration
2031 // with a warning. Since this is the implied behavior.
Richard Smithdda56e42011-04-15 14:24:37 +00002032 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCall8b07ec22010-05-15 11:32:37 +00002033 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002034 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00002035 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCall8b07ec22010-05-15 11:32:37 +00002036 } else {
Mike Stump12b8ce12009-08-04 21:02:39 +00002037 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahanian04c44552012-01-24 00:40:15 +00002038 // to the underlying class. Just ignore the forward class with a warning
2039 // as this will force the intended behavior which is to lookup the typedef
2040 // name.
2041 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
2042 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
2043 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
2044 continue;
2045 }
Fariborz Jahanian0d451812009-05-07 21:49:26 +00002046 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002047 }
Douglas Gregordc9166c2011-12-15 20:29:51 +00002048
2049 // Create a declaration to describe this forward declaration.
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00002050 ObjCInterfaceDecl *PrevIDecl
2051 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002052
2053 IdentifierInfo *ClassName = IdentList[i];
2054 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
2055 // A previous decl with a different name is because of
2056 // @compatibility_alias, for example:
2057 // \code
2058 // @class NewImage;
2059 // @compatibility_alias OldImage NewImage;
2060 // \endcode
2061 // A lookup for 'OldImage' will return the 'NewImage' decl.
2062 //
2063 // In such a case use the real declaration name, instead of the alias one,
2064 // otherwise we will break IdentifierResolver and redecls-chain invariants.
2065 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
2066 // has been aliased.
2067 ClassName = PrevIDecl->getIdentifier();
2068 }
2069
Douglas Gregordc9166c2011-12-15 20:29:51 +00002070 ObjCInterfaceDecl *IDecl
2071 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002072 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002073 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002074
Douglas Gregordc9166c2011-12-15 20:29:51 +00002075 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeafd0b2011-12-27 22:43:10 +00002076 CheckObjCDeclScope(IDecl);
2077 DeclsInGroup.push_back(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002078 }
Rafael Espindolaab417692013-07-09 12:05:01 +00002079
2080 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002081}
2082
John McCall54507ab2011-06-16 01:15:19 +00002083static bool tryMatchRecordTypes(ASTContext &Context,
2084 Sema::MethodMatchStrategy strategy,
2085 const Type *left, const Type *right);
2086
John McCall31168b02011-06-15 23:02:42 +00002087static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
2088 QualType leftQT, QualType rightQT) {
2089 const Type *left =
2090 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2091 const Type *right =
2092 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2093
2094 if (left == right) return true;
2095
2096 // If we're doing a strict match, the types have to match exactly.
2097 if (strategy == Sema::MMS_strict) return false;
2098
2099 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2100
2101 // Otherwise, use this absurdly complicated algorithm to try to
2102 // validate the basic, low-level compatibility of the two types.
2103
2104 // As a minimum, require the sizes and alignments to match.
2105 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
2106 return false;
2107
2108 // Consider all the kinds of non-dependent canonical types:
2109 // - functions and arrays aren't possible as return and parameter types
2110
2111 // - vector types of equal size can be arbitrarily mixed
2112 if (isa<VectorType>(left)) return isa<VectorType>(right);
2113 if (isa<VectorType>(right)) return false;
2114
2115 // - references should only match references of identical type
John McCall54507ab2011-06-16 01:15:19 +00002116 // - structs, unions, and Objective-C objects must match more-or-less
2117 // exactly
John McCall31168b02011-06-15 23:02:42 +00002118 // - everything else should be a scalar
2119 if (!left->isScalarType() || !right->isScalarType())
John McCall54507ab2011-06-16 01:15:19 +00002120 return tryMatchRecordTypes(Context, strategy, left, right);
John McCall31168b02011-06-15 23:02:42 +00002121
John McCall9320b872011-09-09 05:25:32 +00002122 // Make scalars agree in kind, except count bools as chars, and group
2123 // all non-member pointers together.
John McCall31168b02011-06-15 23:02:42 +00002124 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2125 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2126 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2127 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall9320b872011-09-09 05:25:32 +00002128 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2129 leftSK = Type::STK_ObjCObjectPointer;
2130 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2131 rightSK = Type::STK_ObjCObjectPointer;
John McCall31168b02011-06-15 23:02:42 +00002132
2133 // Note that data member pointers and function member pointers don't
2134 // intermix because of the size differences.
2135
2136 return (leftSK == rightSK);
2137}
Chris Lattnerda463fe2007-12-12 07:09:47 +00002138
John McCall54507ab2011-06-16 01:15:19 +00002139static bool tryMatchRecordTypes(ASTContext &Context,
2140 Sema::MethodMatchStrategy strategy,
2141 const Type *lt, const Type *rt) {
2142 assert(lt && rt && lt != rt);
2143
2144 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2145 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2146 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2147
2148 // Require union-hood to match.
2149 if (left->isUnion() != right->isUnion()) return false;
2150
2151 // Require an exact match if either is non-POD.
2152 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2153 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2154 return false;
2155
2156 // Require size and alignment to match.
2157 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2158
2159 // Require fields to match.
2160 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2161 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2162 for (; li != le && ri != re; ++li, ++ri) {
2163 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2164 return false;
2165 }
2166 return (li == le && ri == re);
2167}
2168
Chris Lattnerda463fe2007-12-12 07:09:47 +00002169/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2170/// returns true, or false, accordingly.
2171/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCall31168b02011-06-15 23:02:42 +00002172bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2173 const ObjCMethodDecl *right,
2174 MethodMatchStrategy strategy) {
Alp Toker314cc812014-01-25 16:55:45 +00002175 if (!matchTypes(Context, strategy, left->getReturnType(),
2176 right->getReturnType()))
John McCall31168b02011-06-15 23:02:42 +00002177 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002178
Douglas Gregor560b7fa2013-02-07 19:13:24 +00002179 // If either is hidden, it is not considered to match.
2180 if (left->isHidden() || right->isHidden())
2181 return false;
2182
David Blaikiebbafb8a2012-03-11 07:00:24 +00002183 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002184 (left->hasAttr<NSReturnsRetainedAttr>()
2185 != right->hasAttr<NSReturnsRetainedAttr>() ||
2186 left->hasAttr<NSConsumesSelfAttr>()
2187 != right->hasAttr<NSConsumesSelfAttr>()))
2188 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002189
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002190 ObjCMethodDecl::param_const_iterator
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002191 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2192 re = right->param_end();
Mike Stump11289f42009-09-09 15:08:12 +00002193
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002194 for (; li != le && ri != re; ++li, ++ri) {
John McCall31168b02011-06-15 23:02:42 +00002195 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002196 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCall31168b02011-06-15 23:02:42 +00002197
2198 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2199 return false;
2200
David Blaikiebbafb8a2012-03-11 07:00:24 +00002201 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002202 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2203 return false;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002204 }
2205 return true;
2206}
2207
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002208void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002209 // Record at the head of the list whether there were 0, 1, or >= 2 methods
2210 // inside categories.
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00002211 if (ObjCCategoryDecl *
2212 CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
2213 if (!CD->IsClassExtension() && List->getBits() < 2)
2214 List->setBits(List->getBits()+1);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002215
Douglas Gregorc454afe2012-01-25 00:19:56 +00002216 // If the list is empty, make it a singleton list.
2217 if (List->Method == 0) {
2218 List->Method = Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002219 List->setNext(0);
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002220 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002221 }
2222
2223 // We've seen a method with this name, see if we have already seen this type
2224 // signature.
2225 ObjCMethodList *Previous = List;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002226 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregor600a2f52013-06-21 00:20:25 +00002227 // If we are building a module, keep all of the methods.
2228 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2229 continue;
2230
Douglas Gregore1716012012-01-25 00:49:42 +00002231 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregorc454afe2012-01-25 00:19:56 +00002232 continue;
2233
2234 ObjCMethodDecl *PrevObjCMethod = List->Method;
2235
2236 // Propagate the 'defined' bit.
2237 if (Method->isDefined())
2238 PrevObjCMethod->setDefined(true);
2239
2240 // If a method is deprecated, push it in the global pool.
2241 // This is used for better diagnostics.
2242 if (Method->isDeprecated()) {
2243 if (!PrevObjCMethod->isDeprecated())
2244 List->Method = Method;
2245 }
2246 // If new method is unavailable, push it into global pool
2247 // unless previous one is deprecated.
2248 if (Method->isUnavailable()) {
2249 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2250 List->Method = Method;
2251 }
2252
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002253 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002254 }
2255
2256 // We have a new signature for an existing method - add it.
2257 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregore1716012012-01-25 00:49:42 +00002258 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002259 Previous->setNext(new (Mem) ObjCMethodList(Method, 0));
Douglas Gregorc454afe2012-01-25 00:19:56 +00002260}
2261
Sebastian Redl75d8a322010-08-02 23:18:59 +00002262/// \brief Read the contents of the method pool for a given selector from
2263/// external storage.
Douglas Gregore1716012012-01-25 00:49:42 +00002264void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002265 assert(ExternalSource && "We need an external AST source");
Douglas Gregore1716012012-01-25 00:49:42 +00002266 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002267}
2268
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002269void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002270 bool instance) {
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002271 // Ignore methods of invalid containers.
2272 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002273 return;
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002274
Douglas Gregor70f449b2012-01-25 00:59:09 +00002275 if (ExternalSource)
2276 ReadMethodPool(Method->getSelector());
2277
Sebastian Redl75d8a322010-08-02 23:18:59 +00002278 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor70f449b2012-01-25 00:59:09 +00002279 if (Pos == MethodPool.end())
2280 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2281 GlobalMethods())).first;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002282
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002283 Method->setDefined(impl);
Douglas Gregorc454afe2012-01-25 00:19:56 +00002284
Sebastian Redl75d8a322010-08-02 23:18:59 +00002285 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002286 addMethodToGlobalList(&Entry, Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002287}
2288
John McCall31168b02011-06-15 23:02:42 +00002289/// Determines if this is an "acceptable" loose mismatch in the global
2290/// method pool. This exists mostly as a hack to get around certain
2291/// global mismatches which we can't afford to make warnings / errors.
2292/// Really, what we want is a way to take a method out of the global
2293/// method pool.
2294static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2295 ObjCMethodDecl *other) {
2296 if (!chosen->isInstanceMethod())
2297 return false;
2298
2299 Selector sel = chosen->getSelector();
2300 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2301 return false;
2302
2303 // Don't complain about mismatches for -length if the method we
2304 // chose has an integral result type.
Alp Toker314cc812014-01-25 16:55:45 +00002305 return (chosen->getReturnType()->isIntegerType());
John McCall31168b02011-06-15 23:02:42 +00002306}
2307
Sebastian Redl75d8a322010-08-02 23:18:59 +00002308ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00002309 bool receiverIdOrClass,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002310 bool warn, bool instance) {
Douglas Gregor70f449b2012-01-25 00:59:09 +00002311 if (ExternalSource)
2312 ReadMethodPool(Sel);
2313
Sebastian Redl75d8a322010-08-02 23:18:59 +00002314 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor70f449b2012-01-25 00:59:09 +00002315 if (Pos == MethodPool.end())
2316 return 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002317
Douglas Gregor77f49a42013-01-16 18:47:38 +00002318 // Gather the non-hidden methods.
Sebastian Redl75d8a322010-08-02 23:18:59 +00002319 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Robert Wilhelmb869a8f2013-08-10 12:33:24 +00002320 SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002321 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Douglas Gregor77f49a42013-01-16 18:47:38 +00002322 if (M->Method && !M->Method->isHidden()) {
2323 // If we're not supposed to warn about mismatches, we're done.
2324 if (!warn)
2325 return M->Method;
Mike Stump11289f42009-09-09 15:08:12 +00002326
Douglas Gregor77f49a42013-01-16 18:47:38 +00002327 Methods.push_back(M->Method);
Sebastian Redl75d8a322010-08-02 23:18:59 +00002328 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00002329 }
Douglas Gregor77f49a42013-01-16 18:47:38 +00002330
2331 // If there aren't any visible methods, we're done.
2332 // FIXME: Recover if there are any known-but-hidden methods?
2333 if (Methods.empty())
2334 return 0;
2335
2336 if (Methods.size() == 1)
2337 return Methods[0];
2338
2339 // We found multiple methods, so we may have to complain.
2340 bool issueDiagnostic = false, issueError = false;
2341
2342 // We support a warning which complains about *any* difference in
2343 // method signature.
2344 bool strictSelectorMatch =
2345 (receiverIdOrClass && warn &&
2346 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2347 R.getBegin())
2348 != DiagnosticsEngine::Ignored));
2349 if (strictSelectorMatch) {
2350 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2351 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2352 issueDiagnostic = true;
2353 break;
2354 }
2355 }
2356 }
2357
2358 // If we didn't see any strict differences, we won't see any loose
2359 // differences. In ARC, however, we also need to check for loose
2360 // mismatches, because most of them are errors.
2361 if (!strictSelectorMatch ||
2362 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2363 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2364 // This checks if the methods differ in type mismatch.
2365 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2366 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2367 issueDiagnostic = true;
2368 if (getLangOpts().ObjCAutoRefCount)
2369 issueError = true;
2370 break;
2371 }
2372 }
2373
2374 if (issueDiagnostic) {
2375 if (issueError)
2376 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2377 else if (strictSelectorMatch)
2378 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2379 else
2380 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2381
2382 Diag(Methods[0]->getLocStart(),
2383 issueError ? diag::note_possibility : diag::note_using)
2384 << Methods[0]->getSourceRange();
2385 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2386 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2387 << Methods[I]->getSourceRange();
2388 }
2389 }
2390 return Methods[0];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002391}
2392
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002393ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redl75d8a322010-08-02 23:18:59 +00002394 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2395 if (Pos == MethodPool.end())
2396 return 0;
2397
2398 GlobalMethods &Methods = Pos->second;
2399
2400 if (Methods.first.Method && Methods.first.Method->isDefined())
2401 return Methods.first.Method;
2402 if (Methods.second.Method && Methods.second.Method->isDefined())
2403 return Methods.second.Method;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002404 return 0;
2405}
2406
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002407static void
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002408HelperSelectorsForTypoCorrection(
2409 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2410 StringRef Typo, const ObjCMethodDecl * Method) {
2411 const unsigned MaxEditDistance = 1;
2412 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieuea8d3702013-06-06 02:22:29 +00002413 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002414
2415 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2416 if (MinPossibleEditDistance > 0 &&
2417 Typo.size() / MinPossibleEditDistance < 1)
2418 return;
2419 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2420 if (EditDistance > MaxEditDistance)
2421 return;
2422 if (EditDistance == BestEditDistance)
2423 BestMethod.push_back(Method);
2424 else if (EditDistance < BestEditDistance) {
2425 BestMethod.clear();
2426 BestMethod.push_back(Method);
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002427 }
2428}
2429
Fariborz Jahanian75481672013-06-17 17:10:54 +00002430static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2431 QualType ObjectType) {
2432 if (ObjectType.isNull())
2433 return true;
2434 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2435 return true;
2436 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != 0;
2437}
2438
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002439const ObjCMethodDecl *
Fariborz Jahanian75481672013-06-17 17:10:54 +00002440Sema::SelectorsForTypoCorrection(Selector Sel,
2441 QualType ObjectType) {
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002442 unsigned NumArgs = Sel.getNumArgs();
2443 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002444 bool ObjectIsId = true, ObjectIsClass = true;
2445 if (ObjectType.isNull())
2446 ObjectIsId = ObjectIsClass = false;
2447 else if (!ObjectType->isObjCObjectPointerType())
2448 return 0;
2449 else if (const ObjCObjectPointerType *ObjCPtr =
2450 ObjectType->getAsObjCInterfacePointerType()) {
2451 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2452 ObjectIsId = ObjectIsClass = false;
2453 }
2454 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2455 ObjectIsClass = false;
2456 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2457 ObjectIsId = false;
2458 else
2459 return 0;
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002460
2461 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2462 e = MethodPool.end(); b != e; b++) {
2463 // instance methods
2464 for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
2465 if (M->Method &&
Fariborz Jahanian06499232013-06-18 17:10:58 +00002466 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2467 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002468 if (ObjectIsId)
2469 Methods.push_back(M->Method);
2470 else if (!ObjectIsClass &&
2471 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2472 Methods.push_back(M->Method);
2473 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002474 // class methods
2475 for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
2476 if (M->Method &&
Fariborz Jahanian06499232013-06-18 17:10:58 +00002477 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2478 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002479 if (ObjectIsClass)
2480 Methods.push_back(M->Method);
2481 else if (!ObjectIsId &&
2482 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2483 Methods.push_back(M->Method);
2484 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002485 }
2486
2487 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2488 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2489 HelperSelectorsForTypoCorrection(SelectedMethods,
2490 Sel.getAsString(), Methods[i]);
2491 }
2492 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : NULL;
2493}
2494
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002495/// DiagnoseDuplicateIvars -
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002496/// Check for duplicate ivars in the entire class at the start of
James Dennett634962f2012-06-14 21:40:34 +00002497/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002498/// add ivars to a class in random order which will not be known until
James Dennett634962f2012-06-14 21:40:34 +00002499/// class's \@implementation is seen.
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002500void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2501 ObjCInterfaceDecl *SID) {
Aaron Ballman59abbd42014-03-13 21:09:43 +00002502 for (auto *Ivar : ID->ivars()) {
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002503 if (Ivar->isInvalidDecl())
2504 continue;
2505 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2506 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2507 if (prevIvar) {
2508 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2509 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2510 Ivar->setInvalidDecl();
2511 }
2512 }
2513 }
2514}
2515
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002516Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2517 switch (CurContext->getDeclKind()) {
2518 case Decl::ObjCInterface:
2519 return Sema::OCK_Interface;
2520 case Decl::ObjCProtocol:
2521 return Sema::OCK_Protocol;
2522 case Decl::ObjCCategory:
2523 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2524 return Sema::OCK_ClassExtension;
2525 else
2526 return Sema::OCK_Category;
2527 case Decl::ObjCImplementation:
2528 return Sema::OCK_Implementation;
2529 case Decl::ObjCCategoryImpl:
2530 return Sema::OCK_CategoryImplementation;
2531
2532 default:
2533 return Sema::OCK_None;
2534 }
2535}
2536
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002537// Note: For class/category implementations, allMethods is always null.
Robert Wilhelm57c67112013-07-17 21:14:35 +00002538Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods,
Fariborz Jahaniandfb76872013-07-17 00:05:08 +00002539 ArrayRef<DeclGroupPtrTy> allTUVars) {
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002540 if (getObjCContainerKind() == Sema::OCK_None)
2541 return 0;
2542
2543 assert(AtEnd.isValid() && "Invalid location for '@end'");
2544
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002545 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2546 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002547
Mike Stump11289f42009-09-09 15:08:12 +00002548 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00002549 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2550 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002551 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00002552
Steve Naroff35c62ae2009-01-08 17:28:14 +00002553 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2554 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2555 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2556
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002557 for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002558 ObjCMethodDecl *Method =
John McCall48871652010-08-21 09:40:31 +00002559 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002560
2561 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00002562 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002563 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002564 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002565 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002566 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002567 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002568 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002569 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002570 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002571 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002572 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002573 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002574 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002575 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002576 if (!Context.getSourceManager().isInSystemHeader(
2577 Method->getLocation()))
2578 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2579 << Method->getDeclName();
2580 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2581 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002582 InsMap[Method->getSelector()] = Method;
2583 /// The following allows us to typecheck messages to "id".
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002584 AddInstanceMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002585 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002586 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002587 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002588 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002589 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002590 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002591 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002592 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002593 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002594 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002595 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002596 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002597 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002598 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002599 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002600 if (!Context.getSourceManager().isInSystemHeader(
2601 Method->getLocation()))
2602 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2603 << Method->getDeclName();
2604 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2605 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002606 ClsMap[Method->getSelector()] = Method;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002607 AddFactoryMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002608 }
2609 }
2610 }
Douglas Gregorb8982092013-01-21 19:42:21 +00002611 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2612 // Nothing to do here.
Steve Naroffb3a87982009-01-09 15:36:25 +00002613 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002614 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00002615 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002616 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00002617
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002618 if (C->IsClassExtension()) {
2619 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2620 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002621 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002622 }
Steve Naroffb3a87982009-01-09 15:36:25 +00002623 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002624 if (CDecl->getIdentifier())
2625 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2626 // user-defined setter/getter. It also synthesizes setter/getter methods
2627 // and adds them to the DeclContext and global method pools.
Aaron Ballmand174edf2014-03-13 19:11:50 +00002628 for (auto *I : CDecl->properties())
Aaron Ballmandc4bea42014-03-13 18:47:37 +00002629 ProcessPropertyDecl(I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00002630 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00002631 }
2632 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002633 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002634 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002635 // Any property declared in a class extension might have user
2636 // declared setter or getter in current class extension or one
2637 // of the other class extensions. Mark them as synthesized as
2638 // property will be synthesized when property with same name is
2639 // seen in the @implementation.
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00002640 for (const auto *Ext : IDecl->visible_extensions()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00002641 for (const auto *Property : Ext->properties()) {
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002642 // Skip over properties declared @dynamic
2643 if (const ObjCPropertyImplDecl *PIDecl
2644 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2645 if (PIDecl->getPropertyImplementation()
2646 == ObjCPropertyImplDecl::Dynamic)
2647 continue;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002648
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00002649 for (const auto *Ext : IDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002650 if (ObjCMethodDecl *GetterMethod
2651 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002652 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002653 if (!Property->isReadOnly())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002654 if (ObjCMethodDecl *SetterMethod
2655 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002656 SetterMethod->setPropertyAccessor(true);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002657 }
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002658 }
2659 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00002660 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002661 AtomicPropertySetterGetterRules(IC, IDecl);
John McCall31168b02011-06-15 23:02:42 +00002662 DiagnoseOwningPropertyGetterSynthesis(IC);
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002663 DiagnoseUnusedBackingIvarInAccessor(S, IC);
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002664 if (IDecl->hasDesignatedInitializers())
2665 DiagnoseMissingDesignatedInitOverrides(IC, IDecl);
2666
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002667 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2668 if (IDecl->getSuperClass() == NULL) {
2669 // This class has no superclass, so check that it has been marked with
2670 // __attribute((objc_root_class)).
2671 if (!HasRootClassAttr) {
2672 SourceLocation DeclLoc(IDecl->getLocation());
2673 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2674 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2675 << IDecl->getIdentifier();
2676 // See if NSObject is in the current scope, and if it is, suggest
2677 // adding " : NSObject " to the class declaration.
2678 NamedDecl *IF = LookupSingleName(TUScope,
2679 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2680 DeclLoc, LookupOrdinaryName);
2681 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2682 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2683 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2684 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2685 } else {
2686 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2687 }
2688 }
2689 } else if (HasRootClassAttr) {
2690 // Complain that only root classes may have this attribute.
2691 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2692 }
2693
John McCall5fb5df92012-06-20 06:18:46 +00002694 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002695 while (IDecl->getSuperClass()) {
2696 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2697 IDecl = IDecl->getSuperClass();
2698 }
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002699 }
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002700 }
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00002701 SetIvarInitializers(IC);
Mike Stump11289f42009-09-09 15:08:12 +00002702 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00002703 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002704 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00002705
Chris Lattnerda463fe2007-12-12 07:09:47 +00002706 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00002707 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00002708 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002709 if (ObjCCategoryDecl *Cat
2710 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2711 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002712 }
2713 }
2714 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002715 if (isInterfaceDeclKind) {
2716 // Reject invalid vardecls.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002717 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002718 DeclGroupRef DG = allTUVars[i].get();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002719 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2720 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00002721 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00002722 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00002723 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002724 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00002725 }
Fariborz Jahanian4327b322011-08-29 17:33:12 +00002726 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002727
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002728 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002729 DeclGroupRef DG = allTUVars[i].get();
Argyrios Kyrtzidis8ad3bab2011-11-23 20:27:36 +00002730 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2731 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002732 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2733 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002734
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +00002735 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002736 return ClassDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002737}
2738
2739
2740/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2741/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00002742static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002743CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCallca872902011-05-01 03:04:29 +00002744 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002745}
2746
Douglas Gregor33823722011-06-11 01:09:30 +00002747/// \brief Check whether the declared result type of the given Objective-C
2748/// method declaration is compatible with the method's class.
2749///
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002750static Sema::ResultTypeCompatibilityKind
Douglas Gregor33823722011-06-11 01:09:30 +00002751CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2752 ObjCInterfaceDecl *CurrentClass) {
Alp Toker314cc812014-01-25 16:55:45 +00002753 QualType ResultType = Method->getReturnType();
2754
Douglas Gregor33823722011-06-11 01:09:30 +00002755 // If an Objective-C method inherits its related result type, then its
2756 // declared result type must be compatible with its own class type. The
2757 // declared result type is compatible if:
2758 if (const ObjCObjectPointerType *ResultObjectType
2759 = ResultType->getAs<ObjCObjectPointerType>()) {
2760 // - it is id or qualified id, or
2761 if (ResultObjectType->isObjCIdType() ||
2762 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002763 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002764
2765 if (CurrentClass) {
2766 if (ObjCInterfaceDecl *ResultClass
2767 = ResultObjectType->getInterfaceDecl()) {
2768 // - it is the same as the method's class type, or
Douglas Gregor0b144e12011-12-15 00:29:59 +00002769 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002770 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002771
2772 // - it is a superclass of the method's class type
2773 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002774 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002775 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00002776 } else {
2777 // Any Objective-C pointer type might be acceptable for a protocol
2778 // method; we just don't know.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002779 return Sema::RTC_Unknown;
Douglas Gregor33823722011-06-11 01:09:30 +00002780 }
2781 }
2782
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002783 return Sema::RTC_Incompatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002784}
2785
John McCalld2930c22011-07-22 02:45:48 +00002786namespace {
2787/// A helper class for searching for methods which a particular method
2788/// overrides.
2789class OverrideSearch {
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002790public:
John McCalld2930c22011-07-22 02:45:48 +00002791 Sema &S;
2792 ObjCMethodDecl *Method;
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002793 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCalld2930c22011-07-22 02:45:48 +00002794 bool Recursive;
2795
2796public:
2797 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2798 Selector selector = method->getSelector();
2799
2800 // Bypass this search if we've never seen an instance/class method
2801 // with this selector before.
2802 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2803 if (it == S.MethodPool.end()) {
Axel Naumanndd433f02012-10-18 19:05:02 +00002804 if (!S.getExternalSource()) return;
Douglas Gregore1716012012-01-25 00:49:42 +00002805 S.ReadMethodPool(selector);
2806
2807 it = S.MethodPool.find(selector);
2808 if (it == S.MethodPool.end())
2809 return;
John McCalld2930c22011-07-22 02:45:48 +00002810 }
2811 ObjCMethodList &list =
2812 method->isInstanceMethod() ? it->second.first : it->second.second;
2813 if (!list.Method) return;
2814
2815 ObjCContainerDecl *container
2816 = cast<ObjCContainerDecl>(method->getDeclContext());
2817
2818 // Prevent the search from reaching this container again. This is
2819 // important with categories, which override methods from the
2820 // interface and each other.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002821 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2822 searchFromContainer(container);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002823 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2824 searchFromContainer(Interface);
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002825 } else {
2826 searchFromContainer(container);
2827 }
Douglas Gregor33823722011-06-11 01:09:30 +00002828 }
John McCalld2930c22011-07-22 02:45:48 +00002829
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002830 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCalld2930c22011-07-22 02:45:48 +00002831 iterator begin() const { return Overridden.begin(); }
2832 iterator end() const { return Overridden.end(); }
2833
2834private:
2835 void searchFromContainer(ObjCContainerDecl *container) {
2836 if (container->isInvalidDecl()) return;
2837
2838 switch (container->getDeclKind()) {
2839#define OBJCCONTAINER(type, base) \
2840 case Decl::type: \
2841 searchFrom(cast<type##Decl>(container)); \
2842 break;
2843#define ABSTRACT_DECL(expansion)
2844#define DECL(type, base) \
2845 case Decl::type:
2846#include "clang/AST/DeclNodes.inc"
2847 llvm_unreachable("not an ObjC container!");
2848 }
2849 }
2850
2851 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00002852 if (!protocol->hasDefinition())
2853 return;
2854
John McCalld2930c22011-07-22 02:45:48 +00002855 // A method in a protocol declaration overrides declarations from
2856 // referenced ("parent") protocols.
2857 search(protocol->getReferencedProtocols());
2858 }
2859
2860 void searchFrom(ObjCCategoryDecl *category) {
2861 // A method in a category declaration overrides declarations from
2862 // the main class and from protocols the category references.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002863 // The main class is handled in the constructor.
John McCalld2930c22011-07-22 02:45:48 +00002864 search(category->getReferencedProtocols());
2865 }
2866
2867 void searchFrom(ObjCCategoryImplDecl *impl) {
2868 // A method in a category definition that has a category
2869 // declaration overrides declarations from the category
2870 // declaration.
2871 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2872 search(category);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002873 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2874 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002875
2876 // Otherwise it overrides declarations from the class.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002877 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2878 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002879 }
2880 }
2881
2882 void searchFrom(ObjCInterfaceDecl *iface) {
2883 // A method in a class declaration overrides declarations from
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00002884 if (!iface->hasDefinition())
2885 return;
2886
John McCalld2930c22011-07-22 02:45:48 +00002887 // - categories,
Aaron Ballman15063e12014-03-13 21:35:02 +00002888 for (auto *Cat : iface->known_categories())
2889 search(Cat);
John McCalld2930c22011-07-22 02:45:48 +00002890
2891 // - the super class, and
2892 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2893 search(super);
2894
2895 // - any referenced protocols.
2896 search(iface->getReferencedProtocols());
2897 }
2898
2899 void searchFrom(ObjCImplementationDecl *impl) {
2900 // A method in a class implementation overrides declarations from
2901 // the class interface.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002902 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2903 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002904 }
2905
2906
2907 void search(const ObjCProtocolList &protocols) {
2908 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2909 i != e; ++i)
2910 search(*i);
2911 }
2912
2913 void search(ObjCContainerDecl *container) {
John McCalld2930c22011-07-22 02:45:48 +00002914 // Check for a method in this container which matches this selector.
2915 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00002916 Method->isInstanceMethod(),
2917 /*AllowHidden=*/true);
John McCalld2930c22011-07-22 02:45:48 +00002918
2919 // If we find one, record it and bail out.
2920 if (meth) {
2921 Overridden.insert(meth);
2922 return;
2923 }
2924
2925 // Otherwise, search for methods that a hypothetical method here
2926 // would have overridden.
2927
2928 // Note that we're now in a recursive case.
2929 Recursive = true;
2930
2931 searchFromContainer(container);
2932 }
2933};
Douglas Gregor33823722011-06-11 01:09:30 +00002934}
2935
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002936void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2937 ObjCInterfaceDecl *CurrentClass,
2938 ResultTypeCompatibilityKind RTC) {
2939 // Search for overridden methods and merge information down from them.
2940 OverrideSearch overrides(*this, ObjCMethod);
2941 // Keep track if the method overrides any method in the class's base classes,
2942 // its protocols, or its categories' protocols; we will keep that info
2943 // in the ObjCMethodDecl.
2944 // For this info, a method in an implementation is not considered as
2945 // overriding the same method in the interface or its categories.
2946 bool hasOverriddenMethodsInBaseOrProtocol = false;
2947 for (OverrideSearch::iterator
2948 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2949 ObjCMethodDecl *overridden = *i;
2950
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00002951 if (!hasOverriddenMethodsInBaseOrProtocol) {
2952 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2953 CurrentClass != overridden->getClassInterface() ||
2954 overridden->isOverriding()) {
2955 hasOverriddenMethodsInBaseOrProtocol = true;
2956
2957 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
2958 // OverrideSearch will return as "overridden" the same method in the
2959 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
2960 // check whether a category of a base class introduced a method with the
2961 // same selector, after the interface method declaration.
2962 // To avoid unnecessary lookups in the majority of cases, we use the
2963 // extra info bits in GlobalMethodPool to check whether there were any
2964 // category methods with this selector.
2965 GlobalMethodPool::iterator It =
2966 MethodPool.find(ObjCMethod->getSelector());
2967 if (It != MethodPool.end()) {
2968 ObjCMethodList &List =
2969 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
2970 unsigned CategCount = List.getBits();
2971 if (CategCount > 0) {
2972 // If the method is in a category we'll do lookup if there were at
2973 // least 2 category methods recorded, otherwise only one will do.
2974 if (CategCount > 1 ||
2975 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
2976 OverrideSearch overrides(*this, overridden);
2977 for (OverrideSearch::iterator
2978 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
2979 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00002980 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
2981 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00002982 hasOverriddenMethodsInBaseOrProtocol = true;
2983 overridden->setOverriding(true);
2984 break;
2985 }
2986 }
2987 }
2988 }
2989 }
2990 }
2991 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002992
2993 // Propagate down the 'related result type' bit from overridden methods.
2994 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2995 ObjCMethod->SetRelatedResultType();
2996
2997 // Then merge the declarations.
2998 mergeObjCMethodDecls(ObjCMethod, overridden);
2999
3000 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3001 continue; // Conflicting properties are detected elsewhere.
3002
3003 // Check for overriding methods
3004 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3005 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3006 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3007 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3008
3009 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanian31a25682012-07-05 22:26:07 +00003010 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3011 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003012 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3013 E = ObjCMethod->param_end();
Douglas Gregor0bf70f42012-05-17 23:13:29 +00003014 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3015 PrevE = overridden->param_end();
3016 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003017 assert(PrevI != overridden->param_end() && "Param mismatch");
3018 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3019 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3020 // If type of argument of method in this class does not match its
3021 // respective argument type in the super class method, issue warning;
3022 if (!Context.typesAreCompatible(T1, T2)) {
3023 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3024 << T1 << T2;
3025 Diag(overridden->getLocation(), diag::note_previous_declaration);
3026 break;
3027 }
3028 }
3029 }
3030 }
3031
3032 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3033}
3034
John McCall48871652010-08-21 09:40:31 +00003035Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003036 Scope *S,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003037 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003038 tok::TokenKind MethodType,
John McCallba7bf592010-08-24 05:47:05 +00003039 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003040 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003041 Selector Sel,
3042 // optional arguments. The number of types/arguments is obtained
3043 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003044 ObjCArgInfo *ArgInfo,
Fariborz Jahanian60462092010-04-08 00:30:06 +00003045 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattnerda463fe2007-12-12 07:09:47 +00003046 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003047 bool isVariadic, bool MethodDefinition) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003048 // Make sure we can establish a context for the method.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003049 if (!CurContext->isObjCContainer()) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003050 Diag(MethodLoc, diag::error_missing_method_context);
John McCall48871652010-08-21 09:40:31 +00003051 return 0;
Steve Naroff83777fe2008-02-29 21:48:07 +00003052 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003053 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3054 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003055 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00003056
Douglas Gregorbab8a962011-09-08 01:46:34 +00003057 bool HasRelatedResultType = false;
Alp Toker314cc812014-01-25 16:55:45 +00003058 TypeSourceInfo *ReturnTInfo = 0;
Steve Naroff32606412009-02-20 22:59:16 +00003059 if (ReturnType) {
Alp Toker314cc812014-01-25 16:55:45 +00003060 resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo);
Mike Stump11289f42009-09-09 15:08:12 +00003061
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003062 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
John McCall48871652010-08-21 09:40:31 +00003063 return 0;
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003064
Douglas Gregorbab8a962011-09-08 01:46:34 +00003065 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003066 } else { // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003067 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianb21138f2011-07-21 17:38:14 +00003068 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003069 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003070 }
Mike Stump11289f42009-09-09 15:08:12 +00003071
Alp Toker314cc812014-01-25 16:55:45 +00003072 ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create(
3073 Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext,
3074 MethodType == tok::minus, isVariadic,
3075 /*isPropertyAccessor=*/false,
3076 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
3077 MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional
3078 : ObjCMethodDecl::Required,
3079 HasRelatedResultType);
Mike Stump11289f42009-09-09 15:08:12 +00003080
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003081 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00003082
Chris Lattner23b0faf2009-04-11 19:42:43 +00003083 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00003084 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00003085 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00003086
David Blaikie7d170102013-05-15 07:37:26 +00003087 if (!ArgInfo[i].Type) {
John McCall856bbea2009-10-23 21:48:59 +00003088 ArgType = Context.getObjCIdType();
3089 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003090 } else {
John McCall856bbea2009-10-23 21:48:59 +00003091 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003092 }
Mike Stump11289f42009-09-09 15:08:12 +00003093
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003094 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3095 LookupOrdinaryName, ForRedeclaration);
3096 LookupName(R, S);
3097 if (R.isSingleResult()) {
3098 NamedDecl *PrevDecl = R.getFoundDecl();
3099 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003100 Diag(ArgInfo[i].NameLoc,
3101 (MethodDefinition ? diag::warn_method_param_redefinition
3102 : diag::warn_method_param_declaration))
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003103 << ArgInfo[i].Name;
3104 Diag(PrevDecl->getLocation(),
3105 diag::note_previous_declaration);
3106 }
3107 }
3108
Abramo Bagnaradff19302011-03-08 08:55:46 +00003109 SourceLocation StartLoc = DI
3110 ? DI->getTypeLoc().getBeginLoc()
3111 : ArgInfo[i].NameLoc;
3112
John McCalld44f4d72011-04-23 02:46:06 +00003113 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3114 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003115 ArgType, DI, SC_None);
Mike Stump11289f42009-09-09 15:08:12 +00003116
John McCall82490832011-05-02 00:30:12 +00003117 Param->setObjCMethodScopeInfo(i);
3118
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003119 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003120 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00003121
Chris Lattner9713a1c2009-04-11 19:34:56 +00003122 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00003123 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00003124
Fariborz Jahanian52d02f62012-01-14 18:44:35 +00003125 if (Param->hasAttr<BlocksAttr>()) {
3126 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3127 Param->setInvalidDecl();
3128 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003129 S->AddDecl(Param);
3130 IdResolver.AddDecl(Param);
3131
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003132 Params.push_back(Param);
3133 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003134
Fariborz Jahanian60462092010-04-08 00:30:06 +00003135 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCall48871652010-08-21 09:40:31 +00003136 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003137 QualType ArgType = Param->getType();
3138 if (ArgType.isNull())
3139 ArgType = Context.getObjCIdType();
3140 else
3141 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor84280642011-07-12 04:42:08 +00003142 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003143
Fariborz Jahanian60462092010-04-08 00:30:06 +00003144 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003145 Params.push_back(Param);
3146 }
3147
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003148 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003149 ObjCMethod->setObjCDeclQualifier(
3150 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00003151
3152 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00003153 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00003154
Douglas Gregor87e92752010-12-21 17:34:17 +00003155 // Add the method now.
John McCalld2930c22011-07-22 02:45:48 +00003156 const ObjCMethodDecl *PrevMethod = 0;
3157 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00003158 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003159 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3160 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003161 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003162 PrevMethod = ImpDecl->getClassMethod(Sel);
3163 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003164 }
Douglas Gregor33823722011-06-11 01:09:30 +00003165
Fariborz Jahanian512a4cc92011-10-22 01:21:15 +00003166 ObjCMethodDecl *IMD = 0;
3167 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3168 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3169 ObjCMethod->isInstanceMethod());
Fariborz Jahaniandb4fc282013-07-09 22:02:20 +00003170 if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
3171 !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
3172 // merge the attribute into implementation.
Aaron Ballman36a53502014-01-16 13:03:14 +00003173 ObjCMethod->addAttr(ObjCRequiresSuperAttr::CreateImplicit(Context,
3174 ObjCMethod->getLocation()));
Fariborz Jahaniandb4fc282013-07-09 22:02:20 +00003175 }
Fariborz Jahanian7e350d22013-12-17 22:44:28 +00003176 if (isa<ObjCCategoryImplDecl>(ImpDecl)) {
Fariborz Jahanianf40ef452014-01-28 22:46:29 +00003177 ObjCMethodFamily family =
3178 ObjCMethod->getSelector().getMethodFamily();
Fariborz Jahanian1b30b592013-12-18 00:52:54 +00003179 if (family == OMF_dealloc && IMD && IMD->isOverriding())
Fariborz Jahanian7e350d22013-12-17 22:44:28 +00003180 Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category)
3181 << ObjCMethod->getDeclName();
Fariborz Jahanian7e350d22013-12-17 22:44:28 +00003182 }
Douglas Gregor87e92752010-12-21 17:34:17 +00003183 } else {
3184 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003185 }
John McCalld2930c22011-07-22 02:45:48 +00003186
Chris Lattnerda463fe2007-12-12 07:09:47 +00003187 if (PrevMethod) {
3188 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00003189 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00003190 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00003191 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian096f7c12013-05-13 17:27:00 +00003192 ObjCMethod->setInvalidDecl();
3193 return ObjCMethod;
Mike Stump11289f42009-09-09 15:08:12 +00003194 }
John McCall28a6aea2009-11-04 02:18:39 +00003195
Douglas Gregor33823722011-06-11 01:09:30 +00003196 // If this Objective-C method does not have a related result type, but we
3197 // are allowed to infer related result types, try to do so based on the
3198 // method family.
3199 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3200 if (!CurrentClass) {
3201 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3202 CurrentClass = Cat->getClassInterface();
3203 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3204 CurrentClass = Impl->getClassInterface();
3205 else if (ObjCCategoryImplDecl *CatImpl
3206 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3207 CurrentClass = CatImpl->getClassInterface();
3208 }
John McCalld2930c22011-07-22 02:45:48 +00003209
Douglas Gregorbab8a962011-09-08 01:46:34 +00003210 ResultTypeCompatibilityKind RTC
3211 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCalld2930c22011-07-22 02:45:48 +00003212
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003213 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCalld2930c22011-07-22 02:45:48 +00003214
John McCall31168b02011-06-15 23:02:42 +00003215 bool ARCError = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003216 if (getLangOpts().ObjCAutoRefCount)
John McCalle48f3892013-04-04 01:38:37 +00003217 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCall31168b02011-06-15 23:02:42 +00003218
Douglas Gregorbab8a962011-09-08 01:46:34 +00003219 // Infer the related result type when possible.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003220 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregorbab8a962011-09-08 01:46:34 +00003221 !ObjCMethod->hasRelatedResultType() &&
3222 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor33823722011-06-11 01:09:30 +00003223 bool InferRelatedResultType = false;
3224 switch (ObjCMethod->getMethodFamily()) {
3225 case OMF_None:
3226 case OMF_copy:
3227 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00003228 case OMF_finalize:
Douglas Gregor33823722011-06-11 01:09:30 +00003229 case OMF_mutableCopy:
3230 case OMF_release:
3231 case OMF_retainCount:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00003232 case OMF_performSelector:
Douglas Gregor33823722011-06-11 01:09:30 +00003233 break;
3234
3235 case OMF_alloc:
3236 case OMF_new:
3237 InferRelatedResultType = ObjCMethod->isClassMethod();
3238 break;
3239
3240 case OMF_init:
3241 case OMF_autorelease:
3242 case OMF_retain:
3243 case OMF_self:
3244 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3245 break;
3246 }
3247
John McCalld2930c22011-07-22 02:45:48 +00003248 if (InferRelatedResultType)
Douglas Gregor33823722011-06-11 01:09:30 +00003249 ObjCMethod->SetRelatedResultType();
Douglas Gregor33823722011-06-11 01:09:30 +00003250 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00003251
3252 ActOnDocumentableDecl(ObjCMethod);
3253
John McCall48871652010-08-21 09:40:31 +00003254 return ObjCMethod;
Chris Lattnerda463fe2007-12-12 07:09:47 +00003255}
3256
Chris Lattner438e5012008-12-17 07:13:27 +00003257bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +00003258 // Following is also an error. But it is caused by a missing @end
3259 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003260 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003261 return false;
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003262
3263 // If we switched context to translation unit while we are still lexically in
3264 // an objc container, it means the parser missed emitting an error.
3265 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3266 return false;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003267
Anders Carlssona6b508a2008-11-04 16:57:32 +00003268 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3269 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003270
Anders Carlssona6b508a2008-11-04 16:57:32 +00003271 return true;
3272}
Chris Lattner438e5012008-12-17 07:13:27 +00003273
James Dennett634962f2012-06-14 21:40:34 +00003274/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattner438e5012008-12-17 07:13:27 +00003275/// instance variables of ClassName into Decls.
John McCall48871652010-08-21 09:40:31 +00003276void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00003277 IdentifierInfo *ClassName,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003278 SmallVectorImpl<Decl*> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00003279 // Check that ClassName is a valid class
Douglas Gregorb2ccf012010-04-15 22:33:43 +00003280 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattner438e5012008-12-17 07:13:27 +00003281 if (!Class) {
3282 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3283 return;
3284 }
John McCall5fb5df92012-06-20 06:18:46 +00003285 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00003286 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3287 return;
3288 }
Mike Stump11289f42009-09-09 15:08:12 +00003289
Chris Lattner438e5012008-12-17 07:13:27 +00003290 // Collect the instance variables
Jordy Rosea91768e2011-07-22 02:08:32 +00003291 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003292 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003293 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003294 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003295 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCall48871652010-08-21 09:40:31 +00003296 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaradff19302011-03-08 08:55:46 +00003297 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3298 /*FIXME: StartL=*/ID->getLocation(),
3299 ID->getLocation(),
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003300 ID->getIdentifier(), ID->getType(),
3301 ID->getBitWidth());
John McCall48871652010-08-21 09:40:31 +00003302 Decls.push_back(FD);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003303 }
Mike Stump11289f42009-09-09 15:08:12 +00003304
Chris Lattner438e5012008-12-17 07:13:27 +00003305 // Introduce all of these fields into the appropriate scope.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003306 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00003307 D != Decls.end(); ++D) {
John McCall48871652010-08-21 09:40:31 +00003308 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003309 if (getLangOpts().CPlusPlus)
Chris Lattner438e5012008-12-17 07:13:27 +00003310 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCall48871652010-08-21 09:40:31 +00003311 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003312 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00003313 }
3314}
3315
Douglas Gregorf3564192010-04-26 17:32:49 +00003316/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaradff19302011-03-08 08:55:46 +00003317VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3318 SourceLocation StartLoc,
3319 SourceLocation IdLoc,
3320 IdentifierInfo *Id,
Douglas Gregorf3564192010-04-26 17:32:49 +00003321 bool Invalid) {
3322 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3323 // duration shall not be qualified by an address-space qualifier."
3324 // Since all parameters have automatic store duration, they can not have
3325 // an address space.
3326 if (T.getAddressSpace() != 0) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003327 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregorf3564192010-04-26 17:32:49 +00003328 Invalid = true;
3329 }
3330
3331 // An @catch parameter must be an unqualified object pointer type;
3332 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3333 if (Invalid) {
3334 // Don't do any further checking.
Douglas Gregorf4e837f2010-04-26 17:57:08 +00003335 } else if (T->isDependentType()) {
3336 // Okay: we don't know what this type will instantiate to.
Douglas Gregorf3564192010-04-26 17:32:49 +00003337 } else if (!T->isObjCObjectPointerType()) {
3338 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003339 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregorf3564192010-04-26 17:32:49 +00003340 } else if (T->isObjCQualifiedIdType()) {
3341 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003342 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregorf3564192010-04-26 17:32:49 +00003343 }
3344
Abramo Bagnaradff19302011-03-08 08:55:46 +00003345 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003346 T, TInfo, SC_None);
Douglas Gregor3f324d562010-05-03 18:51:14 +00003347 New->setExceptionVariable(true);
3348
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003349 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003350 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003351 Invalid = true;
3352
Douglas Gregorf3564192010-04-26 17:32:49 +00003353 if (Invalid)
3354 New->setInvalidDecl();
3355 return New;
3356}
3357
John McCall48871652010-08-21 09:40:31 +00003358Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003359 const DeclSpec &DS = D.getDeclSpec();
3360
3361 // We allow the "register" storage class on exception variables because
3362 // GCC did, but we drop it completely. Any other storage class is an error.
3363 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3364 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3365 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithb4a9e862013-04-12 22:46:28 +00003366 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003367 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithb4a9e862013-04-12 22:46:28 +00003368 << DeclSpec::getSpecifierName(SCS);
3369 }
3370 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3371 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3372 diag::err_invalid_thread)
3373 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregorf3564192010-04-26 17:32:49 +00003374 D.getMutableDeclSpec().ClearStorageClassSpecs();
3375
Richard Smithb1402ae2013-03-18 22:52:47 +00003376 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregorf3564192010-04-26 17:32:49 +00003377
3378 // Check that there are no default arguments inside the type of this
3379 // exception object (C++ only).
David Blaikiebbafb8a2012-03-11 07:00:24 +00003380 if (getLangOpts().CPlusPlus)
Douglas Gregorf3564192010-04-26 17:32:49 +00003381 CheckExtraCXXDefaultArguments(D);
3382
Argyrios Kyrtzidisef7022f2011-06-28 03:01:15 +00003383 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCall8cb7bdf2010-06-04 23:28:52 +00003384 QualType ExceptionType = TInfo->getType();
Douglas Gregorf3564192010-04-26 17:32:49 +00003385
Abramo Bagnaradff19302011-03-08 08:55:46 +00003386 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3387 D.getSourceRange().getBegin(),
3388 D.getIdentifierLoc(),
3389 D.getIdentifier(),
Douglas Gregorf3564192010-04-26 17:32:49 +00003390 D.isInvalidType());
3391
3392 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3393 if (D.getCXXScopeSpec().isSet()) {
3394 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3395 << D.getCXXScopeSpec().getRange();
3396 New->setInvalidDecl();
3397 }
3398
3399 // Add the parameter declaration into this scope.
John McCall48871652010-08-21 09:40:31 +00003400 S->AddDecl(New);
Douglas Gregorf3564192010-04-26 17:32:49 +00003401 if (D.getIdentifier())
3402 IdResolver.AddDecl(New);
3403
3404 ProcessDeclAttributes(S, New, D);
3405
3406 if (New->hasAttr<BlocksAttr>())
3407 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCall48871652010-08-21 09:40:31 +00003408 return New;
Douglas Gregore11ee112010-04-23 23:01:43 +00003409}
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003410
3411/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003412/// initialization.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003413void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003414 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003415 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3416 Iv= Iv->getNextIvar()) {
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003417 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00003418 if (QT->isRecordType())
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003419 Ivars.push_back(Iv);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003420 }
3421}
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003422
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003423void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor72e357f2011-07-28 14:54:22 +00003424 // Load referenced selectors from the external source.
3425 if (ExternalSource) {
3426 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3427 ExternalSource->ReadReferencedSelectors(Sels);
3428 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3429 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3430 }
3431
Fariborz Jahanianc9b7c202011-02-04 23:19:27 +00003432 // Warning will be issued only when selector table is
3433 // generated (which means there is at lease one implementation
3434 // in the TU). This is to match gcc's behavior.
3435 if (ReferencedSelectors.empty() ||
3436 !Context.AnyObjCImplementation())
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003437 return;
3438 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3439 ReferencedSelectors.begin(),
3440 E = ReferencedSelectors.end(); S != E; ++S) {
3441 Selector Sel = (*S).first;
3442 if (!LookupImplementedMethodInGlobalPool(Sel))
3443 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3444 }
3445 return;
3446}
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003447
3448ObjCIvarDecl *
3449Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method,
3450 const ObjCPropertyDecl *&PDecl) const {
Fariborz Jahanian1cc7ae12014-01-02 17:24:32 +00003451 if (Method->isClassMethod())
3452 return 0;
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003453 const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
3454 if (!IDecl)
3455 return 0;
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003456 Method = IDecl->lookupMethod(Method->getSelector(), /*isInstance=*/true,
3457 /*shallowCategoryLookup=*/false,
3458 /*followSuper=*/false);
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003459 if (!Method || !Method->isPropertyAccessor())
3460 return 0;
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003461 if ((PDecl = Method->findPropertyDecl()))
Fariborz Jahanian122d94f2014-01-27 22:27:43 +00003462 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) {
3463 // property backing ivar must belong to property's class
3464 // or be a private ivar in class's implementation.
3465 // FIXME. fix the const-ness issue.
3466 IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable(
3467 IV->getIdentifier());
3468 return IV;
3469 }
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003470 return 0;
3471}
3472
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003473namespace {
3474 /// Used by Sema::DiagnoseUnusedBackingIvarInAccessor to check if a property
3475 /// accessor references the backing ivar.
Argyrios Kyrtzidis98045c12014-01-03 19:53:09 +00003476 class UnusedBackingIvarChecker :
3477 public DataRecursiveASTVisitor<UnusedBackingIvarChecker> {
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003478 public:
3479 Sema &S;
3480 const ObjCMethodDecl *Method;
3481 const ObjCIvarDecl *IvarD;
3482 bool AccessedIvar;
3483 bool InvokedSelfMethod;
3484
3485 UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method,
3486 const ObjCIvarDecl *IvarD)
3487 : S(S), Method(Method), IvarD(IvarD),
3488 AccessedIvar(false), InvokedSelfMethod(false) {
3489 assert(IvarD);
3490 }
3491
3492 bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
3493 if (E->getDecl() == IvarD) {
3494 AccessedIvar = true;
3495 return false;
3496 }
3497 return true;
3498 }
3499
3500 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
3501 if (E->getReceiverKind() == ObjCMessageExpr::Instance &&
3502 S.isSelfExpr(E->getInstanceReceiver(), Method)) {
3503 InvokedSelfMethod = true;
3504 }
3505 return true;
3506 }
3507 };
3508}
3509
3510void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S,
3511 const ObjCImplementationDecl *ImplD) {
3512 if (S->hasUnrecoverableErrorOccurred())
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003513 return;
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003514
Aaron Ballmanf26acce2014-03-13 19:50:17 +00003515 for (const auto *CurMethod : ImplD->instance_methods()) {
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003516 unsigned DIAG = diag::warn_unused_property_backing_ivar;
3517 SourceLocation Loc = CurMethod->getLocation();
3518 if (Diags.getDiagnosticLevel(DIAG, Loc) == DiagnosticsEngine::Ignored)
3519 continue;
3520
3521 const ObjCPropertyDecl *PDecl;
3522 const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl);
3523 if (!IV)
3524 continue;
3525
3526 UnusedBackingIvarChecker Checker(*this, CurMethod, IV);
3527 Checker.TraverseStmt(CurMethod->getBody());
3528 if (Checker.AccessedIvar)
3529 continue;
3530
Fariborz Jahanian5b3105d2014-01-02 22:42:09 +00003531 // Do not issue this warning if backing ivar is used somewhere and accessor
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003532 // implementation makes a self call. This is to prevent false positive in
3533 // cases where the ivar is accessed by another method that the accessor
3534 // delegates to.
3535 if (!IV->isReferenced() || !Checker.InvokedSelfMethod) {
Argyrios Kyrtzidisd8a35322014-01-03 19:39:23 +00003536 Diag(Loc, DIAG) << IV;
Fariborz Jahanian5b3105d2014-01-02 22:42:09 +00003537 Diag(PDecl->getLocation(), diag::note_property_declare);
3538 }
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003539 }
3540}