blob: 618c9d99e1bcf8747c7e72c5de3410893e515bef [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
Chris Lattner8123a952008-04-10 02:22:51 +0000327 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000328 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000329 E = MDecl->param_end(); PI != E; ++PI) {
330 ParmVarDecl *Param = (*PI);
331 if (!Param->isInvalidDecl() &&
332 RequireCompleteType(Param->getLocation(), Param->getType(),
333 diag::err_typecheck_decl_incomplete_type))
334 Param->setInvalidDecl();
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000335 if (!Param->isInvalidDecl() &&
336 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
409 } else {
410 const ObjCMethodDecl *SuperMethod =
Jordan Rose41f3f3a2013-03-05 01:27:54 +0000411 SuperClass->lookupMethod(MDecl->getSelector(),
412 MDecl->isInstanceMethod());
Jordan Rose535a5d02012-10-19 16:05:26 +0000413 getCurFunction()->ObjCShouldCallSuper =
414 (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000415 }
Nico Weber80cb6e62011-08-28 22:35:17 +0000416 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000417 }
Chris Lattner4d391482007-12-12 07:09:47 +0000418}
419
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000420namespace {
421
422// Callback to only accept typo corrections that are Objective-C classes.
423// If an ObjCInterfaceDecl* is given to the constructor, then the validation
424// function will reject corrections to that class.
425class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
426 public:
427 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
428 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
429 : CurrentIDecl(IDecl) {}
430
431 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
432 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
433 return ID && !declaresSameEntity(ID, CurrentIDecl);
434 }
435
436 private:
437 ObjCInterfaceDecl *CurrentIDecl;
438};
439
440}
441
John McCalld226f652010-08-21 09:40:31 +0000442Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000443ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
444 IdentifierInfo *ClassName, SourceLocation ClassLoc,
445 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000446 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000447 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000448 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000449 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Chris Lattner4d391482007-12-12 07:09:47 +0000451 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000452 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000453 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000454
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000455 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000456 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000457 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Douglas Gregor7723fec2011-12-15 20:29:51 +0000460 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000461 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +0000462
463 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
464 // A previous decl with a different name is because of
465 // @compatibility_alias, for example:
466 // \code
467 // @class NewImage;
468 // @compatibility_alias OldImage NewImage;
469 // \endcode
470 // A lookup for 'OldImage' will return the 'NewImage' decl.
471 //
472 // In such a case use the real declaration name, instead of the alias one,
473 // otherwise we will break IdentifierResolver and redecls-chain invariants.
474 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
475 // has been aliased.
476 ClassName = PrevIDecl->getIdentifier();
477 }
478
Douglas Gregor7723fec2011-12-15 20:29:51 +0000479 ObjCInterfaceDecl *IDecl
480 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000481 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000482
Douglas Gregor7723fec2011-12-15 20:29:51 +0000483 if (PrevIDecl) {
484 // Class already seen. Was it a definition?
485 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
486 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
487 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000488 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000489 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000490 }
Chris Lattner4d391482007-12-12 07:09:47 +0000491 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000492
493 if (AttrList)
494 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
495 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000496
Douglas Gregor7723fec2011-12-15 20:29:51 +0000497 // Start the definition of this class. If we're in a redefinition case, there
498 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000499 if (!IDecl->hasDefinition())
500 IDecl->startDefinition();
501
Chris Lattner4d391482007-12-12 07:09:47 +0000502 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000503 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000504 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
505 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000506
507 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000508 // Try to correct for a typo in the superclass name without correcting
509 // to the class we're defining.
510 ObjCInterfaceValidatorCCC Validator(IDecl);
511 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000512 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000513 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000514 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
515 Diag(SuperLoc, diag::err_undef_superclass_suggest)
516 << SuperName << ClassName << PrevDecl->getDeclName();
517 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
518 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000519 }
520 }
521
Douglas Gregor60ef3082011-12-15 00:29:59 +0000522 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000523 Diag(SuperLoc, diag::err_recursive_superclass)
524 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000525 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000526 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000527 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000528 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000529
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000530 // Diagnose classes that inherit from deprecated classes.
531 if (SuperClassDecl)
532 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000534 if (PrevDecl && SuperClassDecl == 0) {
535 // The previous declaration was not a class decl. Check if we have a
536 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000537 if (const TypedefNameDecl *TDecl =
538 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000539 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000540 if (T->isObjCObjectType()) {
Fariborz Jahanian740991b2013-04-04 18:45:52 +0000541 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000542 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian740991b2013-04-04 18:45:52 +0000543 // This handles the following case:
544 // @interface NewI @end
545 // typedef NewI DeprI __attribute__((deprecated("blah")))
546 // @interface SI : DeprI /* warn here */ @end
547 (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc);
548 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000549 }
550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000552 // This handles the following case:
553 //
554 // typedef int SuperClass;
555 // @interface MyClass : SuperClass {} @end
556 //
557 if (!SuperClassDecl) {
558 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
559 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000560 }
561 }
Mike Stump1eb44332009-09-09 15:08:12 +0000562
Richard Smith162e1c12011-04-15 14:24:37 +0000563 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000564 if (!SuperClassDecl)
565 Diag(SuperLoc, diag::err_undef_superclass)
566 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000567 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000568 Context.getObjCInterfaceType(SuperClassDecl),
569 diag::err_forward_superclass,
570 SuperClassDecl->getDeclName(),
571 ClassName,
572 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000573 SuperClassDecl = 0;
574 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000575 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000576 IDecl->setSuperClass(SuperClassDecl);
577 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000578 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000579 }
Chris Lattner4d391482007-12-12 07:09:47 +0000580 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000581 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Sebastian Redl0b17c612010-08-13 00:28:03 +0000584 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000585 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000586 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000587 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000588 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000589 }
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Anders Carlsson15281452008-11-04 16:57:32 +0000591 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000592 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000593}
594
Richard Smithde01b7a2012-08-08 23:32:13 +0000595/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000596/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000597Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
598 IdentifierInfo *AliasName,
599 SourceLocation AliasLocation,
600 IdentifierInfo *ClassName,
601 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000602 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000603 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000604 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000605 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000606 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000607 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000608 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000609 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000610 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000611 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000612 }
613 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000614 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000615 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000616 if (const TypedefNameDecl *TDecl =
617 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000618 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000619 if (T->isObjCObjectType()) {
620 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000621 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000622 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000623 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000624 }
625 }
626 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000627 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
628 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000629 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000630 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000631 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000632 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000635 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000636 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000637 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Anders Carlsson15281452008-11-04 16:57:32 +0000639 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000640 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000641
John McCalld226f652010-08-21 09:40:31 +0000642 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000643}
644
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000645bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000646 IdentifierInfo *PName,
647 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000648 const ObjCList<ObjCProtocolDecl> &PList) {
649
650 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000651 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
652 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000653 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
654 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000655 if (PDecl->getIdentifier() == PName) {
656 Diag(Ploc, diag::err_protocol_has_circular_dependency);
657 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000658 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000659 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000660
661 if (!PDecl->hasDefinition())
662 continue;
663
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000664 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
665 PDecl->getLocation(), PDecl->getReferencedProtocols()))
666 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000667 }
668 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000669 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000670}
671
John McCalld226f652010-08-21 09:40:31 +0000672Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000673Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
674 IdentifierInfo *ProtocolName,
675 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000676 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000677 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000678 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000679 SourceLocation EndProtoLoc,
680 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000681 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000682 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000683 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000684 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
685 ForRedeclaration);
686 ObjCProtocolDecl *PDecl = 0;
687 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
688 // If we already have a definition, complain.
689 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
690 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Douglas Gregor27c6da22012-01-01 20:30:41 +0000692 // Create a new protocol that is completely distinct from previous
693 // declarations, and do not make this protocol available for name lookup.
694 // That way, we'll end up completely ignoring the duplicate.
695 // FIXME: Can we turn this into an error?
696 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
697 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000698 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000699 PDecl->startDefinition();
700 } else {
701 if (PrevDecl) {
702 // Check for circular dependencies among protocol declarations. This can
703 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000704 ObjCList<ObjCProtocolDecl> PList;
705 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
706 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000707 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000708 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000709
710 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000711 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000712 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000713 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000714
Douglas Gregor6e378de2009-04-23 23:18:26 +0000715 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000716 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000717 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000718
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000719 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000720 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000721
722 // Merge attributes from previous declarations.
723 if (PrevDecl)
724 mergeDeclAttributes(PDecl, PrevDecl);
725
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000726 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000727 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000728 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000729 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000730 }
Mike Stump1eb44332009-09-09 15:08:12 +0000731
732 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000733 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000734}
735
736/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000737/// issues an error if they are not declared. It returns list of
738/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000739void
Chris Lattnere13b9592008-07-26 04:03:38 +0000740Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000741 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000742 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000743 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000744 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000745 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
746 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000747 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000748 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000749 TypoCorrection Corrected = CorrectTypo(
750 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000751 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000752 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000753 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000754 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000755 Diag(PDecl->getLocation(), diag::note_previous_decl)
756 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000757 }
758 }
759
760 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000761 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000762 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000763 continue;
764 }
Fariborz Jahanian3c9a0242013-04-09 17:52:29 +0000765 // If this is a forward protocol declaration, get its definition.
766 if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
767 PDecl = PDecl->getDefinition();
768
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000769 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000770
771 // If this is a forward declaration and we are supposed to warn in this
772 // case, do it.
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000773 // FIXME: Recover nicely in the hidden case.
774 if (WarnOnDeclarations &&
775 (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000776 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000777 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000778 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000779 }
780}
781
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000782/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000783/// a class method in its extension.
784///
Mike Stump1eb44332009-09-09 15:08:12 +0000785void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000786 ObjCInterfaceDecl *ID) {
787 if (!ID)
788 return; // Possibly due to previous error
789
790 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000791 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
792 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000793 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000794 MethodMap[MD->getSelector()] = MD;
795 }
796
797 if (MethodMap.empty())
798 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000799 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
800 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000801 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000802 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
803 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
804 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
805 << Method->getDeclName();
806 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
807 }
808 }
809}
810
James Dennett1dfbd922012-06-14 21:40:34 +0000811/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000812Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000813Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000814 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000815 unsigned NumElts,
816 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000817 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000818 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000819 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000820 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
821 ForRedeclaration);
822 ObjCProtocolDecl *PDecl
823 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
824 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000825 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000826
827 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000828 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000829
Douglas Gregor3937f872012-01-01 20:33:24 +0000830 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000831 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000832
833 if (PrevDecl)
834 mergeDeclAttributes(PDecl, PrevDecl);
835
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000836 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000839 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000840}
841
John McCalld226f652010-08-21 09:40:31 +0000842Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000843ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
844 IdentifierInfo *ClassName, SourceLocation ClassLoc,
845 IdentifierInfo *CategoryName,
846 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000847 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000848 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000849 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000850 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000851 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000852 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000853
854 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000855
856 if (!IDecl
857 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000858 diag::err_category_forward_interface,
859 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000860 // Create an invalid ObjCCategoryDecl to serve as context for
861 // the enclosing method declarations. We mark the decl invalid
862 // to make it clear that this isn't a valid AST.
863 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000864 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000865 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000866 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000867
868 if (!IDecl)
869 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000870 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000871 }
872
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000873 if (!CategoryName && IDecl->getImplementation()) {
874 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
875 Diag(IDecl->getImplementation()->getLocation(),
876 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000877 }
878
Fariborz Jahanian25760612010-02-15 21:55:26 +0000879 if (CategoryName) {
880 /// Check for duplicate interface declaration for this category
Douglas Gregord3297242013-01-16 23:00:23 +0000881 if (ObjCCategoryDecl *Previous
882 = IDecl->FindCategoryDeclaration(CategoryName)) {
883 // Class extensions can be declared multiple times, categories cannot.
884 Diag(CategoryLoc, diag::warn_dup_category_def)
885 << ClassName << CategoryName;
886 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner70f19542009-02-16 21:26:43 +0000887 }
888 }
Chris Lattner70f19542009-02-16 21:26:43 +0000889
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000890 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
891 ClassLoc, CategoryLoc, CategoryName, IDecl);
892 // FIXME: PushOnScopeChains?
893 CurContext->addDecl(CDecl);
894
Chris Lattner4d391482007-12-12 07:09:47 +0000895 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000896 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000897 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000898 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000899 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000900 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000901 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000902 }
Mike Stump1eb44332009-09-09 15:08:12 +0000903
Anders Carlsson15281452008-11-04 16:57:32 +0000904 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000905 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000906}
907
908/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000909/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000910/// object.
John McCalld226f652010-08-21 09:40:31 +0000911Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000912 SourceLocation AtCatImplLoc,
913 IdentifierInfo *ClassName, SourceLocation ClassLoc,
914 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000915 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000916 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000917 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000918 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
919 if (!CatIDecl) {
920 // Category @implementation with no corresponding @interface.
921 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000922 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
923 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000924 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000925 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000926 }
927 }
928
Mike Stump1eb44332009-09-09 15:08:12 +0000929 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000930 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000931 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000932 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000933 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000934 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000935 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000936 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
937 diag::err_undef_interface)) {
938 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000939 }
Chris Lattner4d391482007-12-12 07:09:47 +0000940
Douglas Gregord0434102009-01-09 00:49:46 +0000941 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000942 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000943
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000944 // If the interface is deprecated/unavailable, warn/error about it.
945 if (IDecl)
946 DiagnoseUseOfDecl(IDecl, ClassLoc);
947
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000948 /// Check that CatName, category name, is not used in another implementation.
949 if (CatIDecl) {
950 if (CatIDecl->getImplementation()) {
951 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
952 << CatName;
953 Diag(CatIDecl->getImplementation()->getLocation(),
954 diag::note_previous_definition);
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000955 CDecl->setInvalidDecl();
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000956 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000957 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000958 // Warn on implementating category of deprecated class under
959 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000960 DiagnoseObjCImplementedDeprecations(*this,
961 dyn_cast<NamedDecl>(IDecl),
962 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000963 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000964 }
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Anders Carlsson15281452008-11-04 16:57:32 +0000966 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000967 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000968}
969
John McCalld226f652010-08-21 09:40:31 +0000970Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000971 SourceLocation AtClassImplLoc,
972 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000973 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000974 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000975 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000976 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000977 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000978 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
979 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000980 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000981 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000982 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000983 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000984 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
985 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000986 } else {
987 // We did not find anything with the name ClassName; try to correct for
988 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000989 ObjCInterfaceValidatorCCC Validator;
990 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000991 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000992 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000993 // Suggest the (potentially) correct interface name. However, put the
994 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000995 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000996 // provide a code-modification hint or use the typo name for recovery,
997 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000998 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000999 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +00001000 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +00001001 << ClassName << CorrectedName;
1002 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
1003 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +00001004 IDecl = 0;
1005 } else {
1006 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
1007 }
Chris Lattner4d391482007-12-12 07:09:47 +00001008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Chris Lattner4d391482007-12-12 07:09:47 +00001010 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001011 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +00001012 if (SuperClassname) {
1013 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +00001014 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
1015 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001016 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001017 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1018 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +00001019 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +00001020 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001021 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +00001022 if (SDecl && !SDecl->hasDefinition())
1023 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +00001024 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +00001025 Diag(SuperClassLoc, diag::err_undef_superclass)
1026 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +00001027 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001028 // This implementation and its interface do not have the same
1029 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +00001030 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +00001031 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001032 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001033 }
1034 }
1035 }
Mike Stump1eb44332009-09-09 15:08:12 +00001036
Chris Lattner4d391482007-12-12 07:09:47 +00001037 if (!IDecl) {
1038 // Legacy case of @implementation with no corresponding @interface.
1039 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +00001040
Mike Stump390b4cc2009-05-16 07:39:55 +00001041 // FIXME: Do we support attributes on the @implementation? If so we should
1042 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001043 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001044 ClassName, /*PrevDecl=*/0, ClassLoc,
1045 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001046 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001047 if (SDecl) {
1048 IDecl->setSuperClass(SDecl);
1049 IDecl->setSuperClassLoc(SuperClassLoc);
1050 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1051 } else {
1052 IDecl->setEndOfDefinitionLoc(ClassLoc);
1053 }
1054
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001055 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001056 } else {
1057 // Mark the interface as being completed, even if it was just as
1058 // @class ....;
1059 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001060 if (!IDecl->hasDefinition())
1061 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001062 }
Mike Stump1eb44332009-09-09 15:08:12 +00001063
1064 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001065 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
Argyrios Kyrtzidis634c5632013-05-03 18:05:44 +00001066 ClassLoc, AtClassImplLoc, SuperClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Anders Carlsson15281452008-11-04 16:57:32 +00001068 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001069 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Chris Lattner4d391482007-12-12 07:09:47 +00001071 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001072 if (IDecl->getImplementation()) {
1073 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001074 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001075 Diag(IDecl->getImplementation()->getLocation(),
1076 diag::note_previous_definition);
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +00001077 IMPDecl->setInvalidDecl();
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001078 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001079 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001080 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001081 // Warn on implementating deprecated class under
1082 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001083 DiagnoseObjCImplementedDeprecations(*this,
1084 dyn_cast<NamedDecl>(IDecl),
1085 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001086 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001087 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001088}
1089
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001090Sema::DeclGroupPtrTy
1091Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1092 SmallVector<Decl *, 64> DeclsInGroup;
1093 DeclsInGroup.reserve(Decls.size() + 1);
1094
1095 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1096 Decl *Dcl = Decls[i];
1097 if (!Dcl)
1098 continue;
1099 if (Dcl->getDeclContext()->isFileContext())
1100 Dcl->setTopLevelDeclInObjCContainer();
1101 DeclsInGroup.push_back(Dcl);
1102 }
1103
1104 DeclsInGroup.push_back(ObjCImpDecl);
1105
1106 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1107}
1108
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001109void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1110 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001111 SourceLocation RBrace) {
1112 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001113 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001114 if (!IDecl)
1115 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001116 /// Check case of non-existing \@interface decl.
1117 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001118 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001119 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001120 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001121 // Add ivar's to class's DeclContext.
1122 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001123 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001124 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001125 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001126 }
1127
Chris Lattner4d391482007-12-12 07:09:47 +00001128 return;
1129 }
1130 // If implementation has empty ivar list, just return.
1131 if (numIvars == 0)
1132 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Chris Lattner4d391482007-12-12 07:09:47 +00001134 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001135 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001136 if (ImpDecl->getSuperClass())
1137 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1138 for (unsigned i = 0; i < numIvars; i++) {
1139 ObjCIvarDecl* ImplIvar = ivars[i];
1140 if (const ObjCIvarDecl *ClsIvar =
1141 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1142 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1143 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1144 continue;
1145 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001146 // Instance ivar to Implementation's DeclContext.
1147 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001148 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001149 ImpDecl->addDecl(ImplIvar);
1150 }
1151 return;
1152 }
Chris Lattner4d391482007-12-12 07:09:47 +00001153 // Check interface's Ivar list against those in the implementation.
1154 // names and types must match.
1155 //
Chris Lattner4d391482007-12-12 07:09:47 +00001156 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001157 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001158 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1159 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001160 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001161 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001162 assert (ImplIvar && "missing implementation ivar");
1163 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001164
Steve Naroffca331292009-03-03 14:49:36 +00001165 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001166 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001167 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001168 << ImplIvar->getIdentifier()
1169 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001170 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001171 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1172 ImplIvar->getBitWidthValue(Context) !=
1173 ClsIvar->getBitWidthValue(Context)) {
1174 Diag(ImplIvar->getBitWidth()->getLocStart(),
1175 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1176 Diag(ClsIvar->getBitWidth()->getLocStart(),
1177 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001178 }
Steve Naroffca331292009-03-03 14:49:36 +00001179 // Make sure the names are identical.
1180 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001181 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001182 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001183 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001184 }
1185 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001186 }
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Chris Lattner609e4c72007-12-12 18:11:49 +00001188 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001189 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001190 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001191 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001192}
1193
Steve Naroff3c2eb662008-02-10 21:38:56 +00001194void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001195 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001196 // No point warning no definition of method which is 'unavailable'.
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001197 switch (method->getAvailability()) {
1198 case AR_Available:
1199 case AR_Deprecated:
1200 break;
1201
1202 // Don't warn about unavailable or not-yet-introduced methods.
1203 case AR_NotYetIntroduced:
1204 case AR_Unavailable:
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001205 return;
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001206 }
1207
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001208 // FIXME: For now ignore 'IncompleteImpl'.
1209 // Previously we grouped all unimplemented methods under a single
1210 // warning, but some users strongly voiced that they would prefer
1211 // separate warnings. We will give that approach a try, as that
1212 // matches what we do with protocols.
1213
1214 Diag(ImpLoc, DiagID) << method->getDeclName();
1215
1216 // Issue a note to the original declaration.
1217 SourceLocation MethodLoc = method->getLocStart();
1218 if (MethodLoc.isValid())
1219 Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001220}
1221
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001222/// Determines if type B can be substituted for type A. Returns true if we can
1223/// guarantee that anything that the user will do to an object of type A can
1224/// also be done to an object of type B. This is trivially true if the two
1225/// types are the same, or if B is a subclass of A. It becomes more complex
1226/// in cases where protocols are involved.
1227///
1228/// Object types in Objective-C describe the minimum requirements for an
1229/// object, rather than providing a complete description of a type. For
1230/// example, if A is a subclass of B, then B* may refer to an instance of A.
1231/// The principle of substitutability means that we may use an instance of A
1232/// anywhere that we may use an instance of B - it will implement all of the
1233/// ivars of B and all of the methods of B.
1234///
1235/// This substitutability is important when type checking methods, because
1236/// the implementation may have stricter type definitions than the interface.
1237/// The interface specifies minimum requirements, but the implementation may
1238/// have more accurate ones. For example, a method may privately accept
1239/// instances of B, but only publish that it accepts instances of A. Any
1240/// object passed to it will be type checked against B, and so will implicitly
1241/// by a valid A*. Similarly, a method may return a subclass of the class that
1242/// it is declared as returning.
1243///
1244/// This is most important when considering subclassing. A method in a
1245/// subclass must accept any object as an argument that its superclass's
1246/// implementation accepts. It may, however, accept a more general type
1247/// without breaking substitutability (i.e. you can still use the subclass
1248/// anywhere that you can use the superclass, but not vice versa). The
1249/// converse requirement applies to return types: the return type for a
1250/// subclass method must be a valid object of the kind that the superclass
1251/// advertises, but it may be specified more accurately. This avoids the need
1252/// for explicit down-casting by callers.
1253///
1254/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001255static bool isObjCTypeSubstitutable(ASTContext &Context,
1256 const ObjCObjectPointerType *A,
1257 const ObjCObjectPointerType *B,
1258 bool rejectId) {
1259 // Reject a protocol-unqualified id.
1260 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001261
1262 // If B is a qualified id, then A must also be a qualified id and it must
1263 // implement all of the protocols in B. It may not be a qualified class.
1264 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1265 // stricter definition so it is not substitutable for id<A>.
1266 if (B->isObjCQualifiedIdType()) {
1267 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001268 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1269 QualType(B,0),
1270 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001271 }
1272
1273 /*
1274 // id is a special type that bypasses type checking completely. We want a
1275 // warning when it is used in one place but not another.
1276 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1277
1278
1279 // If B is a qualified id, then A must also be a qualified id (which it isn't
1280 // if we've got this far)
1281 if (B->isObjCQualifiedIdType()) return false;
1282 */
1283
1284 // Now we know that A and B are (potentially-qualified) class types. The
1285 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001286 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001287}
1288
John McCall10302c02010-10-28 02:34:38 +00001289static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1290 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1291}
1292
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001293static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001294 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001295 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001296 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001297 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001298 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001299 if (IsProtocolMethodDecl &&
1300 (MethodDecl->getObjCDeclQualifier() !=
1301 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001302 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001303 S.Diag(MethodImpl->getLocation(),
1304 (IsOverridingMode ?
1305 diag::warn_conflicting_overriding_ret_type_modifiers
1306 : diag::warn_conflicting_ret_type_modifiers))
1307 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001308 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1309 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1310 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1311 }
1312 else
1313 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001314 }
1315
John McCall10302c02010-10-28 02:34:38 +00001316 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001317 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001318 return true;
1319 if (!Warn)
1320 return false;
John McCall10302c02010-10-28 02:34:38 +00001321
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001322 unsigned DiagID =
1323 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1324 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001325
1326 // Mismatches between ObjC pointers go into a different warning
1327 // category, and sometimes they're even completely whitelisted.
1328 if (const ObjCObjectPointerType *ImplPtrTy =
1329 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1330 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001331 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001332 // Allow non-matching return types as long as they don't violate
1333 // the principle of substitutability. Specifically, we permit
1334 // return types that are subclasses of the declared return type,
1335 // or that are more-qualified versions of the declared type.
1336 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001337 return false;
John McCall10302c02010-10-28 02:34:38 +00001338
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001339 DiagID =
1340 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1341 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001342 }
1343 }
1344
1345 S.Diag(MethodImpl->getLocation(), DiagID)
1346 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001347 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001348 << MethodImpl->getResultType()
1349 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001350 S.Diag(MethodDecl->getLocation(),
1351 IsOverridingMode ? diag::note_previous_declaration
1352 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001353 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001354 return false;
John McCall10302c02010-10-28 02:34:38 +00001355}
1356
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001357static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001358 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001359 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001360 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001361 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001362 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001363 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001364 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001365 if (IsProtocolMethodDecl &&
1366 (ImplVar->getObjCDeclQualifier() !=
1367 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001368 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001369 if (IsOverridingMode)
1370 S.Diag(ImplVar->getLocation(),
1371 diag::warn_conflicting_overriding_param_modifiers)
1372 << getTypeRange(ImplVar->getTypeSourceInfo())
1373 << MethodImpl->getDeclName();
1374 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001375 diag::warn_conflicting_param_modifiers)
1376 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001377 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001378 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1379 << getTypeRange(IfaceVar->getTypeSourceInfo());
1380 }
1381 else
1382 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001383 }
1384
John McCall10302c02010-10-28 02:34:38 +00001385 QualType ImplTy = ImplVar->getType();
1386 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001387
John McCall10302c02010-10-28 02:34:38 +00001388 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001389 return true;
1390
1391 if (!Warn)
1392 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001393 unsigned DiagID =
1394 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1395 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001396
1397 // Mismatches between ObjC pointers go into a different warning
1398 // category, and sometimes they're even completely whitelisted.
1399 if (const ObjCObjectPointerType *ImplPtrTy =
1400 ImplTy->getAs<ObjCObjectPointerType>()) {
1401 if (const ObjCObjectPointerType *IfacePtrTy =
1402 IfaceTy->getAs<ObjCObjectPointerType>()) {
1403 // Allow non-matching argument types as long as they don't
1404 // violate the principle of substitutability. Specifically, the
1405 // implementation must accept any objects that the superclass
1406 // accepts, however it may also accept others.
1407 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001408 return false;
John McCall10302c02010-10-28 02:34:38 +00001409
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001410 DiagID =
1411 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1412 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001413 }
1414 }
1415
1416 S.Diag(ImplVar->getLocation(), DiagID)
1417 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001418 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1419 S.Diag(IfaceVar->getLocation(),
1420 (IsOverridingMode ? diag::note_previous_declaration
1421 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001422 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001423 return false;
John McCall10302c02010-10-28 02:34:38 +00001424}
John McCallf85e1932011-06-15 23:02:42 +00001425
1426/// In ARC, check whether the conventional meanings of the two methods
1427/// match. If they don't, it's a hard error.
1428static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1429 ObjCMethodDecl *decl) {
1430 ObjCMethodFamily implFamily = impl->getMethodFamily();
1431 ObjCMethodFamily declFamily = decl->getMethodFamily();
1432 if (implFamily == declFamily) return false;
1433
1434 // Since conventions are sorted by selector, the only possibility is
1435 // that the types differ enough to cause one selector or the other
1436 // to fall out of the family.
1437 assert(implFamily == OMF_None || declFamily == OMF_None);
1438
1439 // No further diagnostics required on invalid declarations.
1440 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1441
1442 const ObjCMethodDecl *unmatched = impl;
1443 ObjCMethodFamily family = declFamily;
1444 unsigned errorID = diag::err_arc_lost_method_convention;
1445 unsigned noteID = diag::note_arc_lost_method_convention;
1446 if (declFamily == OMF_None) {
1447 unmatched = decl;
1448 family = implFamily;
1449 errorID = diag::err_arc_gained_method_convention;
1450 noteID = diag::note_arc_gained_method_convention;
1451 }
1452
1453 // Indexes into a %select clause in the diagnostic.
1454 enum FamilySelector {
1455 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1456 };
1457 FamilySelector familySelector = FamilySelector();
1458
1459 switch (family) {
1460 case OMF_None: llvm_unreachable("logic error, no method convention");
1461 case OMF_retain:
1462 case OMF_release:
1463 case OMF_autorelease:
1464 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001465 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001466 case OMF_retainCount:
1467 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001468 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001469 // Mismatches for these methods don't change ownership
1470 // conventions, so we don't care.
1471 return false;
1472
1473 case OMF_init: familySelector = F_init; break;
1474 case OMF_alloc: familySelector = F_alloc; break;
1475 case OMF_copy: familySelector = F_copy; break;
1476 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1477 case OMF_new: familySelector = F_new; break;
1478 }
1479
1480 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1481 ReasonSelector reasonSelector;
1482
1483 // The only reason these methods don't fall within their families is
1484 // due to unusual result types.
1485 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1486 reasonSelector = R_UnrelatedReturn;
1487 } else {
1488 reasonSelector = R_NonObjectReturn;
1489 }
1490
1491 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1492 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1493
1494 return true;
1495}
John McCall10302c02010-10-28 02:34:38 +00001496
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001497void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001498 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001499 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001500 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001501 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1502 return;
1503
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001504 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001505 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001506 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001507
Chris Lattner3aff9192009-04-11 19:58:42 +00001508 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001509 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1510 EF = MethodDecl->param_end();
1511 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001512 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001513 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001514 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001515
Fariborz Jahanian21121902011-08-08 18:03:17 +00001516 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001517 Diag(ImpMethodDecl->getLocation(),
1518 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001519 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001520 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001521}
1522
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001523void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1524 ObjCMethodDecl *Overridden,
1525 bool IsProtocolMethodDecl) {
1526
1527 CheckMethodOverrideReturn(*this, Method, Overridden,
1528 IsProtocolMethodDecl, true,
1529 true);
1530
1531 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001532 IF = Overridden->param_begin(), EM = Method->param_end(),
1533 EF = Overridden->param_end();
1534 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001535 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1536 IsProtocolMethodDecl, true, true);
1537 }
1538
1539 if (Method->isVariadic() != Overridden->isVariadic()) {
1540 Diag(Method->getLocation(),
1541 diag::warn_conflicting_overriding_variadic);
1542 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1543 }
1544}
1545
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001546/// WarnExactTypedMethods - This routine issues a warning if method
1547/// implementation declaration matches exactly that of its declaration.
1548void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1549 ObjCMethodDecl *MethodDecl,
1550 bool IsProtocolMethodDecl) {
1551 // don't issue warning when protocol method is optional because primary
1552 // class is not required to implement it and it is safe for protocol
1553 // to implement it.
1554 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1555 return;
1556 // don't issue warning when primary class's method is
1557 // depecated/unavailable.
1558 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1559 MethodDecl->hasAttr<DeprecatedAttr>())
1560 return;
1561
1562 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1563 IsProtocolMethodDecl, false, false);
1564 if (match)
1565 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001566 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1567 EF = MethodDecl->param_end();
1568 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001569 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1570 *IM, *IF,
1571 IsProtocolMethodDecl, false, false);
1572 if (!match)
1573 break;
1574 }
1575 if (match)
1576 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001577 if (match)
1578 match = !(MethodDecl->isClassMethod() &&
1579 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001580
1581 if (match) {
1582 Diag(ImpMethodDecl->getLocation(),
1583 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001584 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1585 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001586 }
1587}
1588
Mike Stump390b4cc2009-05-16 07:39:55 +00001589/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1590/// improve the efficiency of selector lookups and type checking by associating
1591/// with each protocol / interface / category the flattened instance tables. If
1592/// we used an immutable set to keep the table then it wouldn't add significant
1593/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001594
Steve Naroffefe7f362008-02-08 22:06:17 +00001595/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001596/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001597void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1598 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001599 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001600 const SelectorSet &InsMap,
1601 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001602 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001603 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1604 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1605 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001606 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1607
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001608 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001609 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001610 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001611 // check to see if class implements forwardInvocation method and objects
1612 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001613 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001614 // Under such conditions, which means that every method possible is
1615 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001616 // found" warnings.
1617 // FIXME: Use a general GetUnarySelector method for this.
1618 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1619 Selector fISelector = Context.Selectors.getSelector(1, &II);
1620 if (InsMap.count(fISelector))
1621 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1622 // need be implemented in the implementation.
1623 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1624 }
Mike Stump1eb44332009-09-09 15:08:12 +00001625
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001626 // If this is a forward protocol declaration, get its definition.
1627 if (!PDecl->isThisDeclarationADefinition() &&
1628 PDecl->getDefinition())
1629 PDecl = PDecl->getDefinition();
1630
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001631 // If a method lookup fails locally we still need to look and see if
1632 // the method was implemented by a base class or an inherited
1633 // protocol. This lookup is slow, but occurs rarely in correct code
1634 // and otherwise would terminate in a warning.
1635
Chris Lattner4d391482007-12-12 07:09:47 +00001636 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001637 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001638 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001639 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001640 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001641 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001642 !method->isPropertyAccessor() &&
1643 !InsMap.count(method->getSelector()) &&
1644 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001645 // If a method is not implemented in the category implementation but
1646 // has been declared in its primary class, superclass,
1647 // or in one of their protocols, no need to issue the warning.
1648 // This is because method will be implemented in the primary class
1649 // or one of its super class implementation.
1650
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001651 // Ugly, but necessary. Method declared in protcol might have
1652 // have been synthesized due to a property declared in the class which
1653 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001654 if (ObjCMethodDecl *MethodInClass =
1655 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001656 true /*shallowCategoryLookup*/))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001657 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001658 continue;
1659 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1660 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1661 != DiagnosticsEngine::Ignored) {
1662 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001663 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1664 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001665 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001666 }
1667 }
Chris Lattner4d391482007-12-12 07:09:47 +00001668 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001669 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001670 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001671 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001672 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001673 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1674 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001675 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001676 // See above comment for instance method lookups.
1677 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001678 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001679 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001680 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001681 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1682 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001683 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
1684 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1685 PDecl->getDeclName();
1686 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001687 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001688 }
Chris Lattner780f3292008-07-21 21:32:27 +00001689 // Check on this protocols's referenced protocols, recursively.
1690 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1691 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001692 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001693}
1694
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001695/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001696/// or protocol against those declared in their implementations.
1697///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001698void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1699 const SelectorSet &ClsMap,
1700 SelectorSet &InsMapSeen,
1701 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001702 ObjCImplDecl* IMPDecl,
1703 ObjCContainerDecl* CDecl,
1704 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001705 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001706 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001707 // Check and see if instance methods in class interface have been
1708 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001709 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1710 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001711 if (InsMapSeen.count((*I)->getSelector()))
1712 continue;
1713 InsMapSeen.insert((*I)->getSelector());
Jordan Rose1e4691b2012-10-10 16:42:25 +00001714 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001715 !InsMap.count((*I)->getSelector())) {
1716 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001717 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001718 diag::warn_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001719 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001720 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001721 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001722 IMPDecl->getInstanceMethod((*I)->getSelector());
1723 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1724 "Expected to find the method through lookup as well");
1725 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001726 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001727 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001728 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001729 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1730 isa<ObjCProtocolDecl>(CDecl));
Jordan Rose1e4691b2012-10-10 16:42:25 +00001731 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001732 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001733 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001734 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001735 }
1736 }
Mike Stump1eb44332009-09-09 15:08:12 +00001737
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001738 // Check and see if class methods in class interface have been
1739 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001740 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001741 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001742 if (ClsMapSeen.count((*I)->getSelector()))
1743 continue;
1744 ClsMapSeen.insert((*I)->getSelector());
1745 if (!ClsMap.count((*I)->getSelector())) {
1746 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001747 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001748 diag::warn_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001749 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001750 ObjCMethodDecl *ImpMethodDecl =
1751 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001752 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1753 "Expected to find the method through lookup as well");
1754 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001755 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001756 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1757 isa<ObjCProtocolDecl>(CDecl));
1758 else
1759 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001760 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001761 }
1762 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001763
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001764 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001765 // when checking that methods in implementation match their declaration,
1766 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1767 // extension; as well as those in categories.
Douglas Gregord3297242013-01-16 23:00:23 +00001768 if (!WarnCategoryMethodImpl) {
1769 for (ObjCInterfaceDecl::visible_categories_iterator
1770 Cat = I->visible_categories_begin(),
1771 CatEnd = I->visible_categories_end();
1772 Cat != CatEnd; ++Cat) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001773 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001774 IMPDecl, *Cat, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001775 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001776 }
1777 } else {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001778 // Also methods in class extensions need be looked at next.
Douglas Gregord3297242013-01-16 23:00:23 +00001779 for (ObjCInterfaceDecl::visible_extensions_iterator
1780 Ext = I->visible_extensions_begin(),
1781 ExtEnd = I->visible_extensions_end();
1782 Ext != ExtEnd; ++Ext) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001783 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001784 IMPDecl, *Ext, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001785 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001786 }
1787 }
1788
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001789 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001790 for (ObjCInterfaceDecl::all_protocol_iterator
1791 PI = I->all_referenced_protocol_begin(),
1792 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001793 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1794 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001795 (*PI), IncompleteImpl, false,
1796 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001797
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001798 // FIXME. For now, we are not checking for extact match of methods
1799 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001800 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001801 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001802 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001803 I->getSuperClass(), IncompleteImpl, false);
1804 }
1805}
1806
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001807/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1808/// category matches with those implemented in its primary class and
1809/// warns each time an exact match is found.
1810void Sema::CheckCategoryVsClassMethodMatches(
1811 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001812 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001813
1814 for (ObjCImplementationDecl::instmeth_iterator
1815 I = CatIMPDecl->instmeth_begin(),
1816 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1817 InsMap.insert((*I)->getSelector());
1818
1819 for (ObjCImplementationDecl::classmeth_iterator
1820 I = CatIMPDecl->classmeth_begin(),
1821 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1822 ClsMap.insert((*I)->getSelector());
1823 if (InsMap.empty() && ClsMap.empty())
1824 return;
1825
1826 // Get category's primary class.
1827 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1828 if (!CatDecl)
1829 return;
1830 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1831 if (!IDecl)
1832 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001833 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001834 bool IncompleteImpl = false;
1835 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1836 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001837 IncompleteImpl, false,
1838 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001839}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001840
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001841void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001842 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001843 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001844 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001845 // Check and see if instance methods in class interface have been
1846 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001847 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001848 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001849 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001851 // Check and see if properties declared in the interface have either 1)
1852 // an implementation or 2) there is a @synthesize/@dynamic implementation
1853 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001854 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001855 if (!(LangOpts.ObjCDefaultSynthProperties &&
1856 LangOpts.ObjCRuntime.isNonFragile()) ||
1857 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001858 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001859
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001860 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001861 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001862 I = IMPDecl->classmeth_begin(),
1863 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001864 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001866 // Check for type conflict of methods declared in a class/protocol and
1867 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001868 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001869 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1870 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001871 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001872
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001873 // check all methods implemented in category against those declared
1874 // in its primary class.
1875 if (ObjCCategoryImplDecl *CatDecl =
1876 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1877 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001878
Chris Lattner4d391482007-12-12 07:09:47 +00001879 // Check the protocol list for unimplemented methods in the @implementation
1880 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001881 // Check and see if class methods in class interface have been
1882 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001883
Chris Lattnercddc8882009-03-01 00:56:52 +00001884 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001885 for (ObjCInterfaceDecl::all_protocol_iterator
1886 PI = I->all_referenced_protocol_begin(),
1887 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001888 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001889 InsMap, ClsMap, I);
1890 // Check class extensions (unnamed categories)
Douglas Gregord3297242013-01-16 23:00:23 +00001891 for (ObjCInterfaceDecl::visible_extensions_iterator
1892 Ext = I->visible_extensions_begin(),
1893 ExtEnd = I->visible_extensions_end();
1894 Ext != ExtEnd; ++Ext) {
1895 ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
1896 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001897 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001898 // For extended class, unimplemented methods in its protocols will
1899 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001900 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001901 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1902 E = C->protocol_end(); PI != E; ++PI)
1903 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001904 InsMap, ClsMap, CDecl);
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001905 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001906 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001907 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001908 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001909}
1910
Mike Stump1eb44332009-09-09 15:08:12 +00001911/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001912Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001913Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001914 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001915 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001916 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001917 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001918 for (unsigned i = 0; i != NumElts; ++i) {
1919 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001920 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001921 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001922 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001923 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001924 // Maybe we will complain about the shadowed template parameter.
1925 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1926 // Just pretend that we didn't see the previous declaration.
1927 PrevDecl = 0;
1928 }
1929
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001930 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001931 // GCC apparently allows the following idiom:
1932 //
1933 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1934 // @class XCElementToggler;
1935 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001936 // Here we have chosen to ignore the forward class declaration
1937 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001938 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001939 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001940 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001941 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001942 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001943 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001944 // to the underlying class. Just ignore the forward class with a warning
1945 // as this will force the intended behavior which is to lookup the typedef
1946 // name.
1947 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1948 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1949 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1950 continue;
1951 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001952 }
Chris Lattner4d391482007-12-12 07:09:47 +00001953 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001954
1955 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001956 ObjCInterfaceDecl *PrevIDecl
1957 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +00001958
1959 IdentifierInfo *ClassName = IdentList[i];
1960 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
1961 // A previous decl with a different name is because of
1962 // @compatibility_alias, for example:
1963 // \code
1964 // @class NewImage;
1965 // @compatibility_alias OldImage NewImage;
1966 // \endcode
1967 // A lookup for 'OldImage' will return the 'NewImage' decl.
1968 //
1969 // In such a case use the real declaration name, instead of the alias one,
1970 // otherwise we will break IdentifierResolver and redecls-chain invariants.
1971 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
1972 // has been aliased.
1973 ClassName = PrevIDecl->getIdentifier();
1974 }
1975
Douglas Gregor7723fec2011-12-15 20:29:51 +00001976 ObjCInterfaceDecl *IDecl
1977 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +00001978 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001979 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001980
Douglas Gregor7723fec2011-12-15 20:29:51 +00001981 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001982 CheckObjCDeclScope(IDecl);
1983 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001984 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001985
1986 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001987}
1988
John McCall0f4c4c42011-06-16 01:15:19 +00001989static bool tryMatchRecordTypes(ASTContext &Context,
1990 Sema::MethodMatchStrategy strategy,
1991 const Type *left, const Type *right);
1992
John McCallf85e1932011-06-15 23:02:42 +00001993static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1994 QualType leftQT, QualType rightQT) {
1995 const Type *left =
1996 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1997 const Type *right =
1998 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1999
2000 if (left == right) return true;
2001
2002 // If we're doing a strict match, the types have to match exactly.
2003 if (strategy == Sema::MMS_strict) return false;
2004
2005 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2006
2007 // Otherwise, use this absurdly complicated algorithm to try to
2008 // validate the basic, low-level compatibility of the two types.
2009
2010 // As a minimum, require the sizes and alignments to match.
2011 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
2012 return false;
2013
2014 // Consider all the kinds of non-dependent canonical types:
2015 // - functions and arrays aren't possible as return and parameter types
2016
2017 // - vector types of equal size can be arbitrarily mixed
2018 if (isa<VectorType>(left)) return isa<VectorType>(right);
2019 if (isa<VectorType>(right)) return false;
2020
2021 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00002022 // - structs, unions, and Objective-C objects must match more-or-less
2023 // exactly
John McCallf85e1932011-06-15 23:02:42 +00002024 // - everything else should be a scalar
2025 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00002026 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00002027
John McCall1d9b3b22011-09-09 05:25:32 +00002028 // Make scalars agree in kind, except count bools as chars, and group
2029 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00002030 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2031 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2032 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2033 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00002034 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2035 leftSK = Type::STK_ObjCObjectPointer;
2036 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2037 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00002038
2039 // Note that data member pointers and function member pointers don't
2040 // intermix because of the size differences.
2041
2042 return (leftSK == rightSK);
2043}
Chris Lattner4d391482007-12-12 07:09:47 +00002044
John McCall0f4c4c42011-06-16 01:15:19 +00002045static bool tryMatchRecordTypes(ASTContext &Context,
2046 Sema::MethodMatchStrategy strategy,
2047 const Type *lt, const Type *rt) {
2048 assert(lt && rt && lt != rt);
2049
2050 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2051 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2052 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2053
2054 // Require union-hood to match.
2055 if (left->isUnion() != right->isUnion()) return false;
2056
2057 // Require an exact match if either is non-POD.
2058 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2059 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2060 return false;
2061
2062 // Require size and alignment to match.
2063 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2064
2065 // Require fields to match.
2066 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2067 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2068 for (; li != le && ri != re; ++li, ++ri) {
2069 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2070 return false;
2071 }
2072 return (li == le && ri == re);
2073}
2074
Chris Lattner4d391482007-12-12 07:09:47 +00002075/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2076/// returns true, or false, accordingly.
2077/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00002078bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2079 const ObjCMethodDecl *right,
2080 MethodMatchStrategy strategy) {
2081 if (!matchTypes(Context, strategy,
2082 left->getResultType(), right->getResultType()))
2083 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002084
Douglas Gregor7666b032013-02-07 19:13:24 +00002085 // If either is hidden, it is not considered to match.
2086 if (left->isHidden() || right->isHidden())
2087 return false;
2088
David Blaikie4e4d0842012-03-11 07:00:24 +00002089 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002090 (left->hasAttr<NSReturnsRetainedAttr>()
2091 != right->hasAttr<NSReturnsRetainedAttr>() ||
2092 left->hasAttr<NSConsumesSelfAttr>()
2093 != right->hasAttr<NSConsumesSelfAttr>()))
2094 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002095
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002096 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002097 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2098 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002099
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002100 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002101 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002102 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002103
2104 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2105 return false;
2106
David Blaikie4e4d0842012-03-11 07:00:24 +00002107 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002108 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2109 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002110 }
2111 return true;
2112}
2113
Douglas Gregorff310c72012-05-01 23:37:00 +00002114void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002115 // Record at the head of the list whether there were 0, 1, or >= 2 methods
2116 // inside categories.
Argyrios Kyrtzidisab3d5092013-04-27 00:10:12 +00002117 if (ObjCCategoryDecl *
2118 CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
2119 if (!CD->IsClassExtension() && List->getBits() < 2)
2120 List->setBits(List->getBits()+1);
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002121
Douglas Gregor44fae522012-01-25 00:19:56 +00002122 // If the list is empty, make it a singleton list.
2123 if (List->Method == 0) {
2124 List->Method = Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002125 List->setNext(0);
Douglas Gregorff310c72012-05-01 23:37:00 +00002126 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002127 }
2128
2129 // We've seen a method with this name, see if we have already seen this type
2130 // signature.
2131 ObjCMethodList *Previous = List;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002132 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregorfc46be92013-06-21 00:20:25 +00002133 // If we are building a module, keep all of the methods.
2134 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2135 continue;
2136
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002137 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002138 continue;
2139
2140 ObjCMethodDecl *PrevObjCMethod = List->Method;
2141
2142 // Propagate the 'defined' bit.
2143 if (Method->isDefined())
2144 PrevObjCMethod->setDefined(true);
2145
2146 // If a method is deprecated, push it in the global pool.
2147 // This is used for better diagnostics.
2148 if (Method->isDeprecated()) {
2149 if (!PrevObjCMethod->isDeprecated())
2150 List->Method = Method;
2151 }
2152 // If new method is unavailable, push it into global pool
2153 // unless previous one is deprecated.
2154 if (Method->isUnavailable()) {
2155 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2156 List->Method = Method;
2157 }
2158
Douglas Gregorff310c72012-05-01 23:37:00 +00002159 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002160 }
2161
2162 // We have a new signature for an existing method - add it.
2163 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002164 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002165 Previous->setNext(new (Mem) ObjCMethodList(Method, 0));
Douglas Gregor44fae522012-01-25 00:19:56 +00002166}
2167
Sebastian Redldb9d2142010-08-02 23:18:59 +00002168/// \brief Read the contents of the method pool for a given selector from
2169/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002170void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002171 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002172 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002173}
2174
Douglas Gregorff310c72012-05-01 23:37:00 +00002175void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002176 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002177 // Ignore methods of invalid containers.
2178 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002179 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002180
Douglas Gregor0d266d62012-01-25 00:59:09 +00002181 if (ExternalSource)
2182 ReadMethodPool(Method->getSelector());
2183
Sebastian Redldb9d2142010-08-02 23:18:59 +00002184 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002185 if (Pos == MethodPool.end())
2186 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2187 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002188
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002189 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002190
Sebastian Redldb9d2142010-08-02 23:18:59 +00002191 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002192 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002193}
2194
John McCallf85e1932011-06-15 23:02:42 +00002195/// Determines if this is an "acceptable" loose mismatch in the global
2196/// method pool. This exists mostly as a hack to get around certain
2197/// global mismatches which we can't afford to make warnings / errors.
2198/// Really, what we want is a way to take a method out of the global
2199/// method pool.
2200static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2201 ObjCMethodDecl *other) {
2202 if (!chosen->isInstanceMethod())
2203 return false;
2204
2205 Selector sel = chosen->getSelector();
2206 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2207 return false;
2208
2209 // Don't complain about mismatches for -length if the method we
2210 // chose has an integral result type.
2211 return (chosen->getResultType()->isIntegerType());
2212}
2213
Sebastian Redldb9d2142010-08-02 23:18:59 +00002214ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002215 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002216 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002217 if (ExternalSource)
2218 ReadMethodPool(Sel);
2219
Sebastian Redldb9d2142010-08-02 23:18:59 +00002220 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002221 if (Pos == MethodPool.end())
2222 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002223
Douglas Gregorf0e00042013-01-16 18:47:38 +00002224 // Gather the non-hidden methods.
Sebastian Redldb9d2142010-08-02 23:18:59 +00002225 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorf0e00042013-01-16 18:47:38 +00002226 llvm::SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002227 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Douglas Gregorf0e00042013-01-16 18:47:38 +00002228 if (M->Method && !M->Method->isHidden()) {
2229 // If we're not supposed to warn about mismatches, we're done.
2230 if (!warn)
2231 return M->Method;
Mike Stump1eb44332009-09-09 15:08:12 +00002232
Douglas Gregorf0e00042013-01-16 18:47:38 +00002233 Methods.push_back(M->Method);
Sebastian Redldb9d2142010-08-02 23:18:59 +00002234 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002235 }
Douglas Gregorf0e00042013-01-16 18:47:38 +00002236
2237 // If there aren't any visible methods, we're done.
2238 // FIXME: Recover if there are any known-but-hidden methods?
2239 if (Methods.empty())
2240 return 0;
2241
2242 if (Methods.size() == 1)
2243 return Methods[0];
2244
2245 // We found multiple methods, so we may have to complain.
2246 bool issueDiagnostic = false, issueError = false;
2247
2248 // We support a warning which complains about *any* difference in
2249 // method signature.
2250 bool strictSelectorMatch =
2251 (receiverIdOrClass && warn &&
2252 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2253 R.getBegin())
2254 != DiagnosticsEngine::Ignored));
2255 if (strictSelectorMatch) {
2256 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2257 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2258 issueDiagnostic = true;
2259 break;
2260 }
2261 }
2262 }
2263
2264 // If we didn't see any strict differences, we won't see any loose
2265 // differences. In ARC, however, we also need to check for loose
2266 // mismatches, because most of them are errors.
2267 if (!strictSelectorMatch ||
2268 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2269 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2270 // This checks if the methods differ in type mismatch.
2271 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2272 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2273 issueDiagnostic = true;
2274 if (getLangOpts().ObjCAutoRefCount)
2275 issueError = true;
2276 break;
2277 }
2278 }
2279
2280 if (issueDiagnostic) {
2281 if (issueError)
2282 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2283 else if (strictSelectorMatch)
2284 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2285 else
2286 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2287
2288 Diag(Methods[0]->getLocStart(),
2289 issueError ? diag::note_possibility : diag::note_using)
2290 << Methods[0]->getSourceRange();
2291 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2292 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2293 << Methods[I]->getSourceRange();
2294 }
2295 }
2296 return Methods[0];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002297}
2298
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002299ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002300 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2301 if (Pos == MethodPool.end())
2302 return 0;
2303
2304 GlobalMethods &Methods = Pos->second;
2305
2306 if (Methods.first.Method && Methods.first.Method->isDefined())
2307 return Methods.first.Method;
2308 if (Methods.second.Method && Methods.second.Method->isDefined())
2309 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002310 return 0;
2311}
2312
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002313static void
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002314HelperSelectorsForTypoCorrection(
2315 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2316 StringRef Typo, const ObjCMethodDecl * Method) {
2317 const unsigned MaxEditDistance = 1;
2318 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieu4fe96442013-06-06 02:22:29 +00002319 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002320
2321 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2322 if (MinPossibleEditDistance > 0 &&
2323 Typo.size() / MinPossibleEditDistance < 1)
2324 return;
2325 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2326 if (EditDistance > MaxEditDistance)
2327 return;
2328 if (EditDistance == BestEditDistance)
2329 BestMethod.push_back(Method);
2330 else if (EditDistance < BestEditDistance) {
2331 BestMethod.clear();
2332 BestMethod.push_back(Method);
2333 BestEditDistance = EditDistance;
2334 }
2335}
2336
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002337static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2338 QualType ObjectType) {
2339 if (ObjectType.isNull())
2340 return true;
2341 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2342 return true;
2343 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != 0;
2344}
2345
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002346const ObjCMethodDecl *
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002347Sema::SelectorsForTypoCorrection(Selector Sel,
2348 QualType ObjectType) {
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002349 unsigned NumArgs = Sel.getNumArgs();
2350 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002351 bool ObjectIsId = true, ObjectIsClass = true;
2352 if (ObjectType.isNull())
2353 ObjectIsId = ObjectIsClass = false;
2354 else if (!ObjectType->isObjCObjectPointerType())
2355 return 0;
2356 else if (const ObjCObjectPointerType *ObjCPtr =
2357 ObjectType->getAsObjCInterfacePointerType()) {
2358 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2359 ObjectIsId = ObjectIsClass = false;
2360 }
2361 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2362 ObjectIsClass = false;
2363 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2364 ObjectIsId = false;
2365 else
2366 return 0;
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002367
2368 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2369 e = MethodPool.end(); b != e; b++) {
2370 // instance methods
2371 for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
2372 if (M->Method &&
Fariborz Jahaniancd9c9b52013-06-18 17:10:58 +00002373 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2374 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002375 if (ObjectIsId)
2376 Methods.push_back(M->Method);
2377 else if (!ObjectIsClass &&
2378 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2379 Methods.push_back(M->Method);
2380 }
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002381 // class methods
2382 for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
2383 if (M->Method &&
Fariborz Jahaniancd9c9b52013-06-18 17:10:58 +00002384 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2385 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002386 if (ObjectIsClass)
2387 Methods.push_back(M->Method);
2388 else if (!ObjectIsId &&
2389 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2390 Methods.push_back(M->Method);
2391 }
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002392 }
2393
2394 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2395 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2396 HelperSelectorsForTypoCorrection(SelectedMethods,
2397 Sel.getAsString(), Methods[i]);
2398 }
2399 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : NULL;
2400}
2401
2402static void
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002403HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S,
2404 ObjCMethodList &MethList) {
2405 ObjCMethodList *M = &MethList;
2406 ObjCMethodDecl *TargetMethod = M->Method;
2407 while (TargetMethod &&
2408 isa<ObjCImplDecl>(TargetMethod->getDeclContext())) {
2409 M = M->getNext();
2410 TargetMethod = M ? M->Method : 0;
2411 }
2412 if (!TargetMethod)
2413 return;
2414 bool FirstTime = true;
2415 for (M = M->getNext(); M; M=M->getNext()) {
2416 ObjCMethodDecl *MatchingMethodDecl = M->Method;
2417 if (isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext()))
2418 continue;
2419 if (!S.MatchTwoMethodDeclarations(TargetMethod,
2420 MatchingMethodDecl, Sema::MMS_loose)) {
2421 if (FirstTime) {
2422 FirstTime = false;
2423 S.Diag(TargetMethod->getLocation(), diag::warning_multiple_selectors)
2424 << TargetMethod->getSelector();
2425 }
2426 S.Diag(MatchingMethodDecl->getLocation(), diag::note_also_found);
2427 }
2428 }
2429}
2430
2431void Sema::DiagnoseMismatchedMethodsInGlobalPool() {
2432 unsigned DIAG = diag::warning_multiple_selectors;
2433 if (Diags.getDiagnosticLevel(DIAG, SourceLocation())
2434 == DiagnosticsEngine::Ignored)
2435 return;
2436 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2437 e = MethodPool.end(); b != e; b++) {
2438 // first, instance methods
2439 ObjCMethodList &InstMethList = b->second.first;
2440 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, InstMethList);
Fariborz Jahanian639aa522013-05-30 21:52:50 +00002441 // second, class methods
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002442 ObjCMethodList &ClsMethList = b->second.second;
2443 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, ClsMethList);
2444 }
2445}
2446
2447/// DiagnoseDuplicateIvars -
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002448/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002449/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002450/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002451/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002452void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2453 ObjCInterfaceDecl *SID) {
2454 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2455 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002456 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002457 if (Ivar->isInvalidDecl())
2458 continue;
2459 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2460 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2461 if (prevIvar) {
2462 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2463 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2464 Ivar->setInvalidDecl();
2465 }
2466 }
2467 }
2468}
2469
Erik Verbruggend64251f2011-12-06 09:25:23 +00002470Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2471 switch (CurContext->getDeclKind()) {
2472 case Decl::ObjCInterface:
2473 return Sema::OCK_Interface;
2474 case Decl::ObjCProtocol:
2475 return Sema::OCK_Protocol;
2476 case Decl::ObjCCategory:
2477 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2478 return Sema::OCK_ClassExtension;
2479 else
2480 return Sema::OCK_Category;
2481 case Decl::ObjCImplementation:
2482 return Sema::OCK_Implementation;
2483 case Decl::ObjCCategoryImpl:
2484 return Sema::OCK_CategoryImplementation;
2485
2486 default:
2487 return Sema::OCK_None;
2488 }
2489}
2490
Steve Naroffa56f6162007-12-18 01:30:32 +00002491// Note: For class/category implemenations, allMethods/allProperties is
2492// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002493Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2494 Decl **allMethods, unsigned allNum,
2495 Decl **allProperties, unsigned pNum,
2496 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002497
Erik Verbruggend64251f2011-12-06 09:25:23 +00002498 if (getObjCContainerKind() == Sema::OCK_None)
2499 return 0;
2500
2501 assert(AtEnd.isValid() && "Invalid location for '@end'");
2502
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002503 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2504 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002505
Mike Stump1eb44332009-09-09 15:08:12 +00002506 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002507 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2508 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002509 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002510
Steve Naroff0701bbb2009-01-08 17:28:14 +00002511 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2512 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2513 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2514
Chris Lattner4d391482007-12-12 07:09:47 +00002515 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002516 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002517 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002518
2519 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002520 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002521 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002522 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002523 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002524 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002525 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002526 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002527 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002528 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002529 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002530 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002531 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002532 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002533 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002534 if (!Context.getSourceManager().isInSystemHeader(
2535 Method->getLocation()))
2536 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2537 << Method->getDeclName();
2538 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2539 }
Chris Lattner4d391482007-12-12 07:09:47 +00002540 InsMap[Method->getSelector()] = Method;
2541 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002542 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002543 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002544 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002545 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002546 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002547 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002548 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002549 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002550 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002551 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002552 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002553 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002554 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002555 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002556 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002557 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002558 if (!Context.getSourceManager().isInSystemHeader(
2559 Method->getLocation()))
2560 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2561 << Method->getDeclName();
2562 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2563 }
Chris Lattner4d391482007-12-12 07:09:47 +00002564 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002565 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002566 }
2567 }
2568 }
Douglas Gregorb892d702013-01-21 19:42:21 +00002569 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2570 // Nothing to do here.
Steve Naroff09c47192009-01-09 15:36:25 +00002571 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002572 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002573 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002574 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002575
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002576 if (C->IsClassExtension()) {
2577 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2578 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002579 }
Chris Lattner4d391482007-12-12 07:09:47 +00002580 }
Steve Naroff09c47192009-01-09 15:36:25 +00002581 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002582 if (CDecl->getIdentifier())
2583 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2584 // user-defined setter/getter. It also synthesizes setter/getter methods
2585 // and adds them to the DeclContext and global method pools.
2586 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2587 E = CDecl->prop_end();
2588 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002589 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002590 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002591 }
2592 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002593 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002594 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002595 // Any property declared in a class extension might have user
2596 // declared setter or getter in current class extension or one
2597 // of the other class extensions. Mark them as synthesized as
2598 // property will be synthesized when property with same name is
2599 // seen in the @implementation.
Douglas Gregord3297242013-01-16 23:00:23 +00002600 for (ObjCInterfaceDecl::visible_extensions_iterator
2601 Ext = IDecl->visible_extensions_begin(),
2602 ExtEnd = IDecl->visible_extensions_end();
2603 Ext != ExtEnd; ++Ext) {
2604 for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
2605 E = Ext->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002606 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002607 // Skip over properties declared @dynamic
2608 if (const ObjCPropertyImplDecl *PIDecl
2609 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2610 if (PIDecl->getPropertyImplementation()
2611 == ObjCPropertyImplDecl::Dynamic)
2612 continue;
Douglas Gregord3297242013-01-16 23:00:23 +00002613
2614 for (ObjCInterfaceDecl::visible_extensions_iterator
2615 Ext = IDecl->visible_extensions_begin(),
2616 ExtEnd = IDecl->visible_extensions_end();
2617 Ext != ExtEnd; ++Ext) {
2618 if (ObjCMethodDecl *GetterMethod
2619 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002620 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002621 if (!Property->isReadOnly())
Douglas Gregord3297242013-01-16 23:00:23 +00002622 if (ObjCMethodDecl *SetterMethod
2623 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002624 SetterMethod->setPropertyAccessor(true);
Douglas Gregord3297242013-01-16 23:00:23 +00002625 }
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002626 }
2627 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002628 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002629 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002630 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002631
Patrick Beardb2f68202012-04-06 18:12:22 +00002632 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2633 if (IDecl->getSuperClass() == NULL) {
2634 // This class has no superclass, so check that it has been marked with
2635 // __attribute((objc_root_class)).
2636 if (!HasRootClassAttr) {
2637 SourceLocation DeclLoc(IDecl->getLocation());
2638 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2639 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2640 << IDecl->getIdentifier();
2641 // See if NSObject is in the current scope, and if it is, suggest
2642 // adding " : NSObject " to the class declaration.
2643 NamedDecl *IF = LookupSingleName(TUScope,
2644 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2645 DeclLoc, LookupOrdinaryName);
2646 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2647 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2648 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2649 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2650 } else {
2651 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2652 }
2653 }
2654 } else if (HasRootClassAttr) {
2655 // Complain that only root classes may have this attribute.
2656 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2657 }
2658
John McCall260611a2012-06-20 06:18:46 +00002659 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002660 while (IDecl->getSuperClass()) {
2661 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2662 IDecl = IDecl->getSuperClass();
2663 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002664 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002665 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002666 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002667 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002668 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002669 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002670
Chris Lattner4d391482007-12-12 07:09:47 +00002671 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002672 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002673 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregord3297242013-01-16 23:00:23 +00002674 if (ObjCCategoryDecl *Cat
2675 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2676 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattner4d391482007-12-12 07:09:47 +00002677 }
2678 }
2679 }
Chris Lattner682bf922009-03-29 16:50:03 +00002680 if (isInterfaceDeclKind) {
2681 // Reject invalid vardecls.
2682 for (unsigned i = 0; i != tuvNum; i++) {
2683 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2684 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2685 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002686 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002687 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002688 }
Chris Lattner682bf922009-03-29 16:50:03 +00002689 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002690 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002691 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002692
2693 for (unsigned i = 0; i != tuvNum; i++) {
2694 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002695 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2696 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002697 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2698 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002699
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002700 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002701 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002702}
2703
2704
2705/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2706/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002707static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002708CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002709 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002710}
2711
Ted Kremenek422bae72010-04-18 04:59:38 +00002712static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002713unsigned countAlignAttr(const AttrVec &A) {
2714 unsigned count=0;
2715 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2716 if ((*i)->getKind() == attr::Aligned)
2717 ++count;
2718 return count;
2719}
2720
2721static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002722bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2723 const AttrVec &A) {
2724 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002725 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002726 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002727 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002728
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002729 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002730 // But implementation has attributes. This is invalid.
2731 // Except when implementation has 'Align' attribute which is
2732 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002733 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002734 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002735
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002736 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002737
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002738 unsigned countAlignOnImpl = countAlignAttr(A);
2739 if (!countAlignOnImpl && (A.size() != D.size()))
2740 return true;
2741 else if (countAlignOnImpl) {
2742 unsigned countAlignOnDecl = countAlignAttr(D);
2743 if (countAlignOnDecl && (A.size() != D.size()))
2744 return true;
2745 else if (!countAlignOnDecl &&
2746 ((A.size()-countAlignOnImpl) != D.size()))
2747 return true;
2748 }
2749
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002750 // attributes on method declaration and definition must match exactly.
2751 // Note that we have at most a couple of attributes on methods, so this
2752 // n*n search is good enough.
2753 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002754 if ((*i)->getKind() == attr::Aligned)
2755 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002756 bool match = false;
2757 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2758 if ((*i)->getKind() == (*i1)->getKind()) {
2759 match = true;
2760 break;
2761 }
2762 }
2763 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002764 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002765 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002766
Sean Huntcf807c42010-08-18 23:23:40 +00002767 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002768}
2769
Douglas Gregor926df6c2011-06-11 01:09:30 +00002770/// \brief Check whether the declared result type of the given Objective-C
2771/// method declaration is compatible with the method's class.
2772///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002773static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002774CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2775 ObjCInterfaceDecl *CurrentClass) {
2776 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002777
2778 // If an Objective-C method inherits its related result type, then its
2779 // declared result type must be compatible with its own class type. The
2780 // declared result type is compatible if:
2781 if (const ObjCObjectPointerType *ResultObjectType
2782 = ResultType->getAs<ObjCObjectPointerType>()) {
2783 // - it is id or qualified id, or
2784 if (ResultObjectType->isObjCIdType() ||
2785 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002786 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002787
2788 if (CurrentClass) {
2789 if (ObjCInterfaceDecl *ResultClass
2790 = ResultObjectType->getInterfaceDecl()) {
2791 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002792 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002793 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002794
2795 // - it is a superclass of the method's class type
2796 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002797 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002798 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002799 } else {
2800 // Any Objective-C pointer type might be acceptable for a protocol
2801 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002802 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002803 }
2804 }
2805
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002806 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002807}
2808
John McCall6c2c2502011-07-22 02:45:48 +00002809namespace {
2810/// A helper class for searching for methods which a particular method
2811/// overrides.
2812class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002813public:
John McCall6c2c2502011-07-22 02:45:48 +00002814 Sema &S;
2815 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002816 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002817 bool Recursive;
2818
2819public:
2820 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2821 Selector selector = method->getSelector();
2822
2823 // Bypass this search if we've never seen an instance/class method
2824 // with this selector before.
2825 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2826 if (it == S.MethodPool.end()) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00002827 if (!S.getExternalSource()) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002828 S.ReadMethodPool(selector);
2829
2830 it = S.MethodPool.find(selector);
2831 if (it == S.MethodPool.end())
2832 return;
John McCall6c2c2502011-07-22 02:45:48 +00002833 }
2834 ObjCMethodList &list =
2835 method->isInstanceMethod() ? it->second.first : it->second.second;
2836 if (!list.Method) return;
2837
2838 ObjCContainerDecl *container
2839 = cast<ObjCContainerDecl>(method->getDeclContext());
2840
2841 // Prevent the search from reaching this container again. This is
2842 // important with categories, which override methods from the
2843 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002844 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2845 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002846 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2847 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002848 } else {
2849 searchFromContainer(container);
2850 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002851 }
John McCall6c2c2502011-07-22 02:45:48 +00002852
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002853 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002854 iterator begin() const { return Overridden.begin(); }
2855 iterator end() const { return Overridden.end(); }
2856
2857private:
2858 void searchFromContainer(ObjCContainerDecl *container) {
2859 if (container->isInvalidDecl()) return;
2860
2861 switch (container->getDeclKind()) {
2862#define OBJCCONTAINER(type, base) \
2863 case Decl::type: \
2864 searchFrom(cast<type##Decl>(container)); \
2865 break;
2866#define ABSTRACT_DECL(expansion)
2867#define DECL(type, base) \
2868 case Decl::type:
2869#include "clang/AST/DeclNodes.inc"
2870 llvm_unreachable("not an ObjC container!");
2871 }
2872 }
2873
2874 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002875 if (!protocol->hasDefinition())
2876 return;
2877
John McCall6c2c2502011-07-22 02:45:48 +00002878 // A method in a protocol declaration overrides declarations from
2879 // referenced ("parent") protocols.
2880 search(protocol->getReferencedProtocols());
2881 }
2882
2883 void searchFrom(ObjCCategoryDecl *category) {
2884 // A method in a category declaration overrides declarations from
2885 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002886 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002887 search(category->getReferencedProtocols());
2888 }
2889
2890 void searchFrom(ObjCCategoryImplDecl *impl) {
2891 // A method in a category definition that has a category
2892 // declaration overrides declarations from the category
2893 // declaration.
2894 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2895 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002896 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2897 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002898
2899 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002900 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2901 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002902 }
2903 }
2904
2905 void searchFrom(ObjCInterfaceDecl *iface) {
2906 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002907 if (!iface->hasDefinition())
2908 return;
2909
John McCall6c2c2502011-07-22 02:45:48 +00002910 // - categories,
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002911 for (ObjCInterfaceDecl::known_categories_iterator
2912 cat = iface->known_categories_begin(),
2913 catEnd = iface->known_categories_end();
Douglas Gregord3297242013-01-16 23:00:23 +00002914 cat != catEnd; ++cat) {
2915 search(*cat);
2916 }
John McCall6c2c2502011-07-22 02:45:48 +00002917
2918 // - the super class, and
2919 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2920 search(super);
2921
2922 // - any referenced protocols.
2923 search(iface->getReferencedProtocols());
2924 }
2925
2926 void searchFrom(ObjCImplementationDecl *impl) {
2927 // A method in a class implementation overrides declarations from
2928 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002929 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2930 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002931 }
2932
2933
2934 void search(const ObjCProtocolList &protocols) {
2935 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2936 i != e; ++i)
2937 search(*i);
2938 }
2939
2940 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002941 // Check for a method in this container which matches this selector.
2942 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002943 Method->isInstanceMethod(),
2944 /*AllowHidden=*/true);
John McCall6c2c2502011-07-22 02:45:48 +00002945
2946 // If we find one, record it and bail out.
2947 if (meth) {
2948 Overridden.insert(meth);
2949 return;
2950 }
2951
2952 // Otherwise, search for methods that a hypothetical method here
2953 // would have overridden.
2954
2955 // Note that we're now in a recursive case.
2956 Recursive = true;
2957
2958 searchFromContainer(container);
2959 }
2960};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002961}
2962
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002963void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2964 ObjCInterfaceDecl *CurrentClass,
2965 ResultTypeCompatibilityKind RTC) {
2966 // Search for overridden methods and merge information down from them.
2967 OverrideSearch overrides(*this, ObjCMethod);
2968 // Keep track if the method overrides any method in the class's base classes,
2969 // its protocols, or its categories' protocols; we will keep that info
2970 // in the ObjCMethodDecl.
2971 // For this info, a method in an implementation is not considered as
2972 // overriding the same method in the interface or its categories.
2973 bool hasOverriddenMethodsInBaseOrProtocol = false;
2974 for (OverrideSearch::iterator
2975 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2976 ObjCMethodDecl *overridden = *i;
2977
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00002978 if (!hasOverriddenMethodsInBaseOrProtocol) {
2979 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2980 CurrentClass != overridden->getClassInterface() ||
2981 overridden->isOverriding()) {
2982 hasOverriddenMethodsInBaseOrProtocol = true;
2983
2984 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
2985 // OverrideSearch will return as "overridden" the same method in the
2986 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
2987 // check whether a category of a base class introduced a method with the
2988 // same selector, after the interface method declaration.
2989 // To avoid unnecessary lookups in the majority of cases, we use the
2990 // extra info bits in GlobalMethodPool to check whether there were any
2991 // category methods with this selector.
2992 GlobalMethodPool::iterator It =
2993 MethodPool.find(ObjCMethod->getSelector());
2994 if (It != MethodPool.end()) {
2995 ObjCMethodList &List =
2996 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
2997 unsigned CategCount = List.getBits();
2998 if (CategCount > 0) {
2999 // If the method is in a category we'll do lookup if there were at
3000 // least 2 category methods recorded, otherwise only one will do.
3001 if (CategCount > 1 ||
3002 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
3003 OverrideSearch overrides(*this, overridden);
3004 for (OverrideSearch::iterator
3005 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
3006 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidisab3d5092013-04-27 00:10:12 +00003007 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
3008 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00003009 hasOverriddenMethodsInBaseOrProtocol = true;
3010 overridden->setOverriding(true);
3011 break;
3012 }
3013 }
3014 }
3015 }
3016 }
3017 }
3018 }
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003019
3020 // Propagate down the 'related result type' bit from overridden methods.
3021 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
3022 ObjCMethod->SetRelatedResultType();
3023
3024 // Then merge the declarations.
3025 mergeObjCMethodDecls(ObjCMethod, overridden);
3026
3027 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3028 continue; // Conflicting properties are detected elsewhere.
3029
3030 // Check for overriding methods
3031 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3032 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3033 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3034 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3035
3036 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00003037 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3038 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003039 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3040 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00003041 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3042 PrevE = overridden->param_end();
3043 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003044 assert(PrevI != overridden->param_end() && "Param mismatch");
3045 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3046 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3047 // If type of argument of method in this class does not match its
3048 // respective argument type in the super class method, issue warning;
3049 if (!Context.typesAreCompatible(T1, T2)) {
3050 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3051 << T1 << T2;
3052 Diag(overridden->getLocation(), diag::note_previous_declaration);
3053 break;
3054 }
3055 }
3056 }
3057 }
3058
3059 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3060}
3061
John McCalld226f652010-08-21 09:40:31 +00003062Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003063 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00003064 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003065 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00003066 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003067 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00003068 Selector Sel,
3069 // optional arguments. The number of types/arguments is obtained
3070 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00003071 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003072 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00003073 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00003074 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00003075 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003076 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00003077 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00003078 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00003079 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003080 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3081 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00003082 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00003083
Douglas Gregore97179c2011-09-08 01:46:34 +00003084 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003085 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00003086 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003087 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00003088
Eli Friedmanddb5a392013-06-14 21:14:10 +00003089 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
John McCalld226f652010-08-21 09:40:31 +00003090 return 0;
Eli Friedmanddb5a392013-06-14 21:14:10 +00003091
Douglas Gregore97179c2011-09-08 01:46:34 +00003092 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00003093 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003094 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00003095 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003096 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00003097 }
Mike Stump1eb44332009-09-09 15:08:12 +00003098
3099 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003100 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003101 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003102 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003103 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00003104 MethodType == tok::minus, isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +00003105 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00003106 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00003107 MethodDeclKind == tok::objc_optional
3108 ? ObjCMethodDecl::Optional
3109 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00003110 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00003111
Chris Lattner5f9e2722011-07-23 10:55:15 +00003112 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00003113
Chris Lattner7db638d2009-04-11 19:42:43 +00003114 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00003115 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00003116 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00003117
David Blaikie7247c882013-05-15 07:37:26 +00003118 if (!ArgInfo[i].Type) {
John McCall58e46772009-10-23 21:48:59 +00003119 ArgType = Context.getObjCIdType();
3120 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00003121 } else {
John McCall58e46772009-10-23 21:48:59 +00003122 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnere294d3f2009-04-11 18:57:04 +00003123 }
Mike Stump1eb44332009-09-09 15:08:12 +00003124
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003125 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3126 LookupOrdinaryName, ForRedeclaration);
3127 LookupName(R, S);
3128 if (R.isSingleResult()) {
3129 NamedDecl *PrevDecl = R.getFoundDecl();
3130 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00003131 Diag(ArgInfo[i].NameLoc,
3132 (MethodDefinition ? diag::warn_method_param_redefinition
3133 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003134 << ArgInfo[i].Name;
3135 Diag(PrevDecl->getLocation(),
3136 diag::note_previous_declaration);
3137 }
3138 }
3139
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003140 SourceLocation StartLoc = DI
3141 ? DI->getTypeLoc().getBeginLoc()
3142 : ArgInfo[i].NameLoc;
3143
John McCall81ef3e62011-04-23 02:46:06 +00003144 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3145 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003146 ArgType, DI, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00003147
John McCall70798862011-05-02 00:30:12 +00003148 Param->setObjCMethodScopeInfo(i);
3149
Chris Lattner0ed844b2008-04-04 06:12:32 +00003150 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00003151 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00003152
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00003153 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003154 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003155
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00003156 if (Param->hasAttr<BlocksAttr>()) {
3157 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3158 Param->setInvalidDecl();
3159 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003160 S->AddDecl(Param);
3161 IdResolver.AddDecl(Param);
3162
Chris Lattner0ed844b2008-04-04 06:12:32 +00003163 Params.push_back(Param);
3164 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003165
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003166 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00003167 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003168 QualType ArgType = Param->getType();
3169 if (ArgType.isNull())
3170 ArgType = Context.getObjCIdType();
3171 else
3172 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00003173 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedmanddb5a392013-06-14 21:14:10 +00003174
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003175 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003176 Params.push_back(Param);
3177 }
3178
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003179 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003180 ObjCMethod->setObjCDeclQualifier(
3181 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00003182
3183 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003184 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00003185
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003186 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00003187 const ObjCMethodDecl *PrevMethod = 0;
3188 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00003189 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003190 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3191 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003192 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003193 PrevMethod = ImpDecl->getClassMethod(Sel);
3194 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003195 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00003196
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00003197 ObjCMethodDecl *IMD = 0;
3198 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3199 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3200 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00003201 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00003202 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00003203 SourceLocation MethodLoc = IMD->getLocation();
3204 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
3205 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00003206 Diag(MethodLoc, diag::note_method_declared_at)
3207 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00003208 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00003209 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003210 } else {
3211 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003212 }
John McCall6c2c2502011-07-22 02:45:48 +00003213
Chris Lattner4d391482007-12-12 07:09:47 +00003214 if (PrevMethod) {
3215 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00003216 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00003217 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00003218 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanianfbff0c42013-05-13 17:27:00 +00003219 ObjCMethod->setInvalidDecl();
3220 return ObjCMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00003221 }
John McCall54abf7d2009-11-04 02:18:39 +00003222
Douglas Gregor926df6c2011-06-11 01:09:30 +00003223 // If this Objective-C method does not have a related result type, but we
3224 // are allowed to infer related result types, try to do so based on the
3225 // method family.
3226 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3227 if (!CurrentClass) {
3228 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3229 CurrentClass = Cat->getClassInterface();
3230 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3231 CurrentClass = Impl->getClassInterface();
3232 else if (ObjCCategoryImplDecl *CatImpl
3233 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3234 CurrentClass = CatImpl->getClassInterface();
3235 }
John McCall6c2c2502011-07-22 02:45:48 +00003236
Douglas Gregore97179c2011-09-08 01:46:34 +00003237 ResultTypeCompatibilityKind RTC
3238 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00003239
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003240 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00003241
John McCallf85e1932011-06-15 23:02:42 +00003242 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00003243 if (getLangOpts().ObjCAutoRefCount)
John McCallb8463812013-04-04 01:38:37 +00003244 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCallf85e1932011-06-15 23:02:42 +00003245
Douglas Gregore97179c2011-09-08 01:46:34 +00003246 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003247 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00003248 !ObjCMethod->hasRelatedResultType() &&
3249 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00003250 bool InferRelatedResultType = false;
3251 switch (ObjCMethod->getMethodFamily()) {
3252 case OMF_None:
3253 case OMF_copy:
3254 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00003255 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003256 case OMF_mutableCopy:
3257 case OMF_release:
3258 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00003259 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003260 break;
3261
3262 case OMF_alloc:
3263 case OMF_new:
3264 InferRelatedResultType = ObjCMethod->isClassMethod();
3265 break;
3266
3267 case OMF_init:
3268 case OMF_autorelease:
3269 case OMF_retain:
3270 case OMF_self:
3271 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3272 break;
3273 }
3274
John McCall6c2c2502011-07-22 02:45:48 +00003275 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003276 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003277 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003278
3279 ActOnDocumentableDecl(ObjCMethod);
3280
John McCalld226f652010-08-21 09:40:31 +00003281 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003282}
3283
Chris Lattnercc98eac2008-12-17 07:13:27 +00003284bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003285 // Following is also an error. But it is caused by a missing @end
3286 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003287 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003288 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003289
3290 // If we switched context to translation unit while we are still lexically in
3291 // an objc container, it means the parser missed emitting an error.
3292 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3293 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003294
Anders Carlsson15281452008-11-04 16:57:32 +00003295 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3296 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003297
Anders Carlsson15281452008-11-04 16:57:32 +00003298 return true;
3299}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003300
James Dennett1dfbd922012-06-14 21:40:34 +00003301/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003302/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003303void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003304 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003305 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003306 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003307 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003308 if (!Class) {
3309 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3310 return;
3311 }
John McCall260611a2012-06-20 06:18:46 +00003312 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003313 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3314 return;
3315 }
Mike Stump1eb44332009-09-09 15:08:12 +00003316
Chris Lattnercc98eac2008-12-17 07:13:27 +00003317 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003318 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003319 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003320 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003321 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003322 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003323 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003324 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3325 /*FIXME: StartL=*/ID->getLocation(),
3326 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003327 ID->getIdentifier(), ID->getType(),
3328 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003329 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003330 }
Mike Stump1eb44332009-09-09 15:08:12 +00003331
Chris Lattnercc98eac2008-12-17 07:13:27 +00003332 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003333 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003334 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003335 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003336 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003337 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003338 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003339 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003340 }
3341}
3342
Douglas Gregor160b5632010-04-26 17:32:49 +00003343/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003344VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3345 SourceLocation StartLoc,
3346 SourceLocation IdLoc,
3347 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003348 bool Invalid) {
3349 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3350 // duration shall not be qualified by an address-space qualifier."
3351 // Since all parameters have automatic store duration, they can not have
3352 // an address space.
3353 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003354 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003355 Invalid = true;
3356 }
3357
3358 // An @catch parameter must be an unqualified object pointer type;
3359 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3360 if (Invalid) {
3361 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003362 } else if (T->isDependentType()) {
3363 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003364 } else if (!T->isObjCObjectPointerType()) {
3365 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003366 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003367 } else if (T->isObjCQualifiedIdType()) {
3368 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003369 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003370 }
3371
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003372 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003373 T, TInfo, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003374 New->setExceptionVariable(true);
3375
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003376 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003377 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003378 Invalid = true;
3379
Douglas Gregor160b5632010-04-26 17:32:49 +00003380 if (Invalid)
3381 New->setInvalidDecl();
3382 return New;
3383}
3384
John McCalld226f652010-08-21 09:40:31 +00003385Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003386 const DeclSpec &DS = D.getDeclSpec();
3387
3388 // We allow the "register" storage class on exception variables because
3389 // GCC did, but we drop it completely. Any other storage class is an error.
3390 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3391 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3392 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithec642442013-04-12 22:46:28 +00003393 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003394 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithec642442013-04-12 22:46:28 +00003395 << DeclSpec::getSpecifierName(SCS);
3396 }
3397 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3398 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3399 diag::err_invalid_thread)
3400 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregor160b5632010-04-26 17:32:49 +00003401 D.getMutableDeclSpec().ClearStorageClassSpecs();
3402
Richard Smithc7f81162013-03-18 22:52:47 +00003403 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregor160b5632010-04-26 17:32:49 +00003404
3405 // Check that there are no default arguments inside the type of this
3406 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003407 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003408 CheckExtraCXXDefaultArguments(D);
3409
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003410 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003411 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003412
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003413 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3414 D.getSourceRange().getBegin(),
3415 D.getIdentifierLoc(),
3416 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003417 D.isInvalidType());
3418
3419 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3420 if (D.getCXXScopeSpec().isSet()) {
3421 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3422 << D.getCXXScopeSpec().getRange();
3423 New->setInvalidDecl();
3424 }
3425
3426 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003427 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003428 if (D.getIdentifier())
3429 IdResolver.AddDecl(New);
3430
3431 ProcessDeclAttributes(S, New, D);
3432
3433 if (New->hasAttr<BlocksAttr>())
3434 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003435 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003436}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003437
3438/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003439/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003440void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003441 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003442 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3443 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003444 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003445 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003446 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003447 }
3448}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003449
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003450void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003451 // Load referenced selectors from the external source.
3452 if (ExternalSource) {
3453 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3454 ExternalSource->ReadReferencedSelectors(Sels);
3455 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3456 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3457 }
3458
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00003459 DiagnoseMismatchedMethodsInGlobalPool();
3460
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003461 // Warning will be issued only when selector table is
3462 // generated (which means there is at lease one implementation
3463 // in the TU). This is to match gcc's behavior.
3464 if (ReferencedSelectors.empty() ||
3465 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003466 return;
3467 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3468 ReferencedSelectors.begin(),
3469 E = ReferencedSelectors.end(); S != E; ++S) {
3470 Selector Sel = (*S).first;
3471 if (!LookupImplementedMethodInGlobalPool(Sel))
3472 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3473 }
3474 return;
3475}