blob: 9982493ebc670060ccf390cbd1645ba4591792f3 [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) {
Fariborz Jahanian19a08bb2014-03-18 00:10:37 +0000375 ObjCCategoryDecl *CD = 0;
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000376 ObjCImplDecl *ImplDeclOfMethodDef =
377 dyn_cast<ObjCImplDecl>(MDecl->getDeclContext());
378 ObjCContainerDecl *ContDeclOfMethodDecl =
379 dyn_cast<ObjCContainerDecl>(IMD->getDeclContext());
380 ObjCImplDecl *ImplDeclOfMethodDecl = 0;
381 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
382 ImplDeclOfMethodDecl = OID->getImplementation();
Fariborz Jahanian19a08bb2014-03-18 00:10:37 +0000383 else if ((CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl))) {
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000384 ImplDeclOfMethodDecl = CD->getImplementation();
Fariborz Jahanian19a08bb2014-03-18 00:10:37 +0000385 }
386 bool warn;
387 // No need to issue deprecated warning if deprecated method in class
388 // extension is being implemented in primary class implementation
389 // (no overriding is involved).
390 if (ImplDeclOfMethodDef && CD && CD->IsClassExtension())
391 warn = false;
392 else
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000393 // No need to issue deprecated warning if deprecated mehod in class/category
394 // is being implemented in its own implementation (no overriding is involved).
Fariborz Jahanian19a08bb2014-03-18 00:10:37 +0000395 warn = (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef);
396 if (warn)
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000397 DiagnoseObjCImplementedDeprecations(*this,
Fariborz Jahaniand724a102011-02-15 17:49:58 +0000398 dyn_cast<NamedDecl>(IMD),
399 MDecl->getLocation(), 0);
Fariborz Jahaniand91d21c2012-11-17 20:53:53 +0000400 }
Nico Weber715abaf2011-08-22 17:25:57 +0000401
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +0000402 if (MDecl->getMethodFamily() == OMF_init) {
403 if (MDecl->isDesignatedInitializerForTheInterface()) {
404 getCurFunction()->ObjCIsDesignatedInit = true;
Fariborz Jahaniane3b5c992014-03-14 23:30:18 +0000405 getCurFunction()->ObjCWarnForNoDesignatedInitChain =
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +0000406 IC->getSuperClass() != 0;
407 } else if (IC->hasDesignatedInitializers()) {
408 getCurFunction()->ObjCIsSecondaryInit = true;
Fariborz Jahaniane3b5c992014-03-14 23:30:18 +0000409 getCurFunction()->ObjCWarnForNoInitDelegation = true;
Argyrios Kyrtzidisb66d3cf2013-12-03 21:11:49 +0000410 }
411 }
Argyrios Kyrtzidis22bfa2c2013-12-03 21:11:36 +0000412
Nico Weber1fb82662011-08-28 22:35:17 +0000413 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber715abaf2011-08-22 17:25:57 +0000414 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
415 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
416 // Only do this if the current class actually has a superclass.
Jordan Rosed03d99d2013-03-05 01:27:54 +0000417 if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) {
Jordan Rose2afd6612012-10-19 16:05:26 +0000418 ObjCMethodFamily Family = MDecl->getMethodFamily();
419 if (Family == OMF_dealloc) {
420 if (!(getLangOpts().ObjCAutoRefCount ||
421 getLangOpts().getGC() == LangOptions::GCOnly))
422 getCurFunction()->ObjCShouldCallSuper = true;
423
424 } else if (Family == OMF_finalize) {
425 if (Context.getLangOpts().getGC() != LangOptions::NonGC)
426 getCurFunction()->ObjCShouldCallSuper = true;
427
Fariborz Jahaniance4bbb22013-11-05 00:28:21 +0000428 } else {
Jordan Rose2afd6612012-10-19 16:05:26 +0000429 const ObjCMethodDecl *SuperMethod =
Jordan Rosed03d99d2013-03-05 01:27:54 +0000430 SuperClass->lookupMethod(MDecl->getSelector(),
431 MDecl->isInstanceMethod());
Jordan Rose2afd6612012-10-19 16:05:26 +0000432 getCurFunction()->ObjCShouldCallSuper =
433 (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahaniand6876b22012-09-10 18:04:25 +0000434 }
Nico Weber1fb82662011-08-28 22:35:17 +0000435 }
Nico Weber715abaf2011-08-22 17:25:57 +0000436 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000437}
438
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000439namespace {
440
441// Callback to only accept typo corrections that are Objective-C classes.
442// If an ObjCInterfaceDecl* is given to the constructor, then the validation
443// function will reject corrections to that class.
444class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
445 public:
446 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
447 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
448 : CurrentIDecl(IDecl) {}
449
Craig Toppere14c0f82014-03-12 04:55:44 +0000450 bool ValidateCandidate(const TypoCorrection &candidate) override {
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000451 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
452 return ID && !declaresSameEntity(ID, CurrentIDecl);
453 }
454
455 private:
456 ObjCInterfaceDecl *CurrentIDecl;
457};
458
459}
460
John McCall48871652010-08-21 09:40:31 +0000461Decl *Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000462ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
463 IdentifierInfo *ClassName, SourceLocation ClassLoc,
464 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCall48871652010-08-21 09:40:31 +0000465 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000466 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000467 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000468 assert(ClassName && "Missing class identifier");
Mike Stump11289f42009-09-09 15:08:12 +0000469
Chris Lattnerda463fe2007-12-12 07:09:47 +0000470 // Check for another declaration kind with the same name.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000471 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000472 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor5101c242008-12-05 18:15:24 +0000473
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000474 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000475 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +0000476 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000477 }
Mike Stump11289f42009-09-09 15:08:12 +0000478
Douglas Gregordc9166c2011-12-15 20:29:51 +0000479 // Create a declaration to describe this @interface.
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000480 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +0000481
482 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
483 // A previous decl with a different name is because of
484 // @compatibility_alias, for example:
485 // \code
486 // @class NewImage;
487 // @compatibility_alias OldImage NewImage;
488 // \endcode
489 // A lookup for 'OldImage' will return the 'NewImage' decl.
490 //
491 // In such a case use the real declaration name, instead of the alias one,
492 // otherwise we will break IdentifierResolver and redecls-chain invariants.
493 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
494 // has been aliased.
495 ClassName = PrevIDecl->getIdentifier();
496 }
497
Douglas Gregordc9166c2011-12-15 20:29:51 +0000498 ObjCInterfaceDecl *IDecl
499 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +0000500 PrevIDecl, ClassLoc);
Douglas Gregordc9166c2011-12-15 20:29:51 +0000501
Douglas Gregordc9166c2011-12-15 20:29:51 +0000502 if (PrevIDecl) {
503 // Class already seen. Was it a definition?
504 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
505 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
506 << PrevIDecl->getDeclName();
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000507 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregordc9166c2011-12-15 20:29:51 +0000508 IDecl->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +0000509 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000510 }
Douglas Gregordc9166c2011-12-15 20:29:51 +0000511
512 if (AttrList)
513 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
514 PushOnScopeChains(IDecl, TUScope);
Mike Stump11289f42009-09-09 15:08:12 +0000515
Douglas Gregordc9166c2011-12-15 20:29:51 +0000516 // Start the definition of this class. If we're in a redefinition case, there
517 // may already be a definition, so we'll end up adding to it.
Douglas Gregorc0ac7d62011-12-15 05:27:12 +0000518 if (!IDecl->hasDefinition())
519 IDecl->startDefinition();
520
Chris Lattnerda463fe2007-12-12 07:09:47 +0000521 if (SuperName) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000522 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000523 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
524 LookupOrdinaryName);
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000525
526 if (!PrevDecl) {
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000527 // Try to correct for a typo in the superclass name without correcting
528 // to the class we're defining.
529 ObjCInterfaceValidatorCCC Validator(IDecl);
530 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000531 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain4e8942c2012-01-31 23:49:25 +0000532 NULL, Validator)) {
Richard Smithf9b15102013-08-17 00:46:16 +0000533 diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest)
534 << SuperName << ClassName);
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000535 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000536 }
537 }
538
Douglas Gregor0b144e12011-12-15 00:29:59 +0000539 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000540 Diag(SuperLoc, diag::err_recursive_superclass)
541 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor16408322011-12-15 22:34:59 +0000542 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stump12b8ce12009-08-04 21:02:39 +0000543 } else {
Mike Stump11289f42009-09-09 15:08:12 +0000544 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000545 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000546
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000547 // Diagnose classes that inherit from deprecated classes.
548 if (SuperClassDecl)
549 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000550
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000551 if (PrevDecl && SuperClassDecl == 0) {
552 // The previous declaration was not a class decl. Check if we have a
553 // typedef. If we do, get the underlying class type.
Richard Smithdda56e42011-04-15 14:24:37 +0000554 if (const TypedefNameDecl *TDecl =
555 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000556 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000557 if (T->isObjCObjectType()) {
Fariborz Jahanian83f1be12013-04-04 18:45:52 +0000558 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Douglas Gregor1c283312010-08-11 12:19:30 +0000559 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian83f1be12013-04-04 18:45:52 +0000560 // This handles the following case:
561 // @interface NewI @end
562 // typedef NewI DeprI __attribute__((deprecated("blah")))
563 // @interface SI : DeprI /* warn here */ @end
564 (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc);
565 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000566 }
567 }
Mike Stump11289f42009-09-09 15:08:12 +0000568
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000569 // This handles the following case:
570 //
571 // typedef int SuperClass;
572 // @interface MyClass : SuperClass {} @end
573 //
574 if (!SuperClassDecl) {
575 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
576 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff189d41f2009-02-04 17:14:05 +0000577 }
578 }
Mike Stump11289f42009-09-09 15:08:12 +0000579
Richard Smithdda56e42011-04-15 14:24:37 +0000580 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000581 if (!SuperClassDecl)
582 Diag(SuperLoc, diag::err_undef_superclass)
583 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor4123a862011-11-14 22:10:01 +0000584 else if (RequireCompleteType(SuperLoc,
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000585 Context.getObjCInterfaceType(SuperClassDecl),
586 diag::err_forward_superclass,
587 SuperClassDecl->getDeclName(),
588 ClassName,
589 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahanian3ee91fa2011-06-23 23:16:19 +0000590 SuperClassDecl = 0;
591 }
Steve Naroff189d41f2009-02-04 17:14:05 +0000592 }
Fariborz Jahanian5582f232009-07-09 22:08:26 +0000593 IDecl->setSuperClass(SuperClassDecl);
594 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor16408322011-12-15 22:34:59 +0000595 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff189d41f2009-02-04 17:14:05 +0000596 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000597 } else { // we have a root class.
Douglas Gregor16408322011-12-15 22:34:59 +0000598 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000599 }
Mike Stump11289f42009-09-09 15:08:12 +0000600
Sebastian Redle7c1fe62010-08-13 00:28:03 +0000601 // Check then save referenced protocols.
Chris Lattnerdf59f5a2008-07-26 04:13:19 +0000602 if (NumProtoRefs) {
Roman Divackye6377112012-09-06 15:59:27 +0000603 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000604 ProtoLocs, Context);
Douglas Gregor16408322011-12-15 22:34:59 +0000605 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000606 }
Mike Stump11289f42009-09-09 15:08:12 +0000607
Anders Carlssona6b508a2008-11-04 16:57:32 +0000608 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000609 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000610}
611
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000612/// ActOnTypedefedProtocols - this action finds protocol list as part of the
613/// typedef'ed use for a qualified super class and adds them to the list
614/// of the protocols.
615void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs,
616 IdentifierInfo *SuperName,
617 SourceLocation SuperLoc) {
618 if (!SuperName)
619 return;
620 NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
621 LookupOrdinaryName);
622 if (!IDecl)
623 return;
624
625 if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) {
626 QualType T = TDecl->getUnderlyingType();
627 if (T->isObjCObjectType())
628 if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>())
Aaron Ballman1683f7b2014-03-17 15:55:30 +0000629 for (auto *I : OPT->quals())
630 ProtocolRefs.push_back(I);
Fariborz Jahanianb7c5f742013-09-25 19:36:32 +0000631 }
632}
633
Richard Smithac4e36d2012-08-08 23:32:13 +0000634/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett634962f2012-06-14 21:40:34 +0000635/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithac4e36d2012-08-08 23:32:13 +0000636Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
637 IdentifierInfo *AliasName,
638 SourceLocation AliasLocation,
639 IdentifierInfo *ClassName,
640 SourceLocation ClassLocation) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000641 // Look for previous declaration of alias name
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000642 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000643 LookupOrdinaryName, ForRedeclaration);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000644 if (ADecl) {
Eli Friedmanfd6b3f82013-06-21 01:49:53 +0000645 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattnerd0685032008-11-23 23:20:13 +0000646 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +0000647 return 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000648 }
649 // Check for class declaration
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000650 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000651 LookupOrdinaryName, ForRedeclaration);
Richard Smithdda56e42011-04-15 14:24:37 +0000652 if (const TypedefNameDecl *TDecl =
653 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000654 QualType T = TDecl->getUnderlyingType();
John McCall8b07ec22010-05-15 11:32:37 +0000655 if (T->isObjCObjectType()) {
656 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000657 ClassName = IDecl->getIdentifier();
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000658 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorb8eaf292010-04-15 23:40:53 +0000659 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian17290c32009-01-08 01:10:55 +0000660 }
661 }
662 }
Chris Lattner219b3e92008-03-16 21:17:37 +0000663 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
664 if (CDecl == 0) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000665 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattner219b3e92008-03-16 21:17:37 +0000666 if (CDeclU)
Chris Lattnerd0685032008-11-23 23:20:13 +0000667 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCall48871652010-08-21 09:40:31 +0000668 return 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000669 }
Mike Stump11289f42009-09-09 15:08:12 +0000670
Chris Lattner219b3e92008-03-16 21:17:37 +0000671 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump11289f42009-09-09 15:08:12 +0000672 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000673 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump11289f42009-09-09 15:08:12 +0000674
Anders Carlssona6b508a2008-11-04 16:57:32 +0000675 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor38feed82009-04-24 02:57:34 +0000676 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000677
John McCall48871652010-08-21 09:40:31 +0000678 return AliasDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000679}
680
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000681bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff41d09ad2009-03-05 15:22:01 +0000682 IdentifierInfo *PName,
683 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000684 const ObjCList<ObjCProtocolDecl> &PList) {
685
686 bool res = false;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000687 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
688 E = PList.end(); I != E; ++I) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000689 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
690 Ploc)) {
Steve Naroff41d09ad2009-03-05 15:22:01 +0000691 if (PDecl->getIdentifier() == PName) {
692 Diag(Ploc, diag::err_protocol_has_circular_dependency);
693 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000694 res = true;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000695 }
Douglas Gregore6e48b12012-01-01 19:29:29 +0000696
697 if (!PDecl->hasDefinition())
698 continue;
699
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000700 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
701 PDecl->getLocation(), PDecl->getReferencedProtocols()))
702 res = true;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000703 }
704 }
Fariborz Jahanian7d622732011-05-13 18:02:08 +0000705 return res;
Steve Naroff41d09ad2009-03-05 15:22:01 +0000706}
707
John McCall48871652010-08-21 09:40:31 +0000708Decl *
Chris Lattner3bbae002008-07-26 04:03:38 +0000709Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
710 IdentifierInfo *ProtocolName,
711 SourceLocation ProtocolLoc,
John McCall48871652010-08-21 09:40:31 +0000712 Decl * const *ProtoRefs,
Chris Lattner3bbae002008-07-26 04:03:38 +0000713 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000714 const SourceLocation *ProtoLocs,
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000715 SourceLocation EndProtoLoc,
716 AttributeList *AttrList) {
Fariborz Jahaniancadf7c52011-05-12 22:04:39 +0000717 bool err = false;
Daniel Dunbar26e2ab42008-09-26 04:48:09 +0000718 // FIXME: Deal with AttrList.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000719 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor32c17572012-01-01 20:30:41 +0000720 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
721 ForRedeclaration);
722 ObjCProtocolDecl *PDecl = 0;
723 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
724 // If we already have a definition, complain.
725 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
726 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump11289f42009-09-09 15:08:12 +0000727
Douglas Gregor32c17572012-01-01 20:30:41 +0000728 // Create a new protocol that is completely distinct from previous
729 // declarations, and do not make this protocol available for name lookup.
730 // That way, we'll end up completely ignoring the duplicate.
731 // FIXME: Can we turn this into an error?
732 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
733 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000734 /*PrevDecl=*/0);
Douglas Gregor32c17572012-01-01 20:30:41 +0000735 PDecl->startDefinition();
736 } else {
737 if (PrevDecl) {
738 // Check for circular dependencies among protocol declarations. This can
739 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis95dfc122011-11-13 22:08:30 +0000740 ObjCList<ObjCProtocolDecl> PList;
741 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
742 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor32c17572012-01-01 20:30:41 +0000743 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis95dfc122011-11-13 22:08:30 +0000744 }
Douglas Gregor32c17572012-01-01 20:30:41 +0000745
746 // Create the new declaration.
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +0000747 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidis1f4bee52011-10-17 19:48:06 +0000748 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000749 /*PrevDecl=*/PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000750
Douglas Gregorde9f17e2009-04-23 23:18:26 +0000751 PushOnScopeChains(PDecl, TUScope);
Douglas Gregore6e48b12012-01-01 19:29:29 +0000752 PDecl->startDefinition();
Chris Lattnerf87ca0a2008-03-16 01:23:04 +0000753 }
Douglas Gregore6e48b12012-01-01 19:29:29 +0000754
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000755 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000756 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor32c17572012-01-01 20:30:41 +0000757
758 // Merge attributes from previous declarations.
759 if (PrevDecl)
760 mergeDeclAttributes(PDecl, PrevDecl);
761
Fariborz Jahaniancadf7c52011-05-12 22:04:39 +0000762 if (!err && NumProtoRefs ) {
Chris Lattneracc04a92008-03-16 20:19:15 +0000763 /// Check then save referenced protocols.
Roman Divackye6377112012-09-06 15:59:27 +0000764 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000765 ProtoLocs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000766 }
Mike Stump11289f42009-09-09 15:08:12 +0000767
768 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000769 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000770}
771
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000772static bool NestedProtocolHasNoDefinition(ObjCProtocolDecl *PDecl,
773 ObjCProtocolDecl *&UndefinedProtocol) {
774 if (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()) {
775 UndefinedProtocol = PDecl;
776 return true;
777 }
778
Aaron Ballman0f6e64d2014-03-13 22:58:06 +0000779 for (auto *PI : PDecl->protocols())
780 if (NestedProtocolHasNoDefinition(PI, UndefinedProtocol)) {
781 UndefinedProtocol = PI;
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000782 return true;
783 }
784 return false;
785}
786
Chris Lattnerda463fe2007-12-12 07:09:47 +0000787/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +0000788/// issues an error if they are not declared. It returns list of
789/// protocol declarations in its 'Protocols' argument.
Chris Lattnerda463fe2007-12-12 07:09:47 +0000790void
Chris Lattner3bbae002008-07-26 04:03:38 +0000791Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000792 const IdentifierLocPair *ProtocolId,
Chris Lattnerda463fe2007-12-12 07:09:47 +0000793 unsigned NumProtocols,
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000794 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattnerda463fe2007-12-12 07:09:47 +0000795 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000796 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
797 ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000798 if (!PDecl) {
Kaelyn Uhraine31b8882012-01-13 01:32:50 +0000799 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregorc2fa1692011-06-28 16:20:02 +0000800 TypoCorrection Corrected = CorrectTypo(
801 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain4e8942c2012-01-31 23:49:25 +0000802 LookupObjCProtocolName, TUScope, NULL, Validator);
Richard Smithf9b15102013-08-17 00:46:16 +0000803 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>()))
804 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest)
805 << ProtocolId[i].first);
Douglas Gregor35b0bac2010-01-03 18:01:57 +0000806 }
807
808 if (!PDecl) {
Chris Lattner3b054132008-11-19 05:08:23 +0000809 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000810 << ProtocolId[i].first;
Chris Lattner9c1842b2008-07-26 03:47:43 +0000811 continue;
812 }
Fariborz Jahanianada44a22013-04-09 17:52:29 +0000813 // If this is a forward protocol declaration, get its definition.
814 if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
815 PDecl = PDecl->getDefinition();
816
Douglas Gregor171c45a2009-02-18 21:56:37 +0000817 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattner9c1842b2008-07-26 03:47:43 +0000818
819 // If this is a forward declaration and we are supposed to warn in this
820 // case, do it.
Douglas Gregoreed49792013-01-17 00:38:46 +0000821 // FIXME: Recover nicely in the hidden case.
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000822 ObjCProtocolDecl *UndefinedProtocol;
823
Douglas Gregoreed49792013-01-17 00:38:46 +0000824 if (WarnOnDeclarations &&
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000825 NestedProtocolHasNoDefinition(PDecl, UndefinedProtocol)) {
Chris Lattner3b054132008-11-19 05:08:23 +0000826 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000827 << ProtocolId[i].first;
Fariborz Jahanianbf678e82014-03-11 17:10:51 +0000828 Diag(UndefinedProtocol->getLocation(), diag::note_protocol_decl_undefined)
829 << UndefinedProtocol;
830 }
John McCall48871652010-08-21 09:40:31 +0000831 Protocols.push_back(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000832 }
833}
834
Fariborz Jahanianabf63e7b2009-03-02 19:06:08 +0000835/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000836/// a class method in its extension.
837///
Mike Stump11289f42009-09-09 15:08:12 +0000838void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000839 ObjCInterfaceDecl *ID) {
840 if (!ID)
841 return; // Possibly due to previous error
842
843 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Aaron Ballmanaff18c02014-03-13 19:03:34 +0000844 for (auto *MD : ID->methods())
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000845 MethodMap[MD->getSelector()] = MD;
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000846
847 if (MethodMap.empty())
848 return;
Aaron Ballmanaff18c02014-03-13 19:03:34 +0000849 for (const auto *Method : CAT->methods()) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000850 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
Fariborz Jahanian83d674e2014-03-17 17:46:10 +0000851 if (PrevMethod &&
852 (PrevMethod->isInstanceMethod() == Method->isInstanceMethod()) &&
853 !MatchTwoMethodDeclarations(Method, PrevMethod)) {
Fariborz Jahanian33afd772009-03-02 19:05:07 +0000854 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
855 << Method->getDeclName();
856 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
857 }
858 }
859}
860
James Dennett634962f2012-06-14 21:40:34 +0000861/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorf6102672012-01-01 21:23:57 +0000862Sema::DeclGroupPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +0000863Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000864 const IdentifierLocPair *IdentList,
Fariborz Jahanian1470e932008-12-17 01:07:27 +0000865 unsigned NumElts,
866 AttributeList *attrList) {
Douglas Gregorf6102672012-01-01 21:23:57 +0000867 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattnerda463fe2007-12-12 07:09:47 +0000868 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattnerd7352d62008-07-21 22:17:28 +0000869 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor32c17572012-01-01 20:30:41 +0000870 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
871 ForRedeclaration);
872 ObjCProtocolDecl *PDecl
873 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
874 IdentList[i].second, AtProtocolLoc,
Douglas Gregor05a1f4d2012-01-01 22:06:18 +0000875 PrevDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000876
877 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorf6102672012-01-01 21:23:57 +0000878 CheckObjCDeclScope(PDecl);
Douglas Gregor32c17572012-01-01 20:30:41 +0000879
Douglas Gregor42ff1bb2012-01-01 20:33:24 +0000880 if (attrList)
Douglas Gregor758a8692009-06-17 21:51:59 +0000881 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor32c17572012-01-01 20:30:41 +0000882
883 if (PrevDecl)
884 mergeDeclAttributes(PDecl, PrevDecl);
885
Douglas Gregorf6102672012-01-01 21:23:57 +0000886 DeclsInGroup.push_back(PDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000887 }
Mike Stump11289f42009-09-09 15:08:12 +0000888
Rafael Espindolaab417692013-07-09 12:05:01 +0000889 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000890}
891
John McCall48871652010-08-21 09:40:31 +0000892Decl *Sema::
Chris Lattnerd7352d62008-07-21 22:17:28 +0000893ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
894 IdentifierInfo *ClassName, SourceLocation ClassLoc,
895 IdentifierInfo *CategoryName,
896 SourceLocation CategoryLoc,
John McCall48871652010-08-21 09:40:31 +0000897 Decl * const *ProtoRefs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000898 unsigned NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000899 const SourceLocation *ProtoLocs,
Chris Lattnerd7352d62008-07-21 22:17:28 +0000900 SourceLocation EndProtoLoc) {
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000901 ObjCCategoryDecl *CDecl;
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000902 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek514ff702010-02-23 19:39:46 +0000903
904 /// Check that class of this category is already completely declared.
Douglas Gregor4123a862011-11-14 22:10:01 +0000905
906 if (!IDecl
907 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregor7bfb2d02012-05-04 16:32:21 +0000908 diag::err_category_forward_interface,
909 CategoryName == 0)) {
Ted Kremenek514ff702010-02-23 19:39:46 +0000910 // Create an invalid ObjCCategoryDecl to serve as context for
911 // the enclosing method declarations. We mark the decl invalid
912 // to make it clear that this isn't a valid AST.
913 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000914 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek514ff702010-02-23 19:39:46 +0000915 CDecl->setInvalidDecl();
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +0000916 CurContext->addDecl(CDecl);
Douglas Gregor4123a862011-11-14 22:10:01 +0000917
918 if (!IDecl)
919 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000920 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek514ff702010-02-23 19:39:46 +0000921 }
922
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000923 if (!CategoryName && IDecl->getImplementation()) {
924 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
925 Diag(IDecl->getImplementation()->getLocation(),
926 diag::note_implementation_declared);
Ted Kremenek514ff702010-02-23 19:39:46 +0000927 }
928
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000929 if (CategoryName) {
930 /// Check for duplicate interface declaration for this category
Douglas Gregor048fbfa2013-01-16 23:00:23 +0000931 if (ObjCCategoryDecl *Previous
932 = IDecl->FindCategoryDeclaration(CategoryName)) {
933 // Class extensions can be declared multiple times, categories cannot.
934 Diag(CategoryLoc, diag::warn_dup_category_def)
935 << ClassName << CategoryName;
936 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner9018ca82009-02-16 21:26:43 +0000937 }
938 }
Chris Lattner9018ca82009-02-16 21:26:43 +0000939
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000940 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
941 ClassLoc, CategoryLoc, CategoryName, IDecl);
942 // FIXME: PushOnScopeChains?
943 CurContext->addDecl(CDecl);
944
Chris Lattnerda463fe2007-12-12 07:09:47 +0000945 if (NumProtoRefs) {
Roman Divackye6377112012-09-06 15:59:27 +0000946 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor002b6712010-01-16 15:02:53 +0000947 ProtoLocs, Context);
Fariborz Jahanian092cd6e2009-10-05 20:41:32 +0000948 // Protocols in the class extension belong to the class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +0000949 if (CDecl->IsClassExtension())
Roman Divackye6377112012-09-06 15:59:27 +0000950 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek0ef508d2010-09-01 01:21:15 +0000951 NumProtoRefs, Context);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000952 }
Mike Stump11289f42009-09-09 15:08:12 +0000953
Anders Carlssona6b508a2008-11-04 16:57:32 +0000954 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +0000955 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000956}
957
958/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremenek1b0ea822008-01-07 19:49:32 +0000959/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattnerda463fe2007-12-12 07:09:47 +0000960/// object.
John McCall48871652010-08-21 09:40:31 +0000961Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +0000962 SourceLocation AtCatImplLoc,
963 IdentifierInfo *ClassName, SourceLocation ClassLoc,
964 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorb2ccf012010-04-15 22:33:43 +0000965 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000966 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis4af2cb32012-03-02 19:14:29 +0000967 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000968 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
969 if (!CatIDecl) {
970 // Category @implementation with no corresponding @interface.
971 // Create and install one.
Argyrios Kyrtzidis41fc05c2011-11-23 20:27:26 +0000972 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
973 ClassLoc, CatLoc,
Argyrios Kyrtzidis3a5094b2011-08-30 19:43:26 +0000974 CatName, IDecl);
Argyrios Kyrtzidis41fc05c2011-11-23 20:27:26 +0000975 CatIDecl->setImplicit();
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000976 }
977 }
978
Mike Stump11289f42009-09-09 15:08:12 +0000979 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +0000980 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidis4996f5f2011-12-09 00:31:40 +0000981 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattnerda463fe2007-12-12 07:09:47 +0000982 /// Check that class of this category is already completely declared.
Douglas Gregor4123a862011-11-14 22:10:01 +0000983 if (!IDecl) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +0000984 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCalld2930c22011-07-22 02:45:48 +0000985 CDecl->setInvalidDecl();
Douglas Gregor4123a862011-11-14 22:10:01 +0000986 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
987 diag::err_undef_interface)) {
988 CDecl->setInvalidDecl();
John McCalld2930c22011-07-22 02:45:48 +0000989 }
Chris Lattnerda463fe2007-12-12 07:09:47 +0000990
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000991 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000992 CurContext->addDecl(CDecl);
Douglas Gregorc25d7a72009-01-09 00:49:46 +0000993
Argyrios Kyrtzidisc281c962011-10-06 23:23:27 +0000994 // If the interface is deprecated/unavailable, warn/error about it.
995 if (IDecl)
996 DiagnoseUseOfDecl(IDecl, ClassLoc);
997
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +0000998 /// Check that CatName, category name, is not used in another implementation.
999 if (CatIDecl) {
1000 if (CatIDecl->getImplementation()) {
1001 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
1002 << CatName;
1003 Diag(CatIDecl->getImplementation()->getLocation(),
1004 diag::note_previous_definition);
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +00001005 CDecl->setInvalidDecl();
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +00001006 } else {
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001007 CatIDecl->setImplementation(CDecl);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +00001008 // Warn on implementating category of deprecated class under
1009 // -Wdeprecated-implementations flag.
Fariborz Jahaniand724a102011-02-15 17:49:58 +00001010 DiagnoseObjCImplementedDeprecations(*this,
1011 dyn_cast<NamedDecl>(IDecl),
1012 CDecl->getLocation(), 2);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +00001013 }
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001014 }
Mike Stump11289f42009-09-09 15:08:12 +00001015
Anders Carlssona6b508a2008-11-04 16:57:32 +00001016 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001017 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001018}
1019
John McCall48871652010-08-21 09:40:31 +00001020Decl *Sema::ActOnStartClassImplementation(
Chris Lattnerda463fe2007-12-12 07:09:47 +00001021 SourceLocation AtClassImplLoc,
1022 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump11289f42009-09-09 15:08:12 +00001023 IdentifierInfo *SuperClassname,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001024 SourceLocation SuperClassLoc) {
Richard Smithf9b15102013-08-17 00:46:16 +00001025 ObjCInterfaceDecl *IDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001026 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00001027 NamedDecl *PrevDecl
Douglas Gregorb8eaf292010-04-15 23:40:53 +00001028 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
1029 ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001030 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001031 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner0369c572008-11-23 23:12:31 +00001032 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregor1c283312010-08-11 12:19:30 +00001033 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001034 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
1035 diag::warn_undef_interface);
Douglas Gregor40f7a002010-01-04 17:27:12 +00001036 } else {
Richard Smithf9b15102013-08-17 00:46:16 +00001037 // We did not find anything with the name ClassName; try to correct for
Douglas Gregor40f7a002010-01-04 17:27:12 +00001038 // typos in the class name.
Kaelyn Uhraine31b8882012-01-13 01:32:50 +00001039 ObjCInterfaceValidatorCCC Validator;
Richard Smithf9b15102013-08-17 00:46:16 +00001040 TypoCorrection Corrected =
1041 CorrectTypo(DeclarationNameInfo(ClassName, ClassLoc),
1042 LookupOrdinaryName, TUScope, NULL, Validator);
1043 if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) {
1044 // Suggest the (potentially) correct interface name. Don't provide a
1045 // code-modification hint or use the typo name for recovery, because
1046 // this is just a warning. The program may actually be correct.
1047 diagnoseTypo(Corrected,
1048 PDiag(diag::warn_undef_interface_suggest) << ClassName,
1049 /*ErrorRecovery*/false);
Douglas Gregor40f7a002010-01-04 17:27:12 +00001050 } else {
1051 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
1052 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001053 }
Mike Stump11289f42009-09-09 15:08:12 +00001054
Chris Lattnerda463fe2007-12-12 07:09:47 +00001055 // Check that super class name is valid class name
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001056 ObjCInterfaceDecl* SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001057 if (SuperClassname) {
1058 // Check if a different kind of symbol declared in this scope.
Douglas Gregorb2ccf012010-04-15 22:33:43 +00001059 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
1060 LookupOrdinaryName);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001061 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001062 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1063 << SuperClassname;
Chris Lattner0369c572008-11-23 23:12:31 +00001064 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001065 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001066 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidis3b60cff2012-03-13 01:09:36 +00001067 if (SDecl && !SDecl->hasDefinition())
1068 SDecl = 0;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001069 if (!SDecl)
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001070 Diag(SuperClassLoc, diag::err_undef_superclass)
1071 << SuperClassname << ClassName;
Douglas Gregor0b144e12011-12-15 00:29:59 +00001072 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00001073 // This implementation and its interface do not have the same
1074 // super class.
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001075 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001076 << SDecl->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00001077 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001078 }
1079 }
1080 }
Mike Stump11289f42009-09-09 15:08:12 +00001081
Chris Lattnerda463fe2007-12-12 07:09:47 +00001082 if (!IDecl) {
1083 // Legacy case of @implementation with no corresponding @interface.
1084 // Build, chain & install the interface decl into the identifier.
Daniel Dunbar73a73f52008-08-20 18:02:42 +00001085
Mike Stump87c57ac2009-05-16 07:39:55 +00001086 // FIXME: Do we support attributes on the @implementation? If so we should
1087 // copy them over.
Mike Stump11289f42009-09-09 15:08:12 +00001088 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00001089 ClassName, /*PrevDecl=*/0, ClassLoc,
1090 true);
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001091 IDecl->startDefinition();
Douglas Gregor16408322011-12-15 22:34:59 +00001092 if (SDecl) {
1093 IDecl->setSuperClass(SDecl);
1094 IDecl->setSuperClassLoc(SuperClassLoc);
1095 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1096 } else {
1097 IDecl->setEndOfDefinitionLoc(ClassLoc);
1098 }
1099
Douglas Gregorac345a32009-04-24 00:16:12 +00001100 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor1c283312010-08-11 12:19:30 +00001101 } else {
1102 // Mark the interface as being completed, even if it was just as
1103 // @class ....;
1104 // declaration; the user cannot reopen it.
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00001105 if (!IDecl->hasDefinition())
1106 IDecl->startDefinition();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001107 }
Mike Stump11289f42009-09-09 15:08:12 +00001108
1109 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis52f53fb2011-10-04 04:48:02 +00001110 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
Argyrios Kyrtzidisfac31622013-05-03 18:05:44 +00001111 ClassLoc, AtClassImplLoc, SuperClassLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001112
Anders Carlssona6b508a2008-11-04 16:57:32 +00001113 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001114 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001115
Chris Lattnerda463fe2007-12-12 07:09:47 +00001116 // Check that there is no duplicate implementation of this class.
Douglas Gregor1c283312010-08-11 12:19:30 +00001117 if (IDecl->getImplementation()) {
1118 // FIXME: Don't leak everything!
Chris Lattner4bd8dd82008-11-19 08:23:25 +00001119 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis43cee9352009-07-21 00:06:04 +00001120 Diag(IDecl->getImplementation()->getLocation(),
1121 diag::note_previous_definition);
Argyrios Kyrtzidis0f6d5ca2013-05-30 18:53:21 +00001122 IMPDecl->setInvalidDecl();
Douglas Gregor1c283312010-08-11 12:19:30 +00001123 } else { // add it to the list.
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001124 IDecl->setImplementation(IMPDecl);
Douglas Gregor79947a22009-04-24 00:11:27 +00001125 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahaniand33ab8c2011-02-15 00:59:30 +00001126 // Warn on implementating deprecated class under
1127 // -Wdeprecated-implementations flag.
Fariborz Jahaniand724a102011-02-15 17:49:58 +00001128 DiagnoseObjCImplementedDeprecations(*this,
1129 dyn_cast<NamedDecl>(IDecl),
1130 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis6d9fab72009-07-21 00:05:53 +00001131 }
Argyrios Kyrtzidis9321ad32011-10-06 23:23:20 +00001132 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001133}
1134
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001135Sema::DeclGroupPtrTy
1136Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1137 SmallVector<Decl *, 64> DeclsInGroup;
1138 DeclsInGroup.reserve(Decls.size() + 1);
1139
1140 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1141 Decl *Dcl = Decls[i];
1142 if (!Dcl)
1143 continue;
1144 if (Dcl->getDeclContext()->isFileContext())
1145 Dcl->setTopLevelDeclInObjCContainer();
1146 DeclsInGroup.push_back(Dcl);
1147 }
1148
1149 DeclsInGroup.push_back(ObjCImpDecl);
1150
Rafael Espindolaab417692013-07-09 12:05:01 +00001151 return BuildDeclaratorGroup(DeclsInGroup, false);
Argyrios Kyrtzidis2e85c5f2012-02-23 21:11:20 +00001152}
1153
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001154void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1155 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattnerda463fe2007-12-12 07:09:47 +00001156 SourceLocation RBrace) {
1157 assert(ImpDecl && "missing implementation decl");
Douglas Gregor6e6ad602009-01-20 01:17:11 +00001158 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattnerda463fe2007-12-12 07:09:47 +00001159 if (!IDecl)
1160 return;
James Dennett634962f2012-06-14 21:40:34 +00001161 /// Check case of non-existing \@interface decl.
1162 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattnerda463fe2007-12-12 07:09:47 +00001163 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroffaac654a2009-04-20 20:09:33 +00001164 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor16408322011-12-15 22:34:59 +00001165 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian20912d62010-02-17 17:00:07 +00001166 // Add ivar's to class's DeclContext.
1167 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanianc0309cd2010-02-17 18:10:54 +00001168 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith05afe5e2012-03-13 03:12:56 +00001169 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanianaef66222010-02-19 00:31:17 +00001170 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian20912d62010-02-17 17:00:07 +00001171 }
1172
Chris Lattnerda463fe2007-12-12 07:09:47 +00001173 return;
1174 }
1175 // If implementation has empty ivar list, just return.
1176 if (numIvars == 0)
1177 return;
Mike Stump11289f42009-09-09 15:08:12 +00001178
Chris Lattnerda463fe2007-12-12 07:09:47 +00001179 assert(ivars && "missing @implementation ivars");
John McCall5fb5df92012-06-20 06:18:46 +00001180 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001181 if (ImpDecl->getSuperClass())
1182 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1183 for (unsigned i = 0; i < numIvars; i++) {
1184 ObjCIvarDecl* ImplIvar = ivars[i];
1185 if (const ObjCIvarDecl *ClsIvar =
1186 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1187 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1188 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1189 continue;
1190 }
Fariborz Jahaniane23f26b2013-06-26 22:10:27 +00001191 // Check class extensions (unnamed categories) for duplicate ivars.
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001192 for (const auto *CDecl : IDecl->visible_extensions()) {
Fariborz Jahaniane23f26b2013-06-26 22:10:27 +00001193 if (const ObjCIvarDecl *ClsExtIvar =
1194 CDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1195 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1196 Diag(ClsExtIvar->getLocation(), diag::note_previous_definition);
1197 continue;
1198 }
1199 }
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001200 // Instance ivar to Implementation's DeclContext.
1201 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith05afe5e2012-03-13 03:12:56 +00001202 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanian34e3cef2010-02-19 20:58:54 +00001203 ImpDecl->addDecl(ImplIvar);
1204 }
1205 return;
1206 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001207 // Check interface's Ivar list against those in the implementation.
1208 // names and types must match.
1209 //
Chris Lattnerda463fe2007-12-12 07:09:47 +00001210 unsigned j = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001211 ObjCInterfaceDecl::ivar_iterator
Chris Lattner061227a2007-12-12 17:58:05 +00001212 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1213 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00001214 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie40ed2972012-06-06 20:45:41 +00001215 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001216 assert (ImplIvar && "missing implementation ivar");
1217 assert (ClsIvar && "missing class ivar");
Mike Stump11289f42009-09-09 15:08:12 +00001218
Steve Naroff157599f2009-03-03 14:49:36 +00001219 // First, make sure the types match.
Richard Smithcaf33902011-10-10 18:28:20 +00001220 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattner3b054132008-11-19 05:08:23 +00001221 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001222 << ImplIvar->getIdentifier()
1223 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner0369c572008-11-23 23:12:31 +00001224 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smithcaf33902011-10-10 18:28:20 +00001225 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1226 ImplIvar->getBitWidthValue(Context) !=
1227 ClsIvar->getBitWidthValue(Context)) {
1228 Diag(ImplIvar->getBitWidth()->getLocStart(),
1229 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1230 Diag(ClsIvar->getBitWidth()->getLocStart(),
1231 diag::note_previous_definition);
Mike Stump11289f42009-09-09 15:08:12 +00001232 }
Steve Naroff157599f2009-03-03 14:49:36 +00001233 // Make sure the names are identical.
1234 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattner3b054132008-11-19 05:08:23 +00001235 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattnere3d20d92008-11-23 21:45:46 +00001236 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner0369c572008-11-23 23:12:31 +00001237 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001238 }
1239 --numIvars;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001240 }
Mike Stump11289f42009-09-09 15:08:12 +00001241
Chris Lattner0f29d982007-12-12 18:11:49 +00001242 if (numIvars > 0)
Alp Tokerfff06742013-12-02 03:50:21 +00001243 Diag(ivars[j]->getLocation(), diag::err_inconsistent_ivar_count);
Chris Lattner0f29d982007-12-12 18:11:49 +00001244 else if (IVI != IVE)
Alp Tokerfff06742013-12-02 03:50:21 +00001245 Diag(IVI->getLocation(), diag::err_inconsistent_ivar_count);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001246}
1247
Ted Kremenekf87decd2013-12-13 05:58:44 +00001248static void WarnUndefinedMethod(Sema &S, SourceLocation ImpLoc,
1249 ObjCMethodDecl *method,
1250 bool &IncompleteImpl,
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001251 unsigned DiagID,
1252 NamedDecl *NeededFor = 0) {
Fariborz Jahanian9fc39c42011-06-24 20:31:37 +00001253 // No point warning no definition of method which is 'unavailable'.
Douglas Gregorc2e3d5c2012-12-11 18:53:07 +00001254 switch (method->getAvailability()) {
1255 case AR_Available:
1256 case AR_Deprecated:
1257 break;
1258
1259 // Don't warn about unavailable or not-yet-introduced methods.
1260 case AR_NotYetIntroduced:
1261 case AR_Unavailable:
Fariborz Jahanian9fc39c42011-06-24 20:31:37 +00001262 return;
Douglas Gregorc2e3d5c2012-12-11 18:53:07 +00001263 }
1264
Ted Kremenek65d63572013-03-27 00:02:21 +00001265 // FIXME: For now ignore 'IncompleteImpl'.
1266 // Previously we grouped all unimplemented methods under a single
1267 // warning, but some users strongly voiced that they would prefer
1268 // separate warnings. We will give that approach a try, as that
1269 // matches what we do with protocols.
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001270 {
1271 const Sema::SemaDiagnosticBuilder &B = S.Diag(ImpLoc, DiagID);
1272 B << method;
1273 if (NeededFor)
1274 B << NeededFor;
1275 }
Ted Kremenek65d63572013-03-27 00:02:21 +00001276
1277 // Issue a note to the original declaration.
1278 SourceLocation MethodLoc = method->getLocStart();
1279 if (MethodLoc.isValid())
Ted Kremenekf87decd2013-12-13 05:58:44 +00001280 S.Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff15833ed2008-02-10 21:38:56 +00001281}
1282
David Chisnallb62d15c2010-10-25 17:23:52 +00001283/// Determines if type B can be substituted for type A. Returns true if we can
1284/// guarantee that anything that the user will do to an object of type A can
1285/// also be done to an object of type B. This is trivially true if the two
1286/// types are the same, or if B is a subclass of A. It becomes more complex
1287/// in cases where protocols are involved.
1288///
1289/// Object types in Objective-C describe the minimum requirements for an
1290/// object, rather than providing a complete description of a type. For
1291/// example, if A is a subclass of B, then B* may refer to an instance of A.
1292/// The principle of substitutability means that we may use an instance of A
1293/// anywhere that we may use an instance of B - it will implement all of the
1294/// ivars of B and all of the methods of B.
1295///
1296/// This substitutability is important when type checking methods, because
1297/// the implementation may have stricter type definitions than the interface.
1298/// The interface specifies minimum requirements, but the implementation may
1299/// have more accurate ones. For example, a method may privately accept
1300/// instances of B, but only publish that it accepts instances of A. Any
1301/// object passed to it will be type checked against B, and so will implicitly
1302/// by a valid A*. Similarly, a method may return a subclass of the class that
1303/// it is declared as returning.
1304///
1305/// This is most important when considering subclassing. A method in a
1306/// subclass must accept any object as an argument that its superclass's
1307/// implementation accepts. It may, however, accept a more general type
1308/// without breaking substitutability (i.e. you can still use the subclass
1309/// anywhere that you can use the superclass, but not vice versa). The
1310/// converse requirement applies to return types: the return type for a
1311/// subclass method must be a valid object of the kind that the superclass
1312/// advertises, but it may be specified more accurately. This avoids the need
1313/// for explicit down-casting by callers.
1314///
1315/// Note: This is a stricter requirement than for assignment.
John McCall071df462010-10-28 02:34:38 +00001316static bool isObjCTypeSubstitutable(ASTContext &Context,
1317 const ObjCObjectPointerType *A,
1318 const ObjCObjectPointerType *B,
1319 bool rejectId) {
1320 // Reject a protocol-unqualified id.
1321 if (rejectId && B->isObjCIdType()) return false;
David Chisnallb62d15c2010-10-25 17:23:52 +00001322
1323 // If B is a qualified id, then A must also be a qualified id and it must
1324 // implement all of the protocols in B. It may not be a qualified class.
1325 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1326 // stricter definition so it is not substitutable for id<A>.
1327 if (B->isObjCQualifiedIdType()) {
1328 return A->isObjCQualifiedIdType() &&
John McCall071df462010-10-28 02:34:38 +00001329 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1330 QualType(B,0),
1331 false);
David Chisnallb62d15c2010-10-25 17:23:52 +00001332 }
1333
1334 /*
1335 // id is a special type that bypasses type checking completely. We want a
1336 // warning when it is used in one place but not another.
1337 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1338
1339
1340 // If B is a qualified id, then A must also be a qualified id (which it isn't
1341 // if we've got this far)
1342 if (B->isObjCQualifiedIdType()) return false;
1343 */
1344
1345 // Now we know that A and B are (potentially-qualified) class types. The
1346 // normal rules for assignment apply.
John McCall071df462010-10-28 02:34:38 +00001347 return Context.canAssignObjCInterfaces(A, B);
David Chisnallb62d15c2010-10-25 17:23:52 +00001348}
1349
John McCall071df462010-10-28 02:34:38 +00001350static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1351 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1352}
1353
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001354static bool CheckMethodOverrideReturn(Sema &S,
John McCall071df462010-10-28 02:34:38 +00001355 ObjCMethodDecl *MethodImpl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001356 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001357 bool IsProtocolMethodDecl,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001358 bool IsOverridingMode,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001359 bool Warn) {
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001360 if (IsProtocolMethodDecl &&
1361 (MethodDecl->getObjCDeclQualifier() !=
1362 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001363 if (Warn) {
Alp Toker314cc812014-01-25 16:55:45 +00001364 S.Diag(MethodImpl->getLocation(),
1365 (IsOverridingMode
1366 ? diag::warn_conflicting_overriding_ret_type_modifiers
1367 : diag::warn_conflicting_ret_type_modifiers))
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001368 << MethodImpl->getDeclName()
Alp Toker314cc812014-01-25 16:55:45 +00001369 << getTypeRange(MethodImpl->getReturnTypeSourceInfo());
1370 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1371 << getTypeRange(MethodDecl->getReturnTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001372 }
1373 else
1374 return false;
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001375 }
Alp Toker314cc812014-01-25 16:55:45 +00001376
1377 if (S.Context.hasSameUnqualifiedType(MethodImpl->getReturnType(),
1378 MethodDecl->getReturnType()))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001379 return true;
1380 if (!Warn)
1381 return false;
John McCall071df462010-10-28 02:34:38 +00001382
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001383 unsigned DiagID =
1384 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1385 : diag::warn_conflicting_ret_types;
John McCall071df462010-10-28 02:34:38 +00001386
1387 // Mismatches between ObjC pointers go into a different warning
1388 // category, and sometimes they're even completely whitelisted.
1389 if (const ObjCObjectPointerType *ImplPtrTy =
Alp Toker314cc812014-01-25 16:55:45 +00001390 MethodImpl->getReturnType()->getAs<ObjCObjectPointerType>()) {
John McCall071df462010-10-28 02:34:38 +00001391 if (const ObjCObjectPointerType *IfacePtrTy =
Alp Toker314cc812014-01-25 16:55:45 +00001392 MethodDecl->getReturnType()->getAs<ObjCObjectPointerType>()) {
John McCall071df462010-10-28 02:34:38 +00001393 // Allow non-matching return types as long as they don't violate
1394 // the principle of substitutability. Specifically, we permit
1395 // return types that are subclasses of the declared return type,
1396 // or that are more-qualified versions of the declared type.
1397 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001398 return false;
John McCall071df462010-10-28 02:34:38 +00001399
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001400 DiagID =
1401 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1402 : diag::warn_non_covariant_ret_types;
John McCall071df462010-10-28 02:34:38 +00001403 }
1404 }
1405
1406 S.Diag(MethodImpl->getLocation(), DiagID)
Alp Toker314cc812014-01-25 16:55:45 +00001407 << MethodImpl->getDeclName() << MethodDecl->getReturnType()
1408 << MethodImpl->getReturnType()
1409 << getTypeRange(MethodImpl->getReturnTypeSourceInfo());
1410 S.Diag(MethodDecl->getLocation(), IsOverridingMode
1411 ? diag::note_previous_declaration
1412 : diag::note_previous_definition)
1413 << getTypeRange(MethodDecl->getReturnTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001414 return false;
John McCall071df462010-10-28 02:34:38 +00001415}
1416
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001417static bool CheckMethodOverrideParam(Sema &S,
John McCall071df462010-10-28 02:34:38 +00001418 ObjCMethodDecl *MethodImpl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001419 ObjCMethodDecl *MethodDecl,
John McCall071df462010-10-28 02:34:38 +00001420 ParmVarDecl *ImplVar,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001421 ParmVarDecl *IfaceVar,
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001422 bool IsProtocolMethodDecl,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001423 bool IsOverridingMode,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001424 bool Warn) {
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001425 if (IsProtocolMethodDecl &&
1426 (ImplVar->getObjCDeclQualifier() !=
1427 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001428 if (Warn) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001429 if (IsOverridingMode)
1430 S.Diag(ImplVar->getLocation(),
1431 diag::warn_conflicting_overriding_param_modifiers)
1432 << getTypeRange(ImplVar->getTypeSourceInfo())
1433 << MethodImpl->getDeclName();
1434 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001435 diag::warn_conflicting_param_modifiers)
1436 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001437 << MethodImpl->getDeclName();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001438 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1439 << getTypeRange(IfaceVar->getTypeSourceInfo());
1440 }
1441 else
1442 return false;
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001443 }
1444
John McCall071df462010-10-28 02:34:38 +00001445 QualType ImplTy = ImplVar->getType();
1446 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001447
John McCall071df462010-10-28 02:34:38 +00001448 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001449 return true;
1450
1451 if (!Warn)
1452 return false;
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001453 unsigned DiagID =
1454 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1455 : diag::warn_conflicting_param_types;
John McCall071df462010-10-28 02:34:38 +00001456
1457 // Mismatches between ObjC pointers go into a different warning
1458 // category, and sometimes they're even completely whitelisted.
1459 if (const ObjCObjectPointerType *ImplPtrTy =
1460 ImplTy->getAs<ObjCObjectPointerType>()) {
1461 if (const ObjCObjectPointerType *IfacePtrTy =
1462 IfaceTy->getAs<ObjCObjectPointerType>()) {
1463 // Allow non-matching argument types as long as they don't
1464 // violate the principle of substitutability. Specifically, the
1465 // implementation must accept any objects that the superclass
1466 // accepts, however it may also accept others.
1467 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001468 return false;
John McCall071df462010-10-28 02:34:38 +00001469
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001470 DiagID =
1471 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1472 : diag::warn_non_contravariant_param_types;
John McCall071df462010-10-28 02:34:38 +00001473 }
1474 }
1475
1476 S.Diag(ImplVar->getLocation(), DiagID)
1477 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001478 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1479 S.Diag(IfaceVar->getLocation(),
1480 (IsOverridingMode ? diag::note_previous_declaration
1481 : diag::note_previous_definition))
John McCall071df462010-10-28 02:34:38 +00001482 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001483 return false;
John McCall071df462010-10-28 02:34:38 +00001484}
John McCall31168b02011-06-15 23:02:42 +00001485
1486/// In ARC, check whether the conventional meanings of the two methods
1487/// match. If they don't, it's a hard error.
1488static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1489 ObjCMethodDecl *decl) {
1490 ObjCMethodFamily implFamily = impl->getMethodFamily();
1491 ObjCMethodFamily declFamily = decl->getMethodFamily();
1492 if (implFamily == declFamily) return false;
1493
1494 // Since conventions are sorted by selector, the only possibility is
1495 // that the types differ enough to cause one selector or the other
1496 // to fall out of the family.
1497 assert(implFamily == OMF_None || declFamily == OMF_None);
1498
1499 // No further diagnostics required on invalid declarations.
1500 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1501
1502 const ObjCMethodDecl *unmatched = impl;
1503 ObjCMethodFamily family = declFamily;
1504 unsigned errorID = diag::err_arc_lost_method_convention;
1505 unsigned noteID = diag::note_arc_lost_method_convention;
1506 if (declFamily == OMF_None) {
1507 unmatched = decl;
1508 family = implFamily;
1509 errorID = diag::err_arc_gained_method_convention;
1510 noteID = diag::note_arc_gained_method_convention;
1511 }
1512
1513 // Indexes into a %select clause in the diagnostic.
1514 enum FamilySelector {
1515 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1516 };
1517 FamilySelector familySelector = FamilySelector();
1518
1519 switch (family) {
1520 case OMF_None: llvm_unreachable("logic error, no method convention");
1521 case OMF_retain:
1522 case OMF_release:
1523 case OMF_autorelease:
1524 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00001525 case OMF_finalize:
John McCall31168b02011-06-15 23:02:42 +00001526 case OMF_retainCount:
1527 case OMF_self:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00001528 case OMF_performSelector:
John McCall31168b02011-06-15 23:02:42 +00001529 // Mismatches for these methods don't change ownership
1530 // conventions, so we don't care.
1531 return false;
1532
1533 case OMF_init: familySelector = F_init; break;
1534 case OMF_alloc: familySelector = F_alloc; break;
1535 case OMF_copy: familySelector = F_copy; break;
1536 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1537 case OMF_new: familySelector = F_new; break;
1538 }
1539
1540 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1541 ReasonSelector reasonSelector;
1542
1543 // The only reason these methods don't fall within their families is
1544 // due to unusual result types.
Alp Toker314cc812014-01-25 16:55:45 +00001545 if (unmatched->getReturnType()->isObjCObjectPointerType()) {
John McCall31168b02011-06-15 23:02:42 +00001546 reasonSelector = R_UnrelatedReturn;
1547 } else {
1548 reasonSelector = R_NonObjectReturn;
1549 }
1550
Joerg Sonnenbergerffc6d492013-06-26 21:31:47 +00001551 S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
1552 S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
John McCall31168b02011-06-15 23:02:42 +00001553
1554 return true;
1555}
John McCall071df462010-10-28 02:34:38 +00001556
Fariborz Jahanian7988d7d2008-12-05 18:18:52 +00001557void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001558 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001559 bool IsProtocolMethodDecl) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001560 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00001561 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1562 return;
1563
Fariborz Jahaniand7b0cb52011-02-21 23:49:15 +00001564 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001565 IsProtocolMethodDecl, false,
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001566 true);
Mike Stump11289f42009-09-09 15:08:12 +00001567
Chris Lattner67f35b02009-04-11 19:58:42 +00001568 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001569 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1570 EF = MethodDecl->param_end();
1571 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001572 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001573 IsProtocolMethodDecl, false, true);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001574 }
Fariborz Jahanian3c12dd72011-08-10 17:16:30 +00001575
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001576 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001577 Diag(ImpMethodDecl->getLocation(),
1578 diag::warn_conflicting_variadic);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001579 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001580 }
Fariborz Jahanian5ac085a2011-08-08 18:03:17 +00001581}
1582
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001583void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1584 ObjCMethodDecl *Overridden,
1585 bool IsProtocolMethodDecl) {
1586
1587 CheckMethodOverrideReturn(*this, Method, Overridden,
1588 IsProtocolMethodDecl, true,
1589 true);
1590
1591 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001592 IF = Overridden->param_begin(), EM = Method->param_end(),
1593 EF = Overridden->param_end();
1594 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian9a81f842011-10-10 17:53:29 +00001595 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1596 IsProtocolMethodDecl, true, true);
1597 }
1598
1599 if (Method->isVariadic() != Overridden->isVariadic()) {
1600 Diag(Method->getLocation(),
1601 diag::warn_conflicting_overriding_variadic);
1602 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1603 }
1604}
1605
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001606/// WarnExactTypedMethods - This routine issues a warning if method
1607/// implementation declaration matches exactly that of its declaration.
1608void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1609 ObjCMethodDecl *MethodDecl,
1610 bool IsProtocolMethodDecl) {
1611 // don't issue warning when protocol method is optional because primary
1612 // class is not required to implement it and it is safe for protocol
1613 // to implement it.
1614 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1615 return;
1616 // don't issue warning when primary class's method is
1617 // depecated/unavailable.
1618 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1619 MethodDecl->hasAttr<DeprecatedAttr>())
1620 return;
1621
1622 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1623 IsProtocolMethodDecl, false, false);
1624 if (match)
1625 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0bf70f42012-05-17 23:13:29 +00001626 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1627 EF = MethodDecl->param_end();
1628 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001629 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1630 *IM, *IF,
1631 IsProtocolMethodDecl, false, false);
1632 if (!match)
1633 break;
1634 }
1635 if (match)
1636 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall92918512011-08-08 17:32:19 +00001637 if (match)
1638 match = !(MethodDecl->isClassMethod() &&
1639 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001640
1641 if (match) {
1642 Diag(ImpMethodDecl->getLocation(),
1643 diag::warn_category_method_impl_match);
Ted Kremenek59b10db2012-02-27 22:55:11 +00001644 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1645 << MethodDecl->getDeclName();
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001646 }
1647}
1648
Mike Stump87c57ac2009-05-16 07:39:55 +00001649/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1650/// improve the efficiency of selector lookups and type checking by associating
1651/// with each protocol / interface / category the flattened instance tables. If
1652/// we used an immutable set to keep the table then it wouldn't add significant
1653/// memory cost and it would be handy for lookups.
Daniel Dunbar4684f372008-08-27 05:40:03 +00001654
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001655typedef llvm::DenseSet<IdentifierInfo*> ProtocolNameSet;
Ahmed Charlesaf94d562014-03-09 11:34:25 +00001656typedef std::unique_ptr<ProtocolNameSet> LazyProtocolNameSet;
Ted Kremenek760a2ac2014-03-05 23:18:22 +00001657
1658static void findProtocolsWithExplicitImpls(const ObjCProtocolDecl *PDecl,
1659 ProtocolNameSet &PNS) {
1660 if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>())
1661 PNS.insert(PDecl->getIdentifier());
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001662 for (const auto *PI : PDecl->protocols())
1663 findProtocolsWithExplicitImpls(PI, PNS);
Ted Kremenek760a2ac2014-03-05 23:18:22 +00001664}
1665
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001666/// Recursively populates a set with all conformed protocols in a class
1667/// hierarchy that have the 'objc_protocol_requires_explicit_implementation'
1668/// attribute.
1669static void findProtocolsWithExplicitImpls(const ObjCInterfaceDecl *Super,
1670 ProtocolNameSet &PNS) {
1671 if (!Super)
1672 return;
1673
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001674 for (const auto *I : Super->all_referenced_protocols())
1675 findProtocolsWithExplicitImpls(I, PNS);
Ted Kremenek760a2ac2014-03-05 23:18:22 +00001676
1677 findProtocolsWithExplicitImpls(Super->getSuperClass(), PNS);
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001678}
1679
Steve Naroffa36992242008-02-08 22:06:17 +00001680/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattnerda463fe2007-12-12 07:09:47 +00001681/// Declared in protocol, and those referenced by it.
Ted Kremenek285ee852013-12-13 06:26:10 +00001682static void CheckProtocolMethodDefs(Sema &S,
1683 SourceLocation ImpLoc,
1684 ObjCProtocolDecl *PDecl,
1685 bool& IncompleteImpl,
1686 const Sema::SelectorSet &InsMap,
1687 const Sema::SelectorSet &ClsMap,
Ted Kremenek33e430f2013-12-13 06:26:14 +00001688 ObjCContainerDecl *CDecl,
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001689 LazyProtocolNameSet &ProtocolsExplictImpl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001690 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1691 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1692 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanian2e8074b2010-03-27 21:10:05 +00001693 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1694
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001695 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001696 ObjCInterfaceDecl *NSIDecl = 0;
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001697
1698 // If this protocol is marked 'objc_protocol_requires_explicit_implementation'
1699 // then we should check if any class in the super class hierarchy also
1700 // conforms to this protocol, either directly or via protocol inheritance.
1701 // If so, we can skip checking this protocol completely because we
1702 // know that a parent class already satisfies this protocol.
1703 //
1704 // Note: we could generalize this logic for all protocols, and merely
1705 // add the limit on looking at the super class chain for just
1706 // specially marked protocols. This may be a good optimization. This
1707 // change is restricted to 'objc_protocol_requires_explicit_implementation'
1708 // protocols for now for controlled evaluation.
1709 if (PDecl->hasAttr<ObjCExplicitProtocolImplAttr>()) {
Ahmed Charlesaf94d562014-03-09 11:34:25 +00001710 if (!ProtocolsExplictImpl) {
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001711 ProtocolsExplictImpl.reset(new ProtocolNameSet);
1712 findProtocolsWithExplicitImpls(Super, *ProtocolsExplictImpl);
1713 }
1714 if (ProtocolsExplictImpl->find(PDecl->getIdentifier()) !=
1715 ProtocolsExplictImpl->end())
1716 return;
1717
1718 // If no super class conforms to the protocol, we should not search
1719 // for methods in the super class to implicitly satisfy the protocol.
1720 Super = NULL;
1721 }
1722
Ted Kremenek285ee852013-12-13 06:26:10 +00001723 if (S.getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump11289f42009-09-09 15:08:12 +00001724 // check to see if class implements forwardInvocation method and objects
1725 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001726 // from one object to another.
Mike Stump11289f42009-09-09 15:08:12 +00001727 // Under such conditions, which means that every method possible is
1728 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001729 // found" warnings.
1730 // FIXME: Use a general GetUnarySelector method for this.
Ted Kremenek285ee852013-12-13 06:26:10 +00001731 IdentifierInfo* II = &S.Context.Idents.get("forwardInvocation");
1732 Selector fISelector = S.Context.Selectors.getSelector(1, &II);
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001733 if (InsMap.count(fISelector))
1734 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1735 // need be implemented in the implementation.
Ted Kremenek285ee852013-12-13 06:26:10 +00001736 NSIDecl = IDecl->lookupInheritedClass(&S.Context.Idents.get("NSProxy"));
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001737 }
Mike Stump11289f42009-09-09 15:08:12 +00001738
Fariborz Jahanianc41cf052013-01-07 19:21:03 +00001739 // If this is a forward protocol declaration, get its definition.
1740 if (!PDecl->isThisDeclarationADefinition() &&
1741 PDecl->getDefinition())
1742 PDecl = PDecl->getDefinition();
1743
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001744 // If a method lookup fails locally we still need to look and see if
1745 // the method was implemented by a base class or an inherited
1746 // protocol. This lookup is slow, but occurs rarely in correct code
1747 // and otherwise would terminate in a warning.
1748
Chris Lattnerda463fe2007-12-12 07:09:47 +00001749 // check unimplemented instance methods.
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001750 if (!NSIDecl)
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001751 for (auto *method : PDecl->instance_methods()) {
Mike Stump11289f42009-09-09 15:08:12 +00001752 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rosed01e83a2012-10-10 16:42:25 +00001753 !method->isPropertyAccessor() &&
1754 !InsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001755 (!Super || !Super->lookupMethod(method->getSelector(),
1756 true /* instance */,
1757 false /* shallowCategory */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001758 true /* followsSuper */,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001759 NULL /* category */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001760 // If a method is not implemented in the category implementation but
1761 // has been declared in its primary class, superclass,
1762 // or in one of their protocols, no need to issue the warning.
1763 // This is because method will be implemented in the primary class
1764 // or one of its super class implementation.
1765
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001766 // Ugly, but necessary. Method declared in protcol might have
1767 // have been synthesized due to a property declared in the class which
1768 // uses the protocol.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001769 if (ObjCMethodDecl *MethodInClass =
Ted Kremenek00781502013-11-23 01:01:29 +00001770 IDecl->lookupMethod(method->getSelector(),
1771 true /* instance */,
1772 true /* shallowCategoryLookup */,
1773 false /* followSuper */))
Jordan Rosed01e83a2012-10-10 16:42:25 +00001774 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001775 continue;
1776 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Ted Kremenek285ee852013-12-13 06:26:10 +00001777 if (S.Diags.getDiagnosticLevel(DIAG, ImpLoc)
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001778 != DiagnosticsEngine::Ignored) {
Ted Kremenek285ee852013-12-13 06:26:10 +00001779 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG,
Ted Kremenek2ccf19e2013-12-13 05:58:51 +00001780 PDecl);
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001781 }
Fariborz Jahaniandb3a4c12009-05-22 17:12:32 +00001782 }
1783 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00001784 // check unimplemented class methods
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001785 for (auto *method : PDecl->class_methods()) {
Daniel Dunbarc7dfbfd2008-09-04 20:01:15 +00001786 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1787 !ClsMap.count(method->getSelector()) &&
Ted Kremenek00781502013-11-23 01:01:29 +00001788 (!Super || !Super->lookupMethod(method->getSelector(),
1789 false /* class method */,
1790 false /* shallowCategoryLookup */,
Ted Kremenek28eace62013-11-23 01:01:34 +00001791 true /* followSuper */,
Ted Kremenekf41cf7f12013-12-10 19:43:48 +00001792 NULL /* category */))) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001793 // See above comment for instance method lookups.
Ted Kremenek00781502013-11-23 01:01:29 +00001794 if (C && IDecl->lookupMethod(method->getSelector(),
1795 false /* class */,
1796 true /* shallowCategoryLookup */,
1797 false /* followSuper */))
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001798 continue;
Ted Kremenek00781502013-11-23 01:01:29 +00001799
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001800 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Ted Kremenek285ee852013-12-13 06:26:10 +00001801 if (S.Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
David Blaikie9c902b52011-09-25 23:23:43 +00001802 DiagnosticsEngine::Ignored) {
Ted Kremenek285ee852013-12-13 06:26:10 +00001803 WarnUndefinedMethod(S, ImpLoc, method, IncompleteImpl, DIAG, PDecl);
Fariborz Jahanianc1fb8622010-03-31 18:23:33 +00001804 }
Fariborz Jahanian97752f72010-03-27 19:02:17 +00001805 }
Steve Naroff3ce37a62007-12-14 23:37:57 +00001806 }
Chris Lattner390d39a2008-07-21 21:32:27 +00001807 // Check on this protocols's referenced protocols, recursively.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001808 for (auto *PI : PDecl->protocols())
1809 CheckProtocolMethodDefs(S, ImpLoc, PI, IncompleteImpl, InsMap, ClsMap,
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001810 CDecl, ProtocolsExplictImpl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00001811}
1812
Fariborz Jahanianf9ae68a2011-07-16 00:08:33 +00001813/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001814/// or protocol against those declared in their implementations.
1815///
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001816void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1817 const SelectorSet &ClsMap,
1818 SelectorSet &InsMapSeen,
1819 SelectorSet &ClsMapSeen,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001820 ObjCImplDecl* IMPDecl,
1821 ObjCContainerDecl* CDecl,
1822 bool &IncompleteImpl,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001823 bool ImmediateClass,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001824 bool WarnCategoryMethodImpl) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001825 // Check and see if instance methods in class interface have been
1826 // implemented in the implementation class. If so, their types match.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001827 for (auto *I : CDecl->instance_methods()) {
1828 if (!InsMapSeen.insert(I->getSelector()))
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001829 continue;
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001830 if (!I->isPropertyAccessor() &&
1831 !InsMap.count(I->getSelector())) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001832 if (ImmediateClass)
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001833 WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001834 diag::warn_undef_method_impl);
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001835 continue;
Mike Stump12b8ce12009-08-04 21:02:39 +00001836 } else {
Mike Stump11289f42009-09-09 15:08:12 +00001837 ObjCMethodDecl *ImpMethodDecl =
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001838 IMPDecl->getInstanceMethod(I->getSelector());
1839 assert(CDecl->getInstanceMethod(I->getSelector()) &&
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001840 "Expected to find the method through lookup as well");
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001841 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001842 if (ImpMethodDecl) {
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001843 if (!WarnCategoryMethodImpl)
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001844 WarnConflictingTypedMethods(ImpMethodDecl, I,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001845 isa<ObjCProtocolDecl>(CDecl));
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001846 else if (!I->isPropertyAccessor())
1847 WarnExactTypedMethods(ImpMethodDecl, I, isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001848 }
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001849 }
1850 }
Mike Stump11289f42009-09-09 15:08:12 +00001851
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001852 // Check and see if class methods in class interface have been
1853 // implemented in the implementation class. If so, their types match.
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001854 for (auto *I : CDecl->class_methods()) {
1855 if (!ClsMapSeen.insert(I->getSelector()))
Benjamin Kramer9f8e2d72013-10-14 15:16:10 +00001856 continue;
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001857 if (!ClsMap.count(I->getSelector())) {
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001858 if (ImmediateClass)
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001859 WarnUndefinedMethod(*this, IMPDecl->getLocation(), I, IncompleteImpl,
Ted Kremenek65d63572013-03-27 00:02:21 +00001860 diag::warn_undef_method_impl);
Mike Stump12b8ce12009-08-04 21:02:39 +00001861 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00001862 ObjCMethodDecl *ImpMethodDecl =
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001863 IMPDecl->getClassMethod(I->getSelector());
1864 assert(CDecl->getClassMethod(I->getSelector()) &&
Argyrios Kyrtzidis342e08f2011-08-30 19:43:21 +00001865 "Expected to find the method through lookup as well");
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001866 if (!WarnCategoryMethodImpl)
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001867 WarnConflictingTypedMethods(ImpMethodDecl, I,
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001868 isa<ObjCProtocolDecl>(CDecl));
1869 else
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001870 WarnExactTypedMethods(ImpMethodDecl, I,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001871 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001872 }
1873 }
Fariborz Jahanian73853e52010-10-08 22:59:25 +00001874
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001875 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) {
1876 // Also, check for methods declared in protocols inherited by
1877 // this protocol.
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001878 for (auto *PI : PD->protocols())
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001879 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Aaron Ballman0f6e64d2014-03-13 22:58:06 +00001880 IMPDecl, PI, IncompleteImpl, false,
Fariborz Jahanian8181caa2013-08-14 23:58:55 +00001881 WarnCategoryMethodImpl);
1882 }
1883
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001884 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001885 // when checking that methods in implementation match their declaration,
1886 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1887 // extension; as well as those in categories.
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001888 if (!WarnCategoryMethodImpl) {
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001889 for (auto *Cat : I->visible_categories())
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001890 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Aaron Ballman3fe486a2014-03-13 21:23:55 +00001891 IMPDecl, Cat, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001892 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001893 } else {
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001894 // Also methods in class extensions need be looked at next.
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001895 for (auto *Ext : I->visible_extensions())
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001896 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00001897 IMPDecl, Ext, IncompleteImpl, false,
Fariborz Jahanian6f5309c2012-10-23 23:06:22 +00001898 WarnCategoryMethodImpl);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00001899 }
1900
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001901 // Check for any implementation of a methods declared in protocol.
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001902 for (auto *PI : I->all_referenced_protocols())
Mike Stump11289f42009-09-09 15:08:12 +00001903 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001904 IMPDecl, PI, IncompleteImpl, false,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001905 WarnCategoryMethodImpl);
Aaron Ballmana9f49e32014-03-13 20:55:22 +00001906
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001907 // FIXME. For now, we are not checking for extact match of methods
1908 // in category implementation and its primary class's super class.
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001909 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001910 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump11289f42009-09-09 15:08:12 +00001911 IMPDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001912 I->getSuperClass(), IncompleteImpl, false);
1913 }
1914}
1915
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001916/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1917/// category matches with those implemented in its primary class and
1918/// warns each time an exact match is found.
1919void Sema::CheckCategoryVsClassMethodMatches(
1920 ObjCCategoryImplDecl *CatIMPDecl) {
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001921 // Get category's primary class.
1922 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1923 if (!CatDecl)
1924 return;
1925 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1926 if (!IDecl)
1927 return;
Fariborz Jahanianf3077a22013-12-05 20:52:31 +00001928 ObjCInterfaceDecl *SuperIDecl = IDecl->getSuperClass();
1929 SelectorSet InsMap, ClsMap;
1930
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001931 for (const auto *I : CatIMPDecl->instance_methods()) {
1932 Selector Sel = I->getSelector();
Fariborz Jahanianf3077a22013-12-05 20:52:31 +00001933 // When checking for methods implemented in the category, skip over
1934 // those declared in category class's super class. This is because
1935 // the super class must implement the method.
1936 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, true))
1937 continue;
1938 InsMap.insert(Sel);
1939 }
1940
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001941 for (const auto *I : CatIMPDecl->class_methods()) {
1942 Selector Sel = I->getSelector();
Fariborz Jahanianf3077a22013-12-05 20:52:31 +00001943 if (SuperIDecl && SuperIDecl->lookupMethod(Sel, false))
1944 continue;
1945 ClsMap.insert(Sel);
1946 }
1947 if (InsMap.empty() && ClsMap.empty())
1948 return;
1949
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001950 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001951 bool IncompleteImpl = false;
1952 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1953 CatIMPDecl, IDecl,
Fariborz Jahanian29082a52012-02-09 21:30:24 +00001954 IncompleteImpl, false,
1955 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001956}
Fariborz Jahanian4ceec3f2011-07-24 20:53:26 +00001957
Fariborz Jahanian25491a22010-05-05 21:52:17 +00001958void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump11289f42009-09-09 15:08:12 +00001959 ObjCContainerDecl* CDecl,
Chris Lattner9ef10f42009-03-01 00:56:52 +00001960 bool IncompleteImpl) {
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001961 SelectorSet InsMap;
Chris Lattnerda463fe2007-12-12 07:09:47 +00001962 // Check and see if instance methods in class interface have been
1963 // implemented in the implementation class.
Aaron Ballmanf26acce2014-03-13 19:50:17 +00001964 for (const auto *I : IMPDecl->instance_methods())
1965 InsMap.insert(I->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001966
Fariborz Jahanian6c6aea92009-04-14 23:15:21 +00001967 // Check and see if properties declared in the interface have either 1)
1968 // an implementation or 2) there is a @synthesize/@dynamic implementation
1969 // of the property in the @implementation.
Ted Kremenek348e88c2014-02-21 19:41:34 +00001970 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl)) {
1971 bool SynthesizeProperties = LangOpts.ObjCDefaultSynthProperties &&
1972 LangOpts.ObjCRuntime.isNonFragile() &&
1973 !IDecl->isObjCRequiresPropertyDefs();
1974 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, SynthesizeProperties);
1975 }
1976
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001977 SelectorSet ClsMap;
Aaron Ballmane8a7dc92014-03-13 20:11:06 +00001978 for (const auto *I : IMPDecl->class_methods())
1979 ClsMap.insert(I->getSelector());
Mike Stump11289f42009-09-09 15:08:12 +00001980
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001981 // Check for type conflict of methods declared in a class/protocol and
1982 // its implementation; if any.
Benjamin Kramerb33ffee2012-05-27 13:28:52 +00001983 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump11289f42009-09-09 15:08:12 +00001984 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1985 IMPDecl, CDecl,
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001986 IncompleteImpl, true);
Fariborz Jahanian2bda1b62011-08-03 18:21:12 +00001987
Fariborz Jahanian9f8b19e2011-07-28 23:19:50 +00001988 // check all methods implemented in category against those declared
1989 // in its primary class.
1990 if (ObjCCategoryImplDecl *CatDecl =
1991 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1992 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump11289f42009-09-09 15:08:12 +00001993
Chris Lattnerda463fe2007-12-12 07:09:47 +00001994 // Check the protocol list for unimplemented methods in the @implementation
1995 // class.
Fariborz Jahanian07b71652009-05-01 20:07:12 +00001996 // Check and see if class methods in class interface have been
1997 // implemented in the implementation class.
Mike Stump11289f42009-09-09 15:08:12 +00001998
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00001999 LazyProtocolNameSet ExplicitImplProtocols;
2000
Chris Lattner9ef10f42009-03-01 00:56:52 +00002001 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Aaron Ballmana9f49e32014-03-13 20:55:22 +00002002 for (auto *PI : I->all_referenced_protocols())
2003 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), PI, IncompleteImpl,
2004 InsMap, ClsMap, I, ExplicitImplProtocols);
Chris Lattner9ef10f42009-03-01 00:56:52 +00002005 // Check class extensions (unnamed categories)
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00002006 for (auto *Ext : I->visible_extensions())
2007 ImplMethodsVsClassMethods(S, IMPDecl, Ext, IncompleteImpl);
Chris Lattner9ef10f42009-03-01 00:56:52 +00002008 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanian8764c742009-10-05 21:32:49 +00002009 // For extended class, unimplemented methods in its protocols will
2010 // be reported in the primary class.
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002011 if (!C->IsClassExtension()) {
Aaron Ballman19a41762014-03-14 12:55:57 +00002012 for (auto *P : C->protocols())
2013 CheckProtocolMethodDefs(*this, IMPDecl->getLocation(), P,
Ted Kremenek4b3c66e2014-03-05 08:13:08 +00002014 IncompleteImpl, InsMap, ClsMap, CDecl,
2015 ExplicitImplProtocols);
Ted Kremenek348e88c2014-02-21 19:41:34 +00002016 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl,
2017 /* SynthesizeProperties */ false);
Fariborz Jahanian4f8a5712010-01-20 19:36:21 +00002018 }
Chris Lattner9ef10f42009-03-01 00:56:52 +00002019 } else
David Blaikie83d382b2011-09-23 05:06:16 +00002020 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattnerda463fe2007-12-12 07:09:47 +00002021}
2022
Mike Stump11289f42009-09-09 15:08:12 +00002023/// ActOnForwardClassDeclaration -
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00002024Sema::DeclGroupPtrTy
Chris Lattnerda463fe2007-12-12 07:09:47 +00002025Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattner99a83312009-02-16 19:25:52 +00002026 IdentifierInfo **IdentList,
Ted Kremeneka26da852009-11-17 23:12:20 +00002027 SourceLocation *IdentLocs,
Chris Lattner99a83312009-02-16 19:25:52 +00002028 unsigned NumElts) {
Fariborz Jahanian3a039e32011-08-27 20:50:59 +00002029 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002030 for (unsigned i = 0; i != NumElts; ++i) {
2031 // Check for another declaration kind with the same name.
John McCall9f3059a2009-10-09 21:13:30 +00002032 NamedDecl *PrevDecl
Douglas Gregorb2ccf012010-04-15 22:33:43 +00002033 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorb8eaf292010-04-15 23:40:53 +00002034 LookupOrdinaryName, ForRedeclaration);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002035 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroff946166f2008-06-05 22:57:10 +00002036 // GCC apparently allows the following idiom:
2037 //
2038 // typedef NSObject < XCElementTogglerP > XCElementToggler;
2039 // @class XCElementToggler;
2040 //
Fariborz Jahanian04c44552012-01-24 00:40:15 +00002041 // Here we have chosen to ignore the forward class declaration
2042 // with a warning. Since this is the implied behavior.
Richard Smithdda56e42011-04-15 14:24:37 +00002043 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCall8b07ec22010-05-15 11:32:37 +00002044 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner4bd8dd82008-11-19 08:23:25 +00002045 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner0369c572008-11-23 23:12:31 +00002046 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCall8b07ec22010-05-15 11:32:37 +00002047 } else {
Mike Stump12b8ce12009-08-04 21:02:39 +00002048 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahanian04c44552012-01-24 00:40:15 +00002049 // to the underlying class. Just ignore the forward class with a warning
2050 // as this will force the intended behavior which is to lookup the typedef
2051 // name.
2052 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
2053 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
2054 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
2055 continue;
2056 }
Fariborz Jahanian0d451812009-05-07 21:49:26 +00002057 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002058 }
Douglas Gregordc9166c2011-12-15 20:29:51 +00002059
2060 // Create a declaration to describe this forward declaration.
Douglas Gregorab1ec82e2011-12-16 03:12:41 +00002061 ObjCInterfaceDecl *PrevIDecl
2062 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002063
2064 IdentifierInfo *ClassName = IdentList[i];
2065 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
2066 // A previous decl with a different name is because of
2067 // @compatibility_alias, for example:
2068 // \code
2069 // @class NewImage;
2070 // @compatibility_alias OldImage NewImage;
2071 // \endcode
2072 // A lookup for 'OldImage' will return the 'NewImage' decl.
2073 //
2074 // In such a case use the real declaration name, instead of the alias one,
2075 // otherwise we will break IdentifierResolver and redecls-chain invariants.
2076 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
2077 // has been aliased.
2078 ClassName = PrevIDecl->getIdentifier();
2079 }
2080
Douglas Gregordc9166c2011-12-15 20:29:51 +00002081 ObjCInterfaceDecl *IDecl
2082 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidisdd710632013-06-18 21:26:33 +00002083 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002084 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregordc9166c2011-12-15 20:29:51 +00002085
Douglas Gregordc9166c2011-12-15 20:29:51 +00002086 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeafd0b2011-12-27 22:43:10 +00002087 CheckObjCDeclScope(IDecl);
2088 DeclsInGroup.push_back(IDecl);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002089 }
Rafael Espindolaab417692013-07-09 12:05:01 +00002090
2091 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002092}
2093
John McCall54507ab2011-06-16 01:15:19 +00002094static bool tryMatchRecordTypes(ASTContext &Context,
2095 Sema::MethodMatchStrategy strategy,
2096 const Type *left, const Type *right);
2097
John McCall31168b02011-06-15 23:02:42 +00002098static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
2099 QualType leftQT, QualType rightQT) {
2100 const Type *left =
2101 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2102 const Type *right =
2103 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2104
2105 if (left == right) return true;
2106
2107 // If we're doing a strict match, the types have to match exactly.
2108 if (strategy == Sema::MMS_strict) return false;
2109
2110 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2111
2112 // Otherwise, use this absurdly complicated algorithm to try to
2113 // validate the basic, low-level compatibility of the two types.
2114
2115 // As a minimum, require the sizes and alignments to match.
2116 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
2117 return false;
2118
2119 // Consider all the kinds of non-dependent canonical types:
2120 // - functions and arrays aren't possible as return and parameter types
2121
2122 // - vector types of equal size can be arbitrarily mixed
2123 if (isa<VectorType>(left)) return isa<VectorType>(right);
2124 if (isa<VectorType>(right)) return false;
2125
2126 // - references should only match references of identical type
John McCall54507ab2011-06-16 01:15:19 +00002127 // - structs, unions, and Objective-C objects must match more-or-less
2128 // exactly
John McCall31168b02011-06-15 23:02:42 +00002129 // - everything else should be a scalar
2130 if (!left->isScalarType() || !right->isScalarType())
John McCall54507ab2011-06-16 01:15:19 +00002131 return tryMatchRecordTypes(Context, strategy, left, right);
John McCall31168b02011-06-15 23:02:42 +00002132
John McCall9320b872011-09-09 05:25:32 +00002133 // Make scalars agree in kind, except count bools as chars, and group
2134 // all non-member pointers together.
John McCall31168b02011-06-15 23:02:42 +00002135 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2136 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2137 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2138 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall9320b872011-09-09 05:25:32 +00002139 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2140 leftSK = Type::STK_ObjCObjectPointer;
2141 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2142 rightSK = Type::STK_ObjCObjectPointer;
John McCall31168b02011-06-15 23:02:42 +00002143
2144 // Note that data member pointers and function member pointers don't
2145 // intermix because of the size differences.
2146
2147 return (leftSK == rightSK);
2148}
Chris Lattnerda463fe2007-12-12 07:09:47 +00002149
John McCall54507ab2011-06-16 01:15:19 +00002150static bool tryMatchRecordTypes(ASTContext &Context,
2151 Sema::MethodMatchStrategy strategy,
2152 const Type *lt, const Type *rt) {
2153 assert(lt && rt && lt != rt);
2154
2155 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2156 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2157 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2158
2159 // Require union-hood to match.
2160 if (left->isUnion() != right->isUnion()) return false;
2161
2162 // Require an exact match if either is non-POD.
2163 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2164 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2165 return false;
2166
2167 // Require size and alignment to match.
2168 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2169
2170 // Require fields to match.
2171 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2172 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2173 for (; li != le && ri != re; ++li, ++ri) {
2174 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2175 return false;
2176 }
2177 return (li == le && ri == re);
2178}
2179
Chris Lattnerda463fe2007-12-12 07:09:47 +00002180/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2181/// returns true, or false, accordingly.
2182/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCall31168b02011-06-15 23:02:42 +00002183bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2184 const ObjCMethodDecl *right,
2185 MethodMatchStrategy strategy) {
Alp Toker314cc812014-01-25 16:55:45 +00002186 if (!matchTypes(Context, strategy, left->getReturnType(),
2187 right->getReturnType()))
John McCall31168b02011-06-15 23:02:42 +00002188 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002189
Douglas Gregor560b7fa2013-02-07 19:13:24 +00002190 // If either is hidden, it is not considered to match.
2191 if (left->isHidden() || right->isHidden())
2192 return false;
2193
David Blaikiebbafb8a2012-03-11 07:00:24 +00002194 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002195 (left->hasAttr<NSReturnsRetainedAttr>()
2196 != right->hasAttr<NSReturnsRetainedAttr>() ||
2197 left->hasAttr<NSConsumesSelfAttr>()
2198 != right->hasAttr<NSConsumesSelfAttr>()))
2199 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002200
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002201 ObjCMethodDecl::param_const_iterator
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002202 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2203 re = right->param_end();
Mike Stump11289f42009-09-09 15:08:12 +00002204
Douglas Gregor0bf70f42012-05-17 23:13:29 +00002205 for (; li != le && ri != re; ++li, ++ri) {
John McCall31168b02011-06-15 23:02:42 +00002206 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00002207 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCall31168b02011-06-15 23:02:42 +00002208
2209 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2210 return false;
2211
David Blaikiebbafb8a2012-03-11 07:00:24 +00002212 if (getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00002213 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2214 return false;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002215 }
2216 return true;
2217}
2218
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002219void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002220 // Record at the head of the list whether there were 0, 1, or >= 2 methods
2221 // inside categories.
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00002222 if (ObjCCategoryDecl *
2223 CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
2224 if (!CD->IsClassExtension() && List->getBits() < 2)
2225 List->setBits(List->getBits()+1);
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002226
Douglas Gregorc454afe2012-01-25 00:19:56 +00002227 // If the list is empty, make it a singleton list.
2228 if (List->Method == 0) {
2229 List->Method = Method;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002230 List->setNext(0);
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002231 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002232 }
2233
2234 // We've seen a method with this name, see if we have already seen this type
2235 // signature.
2236 ObjCMethodList *Previous = List;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002237 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregor600a2f52013-06-21 00:20:25 +00002238 // If we are building a module, keep all of the methods.
2239 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2240 continue;
2241
Douglas Gregore1716012012-01-25 00:49:42 +00002242 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregorc454afe2012-01-25 00:19:56 +00002243 continue;
2244
2245 ObjCMethodDecl *PrevObjCMethod = List->Method;
2246
2247 // Propagate the 'defined' bit.
2248 if (Method->isDefined())
2249 PrevObjCMethod->setDefined(true);
2250
2251 // If a method is deprecated, push it in the global pool.
2252 // This is used for better diagnostics.
2253 if (Method->isDeprecated()) {
2254 if (!PrevObjCMethod->isDeprecated())
2255 List->Method = Method;
2256 }
2257 // If new method is unavailable, push it into global pool
2258 // unless previous one is deprecated.
2259 if (Method->isUnavailable()) {
2260 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2261 List->Method = Method;
2262 }
2263
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002264 return;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002265 }
2266
2267 // We have a new signature for an existing method - add it.
2268 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregore1716012012-01-25 00:49:42 +00002269 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002270 Previous->setNext(new (Mem) ObjCMethodList(Method, 0));
Douglas Gregorc454afe2012-01-25 00:19:56 +00002271}
2272
Sebastian Redl75d8a322010-08-02 23:18:59 +00002273/// \brief Read the contents of the method pool for a given selector from
2274/// external storage.
Douglas Gregore1716012012-01-25 00:49:42 +00002275void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorc78d3462009-04-24 21:10:55 +00002276 assert(ExternalSource && "We need an external AST source");
Douglas Gregore1716012012-01-25 00:49:42 +00002277 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorc78d3462009-04-24 21:10:55 +00002278}
2279
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002280void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002281 bool instance) {
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002282 // Ignore methods of invalid containers.
2283 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002284 return;
Argyrios Kyrtzidisb15def22012-03-12 18:34:26 +00002285
Douglas Gregor70f449b2012-01-25 00:59:09 +00002286 if (ExternalSource)
2287 ReadMethodPool(Method->getSelector());
2288
Sebastian Redl75d8a322010-08-02 23:18:59 +00002289 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor70f449b2012-01-25 00:59:09 +00002290 if (Pos == MethodPool.end())
2291 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2292 GlobalMethods())).first;
Douglas Gregorc454afe2012-01-25 00:19:56 +00002293
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002294 Method->setDefined(impl);
Douglas Gregorc454afe2012-01-25 00:19:56 +00002295
Sebastian Redl75d8a322010-08-02 23:18:59 +00002296 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002297 addMethodToGlobalList(&Entry, Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002298}
2299
John McCall31168b02011-06-15 23:02:42 +00002300/// Determines if this is an "acceptable" loose mismatch in the global
2301/// method pool. This exists mostly as a hack to get around certain
2302/// global mismatches which we can't afford to make warnings / errors.
2303/// Really, what we want is a way to take a method out of the global
2304/// method pool.
2305static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2306 ObjCMethodDecl *other) {
2307 if (!chosen->isInstanceMethod())
2308 return false;
2309
2310 Selector sel = chosen->getSelector();
2311 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2312 return false;
2313
2314 // Don't complain about mismatches for -length if the method we
2315 // chose has an integral result type.
Alp Toker314cc812014-01-25 16:55:45 +00002316 return (chosen->getReturnType()->isIntegerType());
John McCall31168b02011-06-15 23:02:42 +00002317}
2318
Sebastian Redl75d8a322010-08-02 23:18:59 +00002319ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian3337b2e2010-08-09 23:27:58 +00002320 bool receiverIdOrClass,
Sebastian Redl75d8a322010-08-02 23:18:59 +00002321 bool warn, bool instance) {
Douglas Gregor70f449b2012-01-25 00:59:09 +00002322 if (ExternalSource)
2323 ReadMethodPool(Sel);
2324
Sebastian Redl75d8a322010-08-02 23:18:59 +00002325 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor70f449b2012-01-25 00:59:09 +00002326 if (Pos == MethodPool.end())
2327 return 0;
Douglas Gregorc78d3462009-04-24 21:10:55 +00002328
Douglas Gregor77f49a42013-01-16 18:47:38 +00002329 // Gather the non-hidden methods.
Sebastian Redl75d8a322010-08-02 23:18:59 +00002330 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Robert Wilhelmb869a8f2013-08-10 12:33:24 +00002331 SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidisd3da6e02013-04-17 00:08:58 +00002332 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Douglas Gregor77f49a42013-01-16 18:47:38 +00002333 if (M->Method && !M->Method->isHidden()) {
2334 // If we're not supposed to warn about mismatches, we're done.
2335 if (!warn)
2336 return M->Method;
Mike Stump11289f42009-09-09 15:08:12 +00002337
Douglas Gregor77f49a42013-01-16 18:47:38 +00002338 Methods.push_back(M->Method);
Sebastian Redl75d8a322010-08-02 23:18:59 +00002339 }
Douglas Gregorc78d3462009-04-24 21:10:55 +00002340 }
Douglas Gregor77f49a42013-01-16 18:47:38 +00002341
2342 // If there aren't any visible methods, we're done.
2343 // FIXME: Recover if there are any known-but-hidden methods?
2344 if (Methods.empty())
2345 return 0;
2346
2347 if (Methods.size() == 1)
2348 return Methods[0];
2349
2350 // We found multiple methods, so we may have to complain.
2351 bool issueDiagnostic = false, issueError = false;
2352
2353 // We support a warning which complains about *any* difference in
2354 // method signature.
2355 bool strictSelectorMatch =
2356 (receiverIdOrClass && warn &&
2357 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2358 R.getBegin())
2359 != DiagnosticsEngine::Ignored));
2360 if (strictSelectorMatch) {
2361 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2362 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2363 issueDiagnostic = true;
2364 break;
2365 }
2366 }
2367 }
2368
2369 // If we didn't see any strict differences, we won't see any loose
2370 // differences. In ARC, however, we also need to check for loose
2371 // mismatches, because most of them are errors.
2372 if (!strictSelectorMatch ||
2373 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2374 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2375 // This checks if the methods differ in type mismatch.
2376 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2377 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2378 issueDiagnostic = true;
2379 if (getLangOpts().ObjCAutoRefCount)
2380 issueError = true;
2381 break;
2382 }
2383 }
2384
2385 if (issueDiagnostic) {
2386 if (issueError)
2387 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2388 else if (strictSelectorMatch)
2389 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2390 else
2391 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2392
2393 Diag(Methods[0]->getLocStart(),
2394 issueError ? diag::note_possibility : diag::note_using)
2395 << Methods[0]->getSourceRange();
2396 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2397 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2398 << Methods[I]->getSourceRange();
2399 }
2400 }
2401 return Methods[0];
Douglas Gregorc78d3462009-04-24 21:10:55 +00002402}
2403
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002404ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redl75d8a322010-08-02 23:18:59 +00002405 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2406 if (Pos == MethodPool.end())
2407 return 0;
2408
2409 GlobalMethods &Methods = Pos->second;
2410
2411 if (Methods.first.Method && Methods.first.Method->isDefined())
2412 return Methods.first.Method;
2413 if (Methods.second.Method && Methods.second.Method->isDefined())
2414 return Methods.second.Method;
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00002415 return 0;
2416}
2417
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002418static void
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002419HelperSelectorsForTypoCorrection(
2420 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2421 StringRef Typo, const ObjCMethodDecl * Method) {
2422 const unsigned MaxEditDistance = 1;
2423 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieuea8d3702013-06-06 02:22:29 +00002424 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002425
2426 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2427 if (MinPossibleEditDistance > 0 &&
2428 Typo.size() / MinPossibleEditDistance < 1)
2429 return;
2430 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2431 if (EditDistance > MaxEditDistance)
2432 return;
2433 if (EditDistance == BestEditDistance)
2434 BestMethod.push_back(Method);
2435 else if (EditDistance < BestEditDistance) {
2436 BestMethod.clear();
2437 BestMethod.push_back(Method);
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002438 }
2439}
2440
Fariborz Jahanian75481672013-06-17 17:10:54 +00002441static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2442 QualType ObjectType) {
2443 if (ObjectType.isNull())
2444 return true;
2445 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2446 return true;
2447 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != 0;
2448}
2449
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002450const ObjCMethodDecl *
Fariborz Jahanian75481672013-06-17 17:10:54 +00002451Sema::SelectorsForTypoCorrection(Selector Sel,
2452 QualType ObjectType) {
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002453 unsigned NumArgs = Sel.getNumArgs();
2454 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002455 bool ObjectIsId = true, ObjectIsClass = true;
2456 if (ObjectType.isNull())
2457 ObjectIsId = ObjectIsClass = false;
2458 else if (!ObjectType->isObjCObjectPointerType())
2459 return 0;
2460 else if (const ObjCObjectPointerType *ObjCPtr =
2461 ObjectType->getAsObjCInterfacePointerType()) {
2462 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2463 ObjectIsId = ObjectIsClass = false;
2464 }
2465 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2466 ObjectIsClass = false;
2467 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2468 ObjectIsId = false;
2469 else
2470 return 0;
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002471
2472 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2473 e = MethodPool.end(); b != e; b++) {
2474 // instance methods
2475 for (ObjCMethodList *M = &b->second.first; 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 (ObjectIsId)
2480 Methods.push_back(M->Method);
2481 else if (!ObjectIsClass &&
2482 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2483 Methods.push_back(M->Method);
2484 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002485 // class methods
2486 for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
2487 if (M->Method &&
Fariborz Jahanian06499232013-06-18 17:10:58 +00002488 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2489 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian4cc55522013-06-18 15:31:36 +00002490 if (ObjectIsClass)
2491 Methods.push_back(M->Method);
2492 else if (!ObjectIsId &&
2493 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2494 Methods.push_back(M->Method);
2495 }
Fariborz Jahanian0c0fc9e2013-06-05 18:46:14 +00002496 }
2497
2498 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2499 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2500 HelperSelectorsForTypoCorrection(SelectedMethods,
2501 Sel.getAsString(), Methods[i]);
2502 }
2503 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : NULL;
2504}
2505
Fariborz Jahanian42f89382013-05-30 21:48:58 +00002506/// DiagnoseDuplicateIvars -
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002507/// Check for duplicate ivars in the entire class at the start of
James Dennett634962f2012-06-14 21:40:34 +00002508/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002509/// add ivars to a class in random order which will not be known until
James Dennett634962f2012-06-14 21:40:34 +00002510/// class's \@implementation is seen.
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002511void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2512 ObjCInterfaceDecl *SID) {
Aaron Ballman59abbd42014-03-13 21:09:43 +00002513 for (auto *Ivar : ID->ivars()) {
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002514 if (Ivar->isInvalidDecl())
2515 continue;
2516 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2517 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2518 if (prevIvar) {
2519 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2520 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2521 Ivar->setInvalidDecl();
2522 }
2523 }
2524 }
2525}
2526
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002527Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2528 switch (CurContext->getDeclKind()) {
2529 case Decl::ObjCInterface:
2530 return Sema::OCK_Interface;
2531 case Decl::ObjCProtocol:
2532 return Sema::OCK_Protocol;
2533 case Decl::ObjCCategory:
2534 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2535 return Sema::OCK_ClassExtension;
2536 else
2537 return Sema::OCK_Category;
2538 case Decl::ObjCImplementation:
2539 return Sema::OCK_Implementation;
2540 case Decl::ObjCCategoryImpl:
2541 return Sema::OCK_CategoryImplementation;
2542
2543 default:
2544 return Sema::OCK_None;
2545 }
2546}
2547
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002548// Note: For class/category implementations, allMethods is always null.
Robert Wilhelm57c67112013-07-17 21:14:35 +00002549Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods,
Fariborz Jahaniandfb76872013-07-17 00:05:08 +00002550 ArrayRef<DeclGroupPtrTy> allTUVars) {
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002551 if (getObjCContainerKind() == Sema::OCK_None)
2552 return 0;
2553
2554 assert(AtEnd.isValid() && "Invalid location for '@end'");
2555
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00002556 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2557 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian9290ede2009-11-16 18:57:01 +00002558
Mike Stump11289f42009-09-09 15:08:12 +00002559 bool isInterfaceDeclKind =
Chris Lattner219b3e92008-03-16 21:17:37 +00002560 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2561 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002562 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroffb3a87982009-01-09 15:36:25 +00002563
Steve Naroff35c62ae2009-01-08 17:28:14 +00002564 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2565 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2566 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2567
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002568 for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) {
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002569 ObjCMethodDecl *Method =
John McCall48871652010-08-21 09:40:31 +00002570 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002571
2572 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorffca3a22009-01-09 17:18:27 +00002573 if (Method->isInstanceMethod()) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002574 /// Check for instance method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002575 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002576 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002577 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002578 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002579 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002580 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002581 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002582 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002583 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002584 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002585 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002586 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002587 if (!Context.getSourceManager().isInSystemHeader(
2588 Method->getLocation()))
2589 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2590 << Method->getDeclName();
2591 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2592 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002593 InsMap[Method->getSelector()] = Method;
2594 /// The following allows us to typecheck messages to "id".
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002595 AddInstanceMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002596 }
Mike Stump12b8ce12009-08-04 21:02:39 +00002597 } else {
Chris Lattnerda463fe2007-12-12 07:09:47 +00002598 /// Check for class method of the same name with incompatible types
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002599 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump11289f42009-09-09 15:08:12 +00002600 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattnerda463fe2007-12-12 07:09:47 +00002601 : false;
Mike Stump11289f42009-09-09 15:08:12 +00002602 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman42b1e9e2008-12-16 20:15:50 +00002603 || (checkIdenticalMethods && match)) {
Chris Lattner0369c572008-11-23 23:12:31 +00002604 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00002605 << Method->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00002606 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregor87e92752010-12-21 17:34:17 +00002607 Method->setInvalidDecl();
Chris Lattnerda463fe2007-12-12 07:09:47 +00002608 } else {
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002609 if (PrevMethod) {
Argyrios Kyrtzidisdcaaa212011-10-14 08:02:31 +00002610 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanianc17c86b2011-12-13 19:40:34 +00002611 if (!Context.getSourceManager().isInSystemHeader(
2612 Method->getLocation()))
2613 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2614 << Method->getDeclName();
2615 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2616 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002617 ClsMap[Method->getSelector()] = Method;
Douglas Gregor0e6fc1a2012-05-01 23:37:00 +00002618 AddFactoryMethodToGlobalPool(Method);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002619 }
2620 }
2621 }
Douglas Gregorb8982092013-01-21 19:42:21 +00002622 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2623 // Nothing to do here.
Steve Naroffb3a87982009-01-09 15:36:25 +00002624 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002625 // Categories are used to extend the class by declaring new methods.
Mike Stump11289f42009-09-09 15:08:12 +00002626 // By the same token, they are also used to add new properties. No
Fariborz Jahanian62293f42008-12-06 19:59:02 +00002627 // need to compare the added property to those in the class.
Daniel Dunbar4684f372008-08-27 05:40:03 +00002628
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002629 if (C->IsClassExtension()) {
2630 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2631 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanianc21f5432010-12-10 23:36:33 +00002632 }
Chris Lattnerda463fe2007-12-12 07:09:47 +00002633 }
Steve Naroffb3a87982009-01-09 15:36:25 +00002634 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian30a42922010-02-15 21:55:26 +00002635 if (CDecl->getIdentifier())
2636 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2637 // user-defined setter/getter. It also synthesizes setter/getter methods
2638 // and adds them to the DeclContext and global method pools.
Aaron Ballmand174edf2014-03-13 19:11:50 +00002639 for (auto *I : CDecl->properties())
Aaron Ballmandc4bea42014-03-13 18:47:37 +00002640 ProcessPropertyDecl(I, CDecl);
Ted Kremenekc7c64312010-01-07 01:20:12 +00002641 CDecl->setAtEndRange(AtEnd);
Steve Naroffb3a87982009-01-09 15:36:25 +00002642 }
2643 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002644 IC->setAtEndRange(AtEnd);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002645 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002646 // Any property declared in a class extension might have user
2647 // declared setter or getter in current class extension or one
2648 // of the other class extensions. Mark them as synthesized as
2649 // property will be synthesized when property with same name is
2650 // seen in the @implementation.
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00002651 for (const auto *Ext : IDecl->visible_extensions()) {
Aaron Ballmand174edf2014-03-13 19:11:50 +00002652 for (const auto *Property : Ext->properties()) {
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002653 // Skip over properties declared @dynamic
2654 if (const ObjCPropertyImplDecl *PIDecl
2655 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2656 if (PIDecl->getPropertyImplementation()
2657 == ObjCPropertyImplDecl::Dynamic)
2658 continue;
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002659
Aaron Ballmanf53d8dd2014-03-13 21:47:07 +00002660 for (const auto *Ext : IDecl->visible_extensions()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002661 if (ObjCMethodDecl *GetterMethod
2662 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002663 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002664 if (!Property->isReadOnly())
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002665 if (ObjCMethodDecl *SetterMethod
2666 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rosed01e83a2012-10-10 16:42:25 +00002667 SetterMethod->setPropertyAccessor(true);
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002668 }
Fariborz Jahanian5d7e9162010-12-11 18:39:37 +00002669 }
2670 }
Fariborz Jahanian25491a22010-05-05 21:52:17 +00002671 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002672 AtomicPropertySetterGetterRules(IC, IDecl);
John McCall31168b02011-06-15 23:02:42 +00002673 DiagnoseOwningPropertyGetterSynthesis(IC);
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00002674 DiagnoseUnusedBackingIvarInAccessor(S, IC);
Argyrios Kyrtzidisdb5ce0f2013-12-03 21:11:54 +00002675 if (IDecl->hasDesignatedInitializers())
2676 DiagnoseMissingDesignatedInitOverrides(IC, IDecl);
2677
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002678 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2679 if (IDecl->getSuperClass() == NULL) {
2680 // This class has no superclass, so check that it has been marked with
2681 // __attribute((objc_root_class)).
2682 if (!HasRootClassAttr) {
2683 SourceLocation DeclLoc(IDecl->getLocation());
2684 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2685 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2686 << IDecl->getIdentifier();
2687 // See if NSObject is in the current scope, and if it is, suggest
2688 // adding " : NSObject " to the class declaration.
2689 NamedDecl *IF = LookupSingleName(TUScope,
2690 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2691 DeclLoc, LookupOrdinaryName);
2692 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2693 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2694 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2695 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2696 } else {
2697 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2698 }
2699 }
2700 } else if (HasRootClassAttr) {
2701 // Complain that only root classes may have this attribute.
2702 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2703 }
2704
John McCall5fb5df92012-06-20 06:18:46 +00002705 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian545643c2010-02-23 23:41:11 +00002706 while (IDecl->getSuperClass()) {
2707 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2708 IDecl = IDecl->getSuperClass();
2709 }
Patrick Beardacfbe9e2012-04-06 18:12:22 +00002710 }
Fariborz Jahanian13e0c902009-11-11 22:40:11 +00002711 }
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00002712 SetIvarInitializers(IC);
Mike Stump11289f42009-09-09 15:08:12 +00002713 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroffb3a87982009-01-09 15:36:25 +00002714 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenekc7c64312010-01-07 01:20:12 +00002715 CatImplClass->setAtEndRange(AtEnd);
Mike Stump11289f42009-09-09 15:08:12 +00002716
Chris Lattnerda463fe2007-12-12 07:09:47 +00002717 // Find category interface decl and then check that all methods declared
Daniel Dunbar4684f372008-08-27 05:40:03 +00002718 // in this interface are implemented in the category @implementation.
Chris Lattner41fd42e2009-02-16 18:32:47 +00002719 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregor048fbfa2013-01-16 23:00:23 +00002720 if (ObjCCategoryDecl *Cat
2721 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2722 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattnerda463fe2007-12-12 07:09:47 +00002723 }
2724 }
2725 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002726 if (isInterfaceDeclKind) {
2727 // Reject invalid vardecls.
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002728 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002729 DeclGroupRef DG = allTUVars[i].get();
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002730 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2731 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar0ca16602009-04-14 02:25:56 +00002732 if (!VDecl->hasExternalStorage())
Steve Naroff42959b22009-04-13 17:58:46 +00002733 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanian629aed92009-03-21 18:06:45 +00002734 }
Chris Lattner5bbb3c82009-03-29 16:50:03 +00002735 }
Fariborz Jahanian3654e652009-03-18 22:33:24 +00002736 }
Fariborz Jahanian4327b322011-08-29 17:33:12 +00002737 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002738
Fariborz Jahanian0080fb52013-07-16 15:33:19 +00002739 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov9ddb76e2013-08-27 13:15:56 +00002740 DeclGroupRef DG = allTUVars[i].get();
Argyrios Kyrtzidis8ad3bab2011-11-23 20:27:36 +00002741 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2742 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisbd8b1502011-10-17 19:48:13 +00002743 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2744 }
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002745
Dmitri Gribenkoe7bb9442012-07-13 01:06:46 +00002746 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggenc6c8d932011-12-06 09:25:23 +00002747 return ClassDecl;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002748}
2749
2750
2751/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2752/// objective-c's type qualifier from the parser version of the same info.
Mike Stump11289f42009-09-09 15:08:12 +00002753static Decl::ObjCDeclQualifier
Ted Kremenek1b0ea822008-01-07 19:49:32 +00002754CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCallca872902011-05-01 03:04:29 +00002755 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattnerda463fe2007-12-12 07:09:47 +00002756}
2757
Douglas Gregor33823722011-06-11 01:09:30 +00002758/// \brief Check whether the declared result type of the given Objective-C
2759/// method declaration is compatible with the method's class.
2760///
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002761static Sema::ResultTypeCompatibilityKind
Douglas Gregor33823722011-06-11 01:09:30 +00002762CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2763 ObjCInterfaceDecl *CurrentClass) {
Alp Toker314cc812014-01-25 16:55:45 +00002764 QualType ResultType = Method->getReturnType();
2765
Douglas Gregor33823722011-06-11 01:09:30 +00002766 // If an Objective-C method inherits its related result type, then its
2767 // declared result type must be compatible with its own class type. The
2768 // declared result type is compatible if:
2769 if (const ObjCObjectPointerType *ResultObjectType
2770 = ResultType->getAs<ObjCObjectPointerType>()) {
2771 // - it is id or qualified id, or
2772 if (ResultObjectType->isObjCIdType() ||
2773 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002774 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002775
2776 if (CurrentClass) {
2777 if (ObjCInterfaceDecl *ResultClass
2778 = ResultObjectType->getInterfaceDecl()) {
2779 // - it is the same as the method's class type, or
Douglas Gregor0b144e12011-12-15 00:29:59 +00002780 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002781 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002782
2783 // - it is a superclass of the method's class type
2784 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002785 return Sema::RTC_Compatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002786 }
Douglas Gregorbab8a962011-09-08 01:46:34 +00002787 } else {
2788 // Any Objective-C pointer type might be acceptable for a protocol
2789 // method; we just don't know.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002790 return Sema::RTC_Unknown;
Douglas Gregor33823722011-06-11 01:09:30 +00002791 }
2792 }
2793
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002794 return Sema::RTC_Incompatible;
Douglas Gregor33823722011-06-11 01:09:30 +00002795}
2796
John McCalld2930c22011-07-22 02:45:48 +00002797namespace {
2798/// A helper class for searching for methods which a particular method
2799/// overrides.
2800class OverrideSearch {
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002801public:
John McCalld2930c22011-07-22 02:45:48 +00002802 Sema &S;
2803 ObjCMethodDecl *Method;
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002804 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCalld2930c22011-07-22 02:45:48 +00002805 bool Recursive;
2806
2807public:
2808 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2809 Selector selector = method->getSelector();
2810
2811 // Bypass this search if we've never seen an instance/class method
2812 // with this selector before.
2813 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2814 if (it == S.MethodPool.end()) {
Axel Naumanndd433f02012-10-18 19:05:02 +00002815 if (!S.getExternalSource()) return;
Douglas Gregore1716012012-01-25 00:49:42 +00002816 S.ReadMethodPool(selector);
2817
2818 it = S.MethodPool.find(selector);
2819 if (it == S.MethodPool.end())
2820 return;
John McCalld2930c22011-07-22 02:45:48 +00002821 }
2822 ObjCMethodList &list =
2823 method->isInstanceMethod() ? it->second.first : it->second.second;
2824 if (!list.Method) return;
2825
2826 ObjCContainerDecl *container
2827 = cast<ObjCContainerDecl>(method->getDeclContext());
2828
2829 // Prevent the search from reaching this container again. This is
2830 // important with categories, which override methods from the
2831 // interface and each other.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002832 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2833 searchFromContainer(container);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002834 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2835 searchFromContainer(Interface);
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002836 } else {
2837 searchFromContainer(container);
2838 }
Douglas Gregor33823722011-06-11 01:09:30 +00002839 }
John McCalld2930c22011-07-22 02:45:48 +00002840
Daniel Dunbard6d74c32012-02-29 03:04:05 +00002841 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCalld2930c22011-07-22 02:45:48 +00002842 iterator begin() const { return Overridden.begin(); }
2843 iterator end() const { return Overridden.end(); }
2844
2845private:
2846 void searchFromContainer(ObjCContainerDecl *container) {
2847 if (container->isInvalidDecl()) return;
2848
2849 switch (container->getDeclKind()) {
2850#define OBJCCONTAINER(type, base) \
2851 case Decl::type: \
2852 searchFrom(cast<type##Decl>(container)); \
2853 break;
2854#define ABSTRACT_DECL(expansion)
2855#define DECL(type, base) \
2856 case Decl::type:
2857#include "clang/AST/DeclNodes.inc"
2858 llvm_unreachable("not an ObjC container!");
2859 }
2860 }
2861
2862 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregore6e48b12012-01-01 19:29:29 +00002863 if (!protocol->hasDefinition())
2864 return;
2865
John McCalld2930c22011-07-22 02:45:48 +00002866 // A method in a protocol declaration overrides declarations from
2867 // referenced ("parent") protocols.
2868 search(protocol->getReferencedProtocols());
2869 }
2870
2871 void searchFrom(ObjCCategoryDecl *category) {
2872 // A method in a category declaration overrides declarations from
2873 // the main class and from protocols the category references.
Douglas Gregorcf4ac442012-05-03 21:25:24 +00002874 // The main class is handled in the constructor.
John McCalld2930c22011-07-22 02:45:48 +00002875 search(category->getReferencedProtocols());
2876 }
2877
2878 void searchFrom(ObjCCategoryImplDecl *impl) {
2879 // A method in a category definition that has a category
2880 // declaration overrides declarations from the category
2881 // declaration.
2882 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2883 search(category);
Douglas Gregorc5928af2012-05-17 22:39:14 +00002884 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2885 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002886
2887 // Otherwise it overrides declarations from the class.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002888 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2889 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002890 }
2891 }
2892
2893 void searchFrom(ObjCInterfaceDecl *iface) {
2894 // A method in a class declaration overrides declarations from
Douglas Gregorc0ac7d62011-12-15 05:27:12 +00002895 if (!iface->hasDefinition())
2896 return;
2897
John McCalld2930c22011-07-22 02:45:48 +00002898 // - categories,
Aaron Ballman15063e12014-03-13 21:35:02 +00002899 for (auto *Cat : iface->known_categories())
2900 search(Cat);
John McCalld2930c22011-07-22 02:45:48 +00002901
2902 // - the super class, and
2903 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2904 search(super);
2905
2906 // - any referenced protocols.
2907 search(iface->getReferencedProtocols());
2908 }
2909
2910 void searchFrom(ObjCImplementationDecl *impl) {
2911 // A method in a class implementation overrides declarations from
2912 // the class interface.
Douglas Gregorc5928af2012-05-17 22:39:14 +00002913 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2914 search(Interface);
John McCalld2930c22011-07-22 02:45:48 +00002915 }
2916
2917
2918 void search(const ObjCProtocolList &protocols) {
2919 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2920 i != e; ++i)
2921 search(*i);
2922 }
2923
2924 void search(ObjCContainerDecl *container) {
John McCalld2930c22011-07-22 02:45:48 +00002925 // Check for a method in this container which matches this selector.
2926 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidisbd8cd3e2013-03-29 21:51:48 +00002927 Method->isInstanceMethod(),
2928 /*AllowHidden=*/true);
John McCalld2930c22011-07-22 02:45:48 +00002929
2930 // If we find one, record it and bail out.
2931 if (meth) {
2932 Overridden.insert(meth);
2933 return;
2934 }
2935
2936 // Otherwise, search for methods that a hypothetical method here
2937 // would have overridden.
2938
2939 // Note that we're now in a recursive case.
2940 Recursive = true;
2941
2942 searchFromContainer(container);
2943 }
2944};
Douglas Gregor33823722011-06-11 01:09:30 +00002945}
2946
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00002947void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2948 ObjCInterfaceDecl *CurrentClass,
2949 ResultTypeCompatibilityKind RTC) {
2950 // Search for overridden methods and merge information down from them.
2951 OverrideSearch overrides(*this, ObjCMethod);
2952 // Keep track if the method overrides any method in the class's base classes,
2953 // its protocols, or its categories' protocols; we will keep that info
2954 // in the ObjCMethodDecl.
2955 // For this info, a method in an implementation is not considered as
2956 // overriding the same method in the interface or its categories.
2957 bool hasOverriddenMethodsInBaseOrProtocol = false;
2958 for (OverrideSearch::iterator
2959 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2960 ObjCMethodDecl *overridden = *i;
2961
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00002962 if (!hasOverriddenMethodsInBaseOrProtocol) {
2963 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2964 CurrentClass != overridden->getClassInterface() ||
2965 overridden->isOverriding()) {
2966 hasOverriddenMethodsInBaseOrProtocol = true;
2967
2968 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
2969 // OverrideSearch will return as "overridden" the same method in the
2970 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
2971 // check whether a category of a base class introduced a method with the
2972 // same selector, after the interface method declaration.
2973 // To avoid unnecessary lookups in the majority of cases, we use the
2974 // extra info bits in GlobalMethodPool to check whether there were any
2975 // category methods with this selector.
2976 GlobalMethodPool::iterator It =
2977 MethodPool.find(ObjCMethod->getSelector());
2978 if (It != MethodPool.end()) {
2979 ObjCMethodList &List =
2980 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
2981 unsigned CategCount = List.getBits();
2982 if (CategCount > 0) {
2983 // If the method is in a category we'll do lookup if there were at
2984 // least 2 category methods recorded, otherwise only one will do.
2985 if (CategCount > 1 ||
2986 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
2987 OverrideSearch overrides(*this, overridden);
2988 for (OverrideSearch::iterator
2989 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
2990 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidis04703a62013-04-27 00:10:12 +00002991 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
2992 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidisc2091d52013-04-17 00:09:08 +00002993 hasOverriddenMethodsInBaseOrProtocol = true;
2994 overridden->setOverriding(true);
2995 break;
2996 }
2997 }
2998 }
2999 }
3000 }
3001 }
3002 }
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003003
3004 // Propagate down the 'related result type' bit from overridden methods.
3005 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
3006 ObjCMethod->SetRelatedResultType();
3007
3008 // Then merge the declarations.
3009 mergeObjCMethodDecls(ObjCMethod, overridden);
3010
3011 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3012 continue; // Conflicting properties are detected elsewhere.
3013
3014 // Check for overriding methods
3015 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3016 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3017 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3018 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3019
3020 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanian31a25682012-07-05 22:26:07 +00003021 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3022 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003023 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3024 E = ObjCMethod->param_end();
Douglas Gregor0bf70f42012-05-17 23:13:29 +00003025 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3026 PrevE = overridden->param_end();
3027 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003028 assert(PrevI != overridden->param_end() && "Param mismatch");
3029 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3030 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3031 // If type of argument of method in this class does not match its
3032 // respective argument type in the super class method, issue warning;
3033 if (!Context.typesAreCompatible(T1, T2)) {
3034 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3035 << T1 << T2;
3036 Diag(overridden->getLocation(), diag::note_previous_declaration);
3037 break;
3038 }
3039 }
3040 }
3041 }
3042
3043 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3044}
3045
John McCall48871652010-08-21 09:40:31 +00003046Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003047 Scope *S,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003048 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003049 tok::TokenKind MethodType,
John McCallba7bf592010-08-24 05:47:05 +00003050 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003051 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattnerda463fe2007-12-12 07:09:47 +00003052 Selector Sel,
3053 // optional arguments. The number of types/arguments is obtained
3054 // from the Sel.getNumArgs().
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003055 ObjCArgInfo *ArgInfo,
Fariborz Jahanian60462092010-04-08 00:30:06 +00003056 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattnerda463fe2007-12-12 07:09:47 +00003057 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003058 bool isVariadic, bool MethodDefinition) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003059 // Make sure we can establish a context for the method.
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003060 if (!CurContext->isObjCContainer()) {
Steve Naroff83777fe2008-02-29 21:48:07 +00003061 Diag(MethodLoc, diag::error_missing_method_context);
John McCall48871652010-08-21 09:40:31 +00003062 return 0;
Steve Naroff83777fe2008-02-29 21:48:07 +00003063 }
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003064 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3065 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003066 QualType resultDeclType;
Mike Stump11289f42009-09-09 15:08:12 +00003067
Douglas Gregorbab8a962011-09-08 01:46:34 +00003068 bool HasRelatedResultType = false;
Alp Toker314cc812014-01-25 16:55:45 +00003069 TypeSourceInfo *ReturnTInfo = 0;
Steve Naroff32606412009-02-20 22:59:16 +00003070 if (ReturnType) {
Alp Toker314cc812014-01-25 16:55:45 +00003071 resultDeclType = GetTypeFromParser(ReturnType, &ReturnTInfo);
Mike Stump11289f42009-09-09 15:08:12 +00003072
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003073 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
John McCall48871652010-08-21 09:40:31 +00003074 return 0;
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003075
Douglas Gregorbab8a962011-09-08 01:46:34 +00003076 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003077 } else { // get the type for "id".
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003078 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianb21138f2011-07-21 17:38:14 +00003079 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidisdfd65702011-10-03 06:36:36 +00003080 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianb5a52ca2011-07-21 17:00:47 +00003081 }
Mike Stump11289f42009-09-09 15:08:12 +00003082
Alp Toker314cc812014-01-25 16:55:45 +00003083 ObjCMethodDecl *ObjCMethod = ObjCMethodDecl::Create(
3084 Context, MethodLoc, EndLoc, Sel, resultDeclType, ReturnTInfo, CurContext,
3085 MethodType == tok::minus, isVariadic,
3086 /*isPropertyAccessor=*/false,
3087 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
3088 MethodDeclKind == tok::objc_optional ? ObjCMethodDecl::Optional
3089 : ObjCMethodDecl::Required,
3090 HasRelatedResultType);
Mike Stump11289f42009-09-09 15:08:12 +00003091
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003092 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump11289f42009-09-09 15:08:12 +00003093
Chris Lattner23b0faf2009-04-11 19:42:43 +00003094 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall856bbea2009-10-23 21:48:59 +00003095 QualType ArgType;
John McCallbcd03502009-12-07 02:54:59 +00003096 TypeSourceInfo *DI;
Mike Stump11289f42009-09-09 15:08:12 +00003097
David Blaikie7d170102013-05-15 07:37:26 +00003098 if (!ArgInfo[i].Type) {
John McCall856bbea2009-10-23 21:48:59 +00003099 ArgType = Context.getObjCIdType();
3100 DI = 0;
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003101 } else {
John McCall856bbea2009-10-23 21:48:59 +00003102 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003103 }
Mike Stump11289f42009-09-09 15:08:12 +00003104
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003105 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3106 LookupOrdinaryName, ForRedeclaration);
3107 LookupName(R, S);
3108 if (R.isSingleResult()) {
3109 NamedDecl *PrevDecl = R.getFoundDecl();
3110 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanianc677f692011-03-12 18:54:30 +00003111 Diag(ArgInfo[i].NameLoc,
3112 (MethodDefinition ? diag::warn_method_param_redefinition
3113 : diag::warn_method_param_declaration))
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003114 << ArgInfo[i].Name;
3115 Diag(PrevDecl->getLocation(),
3116 diag::note_previous_declaration);
3117 }
3118 }
3119
Abramo Bagnaradff19302011-03-08 08:55:46 +00003120 SourceLocation StartLoc = DI
3121 ? DI->getTypeLoc().getBeginLoc()
3122 : ArgInfo[i].NameLoc;
3123
John McCalld44f4d72011-04-23 02:46:06 +00003124 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3125 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003126 ArgType, DI, SC_None);
Mike Stump11289f42009-09-09 15:08:12 +00003127
John McCall82490832011-05-02 00:30:12 +00003128 Param->setObjCMethodScopeInfo(i);
3129
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003130 Param->setObjCDeclQualifier(
Chris Lattnerd8626fd2009-04-11 18:57:04 +00003131 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump11289f42009-09-09 15:08:12 +00003132
Chris Lattner9713a1c2009-04-11 19:34:56 +00003133 // Apply the attributes to the parameter.
Douglas Gregor758a8692009-06-17 21:51:59 +00003134 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump11289f42009-09-09 15:08:12 +00003135
Fariborz Jahanian52d02f62012-01-14 18:44:35 +00003136 if (Param->hasAttr<BlocksAttr>()) {
3137 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3138 Param->setInvalidDecl();
3139 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003140 S->AddDecl(Param);
3141 IdResolver.AddDecl(Param);
3142
Chris Lattnerc5ffed42008-04-04 06:12:32 +00003143 Params.push_back(Param);
3144 }
Fariborz Jahanianca3566f2011-02-09 22:20:01 +00003145
Fariborz Jahanian60462092010-04-08 00:30:06 +00003146 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCall48871652010-08-21 09:40:31 +00003147 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003148 QualType ArgType = Param->getType();
3149 if (ArgType.isNull())
3150 ArgType = Context.getObjCIdType();
3151 else
3152 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor84280642011-07-12 04:42:08 +00003153 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedman31a5bcc2013-06-14 21:14:10 +00003154
Fariborz Jahanian60462092010-04-08 00:30:06 +00003155 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian60462092010-04-08 00:30:06 +00003156 Params.push_back(Param);
3157 }
3158
Argyrios Kyrtzidisb8c3aaf2011-10-03 06:37:04 +00003159 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremenek1b0ea822008-01-07 19:49:32 +00003160 ObjCMethod->setObjCDeclQualifier(
3161 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbarc136e0c2008-09-26 04:12:28 +00003162
3163 if (AttrList)
Douglas Gregor758a8692009-06-17 21:51:59 +00003164 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump11289f42009-09-09 15:08:12 +00003165
Douglas Gregor87e92752010-12-21 17:34:17 +00003166 // Add the method now.
John McCalld2930c22011-07-22 02:45:48 +00003167 const ObjCMethodDecl *PrevMethod = 0;
3168 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattnerda463fe2007-12-12 07:09:47 +00003169 if (MethodType == tok::minus) {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003170 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3171 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003172 } else {
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003173 PrevMethod = ImpDecl->getClassMethod(Sel);
3174 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003175 }
Douglas Gregor33823722011-06-11 01:09:30 +00003176
Fariborz Jahanian512a4cc92011-10-22 01:21:15 +00003177 ObjCMethodDecl *IMD = 0;
3178 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3179 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3180 ObjCMethod->isInstanceMethod());
Fariborz Jahaniandb4fc282013-07-09 22:02:20 +00003181 if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
3182 !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
3183 // merge the attribute into implementation.
Aaron Ballman36a53502014-01-16 13:03:14 +00003184 ObjCMethod->addAttr(ObjCRequiresSuperAttr::CreateImplicit(Context,
3185 ObjCMethod->getLocation()));
Fariborz Jahaniandb4fc282013-07-09 22:02:20 +00003186 }
Fariborz Jahanian7e350d22013-12-17 22:44:28 +00003187 if (isa<ObjCCategoryImplDecl>(ImpDecl)) {
Fariborz Jahanianf40ef452014-01-28 22:46:29 +00003188 ObjCMethodFamily family =
3189 ObjCMethod->getSelector().getMethodFamily();
Fariborz Jahanian1b30b592013-12-18 00:52:54 +00003190 if (family == OMF_dealloc && IMD && IMD->isOverriding())
Fariborz Jahanian7e350d22013-12-17 22:44:28 +00003191 Diag(ObjCMethod->getLocation(), diag::warn_dealloc_in_category)
3192 << ObjCMethod->getDeclName();
Fariborz Jahanian7e350d22013-12-17 22:44:28 +00003193 }
Douglas Gregor87e92752010-12-21 17:34:17 +00003194 } else {
3195 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattnerda463fe2007-12-12 07:09:47 +00003196 }
John McCalld2930c22011-07-22 02:45:48 +00003197
Chris Lattnerda463fe2007-12-12 07:09:47 +00003198 if (PrevMethod) {
3199 // You can never have two method definitions with the same name.
Chris Lattner0369c572008-11-23 23:12:31 +00003200 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattnere4b95692008-11-24 03:33:13 +00003201 << ObjCMethod->getDeclName();
Chris Lattner0369c572008-11-23 23:12:31 +00003202 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian096f7c12013-05-13 17:27:00 +00003203 ObjCMethod->setInvalidDecl();
3204 return ObjCMethod;
Mike Stump11289f42009-09-09 15:08:12 +00003205 }
John McCall28a6aea2009-11-04 02:18:39 +00003206
Douglas Gregor33823722011-06-11 01:09:30 +00003207 // If this Objective-C method does not have a related result type, but we
3208 // are allowed to infer related result types, try to do so based on the
3209 // method family.
3210 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3211 if (!CurrentClass) {
3212 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3213 CurrentClass = Cat->getClassInterface();
3214 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3215 CurrentClass = Impl->getClassInterface();
3216 else if (ObjCCategoryImplDecl *CatImpl
3217 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3218 CurrentClass = CatImpl->getClassInterface();
3219 }
John McCalld2930c22011-07-22 02:45:48 +00003220
Douglas Gregorbab8a962011-09-08 01:46:34 +00003221 ResultTypeCompatibilityKind RTC
3222 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCalld2930c22011-07-22 02:45:48 +00003223
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003224 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCalld2930c22011-07-22 02:45:48 +00003225
John McCall31168b02011-06-15 23:02:42 +00003226 bool ARCError = false;
David Blaikiebbafb8a2012-03-11 07:00:24 +00003227 if (getLangOpts().ObjCAutoRefCount)
John McCalle48f3892013-04-04 01:38:37 +00003228 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCall31168b02011-06-15 23:02:42 +00003229
Douglas Gregorbab8a962011-09-08 01:46:34 +00003230 // Infer the related result type when possible.
Argyrios Kyrtzidis08f96a92012-05-09 16:12:57 +00003231 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregorbab8a962011-09-08 01:46:34 +00003232 !ObjCMethod->hasRelatedResultType() &&
3233 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor33823722011-06-11 01:09:30 +00003234 bool InferRelatedResultType = false;
3235 switch (ObjCMethod->getMethodFamily()) {
3236 case OMF_None:
3237 case OMF_copy:
3238 case OMF_dealloc:
Nico Weber1fb82662011-08-28 22:35:17 +00003239 case OMF_finalize:
Douglas Gregor33823722011-06-11 01:09:30 +00003240 case OMF_mutableCopy:
3241 case OMF_release:
3242 case OMF_retainCount:
Fariborz Jahanianb7a77362011-07-05 22:38:59 +00003243 case OMF_performSelector:
Douglas Gregor33823722011-06-11 01:09:30 +00003244 break;
3245
3246 case OMF_alloc:
3247 case OMF_new:
3248 InferRelatedResultType = ObjCMethod->isClassMethod();
3249 break;
3250
3251 case OMF_init:
3252 case OMF_autorelease:
3253 case OMF_retain:
3254 case OMF_self:
3255 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3256 break;
3257 }
3258
John McCalld2930c22011-07-22 02:45:48 +00003259 if (InferRelatedResultType)
Douglas Gregor33823722011-06-11 01:09:30 +00003260 ObjCMethod->SetRelatedResultType();
Douglas Gregor33823722011-06-11 01:09:30 +00003261 }
Dmitri Gribenkof26054f2012-07-11 21:38:39 +00003262
3263 ActOnDocumentableDecl(ObjCMethod);
3264
John McCall48871652010-08-21 09:40:31 +00003265 return ObjCMethod;
Chris Lattnerda463fe2007-12-12 07:09:47 +00003266}
3267
Chris Lattner438e5012008-12-17 07:13:27 +00003268bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanianf36734d2011-08-22 18:34:22 +00003269 // Following is also an error. But it is caused by a missing @end
3270 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003271 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003272 return false;
Argyrios Kyrtzidis822c4332012-03-23 23:24:23 +00003273
3274 // If we switched context to translation unit while we are still lexically in
3275 // an objc container, it means the parser missed emitting an error.
3276 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3277 return false;
Fariborz Jahanian8d382dc2011-08-22 15:54:49 +00003278
Anders Carlssona6b508a2008-11-04 16:57:32 +00003279 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3280 D->setInvalidDecl();
Mike Stump11289f42009-09-09 15:08:12 +00003281
Anders Carlssona6b508a2008-11-04 16:57:32 +00003282 return true;
3283}
Chris Lattner438e5012008-12-17 07:13:27 +00003284
James Dennett634962f2012-06-14 21:40:34 +00003285/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattner438e5012008-12-17 07:13:27 +00003286/// instance variables of ClassName into Decls.
John McCall48871652010-08-21 09:40:31 +00003287void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattner438e5012008-12-17 07:13:27 +00003288 IdentifierInfo *ClassName,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003289 SmallVectorImpl<Decl*> &Decls) {
Chris Lattner438e5012008-12-17 07:13:27 +00003290 // Check that ClassName is a valid class
Douglas Gregorb2ccf012010-04-15 22:33:43 +00003291 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattner438e5012008-12-17 07:13:27 +00003292 if (!Class) {
3293 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3294 return;
3295 }
John McCall5fb5df92012-06-20 06:18:46 +00003296 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianece1b2b2009-04-21 20:28:41 +00003297 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3298 return;
3299 }
Mike Stump11289f42009-09-09 15:08:12 +00003300
Chris Lattner438e5012008-12-17 07:13:27 +00003301 // Collect the instance variables
Jordy Rosea91768e2011-07-22 02:08:32 +00003302 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003303 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003304 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003305 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosea91768e2011-07-22 02:08:32 +00003306 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCall48871652010-08-21 09:40:31 +00003307 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaradff19302011-03-08 08:55:46 +00003308 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3309 /*FIXME: StartL=*/ID->getLocation(),
3310 ID->getLocation(),
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003311 ID->getIdentifier(), ID->getType(),
3312 ID->getBitWidth());
John McCall48871652010-08-21 09:40:31 +00003313 Decls.push_back(FD);
Fariborz Jahanian7dae1142009-06-04 17:08:55 +00003314 }
Mike Stump11289f42009-09-09 15:08:12 +00003315
Chris Lattner438e5012008-12-17 07:13:27 +00003316 // Introduce all of these fields into the appropriate scope.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003317 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattner438e5012008-12-17 07:13:27 +00003318 D != Decls.end(); ++D) {
John McCall48871652010-08-21 09:40:31 +00003319 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikiebbafb8a2012-03-11 07:00:24 +00003320 if (getLangOpts().CPlusPlus)
Chris Lattner438e5012008-12-17 07:13:27 +00003321 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCall48871652010-08-21 09:40:31 +00003322 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +00003323 Record->addDecl(FD);
Chris Lattner438e5012008-12-17 07:13:27 +00003324 }
3325}
3326
Douglas Gregorf3564192010-04-26 17:32:49 +00003327/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaradff19302011-03-08 08:55:46 +00003328VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3329 SourceLocation StartLoc,
3330 SourceLocation IdLoc,
3331 IdentifierInfo *Id,
Douglas Gregorf3564192010-04-26 17:32:49 +00003332 bool Invalid) {
3333 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3334 // duration shall not be qualified by an address-space qualifier."
3335 // Since all parameters have automatic store duration, they can not have
3336 // an address space.
3337 if (T.getAddressSpace() != 0) {
Abramo Bagnaradff19302011-03-08 08:55:46 +00003338 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregorf3564192010-04-26 17:32:49 +00003339 Invalid = true;
3340 }
3341
3342 // An @catch parameter must be an unqualified object pointer type;
3343 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3344 if (Invalid) {
3345 // Don't do any further checking.
Douglas Gregorf4e837f2010-04-26 17:57:08 +00003346 } else if (T->isDependentType()) {
3347 // Okay: we don't know what this type will instantiate to.
Douglas Gregorf3564192010-04-26 17:32:49 +00003348 } else if (!T->isObjCObjectPointerType()) {
3349 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003350 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregorf3564192010-04-26 17:32:49 +00003351 } else if (T->isObjCQualifiedIdType()) {
3352 Invalid = true;
Abramo Bagnaradff19302011-03-08 08:55:46 +00003353 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregorf3564192010-04-26 17:32:49 +00003354 }
3355
Abramo Bagnaradff19302011-03-08 08:55:46 +00003356 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindola6ae7e502013-04-03 19:27:57 +00003357 T, TInfo, SC_None);
Douglas Gregor3f324d562010-05-03 18:51:14 +00003358 New->setExceptionVariable(true);
3359
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003360 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003361 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor8ca0c642011-12-10 01:22:52 +00003362 Invalid = true;
3363
Douglas Gregorf3564192010-04-26 17:32:49 +00003364 if (Invalid)
3365 New->setInvalidDecl();
3366 return New;
3367}
3368
John McCall48871652010-08-21 09:40:31 +00003369Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003370 const DeclSpec &DS = D.getDeclSpec();
3371
3372 // We allow the "register" storage class on exception variables because
3373 // GCC did, but we drop it completely. Any other storage class is an error.
3374 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3375 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3376 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithb4a9e862013-04-12 22:46:28 +00003377 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregorf3564192010-04-26 17:32:49 +00003378 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithb4a9e862013-04-12 22:46:28 +00003379 << DeclSpec::getSpecifierName(SCS);
3380 }
3381 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3382 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3383 diag::err_invalid_thread)
3384 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregorf3564192010-04-26 17:32:49 +00003385 D.getMutableDeclSpec().ClearStorageClassSpecs();
3386
Richard Smithb1402ae2013-03-18 22:52:47 +00003387 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregorf3564192010-04-26 17:32:49 +00003388
3389 // Check that there are no default arguments inside the type of this
3390 // exception object (C++ only).
David Blaikiebbafb8a2012-03-11 07:00:24 +00003391 if (getLangOpts().CPlusPlus)
Douglas Gregorf3564192010-04-26 17:32:49 +00003392 CheckExtraCXXDefaultArguments(D);
3393
Argyrios Kyrtzidisef7022f2011-06-28 03:01:15 +00003394 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCall8cb7bdf2010-06-04 23:28:52 +00003395 QualType ExceptionType = TInfo->getType();
Douglas Gregorf3564192010-04-26 17:32:49 +00003396
Abramo Bagnaradff19302011-03-08 08:55:46 +00003397 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3398 D.getSourceRange().getBegin(),
3399 D.getIdentifierLoc(),
3400 D.getIdentifier(),
Douglas Gregorf3564192010-04-26 17:32:49 +00003401 D.isInvalidType());
3402
3403 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3404 if (D.getCXXScopeSpec().isSet()) {
3405 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3406 << D.getCXXScopeSpec().getRange();
3407 New->setInvalidDecl();
3408 }
3409
3410 // Add the parameter declaration into this scope.
John McCall48871652010-08-21 09:40:31 +00003411 S->AddDecl(New);
Douglas Gregorf3564192010-04-26 17:32:49 +00003412 if (D.getIdentifier())
3413 IdResolver.AddDecl(New);
3414
3415 ProcessDeclAttributes(S, New, D);
3416
3417 if (New->hasAttr<BlocksAttr>())
3418 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCall48871652010-08-21 09:40:31 +00003419 return New;
Douglas Gregore11ee112010-04-23 23:01:43 +00003420}
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003421
3422/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003423/// initialization.
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003424void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003425 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003426 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3427 Iv= Iv->getNextIvar()) {
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003428 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor527786e2010-05-20 02:24:22 +00003429 if (QT->isRecordType())
Fariborz Jahaniana50b3a22010-08-20 21:21:08 +00003430 Ivars.push_back(Iv);
Fariborz Jahanian38b77a92010-04-27 17:18:58 +00003431 }
3432}
Fariborz Jahanianc83726e2010-04-28 16:11:27 +00003433
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003434void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor72e357f2011-07-28 14:54:22 +00003435 // Load referenced selectors from the external source.
3436 if (ExternalSource) {
3437 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3438 ExternalSource->ReadReferencedSelectors(Sels);
3439 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3440 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3441 }
3442
Fariborz Jahanianc9b7c202011-02-04 23:19:27 +00003443 // Warning will be issued only when selector table is
3444 // generated (which means there is at lease one implementation
3445 // in the TU). This is to match gcc's behavior.
3446 if (ReferencedSelectors.empty() ||
3447 !Context.AnyObjCImplementation())
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +00003448 return;
3449 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3450 ReferencedSelectors.begin(),
3451 E = ReferencedSelectors.end(); S != E; ++S) {
3452 Selector Sel = (*S).first;
3453 if (!LookupImplementedMethodInGlobalPool(Sel))
3454 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3455 }
3456 return;
3457}
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003458
3459ObjCIvarDecl *
3460Sema::GetIvarBackingPropertyAccessor(const ObjCMethodDecl *Method,
3461 const ObjCPropertyDecl *&PDecl) const {
Fariborz Jahanian1cc7ae12014-01-02 17:24:32 +00003462 if (Method->isClassMethod())
3463 return 0;
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003464 const ObjCInterfaceDecl *IDecl = Method->getClassInterface();
3465 if (!IDecl)
3466 return 0;
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003467 Method = IDecl->lookupMethod(Method->getSelector(), /*isInstance=*/true,
3468 /*shallowCategoryLookup=*/false,
3469 /*followSuper=*/false);
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003470 if (!Method || !Method->isPropertyAccessor())
3471 return 0;
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003472 if ((PDecl = Method->findPropertyDecl()))
Fariborz Jahanian122d94f2014-01-27 22:27:43 +00003473 if (ObjCIvarDecl *IV = PDecl->getPropertyIvarDecl()) {
3474 // property backing ivar must belong to property's class
3475 // or be a private ivar in class's implementation.
3476 // FIXME. fix the const-ness issue.
3477 IV = const_cast<ObjCInterfaceDecl *>(IDecl)->lookupInstanceVariable(
3478 IV->getIdentifier());
3479 return IV;
3480 }
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003481 return 0;
3482}
3483
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003484namespace {
3485 /// Used by Sema::DiagnoseUnusedBackingIvarInAccessor to check if a property
3486 /// accessor references the backing ivar.
Argyrios Kyrtzidis98045c12014-01-03 19:53:09 +00003487 class UnusedBackingIvarChecker :
3488 public DataRecursiveASTVisitor<UnusedBackingIvarChecker> {
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003489 public:
3490 Sema &S;
3491 const ObjCMethodDecl *Method;
3492 const ObjCIvarDecl *IvarD;
3493 bool AccessedIvar;
3494 bool InvokedSelfMethod;
3495
3496 UnusedBackingIvarChecker(Sema &S, const ObjCMethodDecl *Method,
3497 const ObjCIvarDecl *IvarD)
3498 : S(S), Method(Method), IvarD(IvarD),
3499 AccessedIvar(false), InvokedSelfMethod(false) {
3500 assert(IvarD);
3501 }
3502
3503 bool VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) {
3504 if (E->getDecl() == IvarD) {
3505 AccessedIvar = true;
3506 return false;
3507 }
3508 return true;
3509 }
3510
3511 bool VisitObjCMessageExpr(ObjCMessageExpr *E) {
3512 if (E->getReceiverKind() == ObjCMessageExpr::Instance &&
3513 S.isSelfExpr(E->getInstanceReceiver(), Method)) {
3514 InvokedSelfMethod = true;
3515 }
3516 return true;
3517 }
3518 };
3519}
3520
3521void Sema::DiagnoseUnusedBackingIvarInAccessor(Scope *S,
3522 const ObjCImplementationDecl *ImplD) {
3523 if (S->hasUnrecoverableErrorOccurred())
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003524 return;
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003525
Aaron Ballmanf26acce2014-03-13 19:50:17 +00003526 for (const auto *CurMethod : ImplD->instance_methods()) {
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003527 unsigned DIAG = diag::warn_unused_property_backing_ivar;
3528 SourceLocation Loc = CurMethod->getLocation();
3529 if (Diags.getDiagnosticLevel(DIAG, Loc) == DiagnosticsEngine::Ignored)
3530 continue;
3531
3532 const ObjCPropertyDecl *PDecl;
3533 const ObjCIvarDecl *IV = GetIvarBackingPropertyAccessor(CurMethod, PDecl);
3534 if (!IV)
3535 continue;
3536
3537 UnusedBackingIvarChecker Checker(*this, CurMethod, IV);
3538 Checker.TraverseStmt(CurMethod->getBody());
3539 if (Checker.AccessedIvar)
3540 continue;
3541
Fariborz Jahanian5b3105d2014-01-02 22:42:09 +00003542 // Do not issue this warning if backing ivar is used somewhere and accessor
Argyrios Kyrtzidis2080d902014-01-03 18:32:18 +00003543 // implementation makes a self call. This is to prevent false positive in
3544 // cases where the ivar is accessed by another method that the accessor
3545 // delegates to.
3546 if (!IV->isReferenced() || !Checker.InvokedSelfMethod) {
Argyrios Kyrtzidisd8a35322014-01-03 19:39:23 +00003547 Diag(Loc, DIAG) << IV;
Fariborz Jahanian5b3105d2014-01-02 22:42:09 +00003548 Diag(PDecl->getLocation(), diag::note_property_declare);
3549 }
Fariborz Jahanian5e3429c2013-10-25 21:44:50 +00003550 }
3551}