blob: 14f8658face69fe63d73feb029f348de267c64e4 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-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 Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
John McCallf85e1932011-06-15 23:02:42 +000015#include "clang/AST/ASTConsumer.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/ASTMutationListener.h"
18#include "clang/AST/DeclObjC.h"
Steve Naroffca331292009-03-03 14:49:36 +000019#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000020#include "clang/AST/ExprObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/Basic/SourceManager.h"
Patrick Beardb2f68202012-04-06 18:12:22 +000022#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/ExternalSemaSource.h"
25#include "clang/Sema/Lookup.h"
26#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
John McCall50df6ae2010-08-25 07:03:20 +000028#include "llvm/ADT/DenseSet.h"
29
Chris Lattner4d391482007-12-12 07:09:47 +000030using namespace clang;
31
John McCallf85e1932011-06-15 23:02:42 +000032/// Check whether the given method, which must be in the 'init'
33/// family, is a valid member of that family.
34///
35/// \param receiverTypeIfCall - if null, check this as if declaring it;
36/// if non-null, check this as if making a call to it with the given
37/// receiver type
38///
39/// \return true to indicate that there was an error and appropriate
40/// actions were taken
41bool Sema::checkInitMethod(ObjCMethodDecl *method,
42 QualType receiverTypeIfCall) {
43 if (method->isInvalidDecl()) return true;
44
45 // This castAs is safe: methods that don't return an object
46 // pointer won't be inferred as inits and will reject an explicit
47 // objc_method_family(init).
48
49 // We ignore protocols here. Should we? What about Class?
50
51 const ObjCObjectType *result = method->getResultType()
52 ->castAs<ObjCObjectPointerType>()->getObjectType();
53
54 if (result->isObjCId()) {
55 return false;
56 } else if (result->isObjCClass()) {
57 // fall through: always an error
58 } else {
59 ObjCInterfaceDecl *resultClass = result->getInterface();
60 assert(resultClass && "unexpected object type!");
61
62 // It's okay for the result type to still be a forward declaration
63 // if we're checking an interface declaration.
Douglas Gregor7723fec2011-12-15 20:29:51 +000064 if (!resultClass->hasDefinition()) {
John McCallf85e1932011-06-15 23:02:42 +000065 if (receiverTypeIfCall.isNull() &&
66 !isa<ObjCImplementationDecl>(method->getDeclContext()))
67 return false;
68
69 // Otherwise, we try to compare class types.
70 } else {
71 // If this method was declared in a protocol, we can't check
72 // anything unless we have a receiver type that's an interface.
73 const ObjCInterfaceDecl *receiverClass = 0;
74 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
75 if (receiverTypeIfCall.isNull())
76 return false;
77
78 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
79 ->getInterfaceDecl();
80
81 // This can be null for calls to e.g. id<Foo>.
82 if (!receiverClass) return false;
83 } else {
84 receiverClass = method->getClassInterface();
85 assert(receiverClass && "method not associated with a class!");
86 }
87
88 // If either class is a subclass of the other, it's fine.
89 if (receiverClass->isSuperClassOf(resultClass) ||
90 resultClass->isSuperClassOf(receiverClass))
91 return false;
92 }
93 }
94
95 SourceLocation loc = method->getLocation();
96
97 // If we're in a system header, and this is not a call, just make
98 // the method unusable.
99 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
100 method->addAttr(new (Context) UnavailableAttr(loc, Context,
101 "init method returns a type unrelated to its receiver type"));
102 return true;
103 }
104
105 // Otherwise, it's an error.
106 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
107 method->setInvalidDecl();
108 return true;
109}
110
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000111void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregorf4d918f2013-01-15 22:43:08 +0000112 const ObjCMethodDecl *Overridden) {
Douglas Gregor926df6c2011-06-11 01:09:30 +0000113 if (Overridden->hasRelatedResultType() &&
114 !NewMethod->hasRelatedResultType()) {
115 // This can only happen when the method follows a naming convention that
116 // implies a related result type, and the original (overridden) method has
117 // a suitable return type, but the new (overriding) method does not have
118 // a suitable return type.
119 QualType ResultType = NewMethod->getResultType();
120 SourceRange ResultTypeRange;
121 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000122 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000123 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
124
125 // Figure out which class this method is part of, if any.
126 ObjCInterfaceDecl *CurrentClass
127 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
128 if (!CurrentClass) {
129 DeclContext *DC = NewMethod->getDeclContext();
130 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
131 CurrentClass = Cat->getClassInterface();
132 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
133 CurrentClass = Impl->getClassInterface();
134 else if (ObjCCategoryImplDecl *CatImpl
135 = dyn_cast<ObjCCategoryImplDecl>(DC))
136 CurrentClass = CatImpl->getClassInterface();
137 }
138
139 if (CurrentClass) {
140 Diag(NewMethod->getLocation(),
141 diag::warn_related_result_type_compatibility_class)
142 << Context.getObjCInterfaceType(CurrentClass)
143 << ResultType
144 << ResultTypeRange;
145 } else {
146 Diag(NewMethod->getLocation(),
147 diag::warn_related_result_type_compatibility_protocol)
148 << ResultType
149 << ResultTypeRange;
150 }
151
Douglas Gregore97179c2011-09-08 01:46:34 +0000152 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
153 Diag(Overridden->getLocation(),
John McCall7cca8212013-03-19 07:04:25 +0000154 diag::note_related_result_type_family)
155 << /*overridden method*/ 0
Douglas Gregore97179c2011-09-08 01:46:34 +0000156 << Family;
157 else
158 Diag(Overridden->getLocation(),
159 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000160 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000161 if (getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000162 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
163 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
164 Diag(NewMethod->getLocation(),
165 diag::err_nsreturns_retained_attribute_mismatch) << 1;
166 Diag(Overridden->getLocation(), diag::note_previous_decl)
167 << "method";
168 }
169 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
170 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
171 Diag(NewMethod->getLocation(),
172 diag::err_nsreturns_retained_attribute_mismatch) << 0;
173 Diag(Overridden->getLocation(), diag::note_previous_decl)
174 << "method";
175 }
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000176 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
177 oe = Overridden->param_end();
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000178 for (ObjCMethodDecl::param_iterator
179 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000180 ni != ne && oi != oe; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000181 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000182 ParmVarDecl *newDecl = (*ni);
183 if (newDecl->hasAttr<NSConsumedAttr>() !=
184 oldDecl->hasAttr<NSConsumedAttr>()) {
185 Diag(newDecl->getLocation(),
186 diag::err_nsconsumed_attribute_mismatch);
187 Diag(oldDecl->getLocation(), diag::note_previous_decl)
188 << "parameter";
189 }
190 }
191 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000192}
193
John McCallf85e1932011-06-15 23:02:42 +0000194/// \brief Check a method declaration for compatibility with the Objective-C
195/// ARC conventions.
John McCallb8463812013-04-04 01:38:37 +0000196bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) {
John McCallf85e1932011-06-15 23:02:42 +0000197 ObjCMethodFamily family = method->getMethodFamily();
198 switch (family) {
199 case OMF_None:
Nico Weber80cb6e62011-08-28 22:35:17 +0000200 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000201 case OMF_retain:
202 case OMF_release:
203 case OMF_autorelease:
204 case OMF_retainCount:
205 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000206 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000207 return false;
208
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000209 case OMF_dealloc:
John McCallb8463812013-04-04 01:38:37 +0000210 if (!Context.hasSameType(method->getResultType(), Context.VoidTy)) {
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000211 SourceRange ResultTypeRange;
212 if (const TypeSourceInfo *ResultTypeInfo
213 = method->getResultTypeSourceInfo())
214 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
215 if (ResultTypeRange.isInvalid())
John McCallb8463812013-04-04 01:38:37 +0000216 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000217 << method->getResultType()
218 << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
219 else
John McCallb8463812013-04-04 01:38:37 +0000220 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000221 << method->getResultType()
222 << FixItHint::CreateReplacement(ResultTypeRange, "void");
223 return true;
224 }
225 return false;
226
John McCallf85e1932011-06-15 23:02:42 +0000227 case OMF_init:
228 // If the method doesn't obey the init rules, don't bother annotating it.
John McCallb8463812013-04-04 01:38:37 +0000229 if (checkInitMethod(method, QualType()))
John McCallf85e1932011-06-15 23:02:42 +0000230 return true;
231
John McCallb8463812013-04-04 01:38:37 +0000232 method->addAttr(new (Context) NSConsumesSelfAttr(SourceLocation(),
233 Context));
John McCallf85e1932011-06-15 23:02:42 +0000234
235 // Don't add a second copy of this attribute, but otherwise don't
236 // let it be suppressed.
237 if (method->hasAttr<NSReturnsRetainedAttr>())
238 return false;
239 break;
240
241 case OMF_alloc:
242 case OMF_copy:
243 case OMF_mutableCopy:
244 case OMF_new:
245 if (method->hasAttr<NSReturnsRetainedAttr>() ||
246 method->hasAttr<NSReturnsNotRetainedAttr>() ||
247 method->hasAttr<NSReturnsAutoreleasedAttr>())
248 return false;
249 break;
250 }
251
John McCallb8463812013-04-04 01:38:37 +0000252 method->addAttr(new (Context) NSReturnsRetainedAttr(SourceLocation(),
253 Context));
John McCallf85e1932011-06-15 23:02:42 +0000254 return false;
255}
256
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000257static void DiagnoseObjCImplementedDeprecations(Sema &S,
258 NamedDecl *ND,
259 SourceLocation ImplLoc,
260 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000261 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000262 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000263 if (select == 0)
Ted Kremenek3306ec12012-02-27 22:55:11 +0000264 S.Diag(ND->getLocation(), diag::note_method_declared_at)
265 << ND->getDeclName();
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000266 else
267 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
268 }
269}
270
Fariborz Jahanian140ab232011-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 Jahaniana1a32f72012-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 Jahaniana1a32f72012-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 Jahanian8c6cb462012-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 McCalld226f652010-08-21 09:40:31 +0000308 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000309
Steve Naroff394f3f42008-07-25 17:57:26 +0000310 // If we don't have a valid method decl, simply return.
311 if (!MDecl)
312 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000313
Chris Lattner4d391482007-12-12 07:09:47 +0000314 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000315 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000316 PushFunctionScope();
317
Chris Lattner4d391482007-12-12 07:09:47 +0000318 // Create Decl objects for each parameter, entrring them in the scope for
319 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000320
321 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000322 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Daniel Dunbar451318c2008-08-26 06:07:48 +0000324 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
325 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000326
Reid Kleckner8c0501c2013-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 Lattner8123a952008-04-10 02:22:51 +0000331 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000332 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000333 E = MDecl->param_end(); PI != E; ++PI) {
334 ParmVarDecl *Param = (*PI);
335 if (!Param->isInvalidDecl() &&
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000336 getLangOpts().ObjCAutoRefCount &&
337 !HasExplicitOwnershipAttr(*this, Param))
338 Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
339 Param->getType();
Fariborz Jahanian918546c2012-08-30 23:56:02 +0000340
Chris Lattner89951a82009-02-20 18:43:26 +0000341 if ((*PI)->getIdentifier())
342 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000343 }
John McCallf85e1932011-06-15 23:02:42 +0000344
345 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000346 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000347 switch (MDecl->getMethodFamily()) {
348 case OMF_retain:
349 case OMF_retainCount:
350 case OMF_release:
351 case OMF_autorelease:
352 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
Fariborz Jahanianb8ed0712013-05-16 19:08:44 +0000353 << 0 << MDecl->getSelector();
John McCallf85e1932011-06-15 23:02:42 +0000354 break;
355
356 case OMF_None:
357 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000358 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000359 case OMF_alloc:
360 case OMF_init:
361 case OMF_mutableCopy:
362 case OMF_copy:
363 case OMF_new:
364 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000365 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000366 break;
367 }
368 }
369
Nico Weber9a1ecf02011-08-22 17:25:57 +0000370 // Warn on deprecated methods under -Wdeprecated-implementations,
371 // and prepare for warning on missing super calls.
372 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian84101132012-09-07 23:46:23 +0000373 ObjCMethodDecl *IMD =
374 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
375
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000376 if (IMD) {
377 ObjCImplDecl *ImplDeclOfMethodDef =
378 dyn_cast<ObjCImplDecl>(MDecl->getDeclContext());
379 ObjCContainerDecl *ContDeclOfMethodDecl =
380 dyn_cast<ObjCContainerDecl>(IMD->getDeclContext());
381 ObjCImplDecl *ImplDeclOfMethodDecl = 0;
382 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
383 ImplDeclOfMethodDecl = OID->getImplementation();
384 else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl))
385 ImplDeclOfMethodDecl = CD->getImplementation();
386 // No need to issue deprecated warning if deprecated mehod in class/category
387 // is being implemented in its own implementation (no overriding is involved).
388 if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef)
389 DiagnoseObjCImplementedDeprecations(*this,
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000390 dyn_cast<NamedDecl>(IMD),
391 MDecl->getLocation(), 0);
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000392 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000393
Nico Weber80cb6e62011-08-28 22:35:17 +0000394 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000395 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
396 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
397 // Only do this if the current class actually has a superclass.
Jordan Rose41f3f3a2013-03-05 01:27:54 +0000398 if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) {
Jordan Rose535a5d02012-10-19 16:05:26 +0000399 ObjCMethodFamily Family = MDecl->getMethodFamily();
400 if (Family == OMF_dealloc) {
401 if (!(getLangOpts().ObjCAutoRefCount ||
402 getLangOpts().getGC() == LangOptions::GCOnly))
403 getCurFunction()->ObjCShouldCallSuper = true;
404
405 } else if (Family == OMF_finalize) {
406 if (Context.getLangOpts().getGC() != LangOptions::NonGC)
407 getCurFunction()->ObjCShouldCallSuper = true;
408
Fariborz Jahanian38b3bd82013-07-09 22:02:20 +0000409 } else if (MDecl->hasAttr<ObjCRequiresSuperAttr>())
410 getCurFunction()->ObjCShouldCallSuper = true;
411 else {
Jordan Rose535a5d02012-10-19 16:05:26 +0000412 const ObjCMethodDecl *SuperMethod =
Jordan Rose41f3f3a2013-03-05 01:27:54 +0000413 SuperClass->lookupMethod(MDecl->getSelector(),
414 MDecl->isInstanceMethod());
Jordan Rose535a5d02012-10-19 16:05:26 +0000415 getCurFunction()->ObjCShouldCallSuper =
416 (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000417 }
Nico Weber80cb6e62011-08-28 22:35:17 +0000418 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000419 }
Chris Lattner4d391482007-12-12 07:09:47 +0000420}
421
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000422namespace {
423
424// Callback to only accept typo corrections that are Objective-C classes.
425// If an ObjCInterfaceDecl* is given to the constructor, then the validation
426// function will reject corrections to that class.
427class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
428 public:
429 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
430 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
431 : CurrentIDecl(IDecl) {}
432
433 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
434 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
435 return ID && !declaresSameEntity(ID, CurrentIDecl);
436 }
437
438 private:
439 ObjCInterfaceDecl *CurrentIDecl;
440};
441
442}
443
John McCalld226f652010-08-21 09:40:31 +0000444Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000445ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
446 IdentifierInfo *ClassName, SourceLocation ClassLoc,
447 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000448 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000449 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000450 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000451 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Chris Lattner4d391482007-12-12 07:09:47 +0000453 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000454 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000455 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000456
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000457 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000458 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000459 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000460 }
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Douglas Gregor7723fec2011-12-15 20:29:51 +0000462 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000463 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +0000464
465 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
466 // A previous decl with a different name is because of
467 // @compatibility_alias, for example:
468 // \code
469 // @class NewImage;
470 // @compatibility_alias OldImage NewImage;
471 // \endcode
472 // A lookup for 'OldImage' will return the 'NewImage' decl.
473 //
474 // In such a case use the real declaration name, instead of the alias one,
475 // otherwise we will break IdentifierResolver and redecls-chain invariants.
476 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
477 // has been aliased.
478 ClassName = PrevIDecl->getIdentifier();
479 }
480
Douglas Gregor7723fec2011-12-15 20:29:51 +0000481 ObjCInterfaceDecl *IDecl
482 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000483 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000484
Douglas Gregor7723fec2011-12-15 20:29:51 +0000485 if (PrevIDecl) {
486 // Class already seen. Was it a definition?
487 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
488 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
489 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000490 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000491 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000492 }
Chris Lattner4d391482007-12-12 07:09:47 +0000493 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000494
495 if (AttrList)
496 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
497 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Douglas Gregor7723fec2011-12-15 20:29:51 +0000499 // Start the definition of this class. If we're in a redefinition case, there
500 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000501 if (!IDecl->hasDefinition())
502 IDecl->startDefinition();
503
Chris Lattner4d391482007-12-12 07:09:47 +0000504 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000505 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000506 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
507 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000508
509 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000510 // Try to correct for a typo in the superclass name without correcting
511 // to the class we're defining.
512 ObjCInterfaceValidatorCCC Validator(IDecl);
513 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000514 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000515 NULL, Validator)) {
Richard Smith2d670972013-08-17 00:46:16 +0000516 diagnoseTypo(Corrected, PDiag(diag::err_undef_superclass_suggest)
517 << SuperName << ClassName);
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000518 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000519 }
520 }
521
Douglas Gregor60ef3082011-12-15 00:29:59 +0000522 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000523 Diag(SuperLoc, diag::err_recursive_superclass)
524 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000525 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000526 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000527 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000528 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000529
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000530 // Diagnose classes that inherit from deprecated classes.
531 if (SuperClassDecl)
532 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000534 if (PrevDecl && SuperClassDecl == 0) {
535 // The previous declaration was not a class decl. Check if we have a
536 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000537 if (const TypedefNameDecl *TDecl =
538 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000539 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000540 if (T->isObjCObjectType()) {
Fariborz Jahanian740991b2013-04-04 18:45:52 +0000541 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000542 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian740991b2013-04-04 18:45:52 +0000543 // This handles the following case:
544 // @interface NewI @end
545 // typedef NewI DeprI __attribute__((deprecated("blah")))
546 // @interface SI : DeprI /* warn here */ @end
547 (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc);
548 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000549 }
550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000552 // This handles the following case:
553 //
554 // typedef int SuperClass;
555 // @interface MyClass : SuperClass {} @end
556 //
557 if (!SuperClassDecl) {
558 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
559 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000560 }
561 }
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Richard Smith162e1c12011-04-15 14:24:37 +0000563 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000564 if (!SuperClassDecl)
565 Diag(SuperLoc, diag::err_undef_superclass)
566 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000567 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000568 Context.getObjCInterfaceType(SuperClassDecl),
569 diag::err_forward_superclass,
570 SuperClassDecl->getDeclName(),
571 ClassName,
572 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000573 SuperClassDecl = 0;
574 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000575 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000576 IDecl->setSuperClass(SuperClassDecl);
577 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000578 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000579 }
Chris Lattner4d391482007-12-12 07:09:47 +0000580 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000581 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Sebastian Redl0b17c612010-08-13 00:28:03 +0000584 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000585 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000586 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000587 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000588 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000589 }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Anders Carlsson15281452008-11-04 16:57:32 +0000591 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000592 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000593}
594
Fariborz Jahaniana924f842013-09-25 19:36:32 +0000595/// ActOnTypedefedProtocols - this action finds protocol list as part of the
596/// typedef'ed use for a qualified super class and adds them to the list
597/// of the protocols.
598void Sema::ActOnTypedefedProtocols(SmallVectorImpl<Decl *> &ProtocolRefs,
599 IdentifierInfo *SuperName,
600 SourceLocation SuperLoc) {
601 if (!SuperName)
602 return;
603 NamedDecl* IDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
604 LookupOrdinaryName);
605 if (!IDecl)
606 return;
607
608 if (const TypedefNameDecl *TDecl = dyn_cast_or_null<TypedefNameDecl>(IDecl)) {
609 QualType T = TDecl->getUnderlyingType();
610 if (T->isObjCObjectType())
611 if (const ObjCObjectType *OPT = T->getAs<ObjCObjectType>())
612 for (ObjCObjectType::qual_iterator I = OPT->qual_begin(),
613 E = OPT->qual_end(); I != E; ++I)
614 ProtocolRefs.push_back(*I);
615 }
616}
617
Richard Smithde01b7a2012-08-08 23:32:13 +0000618/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000619/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000620Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
621 IdentifierInfo *AliasName,
622 SourceLocation AliasLocation,
623 IdentifierInfo *ClassName,
624 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000625 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000626 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000627 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000628 if (ADecl) {
Eli Friedman104f96b2013-06-21 01:49:53 +0000629 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000630 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000631 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000632 }
633 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000634 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000635 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000636 if (const TypedefNameDecl *TDecl =
637 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000638 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000639 if (T->isObjCObjectType()) {
640 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000641 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000642 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000643 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000644 }
645 }
646 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000647 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
648 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000649 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000650 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000651 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000652 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000653 }
Mike Stump1eb44332009-09-09 15:08:12 +0000654
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000655 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000656 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000657 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Anders Carlsson15281452008-11-04 16:57:32 +0000659 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000660 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000661
John McCalld226f652010-08-21 09:40:31 +0000662 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000663}
664
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000665bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000666 IdentifierInfo *PName,
667 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000668 const ObjCList<ObjCProtocolDecl> &PList) {
669
670 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000671 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
672 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000673 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
674 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000675 if (PDecl->getIdentifier() == PName) {
676 Diag(Ploc, diag::err_protocol_has_circular_dependency);
677 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000678 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000679 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000680
681 if (!PDecl->hasDefinition())
682 continue;
683
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000684 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
685 PDecl->getLocation(), PDecl->getReferencedProtocols()))
686 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000687 }
688 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000689 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000690}
691
John McCalld226f652010-08-21 09:40:31 +0000692Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000693Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
694 IdentifierInfo *ProtocolName,
695 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000696 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000697 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000698 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000699 SourceLocation EndProtoLoc,
700 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000701 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000702 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000703 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000704 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
705 ForRedeclaration);
706 ObjCProtocolDecl *PDecl = 0;
707 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
708 // If we already have a definition, complain.
709 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
710 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Douglas Gregor27c6da22012-01-01 20:30:41 +0000712 // Create a new protocol that is completely distinct from previous
713 // declarations, and do not make this protocol available for name lookup.
714 // That way, we'll end up completely ignoring the duplicate.
715 // FIXME: Can we turn this into an error?
716 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
717 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000718 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000719 PDecl->startDefinition();
720 } else {
721 if (PrevDecl) {
722 // Check for circular dependencies among protocol declarations. This can
723 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000724 ObjCList<ObjCProtocolDecl> PList;
725 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
726 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000727 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000728 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000729
730 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000731 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000732 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000733 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000734
Douglas Gregor6e378de2009-04-23 23:18:26 +0000735 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000736 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000737 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000738
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000739 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000740 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000741
742 // Merge attributes from previous declarations.
743 if (PrevDecl)
744 mergeDeclAttributes(PDecl, PrevDecl);
745
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000746 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000747 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000748 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000749 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000750 }
Mike Stump1eb44332009-09-09 15:08:12 +0000751
752 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000753 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000754}
755
756/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000757/// issues an error if they are not declared. It returns list of
758/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000759void
Chris Lattnere13b9592008-07-26 04:03:38 +0000760Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000761 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000762 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000763 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000764 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000765 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
766 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000767 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000768 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000769 TypoCorrection Corrected = CorrectTypo(
770 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000771 LookupObjCProtocolName, TUScope, NULL, Validator);
Richard Smith2d670972013-08-17 00:46:16 +0000772 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>()))
773 diagnoseTypo(Corrected, PDiag(diag::err_undeclared_protocol_suggest)
774 << ProtocolId[i].first);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000775 }
776
777 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000778 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000779 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000780 continue;
781 }
Fariborz Jahanian3c9a0242013-04-09 17:52:29 +0000782 // If this is a forward protocol declaration, get its definition.
783 if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
784 PDecl = PDecl->getDefinition();
785
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000786 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000787
788 // If this is a forward declaration and we are supposed to warn in this
789 // case, do it.
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000790 // FIXME: Recover nicely in the hidden case.
791 if (WarnOnDeclarations &&
792 (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000793 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000794 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000795 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000796 }
797}
798
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000799/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000800/// a class method in its extension.
801///
Mike Stump1eb44332009-09-09 15:08:12 +0000802void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000803 ObjCInterfaceDecl *ID) {
804 if (!ID)
805 return; // Possibly due to previous error
806
807 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000808 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
809 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000810 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000811 MethodMap[MD->getSelector()] = MD;
812 }
813
814 if (MethodMap.empty())
815 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000816 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
817 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000818 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000819 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
820 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
821 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
822 << Method->getDeclName();
823 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
824 }
825 }
826}
827
James Dennett1dfbd922012-06-14 21:40:34 +0000828/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000829Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000830Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000831 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000832 unsigned NumElts,
833 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000834 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000835 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000836 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000837 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
838 ForRedeclaration);
839 ObjCProtocolDecl *PDecl
840 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
841 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000842 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000843
844 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000845 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000846
Douglas Gregor3937f872012-01-01 20:33:24 +0000847 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000848 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000849
850 if (PrevDecl)
851 mergeDeclAttributes(PDecl, PrevDecl);
852
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000853 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000854 }
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Rafael Espindola4549d7f2013-07-09 12:05:01 +0000856 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattner4d391482007-12-12 07:09:47 +0000857}
858
John McCalld226f652010-08-21 09:40:31 +0000859Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000860ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
861 IdentifierInfo *ClassName, SourceLocation ClassLoc,
862 IdentifierInfo *CategoryName,
863 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000864 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000865 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000866 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000867 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000868 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000869 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000870
871 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000872
873 if (!IDecl
874 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000875 diag::err_category_forward_interface,
876 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000877 // Create an invalid ObjCCategoryDecl to serve as context for
878 // the enclosing method declarations. We mark the decl invalid
879 // to make it clear that this isn't a valid AST.
880 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000881 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000882 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000883 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000884
885 if (!IDecl)
886 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000887 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000888 }
889
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000890 if (!CategoryName && IDecl->getImplementation()) {
891 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
892 Diag(IDecl->getImplementation()->getLocation(),
893 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000894 }
895
Fariborz Jahanian25760612010-02-15 21:55:26 +0000896 if (CategoryName) {
897 /// Check for duplicate interface declaration for this category
Douglas Gregord3297242013-01-16 23:00:23 +0000898 if (ObjCCategoryDecl *Previous
899 = IDecl->FindCategoryDeclaration(CategoryName)) {
900 // Class extensions can be declared multiple times, categories cannot.
901 Diag(CategoryLoc, diag::warn_dup_category_def)
902 << ClassName << CategoryName;
903 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner70f19542009-02-16 21:26:43 +0000904 }
905 }
Chris Lattner70f19542009-02-16 21:26:43 +0000906
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000907 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
908 ClassLoc, CategoryLoc, CategoryName, IDecl);
909 // FIXME: PushOnScopeChains?
910 CurContext->addDecl(CDecl);
911
Chris Lattner4d391482007-12-12 07:09:47 +0000912 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000913 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000914 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000915 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000916 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000917 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000918 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000919 }
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Anders Carlsson15281452008-11-04 16:57:32 +0000921 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000922 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000923}
924
925/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000926/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000927/// object.
John McCalld226f652010-08-21 09:40:31 +0000928Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000929 SourceLocation AtCatImplLoc,
930 IdentifierInfo *ClassName, SourceLocation ClassLoc,
931 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000932 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000933 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000934 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000935 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
936 if (!CatIDecl) {
937 // Category @implementation with no corresponding @interface.
938 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000939 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
940 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000941 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000942 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000943 }
944 }
945
Mike Stump1eb44332009-09-09 15:08:12 +0000946 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000947 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000948 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000949 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000950 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000951 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000952 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000953 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
954 diag::err_undef_interface)) {
955 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000956 }
Chris Lattner4d391482007-12-12 07:09:47 +0000957
Douglas Gregord0434102009-01-09 00:49:46 +0000958 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000959 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000960
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000961 // If the interface is deprecated/unavailable, warn/error about it.
962 if (IDecl)
963 DiagnoseUseOfDecl(IDecl, ClassLoc);
964
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000965 /// Check that CatName, category name, is not used in another implementation.
966 if (CatIDecl) {
967 if (CatIDecl->getImplementation()) {
968 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
969 << CatName;
970 Diag(CatIDecl->getImplementation()->getLocation(),
971 diag::note_previous_definition);
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000972 CDecl->setInvalidDecl();
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000973 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000974 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000975 // Warn on implementating category of deprecated class under
976 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000977 DiagnoseObjCImplementedDeprecations(*this,
978 dyn_cast<NamedDecl>(IDecl),
979 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000980 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Anders Carlsson15281452008-11-04 16:57:32 +0000983 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000984 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000985}
986
John McCalld226f652010-08-21 09:40:31 +0000987Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000988 SourceLocation AtClassImplLoc,
989 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000990 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000991 SourceLocation SuperClassLoc) {
Richard Smith2d670972013-08-17 00:46:16 +0000992 ObjCInterfaceDecl *IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000993 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000994 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000995 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
996 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000997 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000998 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000999 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001000 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +00001001 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
1002 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +00001003 } else {
Richard Smith2d670972013-08-17 00:46:16 +00001004 // We did not find anything with the name ClassName; try to correct for
Douglas Gregor95ff7422010-01-04 17:27:12 +00001005 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +00001006 ObjCInterfaceValidatorCCC Validator;
Richard Smith2d670972013-08-17 00:46:16 +00001007 TypoCorrection Corrected =
1008 CorrectTypo(DeclarationNameInfo(ClassName, ClassLoc),
1009 LookupOrdinaryName, TUScope, NULL, Validator);
1010 if (Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>()) {
1011 // Suggest the (potentially) correct interface name. Don't provide a
1012 // code-modification hint or use the typo name for recovery, because
1013 // this is just a warning. The program may actually be correct.
1014 diagnoseTypo(Corrected,
1015 PDiag(diag::warn_undef_interface_suggest) << ClassName,
1016 /*ErrorRecovery*/false);
Douglas Gregor95ff7422010-01-04 17:27:12 +00001017 } else {
1018 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
1019 }
Chris Lattner4d391482007-12-12 07:09:47 +00001020 }
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Chris Lattner4d391482007-12-12 07:09:47 +00001022 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001023 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +00001024 if (SuperClassname) {
1025 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +00001026 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
1027 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001028 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001029 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1030 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +00001031 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +00001032 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001033 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +00001034 if (SDecl && !SDecl->hasDefinition())
1035 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +00001036 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +00001037 Diag(SuperClassLoc, diag::err_undef_superclass)
1038 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +00001039 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001040 // This implementation and its interface do not have the same
1041 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +00001042 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +00001043 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001044 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001045 }
1046 }
1047 }
Mike Stump1eb44332009-09-09 15:08:12 +00001048
Chris Lattner4d391482007-12-12 07:09:47 +00001049 if (!IDecl) {
1050 // Legacy case of @implementation with no corresponding @interface.
1051 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +00001052
Mike Stump390b4cc2009-05-16 07:39:55 +00001053 // FIXME: Do we support attributes on the @implementation? If so we should
1054 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001055 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001056 ClassName, /*PrevDecl=*/0, ClassLoc,
1057 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001058 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001059 if (SDecl) {
1060 IDecl->setSuperClass(SDecl);
1061 IDecl->setSuperClassLoc(SuperClassLoc);
1062 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1063 } else {
1064 IDecl->setEndOfDefinitionLoc(ClassLoc);
1065 }
1066
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001067 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001068 } else {
1069 // Mark the interface as being completed, even if it was just as
1070 // @class ....;
1071 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001072 if (!IDecl->hasDefinition())
1073 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001074 }
Mike Stump1eb44332009-09-09 15:08:12 +00001075
1076 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001077 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
Argyrios Kyrtzidis634c5632013-05-03 18:05:44 +00001078 ClassLoc, AtClassImplLoc, SuperClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001079
Anders Carlsson15281452008-11-04 16:57:32 +00001080 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001081 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Chris Lattner4d391482007-12-12 07:09:47 +00001083 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001084 if (IDecl->getImplementation()) {
1085 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001086 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001087 Diag(IDecl->getImplementation()->getLocation(),
1088 diag::note_previous_definition);
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +00001089 IMPDecl->setInvalidDecl();
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001090 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001091 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001092 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001093 // Warn on implementating deprecated class under
1094 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001095 DiagnoseObjCImplementedDeprecations(*this,
1096 dyn_cast<NamedDecl>(IDecl),
1097 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001098 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001099 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001100}
1101
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001102Sema::DeclGroupPtrTy
1103Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1104 SmallVector<Decl *, 64> DeclsInGroup;
1105 DeclsInGroup.reserve(Decls.size() + 1);
1106
1107 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1108 Decl *Dcl = Decls[i];
1109 if (!Dcl)
1110 continue;
1111 if (Dcl->getDeclContext()->isFileContext())
1112 Dcl->setTopLevelDeclInObjCContainer();
1113 DeclsInGroup.push_back(Dcl);
1114 }
1115
1116 DeclsInGroup.push_back(ObjCImpDecl);
1117
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001118 return BuildDeclaratorGroup(DeclsInGroup, false);
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001119}
1120
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001121void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1122 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001123 SourceLocation RBrace) {
1124 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001125 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001126 if (!IDecl)
1127 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001128 /// Check case of non-existing \@interface decl.
1129 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001130 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001131 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001132 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001133 // Add ivar's to class's DeclContext.
1134 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001135 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001136 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001137 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001138 }
1139
Chris Lattner4d391482007-12-12 07:09:47 +00001140 return;
1141 }
1142 // If implementation has empty ivar list, just return.
1143 if (numIvars == 0)
1144 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Chris Lattner4d391482007-12-12 07:09:47 +00001146 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001147 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001148 if (ImpDecl->getSuperClass())
1149 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1150 for (unsigned i = 0; i < numIvars; i++) {
1151 ObjCIvarDecl* ImplIvar = ivars[i];
1152 if (const ObjCIvarDecl *ClsIvar =
1153 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1154 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1155 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1156 continue;
1157 }
Fariborz Jahanian3b20f582013-06-26 22:10:27 +00001158 // Check class extensions (unnamed categories) for duplicate ivars.
1159 for (ObjCInterfaceDecl::visible_extensions_iterator
1160 Ext = IDecl->visible_extensions_begin(),
1161 ExtEnd = IDecl->visible_extensions_end();
1162 Ext != ExtEnd; ++Ext) {
1163 ObjCCategoryDecl *CDecl = *Ext;
1164 if (const ObjCIvarDecl *ClsExtIvar =
1165 CDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1166 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1167 Diag(ClsExtIvar->getLocation(), diag::note_previous_definition);
1168 continue;
1169 }
1170 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001171 // Instance ivar to Implementation's DeclContext.
1172 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001173 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001174 ImpDecl->addDecl(ImplIvar);
1175 }
1176 return;
1177 }
Chris Lattner4d391482007-12-12 07:09:47 +00001178 // Check interface's Ivar list against those in the implementation.
1179 // names and types must match.
1180 //
Chris Lattner4d391482007-12-12 07:09:47 +00001181 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001182 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001183 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1184 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001185 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001186 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001187 assert (ImplIvar && "missing implementation ivar");
1188 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001189
Steve Naroffca331292009-03-03 14:49:36 +00001190 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001191 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001192 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001193 << ImplIvar->getIdentifier()
1194 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001195 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001196 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1197 ImplIvar->getBitWidthValue(Context) !=
1198 ClsIvar->getBitWidthValue(Context)) {
1199 Diag(ImplIvar->getBitWidth()->getLocStart(),
1200 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1201 Diag(ClsIvar->getBitWidth()->getLocStart(),
1202 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001203 }
Steve Naroffca331292009-03-03 14:49:36 +00001204 // Make sure the names are identical.
1205 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001206 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001207 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001208 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001209 }
1210 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001211 }
Mike Stump1eb44332009-09-09 15:08:12 +00001212
Chris Lattner609e4c72007-12-12 18:11:49 +00001213 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001214 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001215 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001216 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001217}
1218
Steve Naroff3c2eb662008-02-10 21:38:56 +00001219void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001220 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001221 // No point warning no definition of method which is 'unavailable'.
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001222 switch (method->getAvailability()) {
1223 case AR_Available:
1224 case AR_Deprecated:
1225 break;
1226
1227 // Don't warn about unavailable or not-yet-introduced methods.
1228 case AR_NotYetIntroduced:
1229 case AR_Unavailable:
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001230 return;
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001231 }
1232
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001233 // FIXME: For now ignore 'IncompleteImpl'.
1234 // Previously we grouped all unimplemented methods under a single
1235 // warning, but some users strongly voiced that they would prefer
1236 // separate warnings. We will give that approach a try, as that
1237 // matches what we do with protocols.
1238
1239 Diag(ImpLoc, DiagID) << method->getDeclName();
1240
1241 // Issue a note to the original declaration.
1242 SourceLocation MethodLoc = method->getLocStart();
1243 if (MethodLoc.isValid())
1244 Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001245}
1246
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001247/// Determines if type B can be substituted for type A. Returns true if we can
1248/// guarantee that anything that the user will do to an object of type A can
1249/// also be done to an object of type B. This is trivially true if the two
1250/// types are the same, or if B is a subclass of A. It becomes more complex
1251/// in cases where protocols are involved.
1252///
1253/// Object types in Objective-C describe the minimum requirements for an
1254/// object, rather than providing a complete description of a type. For
1255/// example, if A is a subclass of B, then B* may refer to an instance of A.
1256/// The principle of substitutability means that we may use an instance of A
1257/// anywhere that we may use an instance of B - it will implement all of the
1258/// ivars of B and all of the methods of B.
1259///
1260/// This substitutability is important when type checking methods, because
1261/// the implementation may have stricter type definitions than the interface.
1262/// The interface specifies minimum requirements, but the implementation may
1263/// have more accurate ones. For example, a method may privately accept
1264/// instances of B, but only publish that it accepts instances of A. Any
1265/// object passed to it will be type checked against B, and so will implicitly
1266/// by a valid A*. Similarly, a method may return a subclass of the class that
1267/// it is declared as returning.
1268///
1269/// This is most important when considering subclassing. A method in a
1270/// subclass must accept any object as an argument that its superclass's
1271/// implementation accepts. It may, however, accept a more general type
1272/// without breaking substitutability (i.e. you can still use the subclass
1273/// anywhere that you can use the superclass, but not vice versa). The
1274/// converse requirement applies to return types: the return type for a
1275/// subclass method must be a valid object of the kind that the superclass
1276/// advertises, but it may be specified more accurately. This avoids the need
1277/// for explicit down-casting by callers.
1278///
1279/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001280static bool isObjCTypeSubstitutable(ASTContext &Context,
1281 const ObjCObjectPointerType *A,
1282 const ObjCObjectPointerType *B,
1283 bool rejectId) {
1284 // Reject a protocol-unqualified id.
1285 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001286
1287 // If B is a qualified id, then A must also be a qualified id and it must
1288 // implement all of the protocols in B. It may not be a qualified class.
1289 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1290 // stricter definition so it is not substitutable for id<A>.
1291 if (B->isObjCQualifiedIdType()) {
1292 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001293 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1294 QualType(B,0),
1295 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001296 }
1297
1298 /*
1299 // id is a special type that bypasses type checking completely. We want a
1300 // warning when it is used in one place but not another.
1301 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1302
1303
1304 // If B is a qualified id, then A must also be a qualified id (which it isn't
1305 // if we've got this far)
1306 if (B->isObjCQualifiedIdType()) return false;
1307 */
1308
1309 // Now we know that A and B are (potentially-qualified) class types. The
1310 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001311 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001312}
1313
John McCall10302c02010-10-28 02:34:38 +00001314static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1315 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1316}
1317
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001318static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001319 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001320 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001321 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001322 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001323 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001324 if (IsProtocolMethodDecl &&
1325 (MethodDecl->getObjCDeclQualifier() !=
1326 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001327 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001328 S.Diag(MethodImpl->getLocation(),
1329 (IsOverridingMode ?
1330 diag::warn_conflicting_overriding_ret_type_modifiers
1331 : diag::warn_conflicting_ret_type_modifiers))
1332 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001333 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1334 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1335 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1336 }
1337 else
1338 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001339 }
1340
John McCall10302c02010-10-28 02:34:38 +00001341 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001342 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001343 return true;
1344 if (!Warn)
1345 return false;
John McCall10302c02010-10-28 02:34:38 +00001346
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001347 unsigned DiagID =
1348 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1349 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001350
1351 // Mismatches between ObjC pointers go into a different warning
1352 // category, and sometimes they're even completely whitelisted.
1353 if (const ObjCObjectPointerType *ImplPtrTy =
1354 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1355 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001356 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001357 // Allow non-matching return types as long as they don't violate
1358 // the principle of substitutability. Specifically, we permit
1359 // return types that are subclasses of the declared return type,
1360 // or that are more-qualified versions of the declared type.
1361 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001362 return false;
John McCall10302c02010-10-28 02:34:38 +00001363
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001364 DiagID =
1365 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1366 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001367 }
1368 }
1369
1370 S.Diag(MethodImpl->getLocation(), DiagID)
1371 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001372 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001373 << MethodImpl->getResultType()
1374 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001375 S.Diag(MethodDecl->getLocation(),
1376 IsOverridingMode ? diag::note_previous_declaration
1377 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001378 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001379 return false;
John McCall10302c02010-10-28 02:34:38 +00001380}
1381
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001382static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001383 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001384 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001385 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001386 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001387 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001388 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001389 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001390 if (IsProtocolMethodDecl &&
1391 (ImplVar->getObjCDeclQualifier() !=
1392 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001393 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001394 if (IsOverridingMode)
1395 S.Diag(ImplVar->getLocation(),
1396 diag::warn_conflicting_overriding_param_modifiers)
1397 << getTypeRange(ImplVar->getTypeSourceInfo())
1398 << MethodImpl->getDeclName();
1399 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001400 diag::warn_conflicting_param_modifiers)
1401 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001402 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001403 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1404 << getTypeRange(IfaceVar->getTypeSourceInfo());
1405 }
1406 else
1407 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001408 }
1409
John McCall10302c02010-10-28 02:34:38 +00001410 QualType ImplTy = ImplVar->getType();
1411 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001412
John McCall10302c02010-10-28 02:34:38 +00001413 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001414 return true;
1415
1416 if (!Warn)
1417 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001418 unsigned DiagID =
1419 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1420 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001421
1422 // Mismatches between ObjC pointers go into a different warning
1423 // category, and sometimes they're even completely whitelisted.
1424 if (const ObjCObjectPointerType *ImplPtrTy =
1425 ImplTy->getAs<ObjCObjectPointerType>()) {
1426 if (const ObjCObjectPointerType *IfacePtrTy =
1427 IfaceTy->getAs<ObjCObjectPointerType>()) {
1428 // Allow non-matching argument types as long as they don't
1429 // violate the principle of substitutability. Specifically, the
1430 // implementation must accept any objects that the superclass
1431 // accepts, however it may also accept others.
1432 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001433 return false;
John McCall10302c02010-10-28 02:34:38 +00001434
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001435 DiagID =
1436 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1437 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001438 }
1439 }
1440
1441 S.Diag(ImplVar->getLocation(), DiagID)
1442 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001443 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1444 S.Diag(IfaceVar->getLocation(),
1445 (IsOverridingMode ? diag::note_previous_declaration
1446 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001447 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001448 return false;
John McCall10302c02010-10-28 02:34:38 +00001449}
John McCallf85e1932011-06-15 23:02:42 +00001450
1451/// In ARC, check whether the conventional meanings of the two methods
1452/// match. If they don't, it's a hard error.
1453static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1454 ObjCMethodDecl *decl) {
1455 ObjCMethodFamily implFamily = impl->getMethodFamily();
1456 ObjCMethodFamily declFamily = decl->getMethodFamily();
1457 if (implFamily == declFamily) return false;
1458
1459 // Since conventions are sorted by selector, the only possibility is
1460 // that the types differ enough to cause one selector or the other
1461 // to fall out of the family.
1462 assert(implFamily == OMF_None || declFamily == OMF_None);
1463
1464 // No further diagnostics required on invalid declarations.
1465 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1466
1467 const ObjCMethodDecl *unmatched = impl;
1468 ObjCMethodFamily family = declFamily;
1469 unsigned errorID = diag::err_arc_lost_method_convention;
1470 unsigned noteID = diag::note_arc_lost_method_convention;
1471 if (declFamily == OMF_None) {
1472 unmatched = decl;
1473 family = implFamily;
1474 errorID = diag::err_arc_gained_method_convention;
1475 noteID = diag::note_arc_gained_method_convention;
1476 }
1477
1478 // Indexes into a %select clause in the diagnostic.
1479 enum FamilySelector {
1480 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1481 };
1482 FamilySelector familySelector = FamilySelector();
1483
1484 switch (family) {
1485 case OMF_None: llvm_unreachable("logic error, no method convention");
1486 case OMF_retain:
1487 case OMF_release:
1488 case OMF_autorelease:
1489 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001490 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001491 case OMF_retainCount:
1492 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001493 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001494 // Mismatches for these methods don't change ownership
1495 // conventions, so we don't care.
1496 return false;
1497
1498 case OMF_init: familySelector = F_init; break;
1499 case OMF_alloc: familySelector = F_alloc; break;
1500 case OMF_copy: familySelector = F_copy; break;
1501 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1502 case OMF_new: familySelector = F_new; break;
1503 }
1504
1505 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1506 ReasonSelector reasonSelector;
1507
1508 // The only reason these methods don't fall within their families is
1509 // due to unusual result types.
1510 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1511 reasonSelector = R_UnrelatedReturn;
1512 } else {
1513 reasonSelector = R_NonObjectReturn;
1514 }
1515
Joerg Sonnenberger73484542013-06-26 21:31:47 +00001516 S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
1517 S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
John McCallf85e1932011-06-15 23:02:42 +00001518
1519 return true;
1520}
John McCall10302c02010-10-28 02:34:38 +00001521
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001522void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001523 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001524 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001525 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001526 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1527 return;
1528
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001529 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001530 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001531 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Chris Lattner3aff9192009-04-11 19:58:42 +00001533 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001534 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1535 EF = MethodDecl->param_end();
1536 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001537 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001538 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001539 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001540
Fariborz Jahanian21121902011-08-08 18:03:17 +00001541 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001542 Diag(ImpMethodDecl->getLocation(),
1543 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001544 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001545 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001546}
1547
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001548void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1549 ObjCMethodDecl *Overridden,
1550 bool IsProtocolMethodDecl) {
1551
1552 CheckMethodOverrideReturn(*this, Method, Overridden,
1553 IsProtocolMethodDecl, true,
1554 true);
1555
1556 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001557 IF = Overridden->param_begin(), EM = Method->param_end(),
1558 EF = Overridden->param_end();
1559 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001560 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1561 IsProtocolMethodDecl, true, true);
1562 }
1563
1564 if (Method->isVariadic() != Overridden->isVariadic()) {
1565 Diag(Method->getLocation(),
1566 diag::warn_conflicting_overriding_variadic);
1567 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1568 }
1569}
1570
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001571/// WarnExactTypedMethods - This routine issues a warning if method
1572/// implementation declaration matches exactly that of its declaration.
1573void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1574 ObjCMethodDecl *MethodDecl,
1575 bool IsProtocolMethodDecl) {
1576 // don't issue warning when protocol method is optional because primary
1577 // class is not required to implement it and it is safe for protocol
1578 // to implement it.
1579 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1580 return;
1581 // don't issue warning when primary class's method is
1582 // depecated/unavailable.
1583 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1584 MethodDecl->hasAttr<DeprecatedAttr>())
1585 return;
1586
1587 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1588 IsProtocolMethodDecl, false, false);
1589 if (match)
1590 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001591 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1592 EF = MethodDecl->param_end();
1593 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001594 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1595 *IM, *IF,
1596 IsProtocolMethodDecl, false, false);
1597 if (!match)
1598 break;
1599 }
1600 if (match)
1601 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001602 if (match)
1603 match = !(MethodDecl->isClassMethod() &&
1604 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001605
1606 if (match) {
1607 Diag(ImpMethodDecl->getLocation(),
1608 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001609 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1610 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001611 }
1612}
1613
Mike Stump390b4cc2009-05-16 07:39:55 +00001614/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1615/// improve the efficiency of selector lookups and type checking by associating
1616/// with each protocol / interface / category the flattened instance tables. If
1617/// we used an immutable set to keep the table then it wouldn't add significant
1618/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001619
Steve Naroffefe7f362008-02-08 22:06:17 +00001620/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001621/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001622void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1623 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001624 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001625 const SelectorSet &InsMap,
1626 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001627 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001628 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1629 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1630 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001631 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1632
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001633 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001634 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001635 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001636 // check to see if class implements forwardInvocation method and objects
1637 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001638 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001639 // Under such conditions, which means that every method possible is
1640 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001641 // found" warnings.
1642 // FIXME: Use a general GetUnarySelector method for this.
1643 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1644 Selector fISelector = Context.Selectors.getSelector(1, &II);
1645 if (InsMap.count(fISelector))
1646 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1647 // need be implemented in the implementation.
1648 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1649 }
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001651 // If this is a forward protocol declaration, get its definition.
1652 if (!PDecl->isThisDeclarationADefinition() &&
1653 PDecl->getDefinition())
1654 PDecl = PDecl->getDefinition();
1655
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001656 // If a method lookup fails locally we still need to look and see if
1657 // the method was implemented by a base class or an inherited
1658 // protocol. This lookup is slow, but occurs rarely in correct code
1659 // and otherwise would terminate in a warning.
1660
Chris Lattner4d391482007-12-12 07:09:47 +00001661 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001662 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001663 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001664 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001665 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001666 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001667 !method->isPropertyAccessor() &&
1668 !InsMap.count(method->getSelector()) &&
1669 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001670 // If a method is not implemented in the category implementation but
1671 // has been declared in its primary class, superclass,
1672 // or in one of their protocols, no need to issue the warning.
1673 // This is because method will be implemented in the primary class
1674 // or one of its super class implementation.
1675
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001676 // Ugly, but necessary. Method declared in protcol might have
1677 // have been synthesized due to a property declared in the class which
1678 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001679 if (ObjCMethodDecl *MethodInClass =
1680 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001681 true /*shallowCategoryLookup*/))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001682 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001683 continue;
1684 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1685 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1686 != DiagnosticsEngine::Ignored) {
1687 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001688 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1689 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001690 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001691 }
1692 }
Chris Lattner4d391482007-12-12 07:09:47 +00001693 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001694 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001695 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001696 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001697 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001698 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1699 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001700 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001701 // See above comment for instance method lookups.
1702 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001703 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001704 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001705 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001706 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1707 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001708 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
1709 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1710 PDecl->getDeclName();
1711 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001712 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001713 }
Chris Lattner780f3292008-07-21 21:32:27 +00001714 // Check on this protocols's referenced protocols, recursively.
1715 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1716 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001717 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001718}
1719
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001720/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001721/// or protocol against those declared in their implementations.
1722///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001723void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1724 const SelectorSet &ClsMap,
1725 SelectorSet &InsMapSeen,
1726 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001727 ObjCImplDecl* IMPDecl,
1728 ObjCContainerDecl* CDecl,
1729 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001730 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001731 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001732 // Check and see if instance methods in class interface have been
1733 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001734 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1735 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001736 if (InsMapSeen.count((*I)->getSelector()))
1737 continue;
1738 InsMapSeen.insert((*I)->getSelector());
Jordan Rose1e4691b2012-10-10 16:42:25 +00001739 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001740 !InsMap.count((*I)->getSelector())) {
1741 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001742 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001743 diag::warn_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001744 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001745 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001746 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001747 IMPDecl->getInstanceMethod((*I)->getSelector());
1748 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1749 "Expected to find the method through lookup as well");
1750 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001751 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001752 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001753 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001754 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1755 isa<ObjCProtocolDecl>(CDecl));
Jordan Rose1e4691b2012-10-10 16:42:25 +00001756 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001757 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001758 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001759 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001760 }
1761 }
Mike Stump1eb44332009-09-09 15:08:12 +00001762
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001763 // Check and see if class methods in class interface have been
1764 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001765 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001766 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001767 if (ClsMapSeen.count((*I)->getSelector()))
1768 continue;
1769 ClsMapSeen.insert((*I)->getSelector());
1770 if (!ClsMap.count((*I)->getSelector())) {
1771 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001772 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001773 diag::warn_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001774 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001775 ObjCMethodDecl *ImpMethodDecl =
1776 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001777 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1778 "Expected to find the method through lookup as well");
1779 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001780 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001781 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1782 isa<ObjCProtocolDecl>(CDecl));
1783 else
1784 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001785 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001786 }
1787 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001788
Fariborz Jahanian41594c52013-08-14 23:58:55 +00001789 if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl> (CDecl)) {
1790 // Also, check for methods declared in protocols inherited by
1791 // this protocol.
1792 for (ObjCProtocolDecl::protocol_iterator
1793 PI = PD->protocol_begin(), E = PD->protocol_end(); PI != E; ++PI)
1794 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1795 IMPDecl, (*PI), IncompleteImpl, false,
1796 WarnCategoryMethodImpl);
1797 }
1798
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001799 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001800 // when checking that methods in implementation match their declaration,
1801 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1802 // extension; as well as those in categories.
Douglas Gregord3297242013-01-16 23:00:23 +00001803 if (!WarnCategoryMethodImpl) {
1804 for (ObjCInterfaceDecl::visible_categories_iterator
1805 Cat = I->visible_categories_begin(),
1806 CatEnd = I->visible_categories_end();
1807 Cat != CatEnd; ++Cat) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001808 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001809 IMPDecl, *Cat, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001810 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001811 }
1812 } else {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001813 // Also methods in class extensions need be looked at next.
Douglas Gregord3297242013-01-16 23:00:23 +00001814 for (ObjCInterfaceDecl::visible_extensions_iterator
1815 Ext = I->visible_extensions_begin(),
1816 ExtEnd = I->visible_extensions_end();
1817 Ext != ExtEnd; ++Ext) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001818 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001819 IMPDecl, *Ext, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001820 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001821 }
1822 }
1823
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001824 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001825 for (ObjCInterfaceDecl::all_protocol_iterator
1826 PI = I->all_referenced_protocol_begin(),
1827 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001828 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1829 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001830 (*PI), IncompleteImpl, false,
1831 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001832
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001833 // FIXME. For now, we are not checking for extact match of methods
1834 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001835 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001836 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001837 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001838 I->getSuperClass(), IncompleteImpl, false);
1839 }
1840}
1841
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001842/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1843/// category matches with those implemented in its primary class and
1844/// warns each time an exact match is found.
1845void Sema::CheckCategoryVsClassMethodMatches(
1846 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001847 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001848
1849 for (ObjCImplementationDecl::instmeth_iterator
1850 I = CatIMPDecl->instmeth_begin(),
1851 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1852 InsMap.insert((*I)->getSelector());
1853
1854 for (ObjCImplementationDecl::classmeth_iterator
1855 I = CatIMPDecl->classmeth_begin(),
1856 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1857 ClsMap.insert((*I)->getSelector());
1858 if (InsMap.empty() && ClsMap.empty())
1859 return;
1860
1861 // Get category's primary class.
1862 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1863 if (!CatDecl)
1864 return;
1865 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1866 if (!IDecl)
1867 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001868 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001869 bool IncompleteImpl = false;
1870 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1871 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001872 IncompleteImpl, false,
1873 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001874}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001875
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001876void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001877 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001878 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001879 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001880 // Check and see if instance methods in class interface have been
1881 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001882 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001883 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001884 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001885
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001886 // Check and see if properties declared in the interface have either 1)
1887 // an implementation or 2) there is a @synthesize/@dynamic implementation
1888 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001889 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001890 if (!(LangOpts.ObjCDefaultSynthProperties &&
1891 LangOpts.ObjCRuntime.isNonFragile()) ||
1892 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001893 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001894
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001895 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001896 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001897 I = IMPDecl->classmeth_begin(),
1898 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001899 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001900
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001901 // Check for type conflict of methods declared in a class/protocol and
1902 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001903 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001904 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1905 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001906 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001907
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001908 // check all methods implemented in category against those declared
1909 // in its primary class.
1910 if (ObjCCategoryImplDecl *CatDecl =
1911 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1912 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001913
Chris Lattner4d391482007-12-12 07:09:47 +00001914 // Check the protocol list for unimplemented methods in the @implementation
1915 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001916 // Check and see if class methods in class interface have been
1917 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001918
Chris Lattnercddc8882009-03-01 00:56:52 +00001919 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001920 for (ObjCInterfaceDecl::all_protocol_iterator
1921 PI = I->all_referenced_protocol_begin(),
1922 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001923 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001924 InsMap, ClsMap, I);
1925 // Check class extensions (unnamed categories)
Douglas Gregord3297242013-01-16 23:00:23 +00001926 for (ObjCInterfaceDecl::visible_extensions_iterator
1927 Ext = I->visible_extensions_begin(),
1928 ExtEnd = I->visible_extensions_end();
1929 Ext != ExtEnd; ++Ext) {
1930 ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
1931 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001932 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001933 // For extended class, unimplemented methods in its protocols will
1934 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001935 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001936 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1937 E = C->protocol_end(); PI != E; ++PI)
1938 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001939 InsMap, ClsMap, CDecl);
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001940 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001941 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001942 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001943 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001944}
1945
Mike Stump1eb44332009-09-09 15:08:12 +00001946/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001947Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001948Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001949 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001950 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001951 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001952 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001953 for (unsigned i = 0; i != NumElts; ++i) {
1954 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001955 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001956 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001957 LookupOrdinaryName, ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001958 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001959 // GCC apparently allows the following idiom:
1960 //
1961 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1962 // @class XCElementToggler;
1963 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001964 // Here we have chosen to ignore the forward class declaration
1965 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001966 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001967 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001968 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001969 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001970 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001971 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001972 // to the underlying class. Just ignore the forward class with a warning
1973 // as this will force the intended behavior which is to lookup the typedef
1974 // name.
1975 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1976 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1977 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1978 continue;
1979 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001980 }
Chris Lattner4d391482007-12-12 07:09:47 +00001981 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001982
1983 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001984 ObjCInterfaceDecl *PrevIDecl
1985 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +00001986
1987 IdentifierInfo *ClassName = IdentList[i];
1988 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
1989 // A previous decl with a different name is because of
1990 // @compatibility_alias, for example:
1991 // \code
1992 // @class NewImage;
1993 // @compatibility_alias OldImage NewImage;
1994 // \endcode
1995 // A lookup for 'OldImage' will return the 'NewImage' decl.
1996 //
1997 // In such a case use the real declaration name, instead of the alias one,
1998 // otherwise we will break IdentifierResolver and redecls-chain invariants.
1999 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
2000 // has been aliased.
2001 ClassName = PrevIDecl->getIdentifier();
2002 }
2003
Douglas Gregor7723fec2011-12-15 20:29:51 +00002004 ObjCInterfaceDecl *IDecl
2005 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +00002006 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00002007 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00002008
Douglas Gregor7723fec2011-12-15 20:29:51 +00002009 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00002010 CheckObjCDeclScope(IDecl);
2011 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00002012 }
Rafael Espindola4549d7f2013-07-09 12:05:01 +00002013
2014 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002015}
2016
John McCall0f4c4c42011-06-16 01:15:19 +00002017static bool tryMatchRecordTypes(ASTContext &Context,
2018 Sema::MethodMatchStrategy strategy,
2019 const Type *left, const Type *right);
2020
John McCallf85e1932011-06-15 23:02:42 +00002021static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
2022 QualType leftQT, QualType rightQT) {
2023 const Type *left =
2024 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2025 const Type *right =
2026 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2027
2028 if (left == right) return true;
2029
2030 // If we're doing a strict match, the types have to match exactly.
2031 if (strategy == Sema::MMS_strict) return false;
2032
2033 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2034
2035 // Otherwise, use this absurdly complicated algorithm to try to
2036 // validate the basic, low-level compatibility of the two types.
2037
2038 // As a minimum, require the sizes and alignments to match.
2039 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
2040 return false;
2041
2042 // Consider all the kinds of non-dependent canonical types:
2043 // - functions and arrays aren't possible as return and parameter types
2044
2045 // - vector types of equal size can be arbitrarily mixed
2046 if (isa<VectorType>(left)) return isa<VectorType>(right);
2047 if (isa<VectorType>(right)) return false;
2048
2049 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00002050 // - structs, unions, and Objective-C objects must match more-or-less
2051 // exactly
John McCallf85e1932011-06-15 23:02:42 +00002052 // - everything else should be a scalar
2053 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00002054 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00002055
John McCall1d9b3b22011-09-09 05:25:32 +00002056 // Make scalars agree in kind, except count bools as chars, and group
2057 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00002058 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2059 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2060 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2061 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00002062 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2063 leftSK = Type::STK_ObjCObjectPointer;
2064 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2065 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00002066
2067 // Note that data member pointers and function member pointers don't
2068 // intermix because of the size differences.
2069
2070 return (leftSK == rightSK);
2071}
Chris Lattner4d391482007-12-12 07:09:47 +00002072
John McCall0f4c4c42011-06-16 01:15:19 +00002073static bool tryMatchRecordTypes(ASTContext &Context,
2074 Sema::MethodMatchStrategy strategy,
2075 const Type *lt, const Type *rt) {
2076 assert(lt && rt && lt != rt);
2077
2078 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2079 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2080 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2081
2082 // Require union-hood to match.
2083 if (left->isUnion() != right->isUnion()) return false;
2084
2085 // Require an exact match if either is non-POD.
2086 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2087 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2088 return false;
2089
2090 // Require size and alignment to match.
2091 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2092
2093 // Require fields to match.
2094 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2095 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2096 for (; li != le && ri != re; ++li, ++ri) {
2097 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2098 return false;
2099 }
2100 return (li == le && ri == re);
2101}
2102
Chris Lattner4d391482007-12-12 07:09:47 +00002103/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2104/// returns true, or false, accordingly.
2105/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00002106bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2107 const ObjCMethodDecl *right,
2108 MethodMatchStrategy strategy) {
2109 if (!matchTypes(Context, strategy,
2110 left->getResultType(), right->getResultType()))
2111 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002112
Douglas Gregor7666b032013-02-07 19:13:24 +00002113 // If either is hidden, it is not considered to match.
2114 if (left->isHidden() || right->isHidden())
2115 return false;
2116
David Blaikie4e4d0842012-03-11 07:00:24 +00002117 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002118 (left->hasAttr<NSReturnsRetainedAttr>()
2119 != right->hasAttr<NSReturnsRetainedAttr>() ||
2120 left->hasAttr<NSConsumesSelfAttr>()
2121 != right->hasAttr<NSConsumesSelfAttr>()))
2122 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002123
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002124 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002125 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2126 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002128 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002129 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002130 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002131
2132 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2133 return false;
2134
David Blaikie4e4d0842012-03-11 07:00:24 +00002135 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002136 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2137 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002138 }
2139 return true;
2140}
2141
Douglas Gregorff310c72012-05-01 23:37:00 +00002142void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002143 // Record at the head of the list whether there were 0, 1, or >= 2 methods
2144 // inside categories.
Argyrios Kyrtzidisab3d5092013-04-27 00:10:12 +00002145 if (ObjCCategoryDecl *
2146 CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
2147 if (!CD->IsClassExtension() && List->getBits() < 2)
2148 List->setBits(List->getBits()+1);
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002149
Douglas Gregor44fae522012-01-25 00:19:56 +00002150 // If the list is empty, make it a singleton list.
2151 if (List->Method == 0) {
2152 List->Method = Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002153 List->setNext(0);
Douglas Gregorff310c72012-05-01 23:37:00 +00002154 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002155 }
2156
2157 // We've seen a method with this name, see if we have already seen this type
2158 // signature.
2159 ObjCMethodList *Previous = List;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002160 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregorfc46be92013-06-21 00:20:25 +00002161 // If we are building a module, keep all of the methods.
2162 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2163 continue;
2164
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002165 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002166 continue;
2167
2168 ObjCMethodDecl *PrevObjCMethod = List->Method;
2169
2170 // Propagate the 'defined' bit.
2171 if (Method->isDefined())
2172 PrevObjCMethod->setDefined(true);
2173
2174 // If a method is deprecated, push it in the global pool.
2175 // This is used for better diagnostics.
2176 if (Method->isDeprecated()) {
2177 if (!PrevObjCMethod->isDeprecated())
2178 List->Method = Method;
2179 }
2180 // If new method is unavailable, push it into global pool
2181 // unless previous one is deprecated.
2182 if (Method->isUnavailable()) {
2183 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2184 List->Method = Method;
2185 }
2186
Douglas Gregorff310c72012-05-01 23:37:00 +00002187 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002188 }
2189
2190 // We have a new signature for an existing method - add it.
2191 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002192 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002193 Previous->setNext(new (Mem) ObjCMethodList(Method, 0));
Douglas Gregor44fae522012-01-25 00:19:56 +00002194}
2195
Sebastian Redldb9d2142010-08-02 23:18:59 +00002196/// \brief Read the contents of the method pool for a given selector from
2197/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002198void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002199 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002200 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002201}
2202
Douglas Gregorff310c72012-05-01 23:37:00 +00002203void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002204 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002205 // Ignore methods of invalid containers.
2206 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002207 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002208
Douglas Gregor0d266d62012-01-25 00:59:09 +00002209 if (ExternalSource)
2210 ReadMethodPool(Method->getSelector());
2211
Sebastian Redldb9d2142010-08-02 23:18:59 +00002212 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002213 if (Pos == MethodPool.end())
2214 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2215 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002216
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002217 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002218
Sebastian Redldb9d2142010-08-02 23:18:59 +00002219 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002220 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002221}
2222
John McCallf85e1932011-06-15 23:02:42 +00002223/// Determines if this is an "acceptable" loose mismatch in the global
2224/// method pool. This exists mostly as a hack to get around certain
2225/// global mismatches which we can't afford to make warnings / errors.
2226/// Really, what we want is a way to take a method out of the global
2227/// method pool.
2228static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2229 ObjCMethodDecl *other) {
2230 if (!chosen->isInstanceMethod())
2231 return false;
2232
2233 Selector sel = chosen->getSelector();
2234 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2235 return false;
2236
2237 // Don't complain about mismatches for -length if the method we
2238 // chose has an integral result type.
2239 return (chosen->getResultType()->isIntegerType());
2240}
2241
Sebastian Redldb9d2142010-08-02 23:18:59 +00002242ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002243 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002244 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002245 if (ExternalSource)
2246 ReadMethodPool(Sel);
2247
Sebastian Redldb9d2142010-08-02 23:18:59 +00002248 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002249 if (Pos == MethodPool.end())
2250 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002251
Douglas Gregorf0e00042013-01-16 18:47:38 +00002252 // Gather the non-hidden methods.
Sebastian Redldb9d2142010-08-02 23:18:59 +00002253 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Robert Wilhelme7205c02013-08-10 12:33:24 +00002254 SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002255 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Douglas Gregorf0e00042013-01-16 18:47:38 +00002256 if (M->Method && !M->Method->isHidden()) {
2257 // If we're not supposed to warn about mismatches, we're done.
2258 if (!warn)
2259 return M->Method;
Mike Stump1eb44332009-09-09 15:08:12 +00002260
Douglas Gregorf0e00042013-01-16 18:47:38 +00002261 Methods.push_back(M->Method);
Sebastian Redldb9d2142010-08-02 23:18:59 +00002262 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002263 }
Douglas Gregorf0e00042013-01-16 18:47:38 +00002264
2265 // If there aren't any visible methods, we're done.
2266 // FIXME: Recover if there are any known-but-hidden methods?
2267 if (Methods.empty())
2268 return 0;
2269
2270 if (Methods.size() == 1)
2271 return Methods[0];
2272
2273 // We found multiple methods, so we may have to complain.
2274 bool issueDiagnostic = false, issueError = false;
2275
2276 // We support a warning which complains about *any* difference in
2277 // method signature.
2278 bool strictSelectorMatch =
2279 (receiverIdOrClass && warn &&
2280 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2281 R.getBegin())
2282 != DiagnosticsEngine::Ignored));
2283 if (strictSelectorMatch) {
2284 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2285 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2286 issueDiagnostic = true;
2287 break;
2288 }
2289 }
2290 }
2291
2292 // If we didn't see any strict differences, we won't see any loose
2293 // differences. In ARC, however, we also need to check for loose
2294 // mismatches, because most of them are errors.
2295 if (!strictSelectorMatch ||
2296 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2297 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2298 // This checks if the methods differ in type mismatch.
2299 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2300 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2301 issueDiagnostic = true;
2302 if (getLangOpts().ObjCAutoRefCount)
2303 issueError = true;
2304 break;
2305 }
2306 }
2307
2308 if (issueDiagnostic) {
2309 if (issueError)
2310 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2311 else if (strictSelectorMatch)
2312 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2313 else
2314 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2315
2316 Diag(Methods[0]->getLocStart(),
2317 issueError ? diag::note_possibility : diag::note_using)
2318 << Methods[0]->getSourceRange();
2319 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2320 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2321 << Methods[I]->getSourceRange();
2322 }
2323 }
2324 return Methods[0];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002325}
2326
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002327ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002328 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2329 if (Pos == MethodPool.end())
2330 return 0;
2331
2332 GlobalMethods &Methods = Pos->second;
2333
2334 if (Methods.first.Method && Methods.first.Method->isDefined())
2335 return Methods.first.Method;
2336 if (Methods.second.Method && Methods.second.Method->isDefined())
2337 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002338 return 0;
2339}
2340
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002341static void
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002342HelperSelectorsForTypoCorrection(
2343 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2344 StringRef Typo, const ObjCMethodDecl * Method) {
2345 const unsigned MaxEditDistance = 1;
2346 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieu4fe96442013-06-06 02:22:29 +00002347 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002348
2349 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2350 if (MinPossibleEditDistance > 0 &&
2351 Typo.size() / MinPossibleEditDistance < 1)
2352 return;
2353 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2354 if (EditDistance > MaxEditDistance)
2355 return;
2356 if (EditDistance == BestEditDistance)
2357 BestMethod.push_back(Method);
2358 else if (EditDistance < BestEditDistance) {
2359 BestMethod.clear();
2360 BestMethod.push_back(Method);
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002361 }
2362}
2363
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002364static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2365 QualType ObjectType) {
2366 if (ObjectType.isNull())
2367 return true;
2368 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2369 return true;
2370 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != 0;
2371}
2372
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002373const ObjCMethodDecl *
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002374Sema::SelectorsForTypoCorrection(Selector Sel,
2375 QualType ObjectType) {
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002376 unsigned NumArgs = Sel.getNumArgs();
2377 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002378 bool ObjectIsId = true, ObjectIsClass = true;
2379 if (ObjectType.isNull())
2380 ObjectIsId = ObjectIsClass = false;
2381 else if (!ObjectType->isObjCObjectPointerType())
2382 return 0;
2383 else if (const ObjCObjectPointerType *ObjCPtr =
2384 ObjectType->getAsObjCInterfacePointerType()) {
2385 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2386 ObjectIsId = ObjectIsClass = false;
2387 }
2388 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2389 ObjectIsClass = false;
2390 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2391 ObjectIsId = false;
2392 else
2393 return 0;
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002394
2395 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2396 e = MethodPool.end(); b != e; b++) {
2397 // instance methods
2398 for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
2399 if (M->Method &&
Fariborz Jahaniancd9c9b52013-06-18 17:10:58 +00002400 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2401 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002402 if (ObjectIsId)
2403 Methods.push_back(M->Method);
2404 else if (!ObjectIsClass &&
2405 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2406 Methods.push_back(M->Method);
2407 }
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002408 // class methods
2409 for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
2410 if (M->Method &&
Fariborz Jahaniancd9c9b52013-06-18 17:10:58 +00002411 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2412 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002413 if (ObjectIsClass)
2414 Methods.push_back(M->Method);
2415 else if (!ObjectIsId &&
2416 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2417 Methods.push_back(M->Method);
2418 }
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002419 }
2420
2421 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2422 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2423 HelperSelectorsForTypoCorrection(SelectedMethods,
2424 Sel.getAsString(), Methods[i]);
2425 }
2426 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : NULL;
2427}
2428
2429static void
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002430HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S,
2431 ObjCMethodList &MethList) {
2432 ObjCMethodList *M = &MethList;
2433 ObjCMethodDecl *TargetMethod = M->Method;
2434 while (TargetMethod &&
2435 isa<ObjCImplDecl>(TargetMethod->getDeclContext())) {
2436 M = M->getNext();
2437 TargetMethod = M ? M->Method : 0;
2438 }
2439 if (!TargetMethod)
2440 return;
2441 bool FirstTime = true;
2442 for (M = M->getNext(); M; M=M->getNext()) {
2443 ObjCMethodDecl *MatchingMethodDecl = M->Method;
2444 if (isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext()))
2445 continue;
2446 if (!S.MatchTwoMethodDeclarations(TargetMethod,
2447 MatchingMethodDecl, Sema::MMS_loose)) {
2448 if (FirstTime) {
2449 FirstTime = false;
2450 S.Diag(TargetMethod->getLocation(), diag::warning_multiple_selectors)
2451 << TargetMethod->getSelector();
2452 }
2453 S.Diag(MatchingMethodDecl->getLocation(), diag::note_also_found);
2454 }
2455 }
2456}
2457
2458void Sema::DiagnoseMismatchedMethodsInGlobalPool() {
2459 unsigned DIAG = diag::warning_multiple_selectors;
2460 if (Diags.getDiagnosticLevel(DIAG, SourceLocation())
2461 == DiagnosticsEngine::Ignored)
2462 return;
2463 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2464 e = MethodPool.end(); b != e; b++) {
2465 // first, instance methods
2466 ObjCMethodList &InstMethList = b->second.first;
2467 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, InstMethList);
Fariborz Jahanian639aa522013-05-30 21:52:50 +00002468 // second, class methods
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002469 ObjCMethodList &ClsMethList = b->second.second;
2470 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, ClsMethList);
2471 }
2472}
2473
2474/// DiagnoseDuplicateIvars -
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002475/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002476/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002477/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002478/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002479void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2480 ObjCInterfaceDecl *SID) {
2481 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2482 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002483 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002484 if (Ivar->isInvalidDecl())
2485 continue;
2486 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2487 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2488 if (prevIvar) {
2489 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2490 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2491 Ivar->setInvalidDecl();
2492 }
2493 }
2494 }
2495}
2496
Erik Verbruggend64251f2011-12-06 09:25:23 +00002497Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2498 switch (CurContext->getDeclKind()) {
2499 case Decl::ObjCInterface:
2500 return Sema::OCK_Interface;
2501 case Decl::ObjCProtocol:
2502 return Sema::OCK_Protocol;
2503 case Decl::ObjCCategory:
2504 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2505 return Sema::OCK_ClassExtension;
2506 else
2507 return Sema::OCK_Category;
2508 case Decl::ObjCImplementation:
2509 return Sema::OCK_Implementation;
2510 case Decl::ObjCCategoryImpl:
2511 return Sema::OCK_CategoryImplementation;
2512
2513 default:
2514 return Sema::OCK_None;
2515 }
2516}
2517
Fariborz Jahaniana3c62462013-07-16 15:33:19 +00002518// Note: For class/category implementations, allMethods is always null.
Robert Wilhelm0111e4d2013-07-17 21:14:35 +00002519Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd, ArrayRef<Decl *> allMethods,
Fariborz Jahanian80f8aca2013-07-17 00:05:08 +00002520 ArrayRef<DeclGroupPtrTy> allTUVars) {
Erik Verbruggend64251f2011-12-06 09:25:23 +00002521 if (getObjCContainerKind() == Sema::OCK_None)
2522 return 0;
2523
2524 assert(AtEnd.isValid() && "Invalid location for '@end'");
2525
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002526 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2527 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002528
Mike Stump1eb44332009-09-09 15:08:12 +00002529 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002530 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2531 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002532 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002533
Steve Naroff0701bbb2009-01-08 17:28:14 +00002534 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2535 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2536 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2537
Fariborz Jahaniana3c62462013-07-16 15:33:19 +00002538 for (unsigned i = 0, e = allMethods.size(); i != e; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002539 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002540 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002541
2542 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002543 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002544 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002545 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002546 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002547 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002548 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002549 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002550 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002551 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002552 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002553 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002554 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002555 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002556 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002557 if (!Context.getSourceManager().isInSystemHeader(
2558 Method->getLocation()))
2559 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2560 << Method->getDeclName();
2561 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2562 }
Chris Lattner4d391482007-12-12 07:09:47 +00002563 InsMap[Method->getSelector()] = Method;
2564 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002565 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002566 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002567 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002568 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002569 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002570 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002571 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002572 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002573 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002574 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002575 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002576 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002577 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002578 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002579 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002580 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002581 if (!Context.getSourceManager().isInSystemHeader(
2582 Method->getLocation()))
2583 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2584 << Method->getDeclName();
2585 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2586 }
Chris Lattner4d391482007-12-12 07:09:47 +00002587 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002588 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002589 }
2590 }
2591 }
Douglas Gregorb892d702013-01-21 19:42:21 +00002592 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2593 // Nothing to do here.
Steve Naroff09c47192009-01-09 15:36:25 +00002594 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002595 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002596 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002597 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002598
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002599 if (C->IsClassExtension()) {
2600 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2601 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002602 }
Chris Lattner4d391482007-12-12 07:09:47 +00002603 }
Steve Naroff09c47192009-01-09 15:36:25 +00002604 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002605 if (CDecl->getIdentifier())
2606 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2607 // user-defined setter/getter. It also synthesizes setter/getter methods
2608 // and adds them to the DeclContext and global method pools.
2609 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2610 E = CDecl->prop_end();
2611 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002612 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002613 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002614 }
2615 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002616 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002617 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002618 // Any property declared in a class extension might have user
2619 // declared setter or getter in current class extension or one
2620 // of the other class extensions. Mark them as synthesized as
2621 // property will be synthesized when property with same name is
2622 // seen in the @implementation.
Douglas Gregord3297242013-01-16 23:00:23 +00002623 for (ObjCInterfaceDecl::visible_extensions_iterator
2624 Ext = IDecl->visible_extensions_begin(),
2625 ExtEnd = IDecl->visible_extensions_end();
2626 Ext != ExtEnd; ++Ext) {
2627 for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
2628 E = Ext->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002629 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002630 // Skip over properties declared @dynamic
2631 if (const ObjCPropertyImplDecl *PIDecl
2632 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2633 if (PIDecl->getPropertyImplementation()
2634 == ObjCPropertyImplDecl::Dynamic)
2635 continue;
Douglas Gregord3297242013-01-16 23:00:23 +00002636
2637 for (ObjCInterfaceDecl::visible_extensions_iterator
2638 Ext = IDecl->visible_extensions_begin(),
2639 ExtEnd = IDecl->visible_extensions_end();
2640 Ext != ExtEnd; ++Ext) {
2641 if (ObjCMethodDecl *GetterMethod
2642 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002643 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002644 if (!Property->isReadOnly())
Douglas Gregord3297242013-01-16 23:00:23 +00002645 if (ObjCMethodDecl *SetterMethod
2646 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002647 SetterMethod->setPropertyAccessor(true);
Douglas Gregord3297242013-01-16 23:00:23 +00002648 }
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002649 }
2650 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002651 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002652 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002653 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002654
Patrick Beardb2f68202012-04-06 18:12:22 +00002655 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2656 if (IDecl->getSuperClass() == NULL) {
2657 // This class has no superclass, so check that it has been marked with
2658 // __attribute((objc_root_class)).
2659 if (!HasRootClassAttr) {
2660 SourceLocation DeclLoc(IDecl->getLocation());
2661 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2662 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2663 << IDecl->getIdentifier();
2664 // See if NSObject is in the current scope, and if it is, suggest
2665 // adding " : NSObject " to the class declaration.
2666 NamedDecl *IF = LookupSingleName(TUScope,
2667 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2668 DeclLoc, LookupOrdinaryName);
2669 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2670 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2671 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2672 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2673 } else {
2674 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2675 }
2676 }
2677 } else if (HasRootClassAttr) {
2678 // Complain that only root classes may have this attribute.
2679 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2680 }
2681
John McCall260611a2012-06-20 06:18:46 +00002682 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002683 while (IDecl->getSuperClass()) {
2684 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2685 IDecl = IDecl->getSuperClass();
2686 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002687 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002688 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002689 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002690 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002691 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002692 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002693
Chris Lattner4d391482007-12-12 07:09:47 +00002694 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002695 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002696 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregord3297242013-01-16 23:00:23 +00002697 if (ObjCCategoryDecl *Cat
2698 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2699 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattner4d391482007-12-12 07:09:47 +00002700 }
2701 }
2702 }
Chris Lattner682bf922009-03-29 16:50:03 +00002703 if (isInterfaceDeclKind) {
2704 // Reject invalid vardecls.
Fariborz Jahaniana3c62462013-07-16 15:33:19 +00002705 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov18062392013-08-27 13:15:56 +00002706 DeclGroupRef DG = allTUVars[i].get();
Chris Lattner682bf922009-03-29 16:50:03 +00002707 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2708 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002709 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002710 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002711 }
Chris Lattner682bf922009-03-29 16:50:03 +00002712 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002713 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002714 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002715
Fariborz Jahaniana3c62462013-07-16 15:33:19 +00002716 for (unsigned i = 0, e = allTUVars.size(); i != e; i++) {
Serge Pavlov18062392013-08-27 13:15:56 +00002717 DeclGroupRef DG = allTUVars[i].get();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002718 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2719 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002720 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2721 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002722
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002723 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002724 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002725}
2726
2727
2728/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2729/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002730static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002731CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002732 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002733}
2734
Ted Kremenek422bae72010-04-18 04:59:38 +00002735static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002736unsigned countAlignAttr(const AttrVec &A) {
2737 unsigned count=0;
2738 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2739 if ((*i)->getKind() == attr::Aligned)
2740 ++count;
2741 return count;
2742}
2743
2744static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002745bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2746 const AttrVec &A) {
2747 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002748 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002749 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002750 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002751
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002752 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002753 // But implementation has attributes. This is invalid.
2754 // Except when implementation has 'Align' attribute which is
2755 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002756 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002757 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002758
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002759 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002760
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002761 unsigned countAlignOnImpl = countAlignAttr(A);
2762 if (!countAlignOnImpl && (A.size() != D.size()))
2763 return true;
2764 else if (countAlignOnImpl) {
2765 unsigned countAlignOnDecl = countAlignAttr(D);
2766 if (countAlignOnDecl && (A.size() != D.size()))
2767 return true;
2768 else if (!countAlignOnDecl &&
2769 ((A.size()-countAlignOnImpl) != D.size()))
2770 return true;
2771 }
2772
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002773 // attributes on method declaration and definition must match exactly.
2774 // Note that we have at most a couple of attributes on methods, so this
2775 // n*n search is good enough.
2776 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002777 if ((*i)->getKind() == attr::Aligned)
2778 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002779 bool match = false;
2780 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2781 if ((*i)->getKind() == (*i1)->getKind()) {
2782 match = true;
2783 break;
2784 }
2785 }
2786 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002787 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002788 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002789
Sean Huntcf807c42010-08-18 23:23:40 +00002790 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002791}
2792
Douglas Gregor926df6c2011-06-11 01:09:30 +00002793/// \brief Check whether the declared result type of the given Objective-C
2794/// method declaration is compatible with the method's class.
2795///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002796static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002797CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2798 ObjCInterfaceDecl *CurrentClass) {
2799 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002800
2801 // If an Objective-C method inherits its related result type, then its
2802 // declared result type must be compatible with its own class type. The
2803 // declared result type is compatible if:
2804 if (const ObjCObjectPointerType *ResultObjectType
2805 = ResultType->getAs<ObjCObjectPointerType>()) {
2806 // - it is id or qualified id, or
2807 if (ResultObjectType->isObjCIdType() ||
2808 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002809 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002810
2811 if (CurrentClass) {
2812 if (ObjCInterfaceDecl *ResultClass
2813 = ResultObjectType->getInterfaceDecl()) {
2814 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002815 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002816 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002817
2818 // - it is a superclass of the method's class type
2819 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002820 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002821 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002822 } else {
2823 // Any Objective-C pointer type might be acceptable for a protocol
2824 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002825 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002826 }
2827 }
2828
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002829 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002830}
2831
John McCall6c2c2502011-07-22 02:45:48 +00002832namespace {
2833/// A helper class for searching for methods which a particular method
2834/// overrides.
2835class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002836public:
John McCall6c2c2502011-07-22 02:45:48 +00002837 Sema &S;
2838 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002839 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002840 bool Recursive;
2841
2842public:
2843 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2844 Selector selector = method->getSelector();
2845
2846 // Bypass this search if we've never seen an instance/class method
2847 // with this selector before.
2848 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2849 if (it == S.MethodPool.end()) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00002850 if (!S.getExternalSource()) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002851 S.ReadMethodPool(selector);
2852
2853 it = S.MethodPool.find(selector);
2854 if (it == S.MethodPool.end())
2855 return;
John McCall6c2c2502011-07-22 02:45:48 +00002856 }
2857 ObjCMethodList &list =
2858 method->isInstanceMethod() ? it->second.first : it->second.second;
2859 if (!list.Method) return;
2860
2861 ObjCContainerDecl *container
2862 = cast<ObjCContainerDecl>(method->getDeclContext());
2863
2864 // Prevent the search from reaching this container again. This is
2865 // important with categories, which override methods from the
2866 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002867 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2868 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002869 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2870 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002871 } else {
2872 searchFromContainer(container);
2873 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002874 }
John McCall6c2c2502011-07-22 02:45:48 +00002875
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002876 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002877 iterator begin() const { return Overridden.begin(); }
2878 iterator end() const { return Overridden.end(); }
2879
2880private:
2881 void searchFromContainer(ObjCContainerDecl *container) {
2882 if (container->isInvalidDecl()) return;
2883
2884 switch (container->getDeclKind()) {
2885#define OBJCCONTAINER(type, base) \
2886 case Decl::type: \
2887 searchFrom(cast<type##Decl>(container)); \
2888 break;
2889#define ABSTRACT_DECL(expansion)
2890#define DECL(type, base) \
2891 case Decl::type:
2892#include "clang/AST/DeclNodes.inc"
2893 llvm_unreachable("not an ObjC container!");
2894 }
2895 }
2896
2897 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002898 if (!protocol->hasDefinition())
2899 return;
2900
John McCall6c2c2502011-07-22 02:45:48 +00002901 // A method in a protocol declaration overrides declarations from
2902 // referenced ("parent") protocols.
2903 search(protocol->getReferencedProtocols());
2904 }
2905
2906 void searchFrom(ObjCCategoryDecl *category) {
2907 // A method in a category declaration overrides declarations from
2908 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002909 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002910 search(category->getReferencedProtocols());
2911 }
2912
2913 void searchFrom(ObjCCategoryImplDecl *impl) {
2914 // A method in a category definition that has a category
2915 // declaration overrides declarations from the category
2916 // declaration.
2917 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2918 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002919 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2920 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002921
2922 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002923 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2924 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002925 }
2926 }
2927
2928 void searchFrom(ObjCInterfaceDecl *iface) {
2929 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002930 if (!iface->hasDefinition())
2931 return;
2932
John McCall6c2c2502011-07-22 02:45:48 +00002933 // - categories,
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002934 for (ObjCInterfaceDecl::known_categories_iterator
2935 cat = iface->known_categories_begin(),
2936 catEnd = iface->known_categories_end();
Douglas Gregord3297242013-01-16 23:00:23 +00002937 cat != catEnd; ++cat) {
2938 search(*cat);
2939 }
John McCall6c2c2502011-07-22 02:45:48 +00002940
2941 // - the super class, and
2942 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2943 search(super);
2944
2945 // - any referenced protocols.
2946 search(iface->getReferencedProtocols());
2947 }
2948
2949 void searchFrom(ObjCImplementationDecl *impl) {
2950 // A method in a class implementation overrides declarations from
2951 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002952 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2953 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002954 }
2955
2956
2957 void search(const ObjCProtocolList &protocols) {
2958 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2959 i != e; ++i)
2960 search(*i);
2961 }
2962
2963 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002964 // Check for a method in this container which matches this selector.
2965 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002966 Method->isInstanceMethod(),
2967 /*AllowHidden=*/true);
John McCall6c2c2502011-07-22 02:45:48 +00002968
2969 // If we find one, record it and bail out.
2970 if (meth) {
2971 Overridden.insert(meth);
2972 return;
2973 }
2974
2975 // Otherwise, search for methods that a hypothetical method here
2976 // would have overridden.
2977
2978 // Note that we're now in a recursive case.
2979 Recursive = true;
2980
2981 searchFromContainer(container);
2982 }
2983};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002984}
2985
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002986void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2987 ObjCInterfaceDecl *CurrentClass,
2988 ResultTypeCompatibilityKind RTC) {
2989 // Search for overridden methods and merge information down from them.
2990 OverrideSearch overrides(*this, ObjCMethod);
2991 // Keep track if the method overrides any method in the class's base classes,
2992 // its protocols, or its categories' protocols; we will keep that info
2993 // in the ObjCMethodDecl.
2994 // For this info, a method in an implementation is not considered as
2995 // overriding the same method in the interface or its categories.
2996 bool hasOverriddenMethodsInBaseOrProtocol = false;
2997 for (OverrideSearch::iterator
2998 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2999 ObjCMethodDecl *overridden = *i;
3000
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00003001 if (!hasOverriddenMethodsInBaseOrProtocol) {
3002 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
3003 CurrentClass != overridden->getClassInterface() ||
3004 overridden->isOverriding()) {
3005 hasOverriddenMethodsInBaseOrProtocol = true;
3006
3007 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
3008 // OverrideSearch will return as "overridden" the same method in the
3009 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
3010 // check whether a category of a base class introduced a method with the
3011 // same selector, after the interface method declaration.
3012 // To avoid unnecessary lookups in the majority of cases, we use the
3013 // extra info bits in GlobalMethodPool to check whether there were any
3014 // category methods with this selector.
3015 GlobalMethodPool::iterator It =
3016 MethodPool.find(ObjCMethod->getSelector());
3017 if (It != MethodPool.end()) {
3018 ObjCMethodList &List =
3019 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
3020 unsigned CategCount = List.getBits();
3021 if (CategCount > 0) {
3022 // If the method is in a category we'll do lookup if there were at
3023 // least 2 category methods recorded, otherwise only one will do.
3024 if (CategCount > 1 ||
3025 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
3026 OverrideSearch overrides(*this, overridden);
3027 for (OverrideSearch::iterator
3028 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
3029 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidisab3d5092013-04-27 00:10:12 +00003030 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
3031 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00003032 hasOverriddenMethodsInBaseOrProtocol = true;
3033 overridden->setOverriding(true);
3034 break;
3035 }
3036 }
3037 }
3038 }
3039 }
3040 }
3041 }
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003042
3043 // Propagate down the 'related result type' bit from overridden methods.
3044 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
3045 ObjCMethod->SetRelatedResultType();
3046
3047 // Then merge the declarations.
3048 mergeObjCMethodDecls(ObjCMethod, overridden);
3049
3050 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3051 continue; // Conflicting properties are detected elsewhere.
3052
3053 // Check for overriding methods
3054 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3055 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3056 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3057 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3058
3059 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00003060 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3061 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003062 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3063 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00003064 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3065 PrevE = overridden->param_end();
3066 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003067 assert(PrevI != overridden->param_end() && "Param mismatch");
3068 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3069 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3070 // If type of argument of method in this class does not match its
3071 // respective argument type in the super class method, issue warning;
3072 if (!Context.typesAreCompatible(T1, T2)) {
3073 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3074 << T1 << T2;
3075 Diag(overridden->getLocation(), diag::note_previous_declaration);
3076 break;
3077 }
3078 }
3079 }
3080 }
3081
3082 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3083}
3084
John McCalld226f652010-08-21 09:40:31 +00003085Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003086 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00003087 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003088 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00003089 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003090 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00003091 Selector Sel,
3092 // optional arguments. The number of types/arguments is obtained
3093 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00003094 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003095 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00003096 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00003097 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00003098 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003099 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00003100 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00003101 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00003102 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003103 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3104 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00003105 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00003106
Douglas Gregore97179c2011-09-08 01:46:34 +00003107 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003108 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00003109 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003110 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00003111
Eli Friedmanddb5a392013-06-14 21:14:10 +00003112 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
John McCalld226f652010-08-21 09:40:31 +00003113 return 0;
Eli Friedmanddb5a392013-06-14 21:14:10 +00003114
Douglas Gregore97179c2011-09-08 01:46:34 +00003115 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00003116 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003117 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00003118 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003119 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00003120 }
Mike Stump1eb44332009-09-09 15:08:12 +00003121
3122 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003123 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003124 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003125 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003126 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00003127 MethodType == tok::minus, isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +00003128 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00003129 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00003130 MethodDeclKind == tok::objc_optional
3131 ? ObjCMethodDecl::Optional
3132 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00003133 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00003134
Chris Lattner5f9e2722011-07-23 10:55:15 +00003135 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00003136
Chris Lattner7db638d2009-04-11 19:42:43 +00003137 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00003138 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00003139 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00003140
David Blaikie7247c882013-05-15 07:37:26 +00003141 if (!ArgInfo[i].Type) {
John McCall58e46772009-10-23 21:48:59 +00003142 ArgType = Context.getObjCIdType();
3143 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00003144 } else {
John McCall58e46772009-10-23 21:48:59 +00003145 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnere294d3f2009-04-11 18:57:04 +00003146 }
Mike Stump1eb44332009-09-09 15:08:12 +00003147
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003148 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3149 LookupOrdinaryName, ForRedeclaration);
3150 LookupName(R, S);
3151 if (R.isSingleResult()) {
3152 NamedDecl *PrevDecl = R.getFoundDecl();
3153 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00003154 Diag(ArgInfo[i].NameLoc,
3155 (MethodDefinition ? diag::warn_method_param_redefinition
3156 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003157 << ArgInfo[i].Name;
3158 Diag(PrevDecl->getLocation(),
3159 diag::note_previous_declaration);
3160 }
3161 }
3162
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003163 SourceLocation StartLoc = DI
3164 ? DI->getTypeLoc().getBeginLoc()
3165 : ArgInfo[i].NameLoc;
3166
John McCall81ef3e62011-04-23 02:46:06 +00003167 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3168 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003169 ArgType, DI, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00003170
John McCall70798862011-05-02 00:30:12 +00003171 Param->setObjCMethodScopeInfo(i);
3172
Chris Lattner0ed844b2008-04-04 06:12:32 +00003173 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00003174 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00003175
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00003176 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003177 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003178
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00003179 if (Param->hasAttr<BlocksAttr>()) {
3180 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3181 Param->setInvalidDecl();
3182 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003183 S->AddDecl(Param);
3184 IdResolver.AddDecl(Param);
3185
Chris Lattner0ed844b2008-04-04 06:12:32 +00003186 Params.push_back(Param);
3187 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003188
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003189 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00003190 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003191 QualType ArgType = Param->getType();
3192 if (ArgType.isNull())
3193 ArgType = Context.getObjCIdType();
3194 else
3195 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00003196 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedmanddb5a392013-06-14 21:14:10 +00003197
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003198 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003199 Params.push_back(Param);
3200 }
3201
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003202 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003203 ObjCMethod->setObjCDeclQualifier(
3204 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00003205
3206 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003207 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00003208
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003209 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00003210 const ObjCMethodDecl *PrevMethod = 0;
3211 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00003212 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003213 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3214 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003215 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003216 PrevMethod = ImpDecl->getClassMethod(Sel);
3217 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003218 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00003219
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00003220 ObjCMethodDecl *IMD = 0;
3221 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3222 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3223 ObjCMethod->isInstanceMethod());
Fariborz Jahanian38b3bd82013-07-09 22:02:20 +00003224 if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
3225 !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
3226 // merge the attribute into implementation.
3227 ObjCMethod->addAttr(
3228 new (Context) ObjCRequiresSuperAttr(ObjCMethod->getLocation(), Context));
3229 }
Sean Huntcf807c42010-08-18 23:23:40 +00003230 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00003231 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00003232 SourceLocation MethodLoc = IMD->getLocation();
3233 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
3234 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00003235 Diag(MethodLoc, diag::note_method_declared_at)
3236 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00003237 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00003238 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003239 } else {
3240 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003241 }
John McCall6c2c2502011-07-22 02:45:48 +00003242
Chris Lattner4d391482007-12-12 07:09:47 +00003243 if (PrevMethod) {
3244 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00003245 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00003246 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00003247 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanianfbff0c42013-05-13 17:27:00 +00003248 ObjCMethod->setInvalidDecl();
3249 return ObjCMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00003250 }
John McCall54abf7d2009-11-04 02:18:39 +00003251
Douglas Gregor926df6c2011-06-11 01:09:30 +00003252 // If this Objective-C method does not have a related result type, but we
3253 // are allowed to infer related result types, try to do so based on the
3254 // method family.
3255 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3256 if (!CurrentClass) {
3257 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3258 CurrentClass = Cat->getClassInterface();
3259 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3260 CurrentClass = Impl->getClassInterface();
3261 else if (ObjCCategoryImplDecl *CatImpl
3262 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3263 CurrentClass = CatImpl->getClassInterface();
3264 }
John McCall6c2c2502011-07-22 02:45:48 +00003265
Douglas Gregore97179c2011-09-08 01:46:34 +00003266 ResultTypeCompatibilityKind RTC
3267 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00003268
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003269 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00003270
John McCallf85e1932011-06-15 23:02:42 +00003271 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00003272 if (getLangOpts().ObjCAutoRefCount)
John McCallb8463812013-04-04 01:38:37 +00003273 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCallf85e1932011-06-15 23:02:42 +00003274
Douglas Gregore97179c2011-09-08 01:46:34 +00003275 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003276 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00003277 !ObjCMethod->hasRelatedResultType() &&
3278 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00003279 bool InferRelatedResultType = false;
3280 switch (ObjCMethod->getMethodFamily()) {
3281 case OMF_None:
3282 case OMF_copy:
3283 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00003284 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003285 case OMF_mutableCopy:
3286 case OMF_release:
3287 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00003288 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003289 break;
3290
3291 case OMF_alloc:
3292 case OMF_new:
3293 InferRelatedResultType = ObjCMethod->isClassMethod();
3294 break;
3295
3296 case OMF_init:
3297 case OMF_autorelease:
3298 case OMF_retain:
3299 case OMF_self:
3300 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3301 break;
3302 }
3303
John McCall6c2c2502011-07-22 02:45:48 +00003304 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003305 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003306 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003307
3308 ActOnDocumentableDecl(ObjCMethod);
3309
John McCalld226f652010-08-21 09:40:31 +00003310 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003311}
3312
Chris Lattnercc98eac2008-12-17 07:13:27 +00003313bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003314 // Following is also an error. But it is caused by a missing @end
3315 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003316 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003317 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003318
3319 // If we switched context to translation unit while we are still lexically in
3320 // an objc container, it means the parser missed emitting an error.
3321 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3322 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003323
Anders Carlsson15281452008-11-04 16:57:32 +00003324 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3325 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003326
Anders Carlsson15281452008-11-04 16:57:32 +00003327 return true;
3328}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003329
James Dennett1dfbd922012-06-14 21:40:34 +00003330/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003331/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003332void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003333 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003334 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003335 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003336 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003337 if (!Class) {
3338 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3339 return;
3340 }
John McCall260611a2012-06-20 06:18:46 +00003341 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003342 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3343 return;
3344 }
Mike Stump1eb44332009-09-09 15:08:12 +00003345
Chris Lattnercc98eac2008-12-17 07:13:27 +00003346 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003347 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003348 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003349 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003350 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003351 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003352 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003353 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3354 /*FIXME: StartL=*/ID->getLocation(),
3355 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003356 ID->getIdentifier(), ID->getType(),
3357 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003358 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003359 }
Mike Stump1eb44332009-09-09 15:08:12 +00003360
Chris Lattnercc98eac2008-12-17 07:13:27 +00003361 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003362 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003363 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003364 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003365 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003366 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003367 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003368 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003369 }
3370}
3371
Douglas Gregor160b5632010-04-26 17:32:49 +00003372/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003373VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3374 SourceLocation StartLoc,
3375 SourceLocation IdLoc,
3376 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003377 bool Invalid) {
3378 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3379 // duration shall not be qualified by an address-space qualifier."
3380 // Since all parameters have automatic store duration, they can not have
3381 // an address space.
3382 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003383 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003384 Invalid = true;
3385 }
3386
3387 // An @catch parameter must be an unqualified object pointer type;
3388 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3389 if (Invalid) {
3390 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003391 } else if (T->isDependentType()) {
3392 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003393 } else if (!T->isObjCObjectPointerType()) {
3394 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003395 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003396 } else if (T->isObjCQualifiedIdType()) {
3397 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003398 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003399 }
3400
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003401 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003402 T, TInfo, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003403 New->setExceptionVariable(true);
3404
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003405 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003406 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003407 Invalid = true;
3408
Douglas Gregor160b5632010-04-26 17:32:49 +00003409 if (Invalid)
3410 New->setInvalidDecl();
3411 return New;
3412}
3413
John McCalld226f652010-08-21 09:40:31 +00003414Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003415 const DeclSpec &DS = D.getDeclSpec();
3416
3417 // We allow the "register" storage class on exception variables because
3418 // GCC did, but we drop it completely. Any other storage class is an error.
3419 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3420 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3421 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithec642442013-04-12 22:46:28 +00003422 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003423 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithec642442013-04-12 22:46:28 +00003424 << DeclSpec::getSpecifierName(SCS);
3425 }
3426 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3427 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3428 diag::err_invalid_thread)
3429 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregor160b5632010-04-26 17:32:49 +00003430 D.getMutableDeclSpec().ClearStorageClassSpecs();
3431
Richard Smithc7f81162013-03-18 22:52:47 +00003432 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregor160b5632010-04-26 17:32:49 +00003433
3434 // Check that there are no default arguments inside the type of this
3435 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003436 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003437 CheckExtraCXXDefaultArguments(D);
3438
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003439 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003440 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003441
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003442 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3443 D.getSourceRange().getBegin(),
3444 D.getIdentifierLoc(),
3445 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003446 D.isInvalidType());
3447
3448 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3449 if (D.getCXXScopeSpec().isSet()) {
3450 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3451 << D.getCXXScopeSpec().getRange();
3452 New->setInvalidDecl();
3453 }
3454
3455 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003456 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003457 if (D.getIdentifier())
3458 IdResolver.AddDecl(New);
3459
3460 ProcessDeclAttributes(S, New, D);
3461
3462 if (New->hasAttr<BlocksAttr>())
3463 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003464 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003465}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003466
3467/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003468/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003469void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003470 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003471 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3472 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003473 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003474 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003475 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003476 }
3477}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003478
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003479void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003480 // Load referenced selectors from the external source.
3481 if (ExternalSource) {
3482 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3483 ExternalSource->ReadReferencedSelectors(Sels);
3484 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3485 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3486 }
3487
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00003488 DiagnoseMismatchedMethodsInGlobalPool();
3489
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003490 // Warning will be issued only when selector table is
3491 // generated (which means there is at lease one implementation
3492 // in the TU). This is to match gcc's behavior.
3493 if (ReferencedSelectors.empty() ||
3494 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003495 return;
3496 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3497 ReferencedSelectors.begin(),
3498 E = ReferencedSelectors.end(); S != E; ++S) {
3499 Selector Sel = (*S).first;
3500 if (!LookupImplementedMethodInGlobalPool(Sel))
3501 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3502 }
3503 return;
3504}