blob: bbdf853fc8a140b0c581373f6146de8fbeb11a81 [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)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000516 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
517 Diag(SuperLoc, diag::err_undef_superclass_suggest)
518 << SuperName << ClassName << PrevDecl->getDeclName();
519 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
520 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000521 }
522 }
523
Douglas Gregor60ef3082011-12-15 00:29:59 +0000524 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000525 Diag(SuperLoc, diag::err_recursive_superclass)
526 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000527 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000528 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000529 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000530 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000531
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000532 // Diagnose classes that inherit from deprecated classes.
533 if (SuperClassDecl)
534 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000536 if (PrevDecl && SuperClassDecl == 0) {
537 // The previous declaration was not a class decl. Check if we have a
538 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000539 if (const TypedefNameDecl *TDecl =
540 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000541 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000542 if (T->isObjCObjectType()) {
Fariborz Jahanian740991b2013-04-04 18:45:52 +0000543 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000544 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian740991b2013-04-04 18:45:52 +0000545 // This handles the following case:
546 // @interface NewI @end
547 // typedef NewI DeprI __attribute__((deprecated("blah")))
548 // @interface SI : DeprI /* warn here */ @end
549 (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc);
550 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000551 }
552 }
Mike Stump1eb44332009-09-09 15:08:12 +0000553
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000554 // This handles the following case:
555 //
556 // typedef int SuperClass;
557 // @interface MyClass : SuperClass {} @end
558 //
559 if (!SuperClassDecl) {
560 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
561 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000562 }
563 }
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Richard Smith162e1c12011-04-15 14:24:37 +0000565 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000566 if (!SuperClassDecl)
567 Diag(SuperLoc, diag::err_undef_superclass)
568 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000569 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000570 Context.getObjCInterfaceType(SuperClassDecl),
571 diag::err_forward_superclass,
572 SuperClassDecl->getDeclName(),
573 ClassName,
574 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000575 SuperClassDecl = 0;
576 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000577 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000578 IDecl->setSuperClass(SuperClassDecl);
579 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000580 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000581 }
Chris Lattner4d391482007-12-12 07:09:47 +0000582 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000583 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000584 }
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Sebastian Redl0b17c612010-08-13 00:28:03 +0000586 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000587 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000588 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000589 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000590 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000591 }
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Anders Carlsson15281452008-11-04 16:57:32 +0000593 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000594 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000595}
596
Richard Smithde01b7a2012-08-08 23:32:13 +0000597/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000598/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000599Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
600 IdentifierInfo *AliasName,
601 SourceLocation AliasLocation,
602 IdentifierInfo *ClassName,
603 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000604 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000605 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000606 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000607 if (ADecl) {
Eli Friedman104f96b2013-06-21 01:49:53 +0000608 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000609 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000610 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000611 }
612 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000613 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000614 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000615 if (const TypedefNameDecl *TDecl =
616 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000617 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000618 if (T->isObjCObjectType()) {
619 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000620 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000621 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000622 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000623 }
624 }
625 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000626 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
627 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000628 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000629 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000630 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000631 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000632 }
Mike Stump1eb44332009-09-09 15:08:12 +0000633
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000634 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000635 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000636 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Anders Carlsson15281452008-11-04 16:57:32 +0000638 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000639 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000640
John McCalld226f652010-08-21 09:40:31 +0000641 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000642}
643
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000644bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000645 IdentifierInfo *PName,
646 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000647 const ObjCList<ObjCProtocolDecl> &PList) {
648
649 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000650 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
651 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000652 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
653 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000654 if (PDecl->getIdentifier() == PName) {
655 Diag(Ploc, diag::err_protocol_has_circular_dependency);
656 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000657 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000658 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000659
660 if (!PDecl->hasDefinition())
661 continue;
662
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000663 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
664 PDecl->getLocation(), PDecl->getReferencedProtocols()))
665 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000666 }
667 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000668 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000669}
670
John McCalld226f652010-08-21 09:40:31 +0000671Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000672Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
673 IdentifierInfo *ProtocolName,
674 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000675 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000676 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000677 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000678 SourceLocation EndProtoLoc,
679 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000680 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000681 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000682 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000683 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
684 ForRedeclaration);
685 ObjCProtocolDecl *PDecl = 0;
686 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
687 // If we already have a definition, complain.
688 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
689 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Douglas Gregor27c6da22012-01-01 20:30:41 +0000691 // Create a new protocol that is completely distinct from previous
692 // declarations, and do not make this protocol available for name lookup.
693 // That way, we'll end up completely ignoring the duplicate.
694 // FIXME: Can we turn this into an error?
695 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
696 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000697 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000698 PDecl->startDefinition();
699 } else {
700 if (PrevDecl) {
701 // Check for circular dependencies among protocol declarations. This can
702 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000703 ObjCList<ObjCProtocolDecl> PList;
704 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
705 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000706 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000707 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000708
709 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000710 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000711 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000712 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000713
Douglas Gregor6e378de2009-04-23 23:18:26 +0000714 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000715 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000716 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000717
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000718 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000719 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000720
721 // Merge attributes from previous declarations.
722 if (PrevDecl)
723 mergeDeclAttributes(PDecl, PrevDecl);
724
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000725 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000726 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000727 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000728 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
731 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000732 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000733}
734
735/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000736/// issues an error if they are not declared. It returns list of
737/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000738void
Chris Lattnere13b9592008-07-26 04:03:38 +0000739Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000740 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000741 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000742 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000743 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000744 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
745 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000746 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000747 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000748 TypoCorrection Corrected = CorrectTypo(
749 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000750 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000751 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000752 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000753 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000754 Diag(PDecl->getLocation(), diag::note_previous_decl)
755 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000756 }
757 }
758
759 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000760 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000761 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000762 continue;
763 }
Fariborz Jahanian3c9a0242013-04-09 17:52:29 +0000764 // If this is a forward protocol declaration, get its definition.
765 if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
766 PDecl = PDecl->getDefinition();
767
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000768 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000769
770 // If this is a forward declaration and we are supposed to warn in this
771 // case, do it.
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000772 // FIXME: Recover nicely in the hidden case.
773 if (WarnOnDeclarations &&
774 (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000775 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000776 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000777 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000778 }
779}
780
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000781/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000782/// a class method in its extension.
783///
Mike Stump1eb44332009-09-09 15:08:12 +0000784void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000785 ObjCInterfaceDecl *ID) {
786 if (!ID)
787 return; // Possibly due to previous error
788
789 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000790 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
791 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000792 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000793 MethodMap[MD->getSelector()] = MD;
794 }
795
796 if (MethodMap.empty())
797 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000798 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
799 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000800 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000801 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
802 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
803 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
804 << Method->getDeclName();
805 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
806 }
807 }
808}
809
James Dennett1dfbd922012-06-14 21:40:34 +0000810/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000811Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000812Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000813 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000814 unsigned NumElts,
815 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000816 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000817 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000818 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000819 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
820 ForRedeclaration);
821 ObjCProtocolDecl *PDecl
822 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
823 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000824 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000825
826 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000827 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000828
Douglas Gregor3937f872012-01-01 20:33:24 +0000829 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000830 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000831
832 if (PrevDecl)
833 mergeDeclAttributes(PDecl, PrevDecl);
834
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000835 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000836 }
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Rafael Espindola4549d7f2013-07-09 12:05:01 +0000838 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattner4d391482007-12-12 07:09:47 +0000839}
840
John McCalld226f652010-08-21 09:40:31 +0000841Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000842ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
843 IdentifierInfo *ClassName, SourceLocation ClassLoc,
844 IdentifierInfo *CategoryName,
845 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000846 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000847 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000848 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000849 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000850 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000851 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000852
853 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000854
855 if (!IDecl
856 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000857 diag::err_category_forward_interface,
858 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000859 // Create an invalid ObjCCategoryDecl to serve as context for
860 // the enclosing method declarations. We mark the decl invalid
861 // to make it clear that this isn't a valid AST.
862 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000863 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000864 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000865 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000866
867 if (!IDecl)
868 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000869 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000870 }
871
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000872 if (!CategoryName && IDecl->getImplementation()) {
873 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
874 Diag(IDecl->getImplementation()->getLocation(),
875 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000876 }
877
Fariborz Jahanian25760612010-02-15 21:55:26 +0000878 if (CategoryName) {
879 /// Check for duplicate interface declaration for this category
Douglas Gregord3297242013-01-16 23:00:23 +0000880 if (ObjCCategoryDecl *Previous
881 = IDecl->FindCategoryDeclaration(CategoryName)) {
882 // Class extensions can be declared multiple times, categories cannot.
883 Diag(CategoryLoc, diag::warn_dup_category_def)
884 << ClassName << CategoryName;
885 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner70f19542009-02-16 21:26:43 +0000886 }
887 }
Chris Lattner70f19542009-02-16 21:26:43 +0000888
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000889 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
890 ClassLoc, CategoryLoc, CategoryName, IDecl);
891 // FIXME: PushOnScopeChains?
892 CurContext->addDecl(CDecl);
893
Chris Lattner4d391482007-12-12 07:09:47 +0000894 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000895 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000896 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000897 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000898 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000899 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000900 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000901 }
Mike Stump1eb44332009-09-09 15:08:12 +0000902
Anders Carlsson15281452008-11-04 16:57:32 +0000903 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000904 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000905}
906
907/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000908/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000909/// object.
John McCalld226f652010-08-21 09:40:31 +0000910Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000911 SourceLocation AtCatImplLoc,
912 IdentifierInfo *ClassName, SourceLocation ClassLoc,
913 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000914 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000915 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000916 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000917 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
918 if (!CatIDecl) {
919 // Category @implementation with no corresponding @interface.
920 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000921 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
922 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000923 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000924 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000925 }
926 }
927
Mike Stump1eb44332009-09-09 15:08:12 +0000928 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000929 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000930 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000931 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000932 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000933 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000934 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000935 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
936 diag::err_undef_interface)) {
937 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000938 }
Chris Lattner4d391482007-12-12 07:09:47 +0000939
Douglas Gregord0434102009-01-09 00:49:46 +0000940 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000941 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000942
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000943 // If the interface is deprecated/unavailable, warn/error about it.
944 if (IDecl)
945 DiagnoseUseOfDecl(IDecl, ClassLoc);
946
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000947 /// Check that CatName, category name, is not used in another implementation.
948 if (CatIDecl) {
949 if (CatIDecl->getImplementation()) {
950 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
951 << CatName;
952 Diag(CatIDecl->getImplementation()->getLocation(),
953 diag::note_previous_definition);
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000954 CDecl->setInvalidDecl();
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000955 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000956 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000957 // Warn on implementating category of deprecated class under
958 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000959 DiagnoseObjCImplementedDeprecations(*this,
960 dyn_cast<NamedDecl>(IDecl),
961 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000962 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000963 }
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Anders Carlsson15281452008-11-04 16:57:32 +0000965 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000966 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000967}
968
John McCalld226f652010-08-21 09:40:31 +0000969Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000970 SourceLocation AtClassImplLoc,
971 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000972 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000973 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000974 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000975 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000976 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000977 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
978 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000979 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000980 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000981 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000982 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000983 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
984 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000985 } else {
986 // We did not find anything with the name ClassName; try to correct for
987 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000988 ObjCInterfaceValidatorCCC Validator;
989 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000990 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000991 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000992 // Suggest the (potentially) correct interface name. However, put the
993 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000994 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000995 // provide a code-modification hint or use the typo name for recovery,
996 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000997 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000998 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000999 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +00001000 << ClassName << CorrectedName;
1001 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
1002 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +00001003 IDecl = 0;
1004 } else {
1005 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
1006 }
Chris Lattner4d391482007-12-12 07:09:47 +00001007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Chris Lattner4d391482007-12-12 07:09:47 +00001009 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001010 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +00001011 if (SuperClassname) {
1012 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +00001013 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
1014 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001015 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001016 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1017 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +00001018 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +00001019 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001020 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +00001021 if (SDecl && !SDecl->hasDefinition())
1022 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +00001023 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +00001024 Diag(SuperClassLoc, diag::err_undef_superclass)
1025 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +00001026 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001027 // This implementation and its interface do not have the same
1028 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +00001029 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +00001030 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001031 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001032 }
1033 }
1034 }
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Chris Lattner4d391482007-12-12 07:09:47 +00001036 if (!IDecl) {
1037 // Legacy case of @implementation with no corresponding @interface.
1038 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +00001039
Mike Stump390b4cc2009-05-16 07:39:55 +00001040 // FIXME: Do we support attributes on the @implementation? If so we should
1041 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001042 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001043 ClassName, /*PrevDecl=*/0, ClassLoc,
1044 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001045 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001046 if (SDecl) {
1047 IDecl->setSuperClass(SDecl);
1048 IDecl->setSuperClassLoc(SuperClassLoc);
1049 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1050 } else {
1051 IDecl->setEndOfDefinitionLoc(ClassLoc);
1052 }
1053
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001054 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001055 } else {
1056 // Mark the interface as being completed, even if it was just as
1057 // @class ....;
1058 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001059 if (!IDecl->hasDefinition())
1060 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001061 }
Mike Stump1eb44332009-09-09 15:08:12 +00001062
1063 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001064 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
Argyrios Kyrtzidis634c5632013-05-03 18:05:44 +00001065 ClassLoc, AtClassImplLoc, SuperClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Anders Carlsson15281452008-11-04 16:57:32 +00001067 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001068 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001069
Chris Lattner4d391482007-12-12 07:09:47 +00001070 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001071 if (IDecl->getImplementation()) {
1072 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001073 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001074 Diag(IDecl->getImplementation()->getLocation(),
1075 diag::note_previous_definition);
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +00001076 IMPDecl->setInvalidDecl();
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001077 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001078 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001079 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001080 // Warn on implementating deprecated class under
1081 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001082 DiagnoseObjCImplementedDeprecations(*this,
1083 dyn_cast<NamedDecl>(IDecl),
1084 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001085 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001086 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001087}
1088
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001089Sema::DeclGroupPtrTy
1090Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1091 SmallVector<Decl *, 64> DeclsInGroup;
1092 DeclsInGroup.reserve(Decls.size() + 1);
1093
1094 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1095 Decl *Dcl = Decls[i];
1096 if (!Dcl)
1097 continue;
1098 if (Dcl->getDeclContext()->isFileContext())
1099 Dcl->setTopLevelDeclInObjCContainer();
1100 DeclsInGroup.push_back(Dcl);
1101 }
1102
1103 DeclsInGroup.push_back(ObjCImpDecl);
1104
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001105 return BuildDeclaratorGroup(DeclsInGroup, false);
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001106}
1107
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001108void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1109 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001110 SourceLocation RBrace) {
1111 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001112 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001113 if (!IDecl)
1114 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001115 /// Check case of non-existing \@interface decl.
1116 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001117 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001118 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001119 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001120 // Add ivar's to class's DeclContext.
1121 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001122 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001123 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001124 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001125 }
1126
Chris Lattner4d391482007-12-12 07:09:47 +00001127 return;
1128 }
1129 // If implementation has empty ivar list, just return.
1130 if (numIvars == 0)
1131 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001132
Chris Lattner4d391482007-12-12 07:09:47 +00001133 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001134 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001135 if (ImpDecl->getSuperClass())
1136 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1137 for (unsigned i = 0; i < numIvars; i++) {
1138 ObjCIvarDecl* ImplIvar = ivars[i];
1139 if (const ObjCIvarDecl *ClsIvar =
1140 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1141 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1142 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1143 continue;
1144 }
Fariborz Jahanian3b20f582013-06-26 22:10:27 +00001145 // Check class extensions (unnamed categories) for duplicate ivars.
1146 for (ObjCInterfaceDecl::visible_extensions_iterator
1147 Ext = IDecl->visible_extensions_begin(),
1148 ExtEnd = IDecl->visible_extensions_end();
1149 Ext != ExtEnd; ++Ext) {
1150 ObjCCategoryDecl *CDecl = *Ext;
1151 if (const ObjCIvarDecl *ClsExtIvar =
1152 CDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1153 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1154 Diag(ClsExtIvar->getLocation(), diag::note_previous_definition);
1155 continue;
1156 }
1157 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001158 // Instance ivar to Implementation's DeclContext.
1159 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001160 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001161 ImpDecl->addDecl(ImplIvar);
1162 }
1163 return;
1164 }
Chris Lattner4d391482007-12-12 07:09:47 +00001165 // Check interface's Ivar list against those in the implementation.
1166 // names and types must match.
1167 //
Chris Lattner4d391482007-12-12 07:09:47 +00001168 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001169 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001170 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1171 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001172 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001173 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001174 assert (ImplIvar && "missing implementation ivar");
1175 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Steve Naroffca331292009-03-03 14:49:36 +00001177 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001178 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001179 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001180 << ImplIvar->getIdentifier()
1181 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001182 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001183 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1184 ImplIvar->getBitWidthValue(Context) !=
1185 ClsIvar->getBitWidthValue(Context)) {
1186 Diag(ImplIvar->getBitWidth()->getLocStart(),
1187 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1188 Diag(ClsIvar->getBitWidth()->getLocStart(),
1189 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001190 }
Steve Naroffca331292009-03-03 14:49:36 +00001191 // Make sure the names are identical.
1192 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001193 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001194 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001195 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001196 }
1197 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001198 }
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Chris Lattner609e4c72007-12-12 18:11:49 +00001200 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001201 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001202 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001203 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001204}
1205
Steve Naroff3c2eb662008-02-10 21:38:56 +00001206void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001207 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001208 // No point warning no definition of method which is 'unavailable'.
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001209 switch (method->getAvailability()) {
1210 case AR_Available:
1211 case AR_Deprecated:
1212 break;
1213
1214 // Don't warn about unavailable or not-yet-introduced methods.
1215 case AR_NotYetIntroduced:
1216 case AR_Unavailable:
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001217 return;
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001218 }
1219
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001220 // FIXME: For now ignore 'IncompleteImpl'.
1221 // Previously we grouped all unimplemented methods under a single
1222 // warning, but some users strongly voiced that they would prefer
1223 // separate warnings. We will give that approach a try, as that
1224 // matches what we do with protocols.
1225
1226 Diag(ImpLoc, DiagID) << method->getDeclName();
1227
1228 // Issue a note to the original declaration.
1229 SourceLocation MethodLoc = method->getLocStart();
1230 if (MethodLoc.isValid())
1231 Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001232}
1233
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001234/// Determines if type B can be substituted for type A. Returns true if we can
1235/// guarantee that anything that the user will do to an object of type A can
1236/// also be done to an object of type B. This is trivially true if the two
1237/// types are the same, or if B is a subclass of A. It becomes more complex
1238/// in cases where protocols are involved.
1239///
1240/// Object types in Objective-C describe the minimum requirements for an
1241/// object, rather than providing a complete description of a type. For
1242/// example, if A is a subclass of B, then B* may refer to an instance of A.
1243/// The principle of substitutability means that we may use an instance of A
1244/// anywhere that we may use an instance of B - it will implement all of the
1245/// ivars of B and all of the methods of B.
1246///
1247/// This substitutability is important when type checking methods, because
1248/// the implementation may have stricter type definitions than the interface.
1249/// The interface specifies minimum requirements, but the implementation may
1250/// have more accurate ones. For example, a method may privately accept
1251/// instances of B, but only publish that it accepts instances of A. Any
1252/// object passed to it will be type checked against B, and so will implicitly
1253/// by a valid A*. Similarly, a method may return a subclass of the class that
1254/// it is declared as returning.
1255///
1256/// This is most important when considering subclassing. A method in a
1257/// subclass must accept any object as an argument that its superclass's
1258/// implementation accepts. It may, however, accept a more general type
1259/// without breaking substitutability (i.e. you can still use the subclass
1260/// anywhere that you can use the superclass, but not vice versa). The
1261/// converse requirement applies to return types: the return type for a
1262/// subclass method must be a valid object of the kind that the superclass
1263/// advertises, but it may be specified more accurately. This avoids the need
1264/// for explicit down-casting by callers.
1265///
1266/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001267static bool isObjCTypeSubstitutable(ASTContext &Context,
1268 const ObjCObjectPointerType *A,
1269 const ObjCObjectPointerType *B,
1270 bool rejectId) {
1271 // Reject a protocol-unqualified id.
1272 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001273
1274 // If B is a qualified id, then A must also be a qualified id and it must
1275 // implement all of the protocols in B. It may not be a qualified class.
1276 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1277 // stricter definition so it is not substitutable for id<A>.
1278 if (B->isObjCQualifiedIdType()) {
1279 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001280 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1281 QualType(B,0),
1282 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001283 }
1284
1285 /*
1286 // id is a special type that bypasses type checking completely. We want a
1287 // warning when it is used in one place but not another.
1288 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1289
1290
1291 // If B is a qualified id, then A must also be a qualified id (which it isn't
1292 // if we've got this far)
1293 if (B->isObjCQualifiedIdType()) return false;
1294 */
1295
1296 // Now we know that A and B are (potentially-qualified) class types. The
1297 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001298 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001299}
1300
John McCall10302c02010-10-28 02:34:38 +00001301static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1302 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1303}
1304
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001305static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001306 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001307 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001308 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001309 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001310 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001311 if (IsProtocolMethodDecl &&
1312 (MethodDecl->getObjCDeclQualifier() !=
1313 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001314 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001315 S.Diag(MethodImpl->getLocation(),
1316 (IsOverridingMode ?
1317 diag::warn_conflicting_overriding_ret_type_modifiers
1318 : diag::warn_conflicting_ret_type_modifiers))
1319 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001320 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1321 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1322 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1323 }
1324 else
1325 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001326 }
1327
John McCall10302c02010-10-28 02:34:38 +00001328 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001329 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001330 return true;
1331 if (!Warn)
1332 return false;
John McCall10302c02010-10-28 02:34:38 +00001333
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001334 unsigned DiagID =
1335 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1336 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001337
1338 // Mismatches between ObjC pointers go into a different warning
1339 // category, and sometimes they're even completely whitelisted.
1340 if (const ObjCObjectPointerType *ImplPtrTy =
1341 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1342 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001343 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001344 // Allow non-matching return types as long as they don't violate
1345 // the principle of substitutability. Specifically, we permit
1346 // return types that are subclasses of the declared return type,
1347 // or that are more-qualified versions of the declared type.
1348 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001349 return false;
John McCall10302c02010-10-28 02:34:38 +00001350
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001351 DiagID =
1352 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1353 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001354 }
1355 }
1356
1357 S.Diag(MethodImpl->getLocation(), DiagID)
1358 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001359 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001360 << MethodImpl->getResultType()
1361 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001362 S.Diag(MethodDecl->getLocation(),
1363 IsOverridingMode ? diag::note_previous_declaration
1364 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001365 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001366 return false;
John McCall10302c02010-10-28 02:34:38 +00001367}
1368
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001369static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001370 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001371 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001372 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001373 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001374 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001375 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001376 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001377 if (IsProtocolMethodDecl &&
1378 (ImplVar->getObjCDeclQualifier() !=
1379 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001380 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001381 if (IsOverridingMode)
1382 S.Diag(ImplVar->getLocation(),
1383 diag::warn_conflicting_overriding_param_modifiers)
1384 << getTypeRange(ImplVar->getTypeSourceInfo())
1385 << MethodImpl->getDeclName();
1386 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001387 diag::warn_conflicting_param_modifiers)
1388 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001389 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001390 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1391 << getTypeRange(IfaceVar->getTypeSourceInfo());
1392 }
1393 else
1394 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001395 }
1396
John McCall10302c02010-10-28 02:34:38 +00001397 QualType ImplTy = ImplVar->getType();
1398 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001399
John McCall10302c02010-10-28 02:34:38 +00001400 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001401 return true;
1402
1403 if (!Warn)
1404 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001405 unsigned DiagID =
1406 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1407 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001408
1409 // Mismatches between ObjC pointers go into a different warning
1410 // category, and sometimes they're even completely whitelisted.
1411 if (const ObjCObjectPointerType *ImplPtrTy =
1412 ImplTy->getAs<ObjCObjectPointerType>()) {
1413 if (const ObjCObjectPointerType *IfacePtrTy =
1414 IfaceTy->getAs<ObjCObjectPointerType>()) {
1415 // Allow non-matching argument types as long as they don't
1416 // violate the principle of substitutability. Specifically, the
1417 // implementation must accept any objects that the superclass
1418 // accepts, however it may also accept others.
1419 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001420 return false;
John McCall10302c02010-10-28 02:34:38 +00001421
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001422 DiagID =
1423 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1424 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001425 }
1426 }
1427
1428 S.Diag(ImplVar->getLocation(), DiagID)
1429 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001430 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1431 S.Diag(IfaceVar->getLocation(),
1432 (IsOverridingMode ? diag::note_previous_declaration
1433 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001434 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001435 return false;
John McCall10302c02010-10-28 02:34:38 +00001436}
John McCallf85e1932011-06-15 23:02:42 +00001437
1438/// In ARC, check whether the conventional meanings of the two methods
1439/// match. If they don't, it's a hard error.
1440static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1441 ObjCMethodDecl *decl) {
1442 ObjCMethodFamily implFamily = impl->getMethodFamily();
1443 ObjCMethodFamily declFamily = decl->getMethodFamily();
1444 if (implFamily == declFamily) return false;
1445
1446 // Since conventions are sorted by selector, the only possibility is
1447 // that the types differ enough to cause one selector or the other
1448 // to fall out of the family.
1449 assert(implFamily == OMF_None || declFamily == OMF_None);
1450
1451 // No further diagnostics required on invalid declarations.
1452 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1453
1454 const ObjCMethodDecl *unmatched = impl;
1455 ObjCMethodFamily family = declFamily;
1456 unsigned errorID = diag::err_arc_lost_method_convention;
1457 unsigned noteID = diag::note_arc_lost_method_convention;
1458 if (declFamily == OMF_None) {
1459 unmatched = decl;
1460 family = implFamily;
1461 errorID = diag::err_arc_gained_method_convention;
1462 noteID = diag::note_arc_gained_method_convention;
1463 }
1464
1465 // Indexes into a %select clause in the diagnostic.
1466 enum FamilySelector {
1467 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1468 };
1469 FamilySelector familySelector = FamilySelector();
1470
1471 switch (family) {
1472 case OMF_None: llvm_unreachable("logic error, no method convention");
1473 case OMF_retain:
1474 case OMF_release:
1475 case OMF_autorelease:
1476 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001477 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001478 case OMF_retainCount:
1479 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001480 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001481 // Mismatches for these methods don't change ownership
1482 // conventions, so we don't care.
1483 return false;
1484
1485 case OMF_init: familySelector = F_init; break;
1486 case OMF_alloc: familySelector = F_alloc; break;
1487 case OMF_copy: familySelector = F_copy; break;
1488 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1489 case OMF_new: familySelector = F_new; break;
1490 }
1491
1492 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1493 ReasonSelector reasonSelector;
1494
1495 // The only reason these methods don't fall within their families is
1496 // due to unusual result types.
1497 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1498 reasonSelector = R_UnrelatedReturn;
1499 } else {
1500 reasonSelector = R_NonObjectReturn;
1501 }
1502
Joerg Sonnenberger73484542013-06-26 21:31:47 +00001503 S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
1504 S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
John McCallf85e1932011-06-15 23:02:42 +00001505
1506 return true;
1507}
John McCall10302c02010-10-28 02:34:38 +00001508
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001509void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001510 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001511 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001512 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001513 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1514 return;
1515
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001516 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001517 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001518 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Chris Lattner3aff9192009-04-11 19:58:42 +00001520 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001521 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1522 EF = MethodDecl->param_end();
1523 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001524 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001525 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001526 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001527
Fariborz Jahanian21121902011-08-08 18:03:17 +00001528 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001529 Diag(ImpMethodDecl->getLocation(),
1530 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001531 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001532 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001533}
1534
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001535void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1536 ObjCMethodDecl *Overridden,
1537 bool IsProtocolMethodDecl) {
1538
1539 CheckMethodOverrideReturn(*this, Method, Overridden,
1540 IsProtocolMethodDecl, true,
1541 true);
1542
1543 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001544 IF = Overridden->param_begin(), EM = Method->param_end(),
1545 EF = Overridden->param_end();
1546 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001547 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1548 IsProtocolMethodDecl, true, true);
1549 }
1550
1551 if (Method->isVariadic() != Overridden->isVariadic()) {
1552 Diag(Method->getLocation(),
1553 diag::warn_conflicting_overriding_variadic);
1554 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1555 }
1556}
1557
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001558/// WarnExactTypedMethods - This routine issues a warning if method
1559/// implementation declaration matches exactly that of its declaration.
1560void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1561 ObjCMethodDecl *MethodDecl,
1562 bool IsProtocolMethodDecl) {
1563 // don't issue warning when protocol method is optional because primary
1564 // class is not required to implement it and it is safe for protocol
1565 // to implement it.
1566 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1567 return;
1568 // don't issue warning when primary class's method is
1569 // depecated/unavailable.
1570 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1571 MethodDecl->hasAttr<DeprecatedAttr>())
1572 return;
1573
1574 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1575 IsProtocolMethodDecl, false, false);
1576 if (match)
1577 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001578 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1579 EF = MethodDecl->param_end();
1580 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001581 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1582 *IM, *IF,
1583 IsProtocolMethodDecl, false, false);
1584 if (!match)
1585 break;
1586 }
1587 if (match)
1588 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001589 if (match)
1590 match = !(MethodDecl->isClassMethod() &&
1591 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001592
1593 if (match) {
1594 Diag(ImpMethodDecl->getLocation(),
1595 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001596 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1597 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001598 }
1599}
1600
Mike Stump390b4cc2009-05-16 07:39:55 +00001601/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1602/// improve the efficiency of selector lookups and type checking by associating
1603/// with each protocol / interface / category the flattened instance tables. If
1604/// we used an immutable set to keep the table then it wouldn't add significant
1605/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001606
Steve Naroffefe7f362008-02-08 22:06:17 +00001607/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001608/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001609void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1610 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001611 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001612 const SelectorSet &InsMap,
1613 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001614 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001615 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1616 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1617 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001618 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1619
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001620 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001621 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001622 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001623 // check to see if class implements forwardInvocation method and objects
1624 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001625 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001626 // Under such conditions, which means that every method possible is
1627 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001628 // found" warnings.
1629 // FIXME: Use a general GetUnarySelector method for this.
1630 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1631 Selector fISelector = Context.Selectors.getSelector(1, &II);
1632 if (InsMap.count(fISelector))
1633 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1634 // need be implemented in the implementation.
1635 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1636 }
Mike Stump1eb44332009-09-09 15:08:12 +00001637
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001638 // If this is a forward protocol declaration, get its definition.
1639 if (!PDecl->isThisDeclarationADefinition() &&
1640 PDecl->getDefinition())
1641 PDecl = PDecl->getDefinition();
1642
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001643 // If a method lookup fails locally we still need to look and see if
1644 // the method was implemented by a base class or an inherited
1645 // protocol. This lookup is slow, but occurs rarely in correct code
1646 // and otherwise would terminate in a warning.
1647
Chris Lattner4d391482007-12-12 07:09:47 +00001648 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001649 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001650 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001651 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001652 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001653 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001654 !method->isPropertyAccessor() &&
1655 !InsMap.count(method->getSelector()) &&
1656 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001657 // If a method is not implemented in the category implementation but
1658 // has been declared in its primary class, superclass,
1659 // or in one of their protocols, no need to issue the warning.
1660 // This is because method will be implemented in the primary class
1661 // or one of its super class implementation.
1662
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001663 // Ugly, but necessary. Method declared in protcol might have
1664 // have been synthesized due to a property declared in the class which
1665 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001666 if (ObjCMethodDecl *MethodInClass =
1667 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001668 true /*shallowCategoryLookup*/))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001669 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001670 continue;
1671 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1672 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1673 != DiagnosticsEngine::Ignored) {
1674 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001675 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1676 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001677 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001678 }
1679 }
Chris Lattner4d391482007-12-12 07:09:47 +00001680 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001681 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001682 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001683 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001684 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001685 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1686 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001687 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001688 // See above comment for instance method lookups.
1689 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001690 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001691 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001692 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001693 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1694 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001695 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
1696 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1697 PDecl->getDeclName();
1698 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001699 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001700 }
Chris Lattner780f3292008-07-21 21:32:27 +00001701 // Check on this protocols's referenced protocols, recursively.
1702 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1703 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001704 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001705}
1706
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001707/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001708/// or protocol against those declared in their implementations.
1709///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001710void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1711 const SelectorSet &ClsMap,
1712 SelectorSet &InsMapSeen,
1713 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001714 ObjCImplDecl* IMPDecl,
1715 ObjCContainerDecl* CDecl,
1716 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001717 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001718 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001719 // Check and see if instance methods in class interface have been
1720 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001721 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1722 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001723 if (InsMapSeen.count((*I)->getSelector()))
1724 continue;
1725 InsMapSeen.insert((*I)->getSelector());
Jordan Rose1e4691b2012-10-10 16:42:25 +00001726 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001727 !InsMap.count((*I)->getSelector())) {
1728 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001729 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001730 diag::warn_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001731 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001732 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001733 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001734 IMPDecl->getInstanceMethod((*I)->getSelector());
1735 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1736 "Expected to find the method through lookup as well");
1737 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001738 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001739 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001740 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001741 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1742 isa<ObjCProtocolDecl>(CDecl));
Jordan Rose1e4691b2012-10-10 16:42:25 +00001743 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001744 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001745 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001746 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001747 }
1748 }
Mike Stump1eb44332009-09-09 15:08:12 +00001749
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001750 // Check and see if class methods in class interface have been
1751 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001752 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001753 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001754 if (ClsMapSeen.count((*I)->getSelector()))
1755 continue;
1756 ClsMapSeen.insert((*I)->getSelector());
1757 if (!ClsMap.count((*I)->getSelector())) {
1758 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001759 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001760 diag::warn_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001761 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001762 ObjCMethodDecl *ImpMethodDecl =
1763 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001764 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1765 "Expected to find the method through lookup as well");
1766 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001767 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001768 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1769 isa<ObjCProtocolDecl>(CDecl));
1770 else
1771 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001772 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001773 }
1774 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001775
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001776 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001777 // when checking that methods in implementation match their declaration,
1778 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1779 // extension; as well as those in categories.
Douglas Gregord3297242013-01-16 23:00:23 +00001780 if (!WarnCategoryMethodImpl) {
1781 for (ObjCInterfaceDecl::visible_categories_iterator
1782 Cat = I->visible_categories_begin(),
1783 CatEnd = I->visible_categories_end();
1784 Cat != CatEnd; ++Cat) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001785 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001786 IMPDecl, *Cat, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001787 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001788 }
1789 } else {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001790 // Also methods in class extensions need be looked at next.
Douglas Gregord3297242013-01-16 23:00:23 +00001791 for (ObjCInterfaceDecl::visible_extensions_iterator
1792 Ext = I->visible_extensions_begin(),
1793 ExtEnd = I->visible_extensions_end();
1794 Ext != ExtEnd; ++Ext) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001795 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001796 IMPDecl, *Ext, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001797 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001798 }
1799 }
1800
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001801 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001802 for (ObjCInterfaceDecl::all_protocol_iterator
1803 PI = I->all_referenced_protocol_begin(),
1804 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001805 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1806 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001807 (*PI), IncompleteImpl, false,
1808 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001809
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001810 // FIXME. For now, we are not checking for extact match of methods
1811 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001812 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001813 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001814 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001815 I->getSuperClass(), IncompleteImpl, false);
1816 }
1817}
1818
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001819/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1820/// category matches with those implemented in its primary class and
1821/// warns each time an exact match is found.
1822void Sema::CheckCategoryVsClassMethodMatches(
1823 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001824 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001825
1826 for (ObjCImplementationDecl::instmeth_iterator
1827 I = CatIMPDecl->instmeth_begin(),
1828 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1829 InsMap.insert((*I)->getSelector());
1830
1831 for (ObjCImplementationDecl::classmeth_iterator
1832 I = CatIMPDecl->classmeth_begin(),
1833 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1834 ClsMap.insert((*I)->getSelector());
1835 if (InsMap.empty() && ClsMap.empty())
1836 return;
1837
1838 // Get category's primary class.
1839 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1840 if (!CatDecl)
1841 return;
1842 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1843 if (!IDecl)
1844 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001845 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001846 bool IncompleteImpl = false;
1847 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1848 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001849 IncompleteImpl, false,
1850 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001851}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001852
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001853void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001854 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001855 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001856 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001857 // Check and see if instance methods in class interface have been
1858 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001859 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001860 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001861 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001862
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001863 // Check and see if properties declared in the interface have either 1)
1864 // an implementation or 2) there is a @synthesize/@dynamic implementation
1865 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001866 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001867 if (!(LangOpts.ObjCDefaultSynthProperties &&
1868 LangOpts.ObjCRuntime.isNonFragile()) ||
1869 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001870 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001871
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001872 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001873 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001874 I = IMPDecl->classmeth_begin(),
1875 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001876 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001878 // Check for type conflict of methods declared in a class/protocol and
1879 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001880 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001881 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1882 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001883 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001884
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001885 // check all methods implemented in category against those declared
1886 // in its primary class.
1887 if (ObjCCategoryImplDecl *CatDecl =
1888 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1889 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001890
Chris Lattner4d391482007-12-12 07:09:47 +00001891 // Check the protocol list for unimplemented methods in the @implementation
1892 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001893 // Check and see if class methods in class interface have been
1894 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Chris Lattnercddc8882009-03-01 00:56:52 +00001896 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001897 for (ObjCInterfaceDecl::all_protocol_iterator
1898 PI = I->all_referenced_protocol_begin(),
1899 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001900 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001901 InsMap, ClsMap, I);
1902 // Check class extensions (unnamed categories)
Douglas Gregord3297242013-01-16 23:00:23 +00001903 for (ObjCInterfaceDecl::visible_extensions_iterator
1904 Ext = I->visible_extensions_begin(),
1905 ExtEnd = I->visible_extensions_end();
1906 Ext != ExtEnd; ++Ext) {
1907 ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
1908 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001909 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001910 // For extended class, unimplemented methods in its protocols will
1911 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001912 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001913 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1914 E = C->protocol_end(); PI != E; ++PI)
1915 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001916 InsMap, ClsMap, CDecl);
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001917 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001918 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001919 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001920 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001921}
1922
Mike Stump1eb44332009-09-09 15:08:12 +00001923/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001924Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001925Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001926 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001927 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001928 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001929 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001930 for (unsigned i = 0; i != NumElts; ++i) {
1931 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001932 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001933 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001934 LookupOrdinaryName, ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001935 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001936 // GCC apparently allows the following idiom:
1937 //
1938 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1939 // @class XCElementToggler;
1940 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001941 // Here we have chosen to ignore the forward class declaration
1942 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001943 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001944 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001945 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001946 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001947 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001948 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001949 // to the underlying class. Just ignore the forward class with a warning
1950 // as this will force the intended behavior which is to lookup the typedef
1951 // name.
1952 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1953 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1954 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1955 continue;
1956 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001957 }
Chris Lattner4d391482007-12-12 07:09:47 +00001958 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001959
1960 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001961 ObjCInterfaceDecl *PrevIDecl
1962 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +00001963
1964 IdentifierInfo *ClassName = IdentList[i];
1965 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
1966 // A previous decl with a different name is because of
1967 // @compatibility_alias, for example:
1968 // \code
1969 // @class NewImage;
1970 // @compatibility_alias OldImage NewImage;
1971 // \endcode
1972 // A lookup for 'OldImage' will return the 'NewImage' decl.
1973 //
1974 // In such a case use the real declaration name, instead of the alias one,
1975 // otherwise we will break IdentifierResolver and redecls-chain invariants.
1976 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
1977 // has been aliased.
1978 ClassName = PrevIDecl->getIdentifier();
1979 }
1980
Douglas Gregor7723fec2011-12-15 20:29:51 +00001981 ObjCInterfaceDecl *IDecl
1982 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +00001983 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001984 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001985
Douglas Gregor7723fec2011-12-15 20:29:51 +00001986 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001987 CheckObjCDeclScope(IDecl);
1988 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001989 }
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001990
1991 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001992}
1993
John McCall0f4c4c42011-06-16 01:15:19 +00001994static bool tryMatchRecordTypes(ASTContext &Context,
1995 Sema::MethodMatchStrategy strategy,
1996 const Type *left, const Type *right);
1997
John McCallf85e1932011-06-15 23:02:42 +00001998static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1999 QualType leftQT, QualType rightQT) {
2000 const Type *left =
2001 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2002 const Type *right =
2003 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2004
2005 if (left == right) return true;
2006
2007 // If we're doing a strict match, the types have to match exactly.
2008 if (strategy == Sema::MMS_strict) return false;
2009
2010 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2011
2012 // Otherwise, use this absurdly complicated algorithm to try to
2013 // validate the basic, low-level compatibility of the two types.
2014
2015 // As a minimum, require the sizes and alignments to match.
2016 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
2017 return false;
2018
2019 // Consider all the kinds of non-dependent canonical types:
2020 // - functions and arrays aren't possible as return and parameter types
2021
2022 // - vector types of equal size can be arbitrarily mixed
2023 if (isa<VectorType>(left)) return isa<VectorType>(right);
2024 if (isa<VectorType>(right)) return false;
2025
2026 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00002027 // - structs, unions, and Objective-C objects must match more-or-less
2028 // exactly
John McCallf85e1932011-06-15 23:02:42 +00002029 // - everything else should be a scalar
2030 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00002031 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00002032
John McCall1d9b3b22011-09-09 05:25:32 +00002033 // Make scalars agree in kind, except count bools as chars, and group
2034 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00002035 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2036 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2037 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2038 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00002039 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2040 leftSK = Type::STK_ObjCObjectPointer;
2041 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2042 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00002043
2044 // Note that data member pointers and function member pointers don't
2045 // intermix because of the size differences.
2046
2047 return (leftSK == rightSK);
2048}
Chris Lattner4d391482007-12-12 07:09:47 +00002049
John McCall0f4c4c42011-06-16 01:15:19 +00002050static bool tryMatchRecordTypes(ASTContext &Context,
2051 Sema::MethodMatchStrategy strategy,
2052 const Type *lt, const Type *rt) {
2053 assert(lt && rt && lt != rt);
2054
2055 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2056 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2057 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2058
2059 // Require union-hood to match.
2060 if (left->isUnion() != right->isUnion()) return false;
2061
2062 // Require an exact match if either is non-POD.
2063 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2064 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2065 return false;
2066
2067 // Require size and alignment to match.
2068 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2069
2070 // Require fields to match.
2071 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2072 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2073 for (; li != le && ri != re; ++li, ++ri) {
2074 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2075 return false;
2076 }
2077 return (li == le && ri == re);
2078}
2079
Chris Lattner4d391482007-12-12 07:09:47 +00002080/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2081/// returns true, or false, accordingly.
2082/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00002083bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2084 const ObjCMethodDecl *right,
2085 MethodMatchStrategy strategy) {
2086 if (!matchTypes(Context, strategy,
2087 left->getResultType(), right->getResultType()))
2088 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002089
Douglas Gregor7666b032013-02-07 19:13:24 +00002090 // If either is hidden, it is not considered to match.
2091 if (left->isHidden() || right->isHidden())
2092 return false;
2093
David Blaikie4e4d0842012-03-11 07:00:24 +00002094 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002095 (left->hasAttr<NSReturnsRetainedAttr>()
2096 != right->hasAttr<NSReturnsRetainedAttr>() ||
2097 left->hasAttr<NSConsumesSelfAttr>()
2098 != right->hasAttr<NSConsumesSelfAttr>()))
2099 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002100
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002101 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002102 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2103 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002104
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002105 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002106 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002107 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002108
2109 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2110 return false;
2111
David Blaikie4e4d0842012-03-11 07:00:24 +00002112 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002113 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2114 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002115 }
2116 return true;
2117}
2118
Douglas Gregorff310c72012-05-01 23:37:00 +00002119void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002120 // Record at the head of the list whether there were 0, 1, or >= 2 methods
2121 // inside categories.
Argyrios Kyrtzidisab3d5092013-04-27 00:10:12 +00002122 if (ObjCCategoryDecl *
2123 CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
2124 if (!CD->IsClassExtension() && List->getBits() < 2)
2125 List->setBits(List->getBits()+1);
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002126
Douglas Gregor44fae522012-01-25 00:19:56 +00002127 // If the list is empty, make it a singleton list.
2128 if (List->Method == 0) {
2129 List->Method = Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002130 List->setNext(0);
Douglas Gregorff310c72012-05-01 23:37:00 +00002131 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002132 }
2133
2134 // We've seen a method with this name, see if we have already seen this type
2135 // signature.
2136 ObjCMethodList *Previous = List;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002137 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregorfc46be92013-06-21 00:20:25 +00002138 // If we are building a module, keep all of the methods.
2139 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2140 continue;
2141
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002142 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002143 continue;
2144
2145 ObjCMethodDecl *PrevObjCMethod = List->Method;
2146
2147 // Propagate the 'defined' bit.
2148 if (Method->isDefined())
2149 PrevObjCMethod->setDefined(true);
2150
2151 // If a method is deprecated, push it in the global pool.
2152 // This is used for better diagnostics.
2153 if (Method->isDeprecated()) {
2154 if (!PrevObjCMethod->isDeprecated())
2155 List->Method = Method;
2156 }
2157 // If new method is unavailable, push it into global pool
2158 // unless previous one is deprecated.
2159 if (Method->isUnavailable()) {
2160 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2161 List->Method = Method;
2162 }
2163
Douglas Gregorff310c72012-05-01 23:37:00 +00002164 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002165 }
2166
2167 // We have a new signature for an existing method - add it.
2168 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002169 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002170 Previous->setNext(new (Mem) ObjCMethodList(Method, 0));
Douglas Gregor44fae522012-01-25 00:19:56 +00002171}
2172
Sebastian Redldb9d2142010-08-02 23:18:59 +00002173/// \brief Read the contents of the method pool for a given selector from
2174/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002175void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002176 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002177 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002178}
2179
Douglas Gregorff310c72012-05-01 23:37:00 +00002180void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002181 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002182 // Ignore methods of invalid containers.
2183 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002184 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002185
Douglas Gregor0d266d62012-01-25 00:59:09 +00002186 if (ExternalSource)
2187 ReadMethodPool(Method->getSelector());
2188
Sebastian Redldb9d2142010-08-02 23:18:59 +00002189 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002190 if (Pos == MethodPool.end())
2191 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2192 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002193
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002194 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002195
Sebastian Redldb9d2142010-08-02 23:18:59 +00002196 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002197 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002198}
2199
John McCallf85e1932011-06-15 23:02:42 +00002200/// Determines if this is an "acceptable" loose mismatch in the global
2201/// method pool. This exists mostly as a hack to get around certain
2202/// global mismatches which we can't afford to make warnings / errors.
2203/// Really, what we want is a way to take a method out of the global
2204/// method pool.
2205static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2206 ObjCMethodDecl *other) {
2207 if (!chosen->isInstanceMethod())
2208 return false;
2209
2210 Selector sel = chosen->getSelector();
2211 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2212 return false;
2213
2214 // Don't complain about mismatches for -length if the method we
2215 // chose has an integral result type.
2216 return (chosen->getResultType()->isIntegerType());
2217}
2218
Sebastian Redldb9d2142010-08-02 23:18:59 +00002219ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002220 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002221 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002222 if (ExternalSource)
2223 ReadMethodPool(Sel);
2224
Sebastian Redldb9d2142010-08-02 23:18:59 +00002225 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002226 if (Pos == MethodPool.end())
2227 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002228
Douglas Gregorf0e00042013-01-16 18:47:38 +00002229 // Gather the non-hidden methods.
Sebastian Redldb9d2142010-08-02 23:18:59 +00002230 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorf0e00042013-01-16 18:47:38 +00002231 llvm::SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002232 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Douglas Gregorf0e00042013-01-16 18:47:38 +00002233 if (M->Method && !M->Method->isHidden()) {
2234 // If we're not supposed to warn about mismatches, we're done.
2235 if (!warn)
2236 return M->Method;
Mike Stump1eb44332009-09-09 15:08:12 +00002237
Douglas Gregorf0e00042013-01-16 18:47:38 +00002238 Methods.push_back(M->Method);
Sebastian Redldb9d2142010-08-02 23:18:59 +00002239 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002240 }
Douglas Gregorf0e00042013-01-16 18:47:38 +00002241
2242 // If there aren't any visible methods, we're done.
2243 // FIXME: Recover if there are any known-but-hidden methods?
2244 if (Methods.empty())
2245 return 0;
2246
2247 if (Methods.size() == 1)
2248 return Methods[0];
2249
2250 // We found multiple methods, so we may have to complain.
2251 bool issueDiagnostic = false, issueError = false;
2252
2253 // We support a warning which complains about *any* difference in
2254 // method signature.
2255 bool strictSelectorMatch =
2256 (receiverIdOrClass && warn &&
2257 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2258 R.getBegin())
2259 != DiagnosticsEngine::Ignored));
2260 if (strictSelectorMatch) {
2261 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2262 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2263 issueDiagnostic = true;
2264 break;
2265 }
2266 }
2267 }
2268
2269 // If we didn't see any strict differences, we won't see any loose
2270 // differences. In ARC, however, we also need to check for loose
2271 // mismatches, because most of them are errors.
2272 if (!strictSelectorMatch ||
2273 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2274 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2275 // This checks if the methods differ in type mismatch.
2276 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2277 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2278 issueDiagnostic = true;
2279 if (getLangOpts().ObjCAutoRefCount)
2280 issueError = true;
2281 break;
2282 }
2283 }
2284
2285 if (issueDiagnostic) {
2286 if (issueError)
2287 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2288 else if (strictSelectorMatch)
2289 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2290 else
2291 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2292
2293 Diag(Methods[0]->getLocStart(),
2294 issueError ? diag::note_possibility : diag::note_using)
2295 << Methods[0]->getSourceRange();
2296 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2297 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2298 << Methods[I]->getSourceRange();
2299 }
2300 }
2301 return Methods[0];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002302}
2303
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002304ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002305 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2306 if (Pos == MethodPool.end())
2307 return 0;
2308
2309 GlobalMethods &Methods = Pos->second;
2310
2311 if (Methods.first.Method && Methods.first.Method->isDefined())
2312 return Methods.first.Method;
2313 if (Methods.second.Method && Methods.second.Method->isDefined())
2314 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002315 return 0;
2316}
2317
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002318static void
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002319HelperSelectorsForTypoCorrection(
2320 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2321 StringRef Typo, const ObjCMethodDecl * Method) {
2322 const unsigned MaxEditDistance = 1;
2323 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieu4fe96442013-06-06 02:22:29 +00002324 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002325
2326 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2327 if (MinPossibleEditDistance > 0 &&
2328 Typo.size() / MinPossibleEditDistance < 1)
2329 return;
2330 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2331 if (EditDistance > MaxEditDistance)
2332 return;
2333 if (EditDistance == BestEditDistance)
2334 BestMethod.push_back(Method);
2335 else if (EditDistance < BestEditDistance) {
2336 BestMethod.clear();
2337 BestMethod.push_back(Method);
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002338 }
2339}
2340
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002341static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2342 QualType ObjectType) {
2343 if (ObjectType.isNull())
2344 return true;
2345 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2346 return true;
2347 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != 0;
2348}
2349
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002350const ObjCMethodDecl *
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002351Sema::SelectorsForTypoCorrection(Selector Sel,
2352 QualType ObjectType) {
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002353 unsigned NumArgs = Sel.getNumArgs();
2354 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002355 bool ObjectIsId = true, ObjectIsClass = true;
2356 if (ObjectType.isNull())
2357 ObjectIsId = ObjectIsClass = false;
2358 else if (!ObjectType->isObjCObjectPointerType())
2359 return 0;
2360 else if (const ObjCObjectPointerType *ObjCPtr =
2361 ObjectType->getAsObjCInterfacePointerType()) {
2362 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2363 ObjectIsId = ObjectIsClass = false;
2364 }
2365 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2366 ObjectIsClass = false;
2367 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2368 ObjectIsId = false;
2369 else
2370 return 0;
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002371
2372 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2373 e = MethodPool.end(); b != e; b++) {
2374 // instance methods
2375 for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
2376 if (M->Method &&
Fariborz Jahaniancd9c9b52013-06-18 17:10:58 +00002377 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2378 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002379 if (ObjectIsId)
2380 Methods.push_back(M->Method);
2381 else if (!ObjectIsClass &&
2382 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2383 Methods.push_back(M->Method);
2384 }
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002385 // class methods
2386 for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
2387 if (M->Method &&
Fariborz Jahaniancd9c9b52013-06-18 17:10:58 +00002388 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2389 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002390 if (ObjectIsClass)
2391 Methods.push_back(M->Method);
2392 else if (!ObjectIsId &&
2393 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2394 Methods.push_back(M->Method);
2395 }
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002396 }
2397
2398 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2399 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2400 HelperSelectorsForTypoCorrection(SelectedMethods,
2401 Sel.getAsString(), Methods[i]);
2402 }
2403 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : NULL;
2404}
2405
2406static void
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002407HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S,
2408 ObjCMethodList &MethList) {
2409 ObjCMethodList *M = &MethList;
2410 ObjCMethodDecl *TargetMethod = M->Method;
2411 while (TargetMethod &&
2412 isa<ObjCImplDecl>(TargetMethod->getDeclContext())) {
2413 M = M->getNext();
2414 TargetMethod = M ? M->Method : 0;
2415 }
2416 if (!TargetMethod)
2417 return;
2418 bool FirstTime = true;
2419 for (M = M->getNext(); M; M=M->getNext()) {
2420 ObjCMethodDecl *MatchingMethodDecl = M->Method;
2421 if (isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext()))
2422 continue;
2423 if (!S.MatchTwoMethodDeclarations(TargetMethod,
2424 MatchingMethodDecl, Sema::MMS_loose)) {
2425 if (FirstTime) {
2426 FirstTime = false;
2427 S.Diag(TargetMethod->getLocation(), diag::warning_multiple_selectors)
2428 << TargetMethod->getSelector();
2429 }
2430 S.Diag(MatchingMethodDecl->getLocation(), diag::note_also_found);
2431 }
2432 }
2433}
2434
2435void Sema::DiagnoseMismatchedMethodsInGlobalPool() {
2436 unsigned DIAG = diag::warning_multiple_selectors;
2437 if (Diags.getDiagnosticLevel(DIAG, SourceLocation())
2438 == DiagnosticsEngine::Ignored)
2439 return;
2440 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2441 e = MethodPool.end(); b != e; b++) {
2442 // first, instance methods
2443 ObjCMethodList &InstMethList = b->second.first;
2444 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, InstMethList);
Fariborz Jahanian639aa522013-05-30 21:52:50 +00002445 // second, class methods
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002446 ObjCMethodList &ClsMethList = b->second.second;
2447 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, ClsMethList);
2448 }
2449}
2450
2451/// DiagnoseDuplicateIvars -
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002452/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002453/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002454/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002455/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002456void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2457 ObjCInterfaceDecl *SID) {
2458 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2459 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002460 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002461 if (Ivar->isInvalidDecl())
2462 continue;
2463 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2464 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2465 if (prevIvar) {
2466 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2467 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2468 Ivar->setInvalidDecl();
2469 }
2470 }
2471 }
2472}
2473
Erik Verbruggend64251f2011-12-06 09:25:23 +00002474Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2475 switch (CurContext->getDeclKind()) {
2476 case Decl::ObjCInterface:
2477 return Sema::OCK_Interface;
2478 case Decl::ObjCProtocol:
2479 return Sema::OCK_Protocol;
2480 case Decl::ObjCCategory:
2481 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2482 return Sema::OCK_ClassExtension;
2483 else
2484 return Sema::OCK_Category;
2485 case Decl::ObjCImplementation:
2486 return Sema::OCK_Implementation;
2487 case Decl::ObjCCategoryImpl:
2488 return Sema::OCK_CategoryImplementation;
2489
2490 default:
2491 return Sema::OCK_None;
2492 }
2493}
2494
Steve Naroffa56f6162007-12-18 01:30:32 +00002495// Note: For class/category implemenations, allMethods/allProperties is
2496// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002497Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2498 Decl **allMethods, unsigned allNum,
2499 Decl **allProperties, unsigned pNum,
2500 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002501
Erik Verbruggend64251f2011-12-06 09:25:23 +00002502 if (getObjCContainerKind() == Sema::OCK_None)
2503 return 0;
2504
2505 assert(AtEnd.isValid() && "Invalid location for '@end'");
2506
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002507 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2508 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002509
Mike Stump1eb44332009-09-09 15:08:12 +00002510 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002511 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2512 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002513 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002514
Steve Naroff0701bbb2009-01-08 17:28:14 +00002515 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2516 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2517 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2518
Chris Lattner4d391482007-12-12 07:09:47 +00002519 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002520 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002521 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002522
2523 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002524 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002525 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002526 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002527 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002528 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002529 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002530 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002531 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002532 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002533 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002534 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002535 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002536 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002537 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002538 if (!Context.getSourceManager().isInSystemHeader(
2539 Method->getLocation()))
2540 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2541 << Method->getDeclName();
2542 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2543 }
Chris Lattner4d391482007-12-12 07:09:47 +00002544 InsMap[Method->getSelector()] = Method;
2545 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002546 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002547 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002548 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002549 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002550 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002551 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002552 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002553 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002554 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002555 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002556 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002557 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002558 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002559 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002560 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002561 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002562 if (!Context.getSourceManager().isInSystemHeader(
2563 Method->getLocation()))
2564 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2565 << Method->getDeclName();
2566 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2567 }
Chris Lattner4d391482007-12-12 07:09:47 +00002568 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002569 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002570 }
2571 }
2572 }
Douglas Gregorb892d702013-01-21 19:42:21 +00002573 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2574 // Nothing to do here.
Steve Naroff09c47192009-01-09 15:36:25 +00002575 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002576 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002577 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002578 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002579
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002580 if (C->IsClassExtension()) {
2581 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2582 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002583 }
Chris Lattner4d391482007-12-12 07:09:47 +00002584 }
Steve Naroff09c47192009-01-09 15:36:25 +00002585 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002586 if (CDecl->getIdentifier())
2587 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2588 // user-defined setter/getter. It also synthesizes setter/getter methods
2589 // and adds them to the DeclContext and global method pools.
2590 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2591 E = CDecl->prop_end();
2592 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002593 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002594 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002595 }
2596 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002597 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002598 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002599 // Any property declared in a class extension might have user
2600 // declared setter or getter in current class extension or one
2601 // of the other class extensions. Mark them as synthesized as
2602 // property will be synthesized when property with same name is
2603 // seen in the @implementation.
Douglas Gregord3297242013-01-16 23:00:23 +00002604 for (ObjCInterfaceDecl::visible_extensions_iterator
2605 Ext = IDecl->visible_extensions_begin(),
2606 ExtEnd = IDecl->visible_extensions_end();
2607 Ext != ExtEnd; ++Ext) {
2608 for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
2609 E = Ext->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002610 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002611 // Skip over properties declared @dynamic
2612 if (const ObjCPropertyImplDecl *PIDecl
2613 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2614 if (PIDecl->getPropertyImplementation()
2615 == ObjCPropertyImplDecl::Dynamic)
2616 continue;
Douglas Gregord3297242013-01-16 23:00:23 +00002617
2618 for (ObjCInterfaceDecl::visible_extensions_iterator
2619 Ext = IDecl->visible_extensions_begin(),
2620 ExtEnd = IDecl->visible_extensions_end();
2621 Ext != ExtEnd; ++Ext) {
2622 if (ObjCMethodDecl *GetterMethod
2623 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002624 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002625 if (!Property->isReadOnly())
Douglas Gregord3297242013-01-16 23:00:23 +00002626 if (ObjCMethodDecl *SetterMethod
2627 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002628 SetterMethod->setPropertyAccessor(true);
Douglas Gregord3297242013-01-16 23:00:23 +00002629 }
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002630 }
2631 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002632 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002633 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002634 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002635
Patrick Beardb2f68202012-04-06 18:12:22 +00002636 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2637 if (IDecl->getSuperClass() == NULL) {
2638 // This class has no superclass, so check that it has been marked with
2639 // __attribute((objc_root_class)).
2640 if (!HasRootClassAttr) {
2641 SourceLocation DeclLoc(IDecl->getLocation());
2642 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2643 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2644 << IDecl->getIdentifier();
2645 // See if NSObject is in the current scope, and if it is, suggest
2646 // adding " : NSObject " to the class declaration.
2647 NamedDecl *IF = LookupSingleName(TUScope,
2648 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2649 DeclLoc, LookupOrdinaryName);
2650 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2651 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2652 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2653 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2654 } else {
2655 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2656 }
2657 }
2658 } else if (HasRootClassAttr) {
2659 // Complain that only root classes may have this attribute.
2660 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2661 }
2662
John McCall260611a2012-06-20 06:18:46 +00002663 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002664 while (IDecl->getSuperClass()) {
2665 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2666 IDecl = IDecl->getSuperClass();
2667 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002668 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002669 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002670 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002671 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002672 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002673 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002674
Chris Lattner4d391482007-12-12 07:09:47 +00002675 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002676 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002677 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregord3297242013-01-16 23:00:23 +00002678 if (ObjCCategoryDecl *Cat
2679 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2680 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattner4d391482007-12-12 07:09:47 +00002681 }
2682 }
2683 }
Chris Lattner682bf922009-03-29 16:50:03 +00002684 if (isInterfaceDeclKind) {
2685 // Reject invalid vardecls.
2686 for (unsigned i = 0; i != tuvNum; i++) {
2687 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2688 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2689 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002690 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002691 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002692 }
Chris Lattner682bf922009-03-29 16:50:03 +00002693 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002694 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002695 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002696
2697 for (unsigned i = 0; i != tuvNum; i++) {
2698 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002699 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2700 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002701 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2702 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002703
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002704 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002705 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002706}
2707
2708
2709/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2710/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002711static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002712CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002713 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002714}
2715
Ted Kremenek422bae72010-04-18 04:59:38 +00002716static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002717unsigned countAlignAttr(const AttrVec &A) {
2718 unsigned count=0;
2719 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2720 if ((*i)->getKind() == attr::Aligned)
2721 ++count;
2722 return count;
2723}
2724
2725static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002726bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2727 const AttrVec &A) {
2728 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002729 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002730 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002731 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002732
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002733 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002734 // But implementation has attributes. This is invalid.
2735 // Except when implementation has 'Align' attribute which is
2736 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002737 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002738 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002739
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002740 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002741
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002742 unsigned countAlignOnImpl = countAlignAttr(A);
2743 if (!countAlignOnImpl && (A.size() != D.size()))
2744 return true;
2745 else if (countAlignOnImpl) {
2746 unsigned countAlignOnDecl = countAlignAttr(D);
2747 if (countAlignOnDecl && (A.size() != D.size()))
2748 return true;
2749 else if (!countAlignOnDecl &&
2750 ((A.size()-countAlignOnImpl) != D.size()))
2751 return true;
2752 }
2753
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002754 // attributes on method declaration and definition must match exactly.
2755 // Note that we have at most a couple of attributes on methods, so this
2756 // n*n search is good enough.
2757 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002758 if ((*i)->getKind() == attr::Aligned)
2759 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002760 bool match = false;
2761 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2762 if ((*i)->getKind() == (*i1)->getKind()) {
2763 match = true;
2764 break;
2765 }
2766 }
2767 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002768 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002769 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002770
Sean Huntcf807c42010-08-18 23:23:40 +00002771 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002772}
2773
Douglas Gregor926df6c2011-06-11 01:09:30 +00002774/// \brief Check whether the declared result type of the given Objective-C
2775/// method declaration is compatible with the method's class.
2776///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002777static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002778CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2779 ObjCInterfaceDecl *CurrentClass) {
2780 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002781
2782 // If an Objective-C method inherits its related result type, then its
2783 // declared result type must be compatible with its own class type. The
2784 // declared result type is compatible if:
2785 if (const ObjCObjectPointerType *ResultObjectType
2786 = ResultType->getAs<ObjCObjectPointerType>()) {
2787 // - it is id or qualified id, or
2788 if (ResultObjectType->isObjCIdType() ||
2789 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002790 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002791
2792 if (CurrentClass) {
2793 if (ObjCInterfaceDecl *ResultClass
2794 = ResultObjectType->getInterfaceDecl()) {
2795 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002796 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002797 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002798
2799 // - it is a superclass of the method's class type
2800 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002801 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002802 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002803 } else {
2804 // Any Objective-C pointer type might be acceptable for a protocol
2805 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002806 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002807 }
2808 }
2809
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002810 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002811}
2812
John McCall6c2c2502011-07-22 02:45:48 +00002813namespace {
2814/// A helper class for searching for methods which a particular method
2815/// overrides.
2816class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002817public:
John McCall6c2c2502011-07-22 02:45:48 +00002818 Sema &S;
2819 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002820 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002821 bool Recursive;
2822
2823public:
2824 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2825 Selector selector = method->getSelector();
2826
2827 // Bypass this search if we've never seen an instance/class method
2828 // with this selector before.
2829 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2830 if (it == S.MethodPool.end()) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00002831 if (!S.getExternalSource()) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002832 S.ReadMethodPool(selector);
2833
2834 it = S.MethodPool.find(selector);
2835 if (it == S.MethodPool.end())
2836 return;
John McCall6c2c2502011-07-22 02:45:48 +00002837 }
2838 ObjCMethodList &list =
2839 method->isInstanceMethod() ? it->second.first : it->second.second;
2840 if (!list.Method) return;
2841
2842 ObjCContainerDecl *container
2843 = cast<ObjCContainerDecl>(method->getDeclContext());
2844
2845 // Prevent the search from reaching this container again. This is
2846 // important with categories, which override methods from the
2847 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002848 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2849 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002850 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2851 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002852 } else {
2853 searchFromContainer(container);
2854 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002855 }
John McCall6c2c2502011-07-22 02:45:48 +00002856
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002857 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002858 iterator begin() const { return Overridden.begin(); }
2859 iterator end() const { return Overridden.end(); }
2860
2861private:
2862 void searchFromContainer(ObjCContainerDecl *container) {
2863 if (container->isInvalidDecl()) return;
2864
2865 switch (container->getDeclKind()) {
2866#define OBJCCONTAINER(type, base) \
2867 case Decl::type: \
2868 searchFrom(cast<type##Decl>(container)); \
2869 break;
2870#define ABSTRACT_DECL(expansion)
2871#define DECL(type, base) \
2872 case Decl::type:
2873#include "clang/AST/DeclNodes.inc"
2874 llvm_unreachable("not an ObjC container!");
2875 }
2876 }
2877
2878 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002879 if (!protocol->hasDefinition())
2880 return;
2881
John McCall6c2c2502011-07-22 02:45:48 +00002882 // A method in a protocol declaration overrides declarations from
2883 // referenced ("parent") protocols.
2884 search(protocol->getReferencedProtocols());
2885 }
2886
2887 void searchFrom(ObjCCategoryDecl *category) {
2888 // A method in a category declaration overrides declarations from
2889 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002890 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002891 search(category->getReferencedProtocols());
2892 }
2893
2894 void searchFrom(ObjCCategoryImplDecl *impl) {
2895 // A method in a category definition that has a category
2896 // declaration overrides declarations from the category
2897 // declaration.
2898 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2899 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002900 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2901 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002902
2903 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002904 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2905 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002906 }
2907 }
2908
2909 void searchFrom(ObjCInterfaceDecl *iface) {
2910 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002911 if (!iface->hasDefinition())
2912 return;
2913
John McCall6c2c2502011-07-22 02:45:48 +00002914 // - categories,
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002915 for (ObjCInterfaceDecl::known_categories_iterator
2916 cat = iface->known_categories_begin(),
2917 catEnd = iface->known_categories_end();
Douglas Gregord3297242013-01-16 23:00:23 +00002918 cat != catEnd; ++cat) {
2919 search(*cat);
2920 }
John McCall6c2c2502011-07-22 02:45:48 +00002921
2922 // - the super class, and
2923 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2924 search(super);
2925
2926 // - any referenced protocols.
2927 search(iface->getReferencedProtocols());
2928 }
2929
2930 void searchFrom(ObjCImplementationDecl *impl) {
2931 // A method in a class implementation overrides declarations from
2932 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002933 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2934 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002935 }
2936
2937
2938 void search(const ObjCProtocolList &protocols) {
2939 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2940 i != e; ++i)
2941 search(*i);
2942 }
2943
2944 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002945 // Check for a method in this container which matches this selector.
2946 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002947 Method->isInstanceMethod(),
2948 /*AllowHidden=*/true);
John McCall6c2c2502011-07-22 02:45:48 +00002949
2950 // If we find one, record it and bail out.
2951 if (meth) {
2952 Overridden.insert(meth);
2953 return;
2954 }
2955
2956 // Otherwise, search for methods that a hypothetical method here
2957 // would have overridden.
2958
2959 // Note that we're now in a recursive case.
2960 Recursive = true;
2961
2962 searchFromContainer(container);
2963 }
2964};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002965}
2966
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002967void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2968 ObjCInterfaceDecl *CurrentClass,
2969 ResultTypeCompatibilityKind RTC) {
2970 // Search for overridden methods and merge information down from them.
2971 OverrideSearch overrides(*this, ObjCMethod);
2972 // Keep track if the method overrides any method in the class's base classes,
2973 // its protocols, or its categories' protocols; we will keep that info
2974 // in the ObjCMethodDecl.
2975 // For this info, a method in an implementation is not considered as
2976 // overriding the same method in the interface or its categories.
2977 bool hasOverriddenMethodsInBaseOrProtocol = false;
2978 for (OverrideSearch::iterator
2979 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2980 ObjCMethodDecl *overridden = *i;
2981
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00002982 if (!hasOverriddenMethodsInBaseOrProtocol) {
2983 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2984 CurrentClass != overridden->getClassInterface() ||
2985 overridden->isOverriding()) {
2986 hasOverriddenMethodsInBaseOrProtocol = true;
2987
2988 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
2989 // OverrideSearch will return as "overridden" the same method in the
2990 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
2991 // check whether a category of a base class introduced a method with the
2992 // same selector, after the interface method declaration.
2993 // To avoid unnecessary lookups in the majority of cases, we use the
2994 // extra info bits in GlobalMethodPool to check whether there were any
2995 // category methods with this selector.
2996 GlobalMethodPool::iterator It =
2997 MethodPool.find(ObjCMethod->getSelector());
2998 if (It != MethodPool.end()) {
2999 ObjCMethodList &List =
3000 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
3001 unsigned CategCount = List.getBits();
3002 if (CategCount > 0) {
3003 // If the method is in a category we'll do lookup if there were at
3004 // least 2 category methods recorded, otherwise only one will do.
3005 if (CategCount > 1 ||
3006 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
3007 OverrideSearch overrides(*this, overridden);
3008 for (OverrideSearch::iterator
3009 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
3010 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidisab3d5092013-04-27 00:10:12 +00003011 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
3012 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00003013 hasOverriddenMethodsInBaseOrProtocol = true;
3014 overridden->setOverriding(true);
3015 break;
3016 }
3017 }
3018 }
3019 }
3020 }
3021 }
3022 }
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003023
3024 // Propagate down the 'related result type' bit from overridden methods.
3025 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
3026 ObjCMethod->SetRelatedResultType();
3027
3028 // Then merge the declarations.
3029 mergeObjCMethodDecls(ObjCMethod, overridden);
3030
3031 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3032 continue; // Conflicting properties are detected elsewhere.
3033
3034 // Check for overriding methods
3035 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3036 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3037 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3038 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3039
3040 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00003041 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3042 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003043 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3044 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00003045 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3046 PrevE = overridden->param_end();
3047 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003048 assert(PrevI != overridden->param_end() && "Param mismatch");
3049 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3050 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3051 // If type of argument of method in this class does not match its
3052 // respective argument type in the super class method, issue warning;
3053 if (!Context.typesAreCompatible(T1, T2)) {
3054 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3055 << T1 << T2;
3056 Diag(overridden->getLocation(), diag::note_previous_declaration);
3057 break;
3058 }
3059 }
3060 }
3061 }
3062
3063 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3064}
3065
John McCalld226f652010-08-21 09:40:31 +00003066Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003067 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00003068 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003069 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00003070 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003071 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00003072 Selector Sel,
3073 // optional arguments. The number of types/arguments is obtained
3074 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00003075 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003076 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00003077 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00003078 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00003079 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003080 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00003081 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00003082 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00003083 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003084 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3085 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00003086 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00003087
Douglas Gregore97179c2011-09-08 01:46:34 +00003088 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003089 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00003090 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003091 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00003092
Eli Friedmanddb5a392013-06-14 21:14:10 +00003093 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
John McCalld226f652010-08-21 09:40:31 +00003094 return 0;
Eli Friedmanddb5a392013-06-14 21:14:10 +00003095
Douglas Gregore97179c2011-09-08 01:46:34 +00003096 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00003097 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003098 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00003099 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003100 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00003101 }
Mike Stump1eb44332009-09-09 15:08:12 +00003102
3103 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003104 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003105 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003106 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003107 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00003108 MethodType == tok::minus, isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +00003109 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00003110 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00003111 MethodDeclKind == tok::objc_optional
3112 ? ObjCMethodDecl::Optional
3113 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00003114 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00003115
Chris Lattner5f9e2722011-07-23 10:55:15 +00003116 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00003117
Chris Lattner7db638d2009-04-11 19:42:43 +00003118 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00003119 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00003120 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00003121
David Blaikie7247c882013-05-15 07:37:26 +00003122 if (!ArgInfo[i].Type) {
John McCall58e46772009-10-23 21:48:59 +00003123 ArgType = Context.getObjCIdType();
3124 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00003125 } else {
John McCall58e46772009-10-23 21:48:59 +00003126 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnere294d3f2009-04-11 18:57:04 +00003127 }
Mike Stump1eb44332009-09-09 15:08:12 +00003128
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003129 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3130 LookupOrdinaryName, ForRedeclaration);
3131 LookupName(R, S);
3132 if (R.isSingleResult()) {
3133 NamedDecl *PrevDecl = R.getFoundDecl();
3134 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00003135 Diag(ArgInfo[i].NameLoc,
3136 (MethodDefinition ? diag::warn_method_param_redefinition
3137 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003138 << ArgInfo[i].Name;
3139 Diag(PrevDecl->getLocation(),
3140 diag::note_previous_declaration);
3141 }
3142 }
3143
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003144 SourceLocation StartLoc = DI
3145 ? DI->getTypeLoc().getBeginLoc()
3146 : ArgInfo[i].NameLoc;
3147
John McCall81ef3e62011-04-23 02:46:06 +00003148 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3149 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003150 ArgType, DI, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00003151
John McCall70798862011-05-02 00:30:12 +00003152 Param->setObjCMethodScopeInfo(i);
3153
Chris Lattner0ed844b2008-04-04 06:12:32 +00003154 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00003155 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00003156
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00003157 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003158 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003159
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00003160 if (Param->hasAttr<BlocksAttr>()) {
3161 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3162 Param->setInvalidDecl();
3163 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003164 S->AddDecl(Param);
3165 IdResolver.AddDecl(Param);
3166
Chris Lattner0ed844b2008-04-04 06:12:32 +00003167 Params.push_back(Param);
3168 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003169
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003170 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00003171 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003172 QualType ArgType = Param->getType();
3173 if (ArgType.isNull())
3174 ArgType = Context.getObjCIdType();
3175 else
3176 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00003177 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedmanddb5a392013-06-14 21:14:10 +00003178
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003179 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003180 Params.push_back(Param);
3181 }
3182
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003183 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003184 ObjCMethod->setObjCDeclQualifier(
3185 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00003186
3187 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003188 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00003189
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003190 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00003191 const ObjCMethodDecl *PrevMethod = 0;
3192 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00003193 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003194 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3195 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003196 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003197 PrevMethod = ImpDecl->getClassMethod(Sel);
3198 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003199 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00003200
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00003201 ObjCMethodDecl *IMD = 0;
3202 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3203 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3204 ObjCMethod->isInstanceMethod());
Fariborz Jahanian38b3bd82013-07-09 22:02:20 +00003205 if (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>() &&
3206 !ObjCMethod->hasAttr<ObjCRequiresSuperAttr>()) {
3207 // merge the attribute into implementation.
3208 ObjCMethod->addAttr(
3209 new (Context) ObjCRequiresSuperAttr(ObjCMethod->getLocation(), Context));
3210 }
Sean Huntcf807c42010-08-18 23:23:40 +00003211 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00003212 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00003213 SourceLocation MethodLoc = IMD->getLocation();
3214 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
3215 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00003216 Diag(MethodLoc, diag::note_method_declared_at)
3217 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00003218 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00003219 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003220 } else {
3221 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003222 }
John McCall6c2c2502011-07-22 02:45:48 +00003223
Chris Lattner4d391482007-12-12 07:09:47 +00003224 if (PrevMethod) {
3225 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00003226 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00003227 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00003228 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanianfbff0c42013-05-13 17:27:00 +00003229 ObjCMethod->setInvalidDecl();
3230 return ObjCMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00003231 }
John McCall54abf7d2009-11-04 02:18:39 +00003232
Douglas Gregor926df6c2011-06-11 01:09:30 +00003233 // If this Objective-C method does not have a related result type, but we
3234 // are allowed to infer related result types, try to do so based on the
3235 // method family.
3236 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3237 if (!CurrentClass) {
3238 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3239 CurrentClass = Cat->getClassInterface();
3240 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3241 CurrentClass = Impl->getClassInterface();
3242 else if (ObjCCategoryImplDecl *CatImpl
3243 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3244 CurrentClass = CatImpl->getClassInterface();
3245 }
John McCall6c2c2502011-07-22 02:45:48 +00003246
Douglas Gregore97179c2011-09-08 01:46:34 +00003247 ResultTypeCompatibilityKind RTC
3248 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00003249
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003250 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00003251
John McCallf85e1932011-06-15 23:02:42 +00003252 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00003253 if (getLangOpts().ObjCAutoRefCount)
John McCallb8463812013-04-04 01:38:37 +00003254 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCallf85e1932011-06-15 23:02:42 +00003255
Douglas Gregore97179c2011-09-08 01:46:34 +00003256 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003257 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00003258 !ObjCMethod->hasRelatedResultType() &&
3259 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00003260 bool InferRelatedResultType = false;
3261 switch (ObjCMethod->getMethodFamily()) {
3262 case OMF_None:
3263 case OMF_copy:
3264 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00003265 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003266 case OMF_mutableCopy:
3267 case OMF_release:
3268 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00003269 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003270 break;
3271
3272 case OMF_alloc:
3273 case OMF_new:
3274 InferRelatedResultType = ObjCMethod->isClassMethod();
3275 break;
3276
3277 case OMF_init:
3278 case OMF_autorelease:
3279 case OMF_retain:
3280 case OMF_self:
3281 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3282 break;
3283 }
3284
John McCall6c2c2502011-07-22 02:45:48 +00003285 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003286 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003287 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003288
3289 ActOnDocumentableDecl(ObjCMethod);
3290
John McCalld226f652010-08-21 09:40:31 +00003291 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003292}
3293
Chris Lattnercc98eac2008-12-17 07:13:27 +00003294bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003295 // Following is also an error. But it is caused by a missing @end
3296 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003297 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003298 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003299
3300 // If we switched context to translation unit while we are still lexically in
3301 // an objc container, it means the parser missed emitting an error.
3302 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3303 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003304
Anders Carlsson15281452008-11-04 16:57:32 +00003305 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3306 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003307
Anders Carlsson15281452008-11-04 16:57:32 +00003308 return true;
3309}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003310
James Dennett1dfbd922012-06-14 21:40:34 +00003311/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003312/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003313void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003314 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003315 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003316 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003317 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003318 if (!Class) {
3319 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3320 return;
3321 }
John McCall260611a2012-06-20 06:18:46 +00003322 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003323 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3324 return;
3325 }
Mike Stump1eb44332009-09-09 15:08:12 +00003326
Chris Lattnercc98eac2008-12-17 07:13:27 +00003327 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003328 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003329 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003330 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003331 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003332 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003333 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003334 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3335 /*FIXME: StartL=*/ID->getLocation(),
3336 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003337 ID->getIdentifier(), ID->getType(),
3338 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003339 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003340 }
Mike Stump1eb44332009-09-09 15:08:12 +00003341
Chris Lattnercc98eac2008-12-17 07:13:27 +00003342 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003343 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003344 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003345 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003346 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003347 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003348 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003349 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003350 }
3351}
3352
Douglas Gregor160b5632010-04-26 17:32:49 +00003353/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003354VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3355 SourceLocation StartLoc,
3356 SourceLocation IdLoc,
3357 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003358 bool Invalid) {
3359 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3360 // duration shall not be qualified by an address-space qualifier."
3361 // Since all parameters have automatic store duration, they can not have
3362 // an address space.
3363 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003364 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003365 Invalid = true;
3366 }
3367
3368 // An @catch parameter must be an unqualified object pointer type;
3369 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3370 if (Invalid) {
3371 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003372 } else if (T->isDependentType()) {
3373 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003374 } else if (!T->isObjCObjectPointerType()) {
3375 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003376 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003377 } else if (T->isObjCQualifiedIdType()) {
3378 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003379 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003380 }
3381
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003382 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003383 T, TInfo, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003384 New->setExceptionVariable(true);
3385
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003386 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003387 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003388 Invalid = true;
3389
Douglas Gregor160b5632010-04-26 17:32:49 +00003390 if (Invalid)
3391 New->setInvalidDecl();
3392 return New;
3393}
3394
John McCalld226f652010-08-21 09:40:31 +00003395Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003396 const DeclSpec &DS = D.getDeclSpec();
3397
3398 // We allow the "register" storage class on exception variables because
3399 // GCC did, but we drop it completely. Any other storage class is an error.
3400 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3401 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3402 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithec642442013-04-12 22:46:28 +00003403 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003404 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithec642442013-04-12 22:46:28 +00003405 << DeclSpec::getSpecifierName(SCS);
3406 }
3407 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3408 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3409 diag::err_invalid_thread)
3410 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregor160b5632010-04-26 17:32:49 +00003411 D.getMutableDeclSpec().ClearStorageClassSpecs();
3412
Richard Smithc7f81162013-03-18 22:52:47 +00003413 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregor160b5632010-04-26 17:32:49 +00003414
3415 // Check that there are no default arguments inside the type of this
3416 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003417 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003418 CheckExtraCXXDefaultArguments(D);
3419
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003420 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003421 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003422
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003423 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3424 D.getSourceRange().getBegin(),
3425 D.getIdentifierLoc(),
3426 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003427 D.isInvalidType());
3428
3429 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3430 if (D.getCXXScopeSpec().isSet()) {
3431 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3432 << D.getCXXScopeSpec().getRange();
3433 New->setInvalidDecl();
3434 }
3435
3436 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003437 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003438 if (D.getIdentifier())
3439 IdResolver.AddDecl(New);
3440
3441 ProcessDeclAttributes(S, New, D);
3442
3443 if (New->hasAttr<BlocksAttr>())
3444 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003445 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003446}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003447
3448/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003449/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003450void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003451 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003452 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3453 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003454 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003455 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003456 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003457 }
3458}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003459
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003460void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003461 // Load referenced selectors from the external source.
3462 if (ExternalSource) {
3463 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3464 ExternalSource->ReadReferencedSelectors(Sels);
3465 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3466 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3467 }
3468
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00003469 DiagnoseMismatchedMethodsInGlobalPool();
3470
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003471 // Warning will be issued only when selector table is
3472 // generated (which means there is at lease one implementation
3473 // in the TU). This is to match gcc's behavior.
3474 if (ReferencedSelectors.empty() ||
3475 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003476 return;
3477 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3478 ReferencedSelectors.begin(),
3479 E = ReferencedSelectors.end(); S != E; ++S) {
3480 Selector Sel = (*S).first;
3481 if (!LookupImplementedMethodInGlobalPool(Sel))
3482 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3483 }
3484 return;
3485}