blob: 87dfe10363015a6a9f12777baa1f8777d56dc38a [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
John McCallf85e1932011-06-15 23:02:42 +000015#include "clang/AST/ASTConsumer.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/ASTMutationListener.h"
18#include "clang/AST/DeclObjC.h"
Steve Naroffca331292009-03-03 14:49:36 +000019#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000020#include "clang/AST/ExprObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/Basic/SourceManager.h"
Patrick Beardb2f68202012-04-06 18:12:22 +000022#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/ExternalSemaSource.h"
25#include "clang/Sema/Lookup.h"
26#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
John McCall50df6ae2010-08-25 07:03:20 +000028#include "llvm/ADT/DenseSet.h"
29
Chris Lattner4d391482007-12-12 07:09:47 +000030using namespace clang;
31
John McCallf85e1932011-06-15 23:02:42 +000032/// Check whether the given method, which must be in the 'init'
33/// family, is a valid member of that family.
34///
35/// \param receiverTypeIfCall - if null, check this as if declaring it;
36/// if non-null, check this as if making a call to it with the given
37/// receiver type
38///
39/// \return true to indicate that there was an error and appropriate
40/// actions were taken
41bool Sema::checkInitMethod(ObjCMethodDecl *method,
42 QualType receiverTypeIfCall) {
43 if (method->isInvalidDecl()) return true;
44
45 // This castAs is safe: methods that don't return an object
46 // pointer won't be inferred as inits and will reject an explicit
47 // objc_method_family(init).
48
49 // We ignore protocols here. Should we? What about Class?
50
51 const ObjCObjectType *result = method->getResultType()
52 ->castAs<ObjCObjectPointerType>()->getObjectType();
53
54 if (result->isObjCId()) {
55 return false;
56 } else if (result->isObjCClass()) {
57 // fall through: always an error
58 } else {
59 ObjCInterfaceDecl *resultClass = result->getInterface();
60 assert(resultClass && "unexpected object type!");
61
62 // It's okay for the result type to still be a forward declaration
63 // if we're checking an interface declaration.
Douglas Gregor7723fec2011-12-15 20:29:51 +000064 if (!resultClass->hasDefinition()) {
John McCallf85e1932011-06-15 23:02:42 +000065 if (receiverTypeIfCall.isNull() &&
66 !isa<ObjCImplementationDecl>(method->getDeclContext()))
67 return false;
68
69 // Otherwise, we try to compare class types.
70 } else {
71 // If this method was declared in a protocol, we can't check
72 // anything unless we have a receiver type that's an interface.
73 const ObjCInterfaceDecl *receiverClass = 0;
74 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
75 if (receiverTypeIfCall.isNull())
76 return false;
77
78 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
79 ->getInterfaceDecl();
80
81 // This can be null for calls to e.g. id<Foo>.
82 if (!receiverClass) return false;
83 } else {
84 receiverClass = method->getClassInterface();
85 assert(receiverClass && "method not associated with a class!");
86 }
87
88 // If either class is a subclass of the other, it's fine.
89 if (receiverClass->isSuperClassOf(resultClass) ||
90 resultClass->isSuperClassOf(receiverClass))
91 return false;
92 }
93 }
94
95 SourceLocation loc = method->getLocation();
96
97 // If we're in a system header, and this is not a call, just make
98 // the method unusable.
99 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
100 method->addAttr(new (Context) UnavailableAttr(loc, Context,
101 "init method returns a type unrelated to its receiver type"));
102 return true;
103 }
104
105 // Otherwise, it's an error.
106 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
107 method->setInvalidDecl();
108 return true;
109}
110
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000111void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregorf4d918f2013-01-15 22:43:08 +0000112 const ObjCMethodDecl *Overridden) {
Douglas Gregor926df6c2011-06-11 01:09:30 +0000113 if (Overridden->hasRelatedResultType() &&
114 !NewMethod->hasRelatedResultType()) {
115 // This can only happen when the method follows a naming convention that
116 // implies a related result type, and the original (overridden) method has
117 // a suitable return type, but the new (overriding) method does not have
118 // a suitable return type.
119 QualType ResultType = NewMethod->getResultType();
120 SourceRange ResultTypeRange;
121 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000122 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000123 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
124
125 // Figure out which class this method is part of, if any.
126 ObjCInterfaceDecl *CurrentClass
127 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
128 if (!CurrentClass) {
129 DeclContext *DC = NewMethod->getDeclContext();
130 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
131 CurrentClass = Cat->getClassInterface();
132 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
133 CurrentClass = Impl->getClassInterface();
134 else if (ObjCCategoryImplDecl *CatImpl
135 = dyn_cast<ObjCCategoryImplDecl>(DC))
136 CurrentClass = CatImpl->getClassInterface();
137 }
138
139 if (CurrentClass) {
140 Diag(NewMethod->getLocation(),
141 diag::warn_related_result_type_compatibility_class)
142 << Context.getObjCInterfaceType(CurrentClass)
143 << ResultType
144 << ResultTypeRange;
145 } else {
146 Diag(NewMethod->getLocation(),
147 diag::warn_related_result_type_compatibility_protocol)
148 << ResultType
149 << ResultTypeRange;
150 }
151
Douglas Gregore97179c2011-09-08 01:46:34 +0000152 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
153 Diag(Overridden->getLocation(),
John McCall7cca8212013-03-19 07:04:25 +0000154 diag::note_related_result_type_family)
155 << /*overridden method*/ 0
Douglas Gregore97179c2011-09-08 01:46:34 +0000156 << Family;
157 else
158 Diag(Overridden->getLocation(),
159 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000160 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000161 if (getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000162 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
163 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
164 Diag(NewMethod->getLocation(),
165 diag::err_nsreturns_retained_attribute_mismatch) << 1;
166 Diag(Overridden->getLocation(), diag::note_previous_decl)
167 << "method";
168 }
169 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
170 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
171 Diag(NewMethod->getLocation(),
172 diag::err_nsreturns_retained_attribute_mismatch) << 0;
173 Diag(Overridden->getLocation(), diag::note_previous_decl)
174 << "method";
175 }
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000176 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
177 oe = Overridden->param_end();
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000178 for (ObjCMethodDecl::param_iterator
179 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000180 ni != ne && oi != oe; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000181 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000182 ParmVarDecl *newDecl = (*ni);
183 if (newDecl->hasAttr<NSConsumedAttr>() !=
184 oldDecl->hasAttr<NSConsumedAttr>()) {
185 Diag(newDecl->getLocation(),
186 diag::err_nsconsumed_attribute_mismatch);
187 Diag(oldDecl->getLocation(), diag::note_previous_decl)
188 << "parameter";
189 }
190 }
191 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000192}
193
John McCallf85e1932011-06-15 23:02:42 +0000194/// \brief Check a method declaration for compatibility with the Objective-C
195/// ARC conventions.
John McCallb8463812013-04-04 01:38:37 +0000196bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) {
John McCallf85e1932011-06-15 23:02:42 +0000197 ObjCMethodFamily family = method->getMethodFamily();
198 switch (family) {
199 case OMF_None:
Nico Weber80cb6e62011-08-28 22:35:17 +0000200 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000201 case OMF_retain:
202 case OMF_release:
203 case OMF_autorelease:
204 case OMF_retainCount:
205 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000206 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000207 return false;
208
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000209 case OMF_dealloc:
John McCallb8463812013-04-04 01:38:37 +0000210 if (!Context.hasSameType(method->getResultType(), Context.VoidTy)) {
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000211 SourceRange ResultTypeRange;
212 if (const TypeSourceInfo *ResultTypeInfo
213 = method->getResultTypeSourceInfo())
214 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
215 if (ResultTypeRange.isInvalid())
John McCallb8463812013-04-04 01:38:37 +0000216 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000217 << method->getResultType()
218 << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
219 else
John McCallb8463812013-04-04 01:38:37 +0000220 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000221 << method->getResultType()
222 << FixItHint::CreateReplacement(ResultTypeRange, "void");
223 return true;
224 }
225 return false;
226
John McCallf85e1932011-06-15 23:02:42 +0000227 case OMF_init:
228 // If the method doesn't obey the init rules, don't bother annotating it.
John McCallb8463812013-04-04 01:38:37 +0000229 if (checkInitMethod(method, QualType()))
John McCallf85e1932011-06-15 23:02:42 +0000230 return true;
231
John McCallb8463812013-04-04 01:38:37 +0000232 method->addAttr(new (Context) NSConsumesSelfAttr(SourceLocation(),
233 Context));
John McCallf85e1932011-06-15 23:02:42 +0000234
235 // Don't add a second copy of this attribute, but otherwise don't
236 // let it be suppressed.
237 if (method->hasAttr<NSReturnsRetainedAttr>())
238 return false;
239 break;
240
241 case OMF_alloc:
242 case OMF_copy:
243 case OMF_mutableCopy:
244 case OMF_new:
245 if (method->hasAttr<NSReturnsRetainedAttr>() ||
246 method->hasAttr<NSReturnsNotRetainedAttr>() ||
247 method->hasAttr<NSReturnsAutoreleasedAttr>())
248 return false;
249 break;
250 }
251
John McCallb8463812013-04-04 01:38:37 +0000252 method->addAttr(new (Context) NSReturnsRetainedAttr(SourceLocation(),
253 Context));
John McCallf85e1932011-06-15 23:02:42 +0000254 return false;
255}
256
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000257static void DiagnoseObjCImplementedDeprecations(Sema &S,
258 NamedDecl *ND,
259 SourceLocation ImplLoc,
260 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000261 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000262 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000263 if (select == 0)
Ted Kremenek3306ec12012-02-27 22:55:11 +0000264 S.Diag(ND->getLocation(), diag::note_method_declared_at)
265 << ND->getDeclName();
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000266 else
267 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
268 }
269}
270
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000271/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
272/// pool.
273void Sema::AddAnyMethodToGlobalPool(Decl *D) {
274 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
275
276 // If we don't have a valid method decl, simply return.
277 if (!MDecl)
278 return;
279 if (MDecl->isInstanceMethod())
280 AddInstanceMethodToGlobalPool(MDecl, true);
281 else
282 AddFactoryMethodToGlobalPool(MDecl, true);
283}
284
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000285/// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer
286/// has explicit ownership attribute; false otherwise.
287static bool
288HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) {
289 QualType T = Param->getType();
290
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000291 if (const PointerType *PT = T->getAs<PointerType>()) {
292 T = PT->getPointeeType();
293 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
294 T = RT->getPointeeType();
295 } else {
296 return true;
297 }
298
299 // If we have a lifetime qualifier, but it's local, we must have
300 // inferred it. So, it is implicit.
301 return !T.getLocalQualifiers().hasObjCLifetime();
302}
303
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +0000304/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
305/// and user declared, in the method definition's AST.
306void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
307 assert((getCurMethodDecl() == 0) && "Methodparsing confused");
John McCalld226f652010-08-21 09:40:31 +0000308 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000309
Steve Naroff394f3f42008-07-25 17:57:26 +0000310 // If we don't have a valid method decl, simply return.
311 if (!MDecl)
312 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000313
Chris Lattner4d391482007-12-12 07:09:47 +0000314 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000315 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000316 PushFunctionScope();
317
Chris Lattner4d391482007-12-12 07:09:47 +0000318 // Create Decl objects for each parameter, entrring them in the scope for
319 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000320
321 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000322 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Daniel Dunbar451318c2008-08-26 06:07:48 +0000324 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
325 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000326
Reid Kleckner8c0501c2013-06-24 14:38:26 +0000327 // The ObjC parser requires parameter names so there's no need to check.
328 CheckParmsForFunctionDef(MDecl->param_begin(), MDecl->param_end(),
329 /*CheckParameterNames=*/false);
330
Chris Lattner8123a952008-04-10 02:22:51 +0000331 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000332 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000333 E = MDecl->param_end(); PI != E; ++PI) {
334 ParmVarDecl *Param = (*PI);
335 if (!Param->isInvalidDecl() &&
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000336 getLangOpts().ObjCAutoRefCount &&
337 !HasExplicitOwnershipAttr(*this, Param))
338 Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
339 Param->getType();
Fariborz Jahanian918546c2012-08-30 23:56:02 +0000340
Chris Lattner89951a82009-02-20 18:43:26 +0000341 if ((*PI)->getIdentifier())
342 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000343 }
John McCallf85e1932011-06-15 23:02:42 +0000344
345 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000346 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000347 switch (MDecl->getMethodFamily()) {
348 case OMF_retain:
349 case OMF_retainCount:
350 case OMF_release:
351 case OMF_autorelease:
352 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
Fariborz Jahanianb8ed0712013-05-16 19:08:44 +0000353 << 0 << MDecl->getSelector();
John McCallf85e1932011-06-15 23:02:42 +0000354 break;
355
356 case OMF_None:
357 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000358 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000359 case OMF_alloc:
360 case OMF_init:
361 case OMF_mutableCopy:
362 case OMF_copy:
363 case OMF_new:
364 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000365 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000366 break;
367 }
368 }
369
Nico Weber9a1ecf02011-08-22 17:25:57 +0000370 // Warn on deprecated methods under -Wdeprecated-implementations,
371 // and prepare for warning on missing super calls.
372 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian84101132012-09-07 23:46:23 +0000373 ObjCMethodDecl *IMD =
374 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
375
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000376 if (IMD) {
377 ObjCImplDecl *ImplDeclOfMethodDef =
378 dyn_cast<ObjCImplDecl>(MDecl->getDeclContext());
379 ObjCContainerDecl *ContDeclOfMethodDecl =
380 dyn_cast<ObjCContainerDecl>(IMD->getDeclContext());
381 ObjCImplDecl *ImplDeclOfMethodDecl = 0;
382 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
383 ImplDeclOfMethodDecl = OID->getImplementation();
384 else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl))
385 ImplDeclOfMethodDecl = CD->getImplementation();
386 // No need to issue deprecated warning if deprecated mehod in class/category
387 // is being implemented in its own implementation (no overriding is involved).
388 if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef)
389 DiagnoseObjCImplementedDeprecations(*this,
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000390 dyn_cast<NamedDecl>(IMD),
391 MDecl->getLocation(), 0);
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000392 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000393
Nico Weber80cb6e62011-08-28 22:35:17 +0000394 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000395 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
396 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
397 // Only do this if the current class actually has a superclass.
Jordan Rose41f3f3a2013-03-05 01:27:54 +0000398 if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) {
Jordan Rose535a5d02012-10-19 16:05:26 +0000399 ObjCMethodFamily Family = MDecl->getMethodFamily();
400 if (Family == OMF_dealloc) {
401 if (!(getLangOpts().ObjCAutoRefCount ||
402 getLangOpts().getGC() == LangOptions::GCOnly))
403 getCurFunction()->ObjCShouldCallSuper = true;
404
405 } else if (Family == OMF_finalize) {
406 if (Context.getLangOpts().getGC() != LangOptions::NonGC)
407 getCurFunction()->ObjCShouldCallSuper = true;
408
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) {
Eli Friedman104f96b2013-06-21 01:49:53 +0000606 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000607 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000608 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000609 }
610 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000611 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000612 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000613 if (const TypedefNameDecl *TDecl =
614 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000615 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000616 if (T->isObjCObjectType()) {
617 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000618 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000619 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000620 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000621 }
622 }
623 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000624 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
625 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000626 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000627 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000628 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000629 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000632 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000633 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000634 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Anders Carlsson15281452008-11-04 16:57:32 +0000636 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000637 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000638
John McCalld226f652010-08-21 09:40:31 +0000639 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000640}
641
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000642bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000643 IdentifierInfo *PName,
644 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000645 const ObjCList<ObjCProtocolDecl> &PList) {
646
647 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000648 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
649 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000650 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
651 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000652 if (PDecl->getIdentifier() == PName) {
653 Diag(Ploc, diag::err_protocol_has_circular_dependency);
654 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000655 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000656 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000657
658 if (!PDecl->hasDefinition())
659 continue;
660
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000661 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
662 PDecl->getLocation(), PDecl->getReferencedProtocols()))
663 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000664 }
665 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000666 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000667}
668
John McCalld226f652010-08-21 09:40:31 +0000669Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000670Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
671 IdentifierInfo *ProtocolName,
672 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000673 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000674 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000675 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000676 SourceLocation EndProtoLoc,
677 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000678 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000679 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000680 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000681 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
682 ForRedeclaration);
683 ObjCProtocolDecl *PDecl = 0;
684 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
685 // If we already have a definition, complain.
686 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
687 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Douglas Gregor27c6da22012-01-01 20:30:41 +0000689 // Create a new protocol that is completely distinct from previous
690 // declarations, and do not make this protocol available for name lookup.
691 // That way, we'll end up completely ignoring the duplicate.
692 // FIXME: Can we turn this into an error?
693 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
694 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000695 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000696 PDecl->startDefinition();
697 } else {
698 if (PrevDecl) {
699 // Check for circular dependencies among protocol declarations. This can
700 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000701 ObjCList<ObjCProtocolDecl> PList;
702 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
703 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000704 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000705 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000706
707 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000708 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000709 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000710 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000711
Douglas Gregor6e378de2009-04-23 23:18:26 +0000712 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000713 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000714 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000715
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000716 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000717 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000718
719 // Merge attributes from previous declarations.
720 if (PrevDecl)
721 mergeDeclAttributes(PDecl, PrevDecl);
722
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000723 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000724 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000725 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000726 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000727 }
Mike Stump1eb44332009-09-09 15:08:12 +0000728
729 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000730 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000731}
732
733/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000734/// issues an error if they are not declared. It returns list of
735/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000736void
Chris Lattnere13b9592008-07-26 04:03:38 +0000737Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000738 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000739 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000740 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000741 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000742 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
743 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000744 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000745 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000746 TypoCorrection Corrected = CorrectTypo(
747 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000748 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000749 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000750 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000751 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000752 Diag(PDecl->getLocation(), diag::note_previous_decl)
753 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000754 }
755 }
756
757 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000758 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000759 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000760 continue;
761 }
Fariborz Jahanian3c9a0242013-04-09 17:52:29 +0000762 // If this is a forward protocol declaration, get its definition.
763 if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
764 PDecl = PDecl->getDefinition();
765
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000766 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000767
768 // If this is a forward declaration and we are supposed to warn in this
769 // case, do it.
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000770 // FIXME: Recover nicely in the hidden case.
771 if (WarnOnDeclarations &&
772 (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000773 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000774 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000775 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000776 }
777}
778
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000779/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000780/// a class method in its extension.
781///
Mike Stump1eb44332009-09-09 15:08:12 +0000782void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000783 ObjCInterfaceDecl *ID) {
784 if (!ID)
785 return; // Possibly due to previous error
786
787 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000788 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
789 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000790 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000791 MethodMap[MD->getSelector()] = MD;
792 }
793
794 if (MethodMap.empty())
795 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000796 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
797 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000798 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000799 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
800 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
801 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
802 << Method->getDeclName();
803 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
804 }
805 }
806}
807
James Dennett1dfbd922012-06-14 21:40:34 +0000808/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000809Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000810Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000811 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000812 unsigned NumElts,
813 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000814 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000815 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000816 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000817 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
818 ForRedeclaration);
819 ObjCProtocolDecl *PDecl
820 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
821 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000822 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000823
824 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000825 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000826
Douglas Gregor3937f872012-01-01 20:33:24 +0000827 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000828 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000829
830 if (PrevDecl)
831 mergeDeclAttributes(PDecl, PrevDecl);
832
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000833 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000834 }
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Rafael Espindola4549d7f2013-07-09 12:05:01 +0000836 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattner4d391482007-12-12 07:09:47 +0000837}
838
John McCalld226f652010-08-21 09:40:31 +0000839Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000840ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
841 IdentifierInfo *ClassName, SourceLocation ClassLoc,
842 IdentifierInfo *CategoryName,
843 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000844 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000845 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000846 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000847 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000848 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000849 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000850
851 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000852
853 if (!IDecl
854 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000855 diag::err_category_forward_interface,
856 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000857 // Create an invalid ObjCCategoryDecl to serve as context for
858 // the enclosing method declarations. We mark the decl invalid
859 // to make it clear that this isn't a valid AST.
860 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000861 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000862 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000863 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000864
865 if (!IDecl)
866 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000867 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000868 }
869
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000870 if (!CategoryName && IDecl->getImplementation()) {
871 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
872 Diag(IDecl->getImplementation()->getLocation(),
873 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000874 }
875
Fariborz Jahanian25760612010-02-15 21:55:26 +0000876 if (CategoryName) {
877 /// Check for duplicate interface declaration for this category
Douglas Gregord3297242013-01-16 23:00:23 +0000878 if (ObjCCategoryDecl *Previous
879 = IDecl->FindCategoryDeclaration(CategoryName)) {
880 // Class extensions can be declared multiple times, categories cannot.
881 Diag(CategoryLoc, diag::warn_dup_category_def)
882 << ClassName << CategoryName;
883 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner70f19542009-02-16 21:26:43 +0000884 }
885 }
Chris Lattner70f19542009-02-16 21:26:43 +0000886
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000887 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
888 ClassLoc, CategoryLoc, CategoryName, IDecl);
889 // FIXME: PushOnScopeChains?
890 CurContext->addDecl(CDecl);
891
Chris Lattner4d391482007-12-12 07:09:47 +0000892 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000893 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000894 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000895 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000896 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000897 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000898 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000899 }
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Anders Carlsson15281452008-11-04 16:57:32 +0000901 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000902 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000903}
904
905/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000906/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000907/// object.
John McCalld226f652010-08-21 09:40:31 +0000908Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000909 SourceLocation AtCatImplLoc,
910 IdentifierInfo *ClassName, SourceLocation ClassLoc,
911 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000912 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000913 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000914 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000915 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
916 if (!CatIDecl) {
917 // Category @implementation with no corresponding @interface.
918 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000919 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
920 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000921 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000922 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000923 }
924 }
925
Mike Stump1eb44332009-09-09 15:08:12 +0000926 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000927 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000928 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000929 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000930 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000931 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000932 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000933 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
934 diag::err_undef_interface)) {
935 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000936 }
Chris Lattner4d391482007-12-12 07:09:47 +0000937
Douglas Gregord0434102009-01-09 00:49:46 +0000938 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000939 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000940
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000941 // If the interface is deprecated/unavailable, warn/error about it.
942 if (IDecl)
943 DiagnoseUseOfDecl(IDecl, ClassLoc);
944
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000945 /// Check that CatName, category name, is not used in another implementation.
946 if (CatIDecl) {
947 if (CatIDecl->getImplementation()) {
948 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
949 << CatName;
950 Diag(CatIDecl->getImplementation()->getLocation(),
951 diag::note_previous_definition);
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +0000952 CDecl->setInvalidDecl();
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000953 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000954 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000955 // Warn on implementating category of deprecated class under
956 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000957 DiagnoseObjCImplementedDeprecations(*this,
958 dyn_cast<NamedDecl>(IDecl),
959 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000960 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000961 }
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Anders Carlsson15281452008-11-04 16:57:32 +0000963 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000964 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000965}
966
John McCalld226f652010-08-21 09:40:31 +0000967Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000968 SourceLocation AtClassImplLoc,
969 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000970 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000971 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000972 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000973 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000974 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000975 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
976 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000977 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000978 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000979 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000980 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000981 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
982 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000983 } else {
984 // We did not find anything with the name ClassName; try to correct for
985 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000986 ObjCInterfaceValidatorCCC Validator;
987 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000988 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000989 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000990 // Suggest the (potentially) correct interface name. However, put the
991 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000992 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000993 // provide a code-modification hint or use the typo name for recovery,
994 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000995 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000996 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000997 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000998 << ClassName << CorrectedName;
999 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
1000 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +00001001 IDecl = 0;
1002 } else {
1003 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
1004 }
Chris Lattner4d391482007-12-12 07:09:47 +00001005 }
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Chris Lattner4d391482007-12-12 07:09:47 +00001007 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001008 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +00001009 if (SuperClassname) {
1010 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +00001011 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
1012 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001013 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001014 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1015 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +00001016 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +00001017 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001018 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +00001019 if (SDecl && !SDecl->hasDefinition())
1020 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +00001021 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +00001022 Diag(SuperClassLoc, diag::err_undef_superclass)
1023 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +00001024 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001025 // This implementation and its interface do not have the same
1026 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +00001027 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +00001028 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001029 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001030 }
1031 }
1032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Chris Lattner4d391482007-12-12 07:09:47 +00001034 if (!IDecl) {
1035 // Legacy case of @implementation with no corresponding @interface.
1036 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +00001037
Mike Stump390b4cc2009-05-16 07:39:55 +00001038 // FIXME: Do we support attributes on the @implementation? If so we should
1039 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001040 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001041 ClassName, /*PrevDecl=*/0, ClassLoc,
1042 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001043 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001044 if (SDecl) {
1045 IDecl->setSuperClass(SDecl);
1046 IDecl->setSuperClassLoc(SuperClassLoc);
1047 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1048 } else {
1049 IDecl->setEndOfDefinitionLoc(ClassLoc);
1050 }
1051
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001052 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001053 } else {
1054 // Mark the interface as being completed, even if it was just as
1055 // @class ....;
1056 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001057 if (!IDecl->hasDefinition())
1058 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001059 }
Mike Stump1eb44332009-09-09 15:08:12 +00001060
1061 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001062 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
Argyrios Kyrtzidis634c5632013-05-03 18:05:44 +00001063 ClassLoc, AtClassImplLoc, SuperClassLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Anders Carlsson15281452008-11-04 16:57:32 +00001065 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001066 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001067
Chris Lattner4d391482007-12-12 07:09:47 +00001068 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001069 if (IDecl->getImplementation()) {
1070 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001071 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001072 Diag(IDecl->getImplementation()->getLocation(),
1073 diag::note_previous_definition);
Argyrios Kyrtzidisdf08c4b2013-05-30 18:53:21 +00001074 IMPDecl->setInvalidDecl();
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001075 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001076 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001077 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001078 // Warn on implementating deprecated class under
1079 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001080 DiagnoseObjCImplementedDeprecations(*this,
1081 dyn_cast<NamedDecl>(IDecl),
1082 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001083 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001084 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001085}
1086
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001087Sema::DeclGroupPtrTy
1088Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1089 SmallVector<Decl *, 64> DeclsInGroup;
1090 DeclsInGroup.reserve(Decls.size() + 1);
1091
1092 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1093 Decl *Dcl = Decls[i];
1094 if (!Dcl)
1095 continue;
1096 if (Dcl->getDeclContext()->isFileContext())
1097 Dcl->setTopLevelDeclInObjCContainer();
1098 DeclsInGroup.push_back(Dcl);
1099 }
1100
1101 DeclsInGroup.push_back(ObjCImpDecl);
1102
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001103 return BuildDeclaratorGroup(DeclsInGroup, false);
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001104}
1105
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001106void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1107 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001108 SourceLocation RBrace) {
1109 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001110 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001111 if (!IDecl)
1112 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001113 /// Check case of non-existing \@interface decl.
1114 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001115 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001116 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001117 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001118 // Add ivar's to class's DeclContext.
1119 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001120 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001121 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001122 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001123 }
1124
Chris Lattner4d391482007-12-12 07:09:47 +00001125 return;
1126 }
1127 // If implementation has empty ivar list, just return.
1128 if (numIvars == 0)
1129 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001130
Chris Lattner4d391482007-12-12 07:09:47 +00001131 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001132 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001133 if (ImpDecl->getSuperClass())
1134 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1135 for (unsigned i = 0; i < numIvars; i++) {
1136 ObjCIvarDecl* ImplIvar = ivars[i];
1137 if (const ObjCIvarDecl *ClsIvar =
1138 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1139 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1140 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1141 continue;
1142 }
Fariborz Jahanian3b20f582013-06-26 22:10:27 +00001143 // Check class extensions (unnamed categories) for duplicate ivars.
1144 for (ObjCInterfaceDecl::visible_extensions_iterator
1145 Ext = IDecl->visible_extensions_begin(),
1146 ExtEnd = IDecl->visible_extensions_end();
1147 Ext != ExtEnd; ++Ext) {
1148 ObjCCategoryDecl *CDecl = *Ext;
1149 if (const ObjCIvarDecl *ClsExtIvar =
1150 CDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1151 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1152 Diag(ClsExtIvar->getLocation(), diag::note_previous_definition);
1153 continue;
1154 }
1155 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001156 // Instance ivar to Implementation's DeclContext.
1157 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001158 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001159 ImpDecl->addDecl(ImplIvar);
1160 }
1161 return;
1162 }
Chris Lattner4d391482007-12-12 07:09:47 +00001163 // Check interface's Ivar list against those in the implementation.
1164 // names and types must match.
1165 //
Chris Lattner4d391482007-12-12 07:09:47 +00001166 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001167 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001168 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1169 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001170 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001171 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001172 assert (ImplIvar && "missing implementation ivar");
1173 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Steve Naroffca331292009-03-03 14:49:36 +00001175 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001176 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001177 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001178 << ImplIvar->getIdentifier()
1179 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001180 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001181 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1182 ImplIvar->getBitWidthValue(Context) !=
1183 ClsIvar->getBitWidthValue(Context)) {
1184 Diag(ImplIvar->getBitWidth()->getLocStart(),
1185 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1186 Diag(ClsIvar->getBitWidth()->getLocStart(),
1187 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001188 }
Steve Naroffca331292009-03-03 14:49:36 +00001189 // Make sure the names are identical.
1190 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001191 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001192 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001193 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001194 }
1195 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001196 }
Mike Stump1eb44332009-09-09 15:08:12 +00001197
Chris Lattner609e4c72007-12-12 18:11:49 +00001198 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001199 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001200 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001201 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001202}
1203
Steve Naroff3c2eb662008-02-10 21:38:56 +00001204void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001205 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001206 // No point warning no definition of method which is 'unavailable'.
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001207 switch (method->getAvailability()) {
1208 case AR_Available:
1209 case AR_Deprecated:
1210 break;
1211
1212 // Don't warn about unavailable or not-yet-introduced methods.
1213 case AR_NotYetIntroduced:
1214 case AR_Unavailable:
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001215 return;
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001216 }
1217
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001218 // FIXME: For now ignore 'IncompleteImpl'.
1219 // Previously we grouped all unimplemented methods under a single
1220 // warning, but some users strongly voiced that they would prefer
1221 // separate warnings. We will give that approach a try, as that
1222 // matches what we do with protocols.
1223
1224 Diag(ImpLoc, DiagID) << method->getDeclName();
1225
1226 // Issue a note to the original declaration.
1227 SourceLocation MethodLoc = method->getLocStart();
1228 if (MethodLoc.isValid())
1229 Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001230}
1231
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001232/// Determines if type B can be substituted for type A. Returns true if we can
1233/// guarantee that anything that the user will do to an object of type A can
1234/// also be done to an object of type B. This is trivially true if the two
1235/// types are the same, or if B is a subclass of A. It becomes more complex
1236/// in cases where protocols are involved.
1237///
1238/// Object types in Objective-C describe the minimum requirements for an
1239/// object, rather than providing a complete description of a type. For
1240/// example, if A is a subclass of B, then B* may refer to an instance of A.
1241/// The principle of substitutability means that we may use an instance of A
1242/// anywhere that we may use an instance of B - it will implement all of the
1243/// ivars of B and all of the methods of B.
1244///
1245/// This substitutability is important when type checking methods, because
1246/// the implementation may have stricter type definitions than the interface.
1247/// The interface specifies minimum requirements, but the implementation may
1248/// have more accurate ones. For example, a method may privately accept
1249/// instances of B, but only publish that it accepts instances of A. Any
1250/// object passed to it will be type checked against B, and so will implicitly
1251/// by a valid A*. Similarly, a method may return a subclass of the class that
1252/// it is declared as returning.
1253///
1254/// This is most important when considering subclassing. A method in a
1255/// subclass must accept any object as an argument that its superclass's
1256/// implementation accepts. It may, however, accept a more general type
1257/// without breaking substitutability (i.e. you can still use the subclass
1258/// anywhere that you can use the superclass, but not vice versa). The
1259/// converse requirement applies to return types: the return type for a
1260/// subclass method must be a valid object of the kind that the superclass
1261/// advertises, but it may be specified more accurately. This avoids the need
1262/// for explicit down-casting by callers.
1263///
1264/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001265static bool isObjCTypeSubstitutable(ASTContext &Context,
1266 const ObjCObjectPointerType *A,
1267 const ObjCObjectPointerType *B,
1268 bool rejectId) {
1269 // Reject a protocol-unqualified id.
1270 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001271
1272 // If B is a qualified id, then A must also be a qualified id and it must
1273 // implement all of the protocols in B. It may not be a qualified class.
1274 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1275 // stricter definition so it is not substitutable for id<A>.
1276 if (B->isObjCQualifiedIdType()) {
1277 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001278 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1279 QualType(B,0),
1280 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001281 }
1282
1283 /*
1284 // id is a special type that bypasses type checking completely. We want a
1285 // warning when it is used in one place but not another.
1286 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1287
1288
1289 // If B is a qualified id, then A must also be a qualified id (which it isn't
1290 // if we've got this far)
1291 if (B->isObjCQualifiedIdType()) return false;
1292 */
1293
1294 // Now we know that A and B are (potentially-qualified) class types. The
1295 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001296 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001297}
1298
John McCall10302c02010-10-28 02:34:38 +00001299static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1300 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1301}
1302
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001303static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001304 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001305 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001306 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001307 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001308 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001309 if (IsProtocolMethodDecl &&
1310 (MethodDecl->getObjCDeclQualifier() !=
1311 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001312 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001313 S.Diag(MethodImpl->getLocation(),
1314 (IsOverridingMode ?
1315 diag::warn_conflicting_overriding_ret_type_modifiers
1316 : diag::warn_conflicting_ret_type_modifiers))
1317 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001318 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1319 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1320 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1321 }
1322 else
1323 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001324 }
1325
John McCall10302c02010-10-28 02:34:38 +00001326 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001327 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001328 return true;
1329 if (!Warn)
1330 return false;
John McCall10302c02010-10-28 02:34:38 +00001331
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001332 unsigned DiagID =
1333 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1334 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001335
1336 // Mismatches between ObjC pointers go into a different warning
1337 // category, and sometimes they're even completely whitelisted.
1338 if (const ObjCObjectPointerType *ImplPtrTy =
1339 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1340 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001341 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001342 // Allow non-matching return types as long as they don't violate
1343 // the principle of substitutability. Specifically, we permit
1344 // return types that are subclasses of the declared return type,
1345 // or that are more-qualified versions of the declared type.
1346 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001347 return false;
John McCall10302c02010-10-28 02:34:38 +00001348
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001349 DiagID =
1350 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1351 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001352 }
1353 }
1354
1355 S.Diag(MethodImpl->getLocation(), DiagID)
1356 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001357 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001358 << MethodImpl->getResultType()
1359 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001360 S.Diag(MethodDecl->getLocation(),
1361 IsOverridingMode ? diag::note_previous_declaration
1362 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001363 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001364 return false;
John McCall10302c02010-10-28 02:34:38 +00001365}
1366
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001367static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001368 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001369 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001370 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001371 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001372 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001373 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001374 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001375 if (IsProtocolMethodDecl &&
1376 (ImplVar->getObjCDeclQualifier() !=
1377 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001378 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001379 if (IsOverridingMode)
1380 S.Diag(ImplVar->getLocation(),
1381 diag::warn_conflicting_overriding_param_modifiers)
1382 << getTypeRange(ImplVar->getTypeSourceInfo())
1383 << MethodImpl->getDeclName();
1384 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001385 diag::warn_conflicting_param_modifiers)
1386 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001387 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001388 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1389 << getTypeRange(IfaceVar->getTypeSourceInfo());
1390 }
1391 else
1392 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001393 }
1394
John McCall10302c02010-10-28 02:34:38 +00001395 QualType ImplTy = ImplVar->getType();
1396 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001397
John McCall10302c02010-10-28 02:34:38 +00001398 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001399 return true;
1400
1401 if (!Warn)
1402 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001403 unsigned DiagID =
1404 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1405 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001406
1407 // Mismatches between ObjC pointers go into a different warning
1408 // category, and sometimes they're even completely whitelisted.
1409 if (const ObjCObjectPointerType *ImplPtrTy =
1410 ImplTy->getAs<ObjCObjectPointerType>()) {
1411 if (const ObjCObjectPointerType *IfacePtrTy =
1412 IfaceTy->getAs<ObjCObjectPointerType>()) {
1413 // Allow non-matching argument types as long as they don't
1414 // violate the principle of substitutability. Specifically, the
1415 // implementation must accept any objects that the superclass
1416 // accepts, however it may also accept others.
1417 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001418 return false;
John McCall10302c02010-10-28 02:34:38 +00001419
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001420 DiagID =
1421 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1422 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001423 }
1424 }
1425
1426 S.Diag(ImplVar->getLocation(), DiagID)
1427 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001428 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1429 S.Diag(IfaceVar->getLocation(),
1430 (IsOverridingMode ? diag::note_previous_declaration
1431 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001432 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001433 return false;
John McCall10302c02010-10-28 02:34:38 +00001434}
John McCallf85e1932011-06-15 23:02:42 +00001435
1436/// In ARC, check whether the conventional meanings of the two methods
1437/// match. If they don't, it's a hard error.
1438static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1439 ObjCMethodDecl *decl) {
1440 ObjCMethodFamily implFamily = impl->getMethodFamily();
1441 ObjCMethodFamily declFamily = decl->getMethodFamily();
1442 if (implFamily == declFamily) return false;
1443
1444 // Since conventions are sorted by selector, the only possibility is
1445 // that the types differ enough to cause one selector or the other
1446 // to fall out of the family.
1447 assert(implFamily == OMF_None || declFamily == OMF_None);
1448
1449 // No further diagnostics required on invalid declarations.
1450 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1451
1452 const ObjCMethodDecl *unmatched = impl;
1453 ObjCMethodFamily family = declFamily;
1454 unsigned errorID = diag::err_arc_lost_method_convention;
1455 unsigned noteID = diag::note_arc_lost_method_convention;
1456 if (declFamily == OMF_None) {
1457 unmatched = decl;
1458 family = implFamily;
1459 errorID = diag::err_arc_gained_method_convention;
1460 noteID = diag::note_arc_gained_method_convention;
1461 }
1462
1463 // Indexes into a %select clause in the diagnostic.
1464 enum FamilySelector {
1465 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1466 };
1467 FamilySelector familySelector = FamilySelector();
1468
1469 switch (family) {
1470 case OMF_None: llvm_unreachable("logic error, no method convention");
1471 case OMF_retain:
1472 case OMF_release:
1473 case OMF_autorelease:
1474 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001475 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001476 case OMF_retainCount:
1477 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001478 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001479 // Mismatches for these methods don't change ownership
1480 // conventions, so we don't care.
1481 return false;
1482
1483 case OMF_init: familySelector = F_init; break;
1484 case OMF_alloc: familySelector = F_alloc; break;
1485 case OMF_copy: familySelector = F_copy; break;
1486 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1487 case OMF_new: familySelector = F_new; break;
1488 }
1489
1490 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1491 ReasonSelector reasonSelector;
1492
1493 // The only reason these methods don't fall within their families is
1494 // due to unusual result types.
1495 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1496 reasonSelector = R_UnrelatedReturn;
1497 } else {
1498 reasonSelector = R_NonObjectReturn;
1499 }
1500
Joerg Sonnenberger73484542013-06-26 21:31:47 +00001501 S.Diag(impl->getLocation(), errorID) << int(familySelector) << int(reasonSelector);
1502 S.Diag(decl->getLocation(), noteID) << int(familySelector) << int(reasonSelector);
John McCallf85e1932011-06-15 23:02:42 +00001503
1504 return true;
1505}
John McCall10302c02010-10-28 02:34:38 +00001506
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001507void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001508 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001509 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001510 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001511 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1512 return;
1513
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001514 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001515 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001516 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001517
Chris Lattner3aff9192009-04-11 19:58:42 +00001518 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001519 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1520 EF = MethodDecl->param_end();
1521 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001522 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001523 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001524 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001525
Fariborz Jahanian21121902011-08-08 18:03:17 +00001526 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001527 Diag(ImpMethodDecl->getLocation(),
1528 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001529 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001530 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001531}
1532
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001533void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1534 ObjCMethodDecl *Overridden,
1535 bool IsProtocolMethodDecl) {
1536
1537 CheckMethodOverrideReturn(*this, Method, Overridden,
1538 IsProtocolMethodDecl, true,
1539 true);
1540
1541 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001542 IF = Overridden->param_begin(), EM = Method->param_end(),
1543 EF = Overridden->param_end();
1544 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001545 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1546 IsProtocolMethodDecl, true, true);
1547 }
1548
1549 if (Method->isVariadic() != Overridden->isVariadic()) {
1550 Diag(Method->getLocation(),
1551 diag::warn_conflicting_overriding_variadic);
1552 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1553 }
1554}
1555
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001556/// WarnExactTypedMethods - This routine issues a warning if method
1557/// implementation declaration matches exactly that of its declaration.
1558void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1559 ObjCMethodDecl *MethodDecl,
1560 bool IsProtocolMethodDecl) {
1561 // don't issue warning when protocol method is optional because primary
1562 // class is not required to implement it and it is safe for protocol
1563 // to implement it.
1564 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1565 return;
1566 // don't issue warning when primary class's method is
1567 // depecated/unavailable.
1568 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1569 MethodDecl->hasAttr<DeprecatedAttr>())
1570 return;
1571
1572 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1573 IsProtocolMethodDecl, false, false);
1574 if (match)
1575 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001576 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1577 EF = MethodDecl->param_end();
1578 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001579 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1580 *IM, *IF,
1581 IsProtocolMethodDecl, false, false);
1582 if (!match)
1583 break;
1584 }
1585 if (match)
1586 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001587 if (match)
1588 match = !(MethodDecl->isClassMethod() &&
1589 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001590
1591 if (match) {
1592 Diag(ImpMethodDecl->getLocation(),
1593 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001594 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1595 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001596 }
1597}
1598
Mike Stump390b4cc2009-05-16 07:39:55 +00001599/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1600/// improve the efficiency of selector lookups and type checking by associating
1601/// with each protocol / interface / category the flattened instance tables. If
1602/// we used an immutable set to keep the table then it wouldn't add significant
1603/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001604
Steve Naroffefe7f362008-02-08 22:06:17 +00001605/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001606/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001607void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1608 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001609 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001610 const SelectorSet &InsMap,
1611 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001612 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001613 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1614 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1615 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001616 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1617
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001618 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001619 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001620 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001621 // check to see if class implements forwardInvocation method and objects
1622 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001623 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001624 // Under such conditions, which means that every method possible is
1625 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001626 // found" warnings.
1627 // FIXME: Use a general GetUnarySelector method for this.
1628 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1629 Selector fISelector = Context.Selectors.getSelector(1, &II);
1630 if (InsMap.count(fISelector))
1631 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1632 // need be implemented in the implementation.
1633 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1634 }
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001636 // If this is a forward protocol declaration, get its definition.
1637 if (!PDecl->isThisDeclarationADefinition() &&
1638 PDecl->getDefinition())
1639 PDecl = PDecl->getDefinition();
1640
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001641 // If a method lookup fails locally we still need to look and see if
1642 // the method was implemented by a base class or an inherited
1643 // protocol. This lookup is slow, but occurs rarely in correct code
1644 // and otherwise would terminate in a warning.
1645
Chris Lattner4d391482007-12-12 07:09:47 +00001646 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001647 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001648 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001649 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001650 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001651 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001652 !method->isPropertyAccessor() &&
1653 !InsMap.count(method->getSelector()) &&
1654 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001655 // If a method is not implemented in the category implementation but
1656 // has been declared in its primary class, superclass,
1657 // or in one of their protocols, no need to issue the warning.
1658 // This is because method will be implemented in the primary class
1659 // or one of its super class implementation.
1660
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001661 // Ugly, but necessary. Method declared in protcol might have
1662 // have been synthesized due to a property declared in the class which
1663 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001664 if (ObjCMethodDecl *MethodInClass =
1665 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001666 true /*shallowCategoryLookup*/))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001667 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001668 continue;
1669 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1670 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1671 != DiagnosticsEngine::Ignored) {
1672 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001673 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1674 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001675 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001676 }
1677 }
Chris Lattner4d391482007-12-12 07:09:47 +00001678 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001679 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001680 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001681 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001682 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001683 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1684 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001685 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001686 // See above comment for instance method lookups.
1687 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001688 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001689 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001690 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001691 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1692 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001693 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
1694 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1695 PDecl->getDeclName();
1696 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001697 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001698 }
Chris Lattner780f3292008-07-21 21:32:27 +00001699 // Check on this protocols's referenced protocols, recursively.
1700 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1701 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001702 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001703}
1704
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001705/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001706/// or protocol against those declared in their implementations.
1707///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001708void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1709 const SelectorSet &ClsMap,
1710 SelectorSet &InsMapSeen,
1711 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001712 ObjCImplDecl* IMPDecl,
1713 ObjCContainerDecl* CDecl,
1714 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001715 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001716 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001717 // Check and see if instance methods in class interface have been
1718 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001719 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1720 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001721 if (InsMapSeen.count((*I)->getSelector()))
1722 continue;
1723 InsMapSeen.insert((*I)->getSelector());
Jordan Rose1e4691b2012-10-10 16:42:25 +00001724 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001725 !InsMap.count((*I)->getSelector())) {
1726 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001727 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001728 diag::warn_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001729 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001730 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001731 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001732 IMPDecl->getInstanceMethod((*I)->getSelector());
1733 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1734 "Expected to find the method through lookup as well");
1735 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001736 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001737 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001738 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001739 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1740 isa<ObjCProtocolDecl>(CDecl));
Jordan Rose1e4691b2012-10-10 16:42:25 +00001741 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001742 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001743 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001744 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001745 }
1746 }
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001748 // Check and see if class methods in class interface have been
1749 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001750 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001751 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001752 if (ClsMapSeen.count((*I)->getSelector()))
1753 continue;
1754 ClsMapSeen.insert((*I)->getSelector());
1755 if (!ClsMap.count((*I)->getSelector())) {
1756 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001757 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001758 diag::warn_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001759 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001760 ObjCMethodDecl *ImpMethodDecl =
1761 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001762 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1763 "Expected to find the method through lookup as well");
1764 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001765 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001766 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1767 isa<ObjCProtocolDecl>(CDecl));
1768 else
1769 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001770 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001771 }
1772 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001773
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001774 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001775 // when checking that methods in implementation match their declaration,
1776 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1777 // extension; as well as those in categories.
Douglas Gregord3297242013-01-16 23:00:23 +00001778 if (!WarnCategoryMethodImpl) {
1779 for (ObjCInterfaceDecl::visible_categories_iterator
1780 Cat = I->visible_categories_begin(),
1781 CatEnd = I->visible_categories_end();
1782 Cat != CatEnd; ++Cat) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001783 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001784 IMPDecl, *Cat, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001785 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001786 }
1787 } else {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001788 // Also methods in class extensions need be looked at next.
Douglas Gregord3297242013-01-16 23:00:23 +00001789 for (ObjCInterfaceDecl::visible_extensions_iterator
1790 Ext = I->visible_extensions_begin(),
1791 ExtEnd = I->visible_extensions_end();
1792 Ext != ExtEnd; ++Ext) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001793 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001794 IMPDecl, *Ext, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001795 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001796 }
1797 }
1798
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001799 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001800 for (ObjCInterfaceDecl::all_protocol_iterator
1801 PI = I->all_referenced_protocol_begin(),
1802 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001803 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1804 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001805 (*PI), IncompleteImpl, false,
1806 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001807
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001808 // FIXME. For now, we are not checking for extact match of methods
1809 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001810 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001811 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001812 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001813 I->getSuperClass(), IncompleteImpl, false);
1814 }
1815}
1816
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001817/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1818/// category matches with those implemented in its primary class and
1819/// warns each time an exact match is found.
1820void Sema::CheckCategoryVsClassMethodMatches(
1821 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001822 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001823
1824 for (ObjCImplementationDecl::instmeth_iterator
1825 I = CatIMPDecl->instmeth_begin(),
1826 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1827 InsMap.insert((*I)->getSelector());
1828
1829 for (ObjCImplementationDecl::classmeth_iterator
1830 I = CatIMPDecl->classmeth_begin(),
1831 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1832 ClsMap.insert((*I)->getSelector());
1833 if (InsMap.empty() && ClsMap.empty())
1834 return;
1835
1836 // Get category's primary class.
1837 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1838 if (!CatDecl)
1839 return;
1840 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1841 if (!IDecl)
1842 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001843 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001844 bool IncompleteImpl = false;
1845 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1846 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001847 IncompleteImpl, false,
1848 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001849}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001850
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001851void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001852 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001853 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001854 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001855 // Check and see if instance methods in class interface have been
1856 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001857 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001858 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001859 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001860
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001861 // Check and see if properties declared in the interface have either 1)
1862 // an implementation or 2) there is a @synthesize/@dynamic implementation
1863 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001864 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001865 if (!(LangOpts.ObjCDefaultSynthProperties &&
1866 LangOpts.ObjCRuntime.isNonFragile()) ||
1867 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001868 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001869
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001870 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001871 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001872 I = IMPDecl->classmeth_begin(),
1873 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001874 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001876 // Check for type conflict of methods declared in a class/protocol and
1877 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001878 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001879 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1880 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001881 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001882
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001883 // check all methods implemented in category against those declared
1884 // in its primary class.
1885 if (ObjCCategoryImplDecl *CatDecl =
1886 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1887 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001888
Chris Lattner4d391482007-12-12 07:09:47 +00001889 // Check the protocol list for unimplemented methods in the @implementation
1890 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001891 // Check and see if class methods in class interface have been
1892 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001893
Chris Lattnercddc8882009-03-01 00:56:52 +00001894 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001895 for (ObjCInterfaceDecl::all_protocol_iterator
1896 PI = I->all_referenced_protocol_begin(),
1897 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001898 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001899 InsMap, ClsMap, I);
1900 // Check class extensions (unnamed categories)
Douglas Gregord3297242013-01-16 23:00:23 +00001901 for (ObjCInterfaceDecl::visible_extensions_iterator
1902 Ext = I->visible_extensions_begin(),
1903 ExtEnd = I->visible_extensions_end();
1904 Ext != ExtEnd; ++Ext) {
1905 ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
1906 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001907 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001908 // For extended class, unimplemented methods in its protocols will
1909 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001910 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001911 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1912 E = C->protocol_end(); PI != E; ++PI)
1913 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001914 InsMap, ClsMap, CDecl);
Fariborz Jahanianc775b1a2013-04-24 17:06:38 +00001915 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001916 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001917 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001918 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001919}
1920
Mike Stump1eb44332009-09-09 15:08:12 +00001921/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001922Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001923Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001924 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001925 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001926 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001927 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001928 for (unsigned i = 0; i != NumElts; ++i) {
1929 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001930 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001931 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001932 LookupOrdinaryName, ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001933 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001934 // GCC apparently allows the following idiom:
1935 //
1936 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1937 // @class XCElementToggler;
1938 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001939 // Here we have chosen to ignore the forward class declaration
1940 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001941 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001942 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001943 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001944 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001945 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001946 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001947 // to the underlying class. Just ignore the forward class with a warning
1948 // as this will force the intended behavior which is to lookup the typedef
1949 // name.
1950 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1951 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1952 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1953 continue;
1954 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001955 }
Chris Lattner4d391482007-12-12 07:09:47 +00001956 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001957
1958 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001959 ObjCInterfaceDecl *PrevIDecl
1960 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +00001961
1962 IdentifierInfo *ClassName = IdentList[i];
1963 if (PrevIDecl && PrevIDecl->getIdentifier() != ClassName) {
1964 // A previous decl with a different name is because of
1965 // @compatibility_alias, for example:
1966 // \code
1967 // @class NewImage;
1968 // @compatibility_alias OldImage NewImage;
1969 // \endcode
1970 // A lookup for 'OldImage' will return the 'NewImage' decl.
1971 //
1972 // In such a case use the real declaration name, instead of the alias one,
1973 // otherwise we will break IdentifierResolver and redecls-chain invariants.
1974 // FIXME: If necessary, add a bit to indicate that this ObjCInterfaceDecl
1975 // has been aliased.
1976 ClassName = PrevIDecl->getIdentifier();
1977 }
1978
Douglas Gregor7723fec2011-12-15 20:29:51 +00001979 ObjCInterfaceDecl *IDecl
1980 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Argyrios Kyrtzidise7e8fca2013-06-18 21:26:33 +00001981 ClassName, PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001982 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001983
Douglas Gregor7723fec2011-12-15 20:29:51 +00001984 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001985 CheckObjCDeclScope(IDecl);
1986 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001987 }
Rafael Espindola4549d7f2013-07-09 12:05:01 +00001988
1989 return BuildDeclaratorGroup(DeclsInGroup, false);
Chris Lattner4d391482007-12-12 07:09:47 +00001990}
1991
John McCall0f4c4c42011-06-16 01:15:19 +00001992static bool tryMatchRecordTypes(ASTContext &Context,
1993 Sema::MethodMatchStrategy strategy,
1994 const Type *left, const Type *right);
1995
John McCallf85e1932011-06-15 23:02:42 +00001996static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1997 QualType leftQT, QualType rightQT) {
1998 const Type *left =
1999 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
2000 const Type *right =
2001 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
2002
2003 if (left == right) return true;
2004
2005 // If we're doing a strict match, the types have to match exactly.
2006 if (strategy == Sema::MMS_strict) return false;
2007
2008 if (left->isIncompleteType() || right->isIncompleteType()) return false;
2009
2010 // Otherwise, use this absurdly complicated algorithm to try to
2011 // validate the basic, low-level compatibility of the two types.
2012
2013 // As a minimum, require the sizes and alignments to match.
2014 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
2015 return false;
2016
2017 // Consider all the kinds of non-dependent canonical types:
2018 // - functions and arrays aren't possible as return and parameter types
2019
2020 // - vector types of equal size can be arbitrarily mixed
2021 if (isa<VectorType>(left)) return isa<VectorType>(right);
2022 if (isa<VectorType>(right)) return false;
2023
2024 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00002025 // - structs, unions, and Objective-C objects must match more-or-less
2026 // exactly
John McCallf85e1932011-06-15 23:02:42 +00002027 // - everything else should be a scalar
2028 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00002029 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00002030
John McCall1d9b3b22011-09-09 05:25:32 +00002031 // Make scalars agree in kind, except count bools as chars, and group
2032 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00002033 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2034 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2035 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2036 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00002037 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2038 leftSK = Type::STK_ObjCObjectPointer;
2039 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2040 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00002041
2042 // Note that data member pointers and function member pointers don't
2043 // intermix because of the size differences.
2044
2045 return (leftSK == rightSK);
2046}
Chris Lattner4d391482007-12-12 07:09:47 +00002047
John McCall0f4c4c42011-06-16 01:15:19 +00002048static bool tryMatchRecordTypes(ASTContext &Context,
2049 Sema::MethodMatchStrategy strategy,
2050 const Type *lt, const Type *rt) {
2051 assert(lt && rt && lt != rt);
2052
2053 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2054 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2055 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2056
2057 // Require union-hood to match.
2058 if (left->isUnion() != right->isUnion()) return false;
2059
2060 // Require an exact match if either is non-POD.
2061 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2062 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2063 return false;
2064
2065 // Require size and alignment to match.
2066 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2067
2068 // Require fields to match.
2069 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2070 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2071 for (; li != le && ri != re; ++li, ++ri) {
2072 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2073 return false;
2074 }
2075 return (li == le && ri == re);
2076}
2077
Chris Lattner4d391482007-12-12 07:09:47 +00002078/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2079/// returns true, or false, accordingly.
2080/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00002081bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2082 const ObjCMethodDecl *right,
2083 MethodMatchStrategy strategy) {
2084 if (!matchTypes(Context, strategy,
2085 left->getResultType(), right->getResultType()))
2086 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002087
Douglas Gregor7666b032013-02-07 19:13:24 +00002088 // If either is hidden, it is not considered to match.
2089 if (left->isHidden() || right->isHidden())
2090 return false;
2091
David Blaikie4e4d0842012-03-11 07:00:24 +00002092 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002093 (left->hasAttr<NSReturnsRetainedAttr>()
2094 != right->hasAttr<NSReturnsRetainedAttr>() ||
2095 left->hasAttr<NSConsumesSelfAttr>()
2096 != right->hasAttr<NSConsumesSelfAttr>()))
2097 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002098
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002099 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002100 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2101 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002102
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002103 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002104 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002105 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002106
2107 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2108 return false;
2109
David Blaikie4e4d0842012-03-11 07:00:24 +00002110 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002111 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2112 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002113 }
2114 return true;
2115}
2116
Douglas Gregorff310c72012-05-01 23:37:00 +00002117void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002118 // Record at the head of the list whether there were 0, 1, or >= 2 methods
2119 // inside categories.
Argyrios Kyrtzidisab3d5092013-04-27 00:10:12 +00002120 if (ObjCCategoryDecl *
2121 CD = dyn_cast<ObjCCategoryDecl>(Method->getDeclContext()))
2122 if (!CD->IsClassExtension() && List->getBits() < 2)
2123 List->setBits(List->getBits()+1);
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002124
Douglas Gregor44fae522012-01-25 00:19:56 +00002125 // If the list is empty, make it a singleton list.
2126 if (List->Method == 0) {
2127 List->Method = Method;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002128 List->setNext(0);
Douglas Gregorff310c72012-05-01 23:37:00 +00002129 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002130 }
2131
2132 // We've seen a method with this name, see if we have already seen this type
2133 // signature.
2134 ObjCMethodList *Previous = List;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002135 for (; List; Previous = List, List = List->getNext()) {
Douglas Gregorfc46be92013-06-21 00:20:25 +00002136 // If we are building a module, keep all of the methods.
2137 if (getLangOpts().Modules && !getLangOpts().CurrentModule.empty())
2138 continue;
2139
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002140 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002141 continue;
2142
2143 ObjCMethodDecl *PrevObjCMethod = List->Method;
2144
2145 // Propagate the 'defined' bit.
2146 if (Method->isDefined())
2147 PrevObjCMethod->setDefined(true);
2148
2149 // If a method is deprecated, push it in the global pool.
2150 // This is used for better diagnostics.
2151 if (Method->isDeprecated()) {
2152 if (!PrevObjCMethod->isDeprecated())
2153 List->Method = Method;
2154 }
2155 // If new method is unavailable, push it into global pool
2156 // unless previous one is deprecated.
2157 if (Method->isUnavailable()) {
2158 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2159 List->Method = Method;
2160 }
2161
Douglas Gregorff310c72012-05-01 23:37:00 +00002162 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002163 }
2164
2165 // We have a new signature for an existing method - add it.
2166 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002167 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002168 Previous->setNext(new (Mem) ObjCMethodList(Method, 0));
Douglas Gregor44fae522012-01-25 00:19:56 +00002169}
2170
Sebastian Redldb9d2142010-08-02 23:18:59 +00002171/// \brief Read the contents of the method pool for a given selector from
2172/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002173void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002174 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002175 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002176}
2177
Douglas Gregorff310c72012-05-01 23:37:00 +00002178void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002179 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002180 // Ignore methods of invalid containers.
2181 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002182 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002183
Douglas Gregor0d266d62012-01-25 00:59:09 +00002184 if (ExternalSource)
2185 ReadMethodPool(Method->getSelector());
2186
Sebastian Redldb9d2142010-08-02 23:18:59 +00002187 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002188 if (Pos == MethodPool.end())
2189 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2190 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002191
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002192 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002193
Sebastian Redldb9d2142010-08-02 23:18:59 +00002194 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002195 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002196}
2197
John McCallf85e1932011-06-15 23:02:42 +00002198/// Determines if this is an "acceptable" loose mismatch in the global
2199/// method pool. This exists mostly as a hack to get around certain
2200/// global mismatches which we can't afford to make warnings / errors.
2201/// Really, what we want is a way to take a method out of the global
2202/// method pool.
2203static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2204 ObjCMethodDecl *other) {
2205 if (!chosen->isInstanceMethod())
2206 return false;
2207
2208 Selector sel = chosen->getSelector();
2209 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2210 return false;
2211
2212 // Don't complain about mismatches for -length if the method we
2213 // chose has an integral result type.
2214 return (chosen->getResultType()->isIntegerType());
2215}
2216
Sebastian Redldb9d2142010-08-02 23:18:59 +00002217ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002218 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002219 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002220 if (ExternalSource)
2221 ReadMethodPool(Sel);
2222
Sebastian Redldb9d2142010-08-02 23:18:59 +00002223 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002224 if (Pos == MethodPool.end())
2225 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002226
Douglas Gregorf0e00042013-01-16 18:47:38 +00002227 // Gather the non-hidden methods.
Sebastian Redldb9d2142010-08-02 23:18:59 +00002228 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorf0e00042013-01-16 18:47:38 +00002229 llvm::SmallVector<ObjCMethodDecl *, 4> Methods;
Argyrios Kyrtzidis2e3d8c02013-04-17 00:08:58 +00002230 for (ObjCMethodList *M = &MethList; M; M = M->getNext()) {
Douglas Gregorf0e00042013-01-16 18:47:38 +00002231 if (M->Method && !M->Method->isHidden()) {
2232 // If we're not supposed to warn about mismatches, we're done.
2233 if (!warn)
2234 return M->Method;
Mike Stump1eb44332009-09-09 15:08:12 +00002235
Douglas Gregorf0e00042013-01-16 18:47:38 +00002236 Methods.push_back(M->Method);
Sebastian Redldb9d2142010-08-02 23:18:59 +00002237 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002238 }
Douglas Gregorf0e00042013-01-16 18:47:38 +00002239
2240 // If there aren't any visible methods, we're done.
2241 // FIXME: Recover if there are any known-but-hidden methods?
2242 if (Methods.empty())
2243 return 0;
2244
2245 if (Methods.size() == 1)
2246 return Methods[0];
2247
2248 // We found multiple methods, so we may have to complain.
2249 bool issueDiagnostic = false, issueError = false;
2250
2251 // We support a warning which complains about *any* difference in
2252 // method signature.
2253 bool strictSelectorMatch =
2254 (receiverIdOrClass && warn &&
2255 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2256 R.getBegin())
2257 != DiagnosticsEngine::Ignored));
2258 if (strictSelectorMatch) {
2259 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2260 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2261 issueDiagnostic = true;
2262 break;
2263 }
2264 }
2265 }
2266
2267 // If we didn't see any strict differences, we won't see any loose
2268 // differences. In ARC, however, we also need to check for loose
2269 // mismatches, because most of them are errors.
2270 if (!strictSelectorMatch ||
2271 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2272 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2273 // This checks if the methods differ in type mismatch.
2274 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2275 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2276 issueDiagnostic = true;
2277 if (getLangOpts().ObjCAutoRefCount)
2278 issueError = true;
2279 break;
2280 }
2281 }
2282
2283 if (issueDiagnostic) {
2284 if (issueError)
2285 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2286 else if (strictSelectorMatch)
2287 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2288 else
2289 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2290
2291 Diag(Methods[0]->getLocStart(),
2292 issueError ? diag::note_possibility : diag::note_using)
2293 << Methods[0]->getSourceRange();
2294 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2295 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2296 << Methods[I]->getSourceRange();
2297 }
2298 }
2299 return Methods[0];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002300}
2301
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002302ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002303 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2304 if (Pos == MethodPool.end())
2305 return 0;
2306
2307 GlobalMethods &Methods = Pos->second;
2308
2309 if (Methods.first.Method && Methods.first.Method->isDefined())
2310 return Methods.first.Method;
2311 if (Methods.second.Method && Methods.second.Method->isDefined())
2312 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002313 return 0;
2314}
2315
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002316static void
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002317HelperSelectorsForTypoCorrection(
2318 SmallVectorImpl<const ObjCMethodDecl *> &BestMethod,
2319 StringRef Typo, const ObjCMethodDecl * Method) {
2320 const unsigned MaxEditDistance = 1;
2321 unsigned BestEditDistance = MaxEditDistance + 1;
Richard Trieu4fe96442013-06-06 02:22:29 +00002322 std::string MethodName = Method->getSelector().getAsString();
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002323
2324 unsigned MinPossibleEditDistance = abs((int)MethodName.size() - (int)Typo.size());
2325 if (MinPossibleEditDistance > 0 &&
2326 Typo.size() / MinPossibleEditDistance < 1)
2327 return;
2328 unsigned EditDistance = Typo.edit_distance(MethodName, true, MaxEditDistance);
2329 if (EditDistance > MaxEditDistance)
2330 return;
2331 if (EditDistance == BestEditDistance)
2332 BestMethod.push_back(Method);
2333 else if (EditDistance < BestEditDistance) {
2334 BestMethod.clear();
2335 BestMethod.push_back(Method);
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002336 }
2337}
2338
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002339static bool HelperIsMethodInObjCType(Sema &S, Selector Sel,
2340 QualType ObjectType) {
2341 if (ObjectType.isNull())
2342 return true;
2343 if (S.LookupMethodInObjectType(Sel, ObjectType, true/*Instance method*/))
2344 return true;
2345 return S.LookupMethodInObjectType(Sel, ObjectType, false/*Class method*/) != 0;
2346}
2347
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002348const ObjCMethodDecl *
Fariborz Jahaniand395e342013-06-17 17:10:54 +00002349Sema::SelectorsForTypoCorrection(Selector Sel,
2350 QualType ObjectType) {
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002351 unsigned NumArgs = Sel.getNumArgs();
2352 SmallVector<const ObjCMethodDecl *, 8> Methods;
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002353 bool ObjectIsId = true, ObjectIsClass = true;
2354 if (ObjectType.isNull())
2355 ObjectIsId = ObjectIsClass = false;
2356 else if (!ObjectType->isObjCObjectPointerType())
2357 return 0;
2358 else if (const ObjCObjectPointerType *ObjCPtr =
2359 ObjectType->getAsObjCInterfacePointerType()) {
2360 ObjectType = QualType(ObjCPtr->getInterfaceType(), 0);
2361 ObjectIsId = ObjectIsClass = false;
2362 }
2363 else if (ObjectType->isObjCIdType() || ObjectType->isObjCQualifiedIdType())
2364 ObjectIsClass = false;
2365 else if (ObjectType->isObjCClassType() || ObjectType->isObjCQualifiedClassType())
2366 ObjectIsId = false;
2367 else
2368 return 0;
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002369
2370 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2371 e = MethodPool.end(); b != e; b++) {
2372 // instance methods
2373 for (ObjCMethodList *M = &b->second.first; M; M=M->getNext())
2374 if (M->Method &&
Fariborz Jahaniancd9c9b52013-06-18 17:10:58 +00002375 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2376 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002377 if (ObjectIsId)
2378 Methods.push_back(M->Method);
2379 else if (!ObjectIsClass &&
2380 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2381 Methods.push_back(M->Method);
2382 }
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002383 // class methods
2384 for (ObjCMethodList *M = &b->second.second; M; M=M->getNext())
2385 if (M->Method &&
Fariborz Jahaniancd9c9b52013-06-18 17:10:58 +00002386 (M->Method->getSelector().getNumArgs() == NumArgs) &&
2387 (M->Method->getSelector() != Sel)) {
Fariborz Jahanian419245e2013-06-18 15:31:36 +00002388 if (ObjectIsClass)
2389 Methods.push_back(M->Method);
2390 else if (!ObjectIsId &&
2391 HelperIsMethodInObjCType(*this, M->Method->getSelector(), ObjectType))
2392 Methods.push_back(M->Method);
2393 }
Fariborz Jahanian9464a082013-06-05 18:46:14 +00002394 }
2395
2396 SmallVector<const ObjCMethodDecl *, 8> SelectedMethods;
2397 for (unsigned i = 0, e = Methods.size(); i < e; i++) {
2398 HelperSelectorsForTypoCorrection(SelectedMethods,
2399 Sel.getAsString(), Methods[i]);
2400 }
2401 return (SelectedMethods.size() == 1) ? SelectedMethods[0] : NULL;
2402}
2403
2404static void
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002405HelperToDiagnoseMismatchedMethodsInGlobalPool(Sema &S,
2406 ObjCMethodList &MethList) {
2407 ObjCMethodList *M = &MethList;
2408 ObjCMethodDecl *TargetMethod = M->Method;
2409 while (TargetMethod &&
2410 isa<ObjCImplDecl>(TargetMethod->getDeclContext())) {
2411 M = M->getNext();
2412 TargetMethod = M ? M->Method : 0;
2413 }
2414 if (!TargetMethod)
2415 return;
2416 bool FirstTime = true;
2417 for (M = M->getNext(); M; M=M->getNext()) {
2418 ObjCMethodDecl *MatchingMethodDecl = M->Method;
2419 if (isa<ObjCImplDecl>(MatchingMethodDecl->getDeclContext()))
2420 continue;
2421 if (!S.MatchTwoMethodDeclarations(TargetMethod,
2422 MatchingMethodDecl, Sema::MMS_loose)) {
2423 if (FirstTime) {
2424 FirstTime = false;
2425 S.Diag(TargetMethod->getLocation(), diag::warning_multiple_selectors)
2426 << TargetMethod->getSelector();
2427 }
2428 S.Diag(MatchingMethodDecl->getLocation(), diag::note_also_found);
2429 }
2430 }
2431}
2432
2433void Sema::DiagnoseMismatchedMethodsInGlobalPool() {
2434 unsigned DIAG = diag::warning_multiple_selectors;
2435 if (Diags.getDiagnosticLevel(DIAG, SourceLocation())
2436 == DiagnosticsEngine::Ignored)
2437 return;
2438 for (GlobalMethodPool::iterator b = MethodPool.begin(),
2439 e = MethodPool.end(); b != e; b++) {
2440 // first, instance methods
2441 ObjCMethodList &InstMethList = b->second.first;
2442 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, InstMethList);
Fariborz Jahanian639aa522013-05-30 21:52:50 +00002443 // second, class methods
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00002444 ObjCMethodList &ClsMethList = b->second.second;
2445 HelperToDiagnoseMismatchedMethodsInGlobalPool(*this, ClsMethList);
2446 }
2447}
2448
2449/// DiagnoseDuplicateIvars -
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002450/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002451/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002452/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002453/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002454void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2455 ObjCInterfaceDecl *SID) {
2456 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2457 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002458 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002459 if (Ivar->isInvalidDecl())
2460 continue;
2461 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2462 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2463 if (prevIvar) {
2464 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2465 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2466 Ivar->setInvalidDecl();
2467 }
2468 }
2469 }
2470}
2471
Erik Verbruggend64251f2011-12-06 09:25:23 +00002472Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2473 switch (CurContext->getDeclKind()) {
2474 case Decl::ObjCInterface:
2475 return Sema::OCK_Interface;
2476 case Decl::ObjCProtocol:
2477 return Sema::OCK_Protocol;
2478 case Decl::ObjCCategory:
2479 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2480 return Sema::OCK_ClassExtension;
2481 else
2482 return Sema::OCK_Category;
2483 case Decl::ObjCImplementation:
2484 return Sema::OCK_Implementation;
2485 case Decl::ObjCCategoryImpl:
2486 return Sema::OCK_CategoryImplementation;
2487
2488 default:
2489 return Sema::OCK_None;
2490 }
2491}
2492
Steve Naroffa56f6162007-12-18 01:30:32 +00002493// Note: For class/category implemenations, allMethods/allProperties is
2494// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002495Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2496 Decl **allMethods, unsigned allNum,
2497 Decl **allProperties, unsigned pNum,
2498 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002499
Erik Verbruggend64251f2011-12-06 09:25:23 +00002500 if (getObjCContainerKind() == Sema::OCK_None)
2501 return 0;
2502
2503 assert(AtEnd.isValid() && "Invalid location for '@end'");
2504
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002505 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2506 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002507
Mike Stump1eb44332009-09-09 15:08:12 +00002508 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002509 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2510 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002511 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002512
Steve Naroff0701bbb2009-01-08 17:28:14 +00002513 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2514 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2515 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2516
Chris Lattner4d391482007-12-12 07:09:47 +00002517 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002518 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002519 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002520
2521 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002522 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002523 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002524 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002525 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002526 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002527 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002528 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002529 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002530 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002531 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002532 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002533 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002534 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002535 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002536 if (!Context.getSourceManager().isInSystemHeader(
2537 Method->getLocation()))
2538 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2539 << Method->getDeclName();
2540 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2541 }
Chris Lattner4d391482007-12-12 07:09:47 +00002542 InsMap[Method->getSelector()] = Method;
2543 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002544 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002545 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002546 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002547 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002548 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002549 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002550 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002551 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002552 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002553 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002554 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002555 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002556 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002557 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002558 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002559 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002560 if (!Context.getSourceManager().isInSystemHeader(
2561 Method->getLocation()))
2562 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2563 << Method->getDeclName();
2564 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2565 }
Chris Lattner4d391482007-12-12 07:09:47 +00002566 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002567 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002568 }
2569 }
2570 }
Douglas Gregorb892d702013-01-21 19:42:21 +00002571 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2572 // Nothing to do here.
Steve Naroff09c47192009-01-09 15:36:25 +00002573 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002574 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002575 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002576 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002577
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002578 if (C->IsClassExtension()) {
2579 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2580 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002581 }
Chris Lattner4d391482007-12-12 07:09:47 +00002582 }
Steve Naroff09c47192009-01-09 15:36:25 +00002583 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002584 if (CDecl->getIdentifier())
2585 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2586 // user-defined setter/getter. It also synthesizes setter/getter methods
2587 // and adds them to the DeclContext and global method pools.
2588 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2589 E = CDecl->prop_end();
2590 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002591 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002592 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002593 }
2594 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002595 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002596 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002597 // Any property declared in a class extension might have user
2598 // declared setter or getter in current class extension or one
2599 // of the other class extensions. Mark them as synthesized as
2600 // property will be synthesized when property with same name is
2601 // seen in the @implementation.
Douglas Gregord3297242013-01-16 23:00:23 +00002602 for (ObjCInterfaceDecl::visible_extensions_iterator
2603 Ext = IDecl->visible_extensions_begin(),
2604 ExtEnd = IDecl->visible_extensions_end();
2605 Ext != ExtEnd; ++Ext) {
2606 for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
2607 E = Ext->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002608 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002609 // Skip over properties declared @dynamic
2610 if (const ObjCPropertyImplDecl *PIDecl
2611 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2612 if (PIDecl->getPropertyImplementation()
2613 == ObjCPropertyImplDecl::Dynamic)
2614 continue;
Douglas Gregord3297242013-01-16 23:00:23 +00002615
2616 for (ObjCInterfaceDecl::visible_extensions_iterator
2617 Ext = IDecl->visible_extensions_begin(),
2618 ExtEnd = IDecl->visible_extensions_end();
2619 Ext != ExtEnd; ++Ext) {
2620 if (ObjCMethodDecl *GetterMethod
2621 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002622 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002623 if (!Property->isReadOnly())
Douglas Gregord3297242013-01-16 23:00:23 +00002624 if (ObjCMethodDecl *SetterMethod
2625 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002626 SetterMethod->setPropertyAccessor(true);
Douglas Gregord3297242013-01-16 23:00:23 +00002627 }
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002628 }
2629 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002630 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002631 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002632 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002633
Patrick Beardb2f68202012-04-06 18:12:22 +00002634 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2635 if (IDecl->getSuperClass() == NULL) {
2636 // This class has no superclass, so check that it has been marked with
2637 // __attribute((objc_root_class)).
2638 if (!HasRootClassAttr) {
2639 SourceLocation DeclLoc(IDecl->getLocation());
2640 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2641 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2642 << IDecl->getIdentifier();
2643 // See if NSObject is in the current scope, and if it is, suggest
2644 // adding " : NSObject " to the class declaration.
2645 NamedDecl *IF = LookupSingleName(TUScope,
2646 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2647 DeclLoc, LookupOrdinaryName);
2648 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2649 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2650 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2651 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2652 } else {
2653 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2654 }
2655 }
2656 } else if (HasRootClassAttr) {
2657 // Complain that only root classes may have this attribute.
2658 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2659 }
2660
John McCall260611a2012-06-20 06:18:46 +00002661 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002662 while (IDecl->getSuperClass()) {
2663 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2664 IDecl = IDecl->getSuperClass();
2665 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002666 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002667 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002668 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002669 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002670 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002671 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002672
Chris Lattner4d391482007-12-12 07:09:47 +00002673 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002674 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002675 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregord3297242013-01-16 23:00:23 +00002676 if (ObjCCategoryDecl *Cat
2677 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2678 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattner4d391482007-12-12 07:09:47 +00002679 }
2680 }
2681 }
Chris Lattner682bf922009-03-29 16:50:03 +00002682 if (isInterfaceDeclKind) {
2683 // Reject invalid vardecls.
2684 for (unsigned i = 0; i != tuvNum; i++) {
2685 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2686 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2687 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002688 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002689 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002690 }
Chris Lattner682bf922009-03-29 16:50:03 +00002691 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002692 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002693 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002694
2695 for (unsigned i = 0; i != tuvNum; i++) {
2696 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002697 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2698 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002699 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2700 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002701
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002702 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002703 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002704}
2705
2706
2707/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2708/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002709static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002710CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002711 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002712}
2713
Ted Kremenek422bae72010-04-18 04:59:38 +00002714static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002715unsigned countAlignAttr(const AttrVec &A) {
2716 unsigned count=0;
2717 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2718 if ((*i)->getKind() == attr::Aligned)
2719 ++count;
2720 return count;
2721}
2722
2723static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002724bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2725 const AttrVec &A) {
2726 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002727 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002728 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002729 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002730
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002731 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002732 // But implementation has attributes. This is invalid.
2733 // Except when implementation has 'Align' attribute which is
2734 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002735 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002736 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002737
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002738 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002739
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002740 unsigned countAlignOnImpl = countAlignAttr(A);
2741 if (!countAlignOnImpl && (A.size() != D.size()))
2742 return true;
2743 else if (countAlignOnImpl) {
2744 unsigned countAlignOnDecl = countAlignAttr(D);
2745 if (countAlignOnDecl && (A.size() != D.size()))
2746 return true;
2747 else if (!countAlignOnDecl &&
2748 ((A.size()-countAlignOnImpl) != D.size()))
2749 return true;
2750 }
2751
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002752 // attributes on method declaration and definition must match exactly.
2753 // Note that we have at most a couple of attributes on methods, so this
2754 // n*n search is good enough.
2755 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002756 if ((*i)->getKind() == attr::Aligned)
2757 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002758 bool match = false;
2759 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2760 if ((*i)->getKind() == (*i1)->getKind()) {
2761 match = true;
2762 break;
2763 }
2764 }
2765 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002766 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002767 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002768
Sean Huntcf807c42010-08-18 23:23:40 +00002769 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002770}
2771
Douglas Gregor926df6c2011-06-11 01:09:30 +00002772/// \brief Check whether the declared result type of the given Objective-C
2773/// method declaration is compatible with the method's class.
2774///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002775static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002776CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2777 ObjCInterfaceDecl *CurrentClass) {
2778 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002779
2780 // If an Objective-C method inherits its related result type, then its
2781 // declared result type must be compatible with its own class type. The
2782 // declared result type is compatible if:
2783 if (const ObjCObjectPointerType *ResultObjectType
2784 = ResultType->getAs<ObjCObjectPointerType>()) {
2785 // - it is id or qualified id, or
2786 if (ResultObjectType->isObjCIdType() ||
2787 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002788 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002789
2790 if (CurrentClass) {
2791 if (ObjCInterfaceDecl *ResultClass
2792 = ResultObjectType->getInterfaceDecl()) {
2793 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002794 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002795 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002796
2797 // - it is a superclass of the method's class type
2798 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002799 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002800 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002801 } else {
2802 // Any Objective-C pointer type might be acceptable for a protocol
2803 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002804 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002805 }
2806 }
2807
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002808 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002809}
2810
John McCall6c2c2502011-07-22 02:45:48 +00002811namespace {
2812/// A helper class for searching for methods which a particular method
2813/// overrides.
2814class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002815public:
John McCall6c2c2502011-07-22 02:45:48 +00002816 Sema &S;
2817 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002818 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002819 bool Recursive;
2820
2821public:
2822 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2823 Selector selector = method->getSelector();
2824
2825 // Bypass this search if we've never seen an instance/class method
2826 // with this selector before.
2827 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2828 if (it == S.MethodPool.end()) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00002829 if (!S.getExternalSource()) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002830 S.ReadMethodPool(selector);
2831
2832 it = S.MethodPool.find(selector);
2833 if (it == S.MethodPool.end())
2834 return;
John McCall6c2c2502011-07-22 02:45:48 +00002835 }
2836 ObjCMethodList &list =
2837 method->isInstanceMethod() ? it->second.first : it->second.second;
2838 if (!list.Method) return;
2839
2840 ObjCContainerDecl *container
2841 = cast<ObjCContainerDecl>(method->getDeclContext());
2842
2843 // Prevent the search from reaching this container again. This is
2844 // important with categories, which override methods from the
2845 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002846 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2847 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002848 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2849 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002850 } else {
2851 searchFromContainer(container);
2852 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002853 }
John McCall6c2c2502011-07-22 02:45:48 +00002854
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002855 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002856 iterator begin() const { return Overridden.begin(); }
2857 iterator end() const { return Overridden.end(); }
2858
2859private:
2860 void searchFromContainer(ObjCContainerDecl *container) {
2861 if (container->isInvalidDecl()) return;
2862
2863 switch (container->getDeclKind()) {
2864#define OBJCCONTAINER(type, base) \
2865 case Decl::type: \
2866 searchFrom(cast<type##Decl>(container)); \
2867 break;
2868#define ABSTRACT_DECL(expansion)
2869#define DECL(type, base) \
2870 case Decl::type:
2871#include "clang/AST/DeclNodes.inc"
2872 llvm_unreachable("not an ObjC container!");
2873 }
2874 }
2875
2876 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002877 if (!protocol->hasDefinition())
2878 return;
2879
John McCall6c2c2502011-07-22 02:45:48 +00002880 // A method in a protocol declaration overrides declarations from
2881 // referenced ("parent") protocols.
2882 search(protocol->getReferencedProtocols());
2883 }
2884
2885 void searchFrom(ObjCCategoryDecl *category) {
2886 // A method in a category declaration overrides declarations from
2887 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002888 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002889 search(category->getReferencedProtocols());
2890 }
2891
2892 void searchFrom(ObjCCategoryImplDecl *impl) {
2893 // A method in a category definition that has a category
2894 // declaration overrides declarations from the category
2895 // declaration.
2896 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2897 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002898 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2899 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002900
2901 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002902 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2903 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002904 }
2905 }
2906
2907 void searchFrom(ObjCInterfaceDecl *iface) {
2908 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002909 if (!iface->hasDefinition())
2910 return;
2911
John McCall6c2c2502011-07-22 02:45:48 +00002912 // - categories,
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002913 for (ObjCInterfaceDecl::known_categories_iterator
2914 cat = iface->known_categories_begin(),
2915 catEnd = iface->known_categories_end();
Douglas Gregord3297242013-01-16 23:00:23 +00002916 cat != catEnd; ++cat) {
2917 search(*cat);
2918 }
John McCall6c2c2502011-07-22 02:45:48 +00002919
2920 // - the super class, and
2921 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2922 search(super);
2923
2924 // - any referenced protocols.
2925 search(iface->getReferencedProtocols());
2926 }
2927
2928 void searchFrom(ObjCImplementationDecl *impl) {
2929 // A method in a class implementation overrides declarations from
2930 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002931 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2932 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002933 }
2934
2935
2936 void search(const ObjCProtocolList &protocols) {
2937 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2938 i != e; ++i)
2939 search(*i);
2940 }
2941
2942 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002943 // Check for a method in this container which matches this selector.
2944 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002945 Method->isInstanceMethod(),
2946 /*AllowHidden=*/true);
John McCall6c2c2502011-07-22 02:45:48 +00002947
2948 // If we find one, record it and bail out.
2949 if (meth) {
2950 Overridden.insert(meth);
2951 return;
2952 }
2953
2954 // Otherwise, search for methods that a hypothetical method here
2955 // would have overridden.
2956
2957 // Note that we're now in a recursive case.
2958 Recursive = true;
2959
2960 searchFromContainer(container);
2961 }
2962};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002963}
2964
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002965void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2966 ObjCInterfaceDecl *CurrentClass,
2967 ResultTypeCompatibilityKind RTC) {
2968 // Search for overridden methods and merge information down from them.
2969 OverrideSearch overrides(*this, ObjCMethod);
2970 // Keep track if the method overrides any method in the class's base classes,
2971 // its protocols, or its categories' protocols; we will keep that info
2972 // in the ObjCMethodDecl.
2973 // For this info, a method in an implementation is not considered as
2974 // overriding the same method in the interface or its categories.
2975 bool hasOverriddenMethodsInBaseOrProtocol = false;
2976 for (OverrideSearch::iterator
2977 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2978 ObjCMethodDecl *overridden = *i;
2979
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00002980 if (!hasOverriddenMethodsInBaseOrProtocol) {
2981 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2982 CurrentClass != overridden->getClassInterface() ||
2983 overridden->isOverriding()) {
2984 hasOverriddenMethodsInBaseOrProtocol = true;
2985
2986 } else if (isa<ObjCImplDecl>(ObjCMethod->getDeclContext())) {
2987 // OverrideSearch will return as "overridden" the same method in the
2988 // interface. For hasOverriddenMethodsInBaseOrProtocol, we need to
2989 // check whether a category of a base class introduced a method with the
2990 // same selector, after the interface method declaration.
2991 // To avoid unnecessary lookups in the majority of cases, we use the
2992 // extra info bits in GlobalMethodPool to check whether there were any
2993 // category methods with this selector.
2994 GlobalMethodPool::iterator It =
2995 MethodPool.find(ObjCMethod->getSelector());
2996 if (It != MethodPool.end()) {
2997 ObjCMethodList &List =
2998 ObjCMethod->isInstanceMethod()? It->second.first: It->second.second;
2999 unsigned CategCount = List.getBits();
3000 if (CategCount > 0) {
3001 // If the method is in a category we'll do lookup if there were at
3002 // least 2 category methods recorded, otherwise only one will do.
3003 if (CategCount > 1 ||
3004 !isa<ObjCCategoryImplDecl>(overridden->getDeclContext())) {
3005 OverrideSearch overrides(*this, overridden);
3006 for (OverrideSearch::iterator
3007 OI= overrides.begin(), OE= overrides.end(); OI!=OE; ++OI) {
3008 ObjCMethodDecl *SuperOverridden = *OI;
Argyrios Kyrtzidisab3d5092013-04-27 00:10:12 +00003009 if (isa<ObjCProtocolDecl>(SuperOverridden->getDeclContext()) ||
3010 CurrentClass != SuperOverridden->getClassInterface()) {
Argyrios Kyrtzidise7a77722013-04-17 00:09:08 +00003011 hasOverriddenMethodsInBaseOrProtocol = true;
3012 overridden->setOverriding(true);
3013 break;
3014 }
3015 }
3016 }
3017 }
3018 }
3019 }
3020 }
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003021
3022 // Propagate down the 'related result type' bit from overridden methods.
3023 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
3024 ObjCMethod->SetRelatedResultType();
3025
3026 // Then merge the declarations.
3027 mergeObjCMethodDecls(ObjCMethod, overridden);
3028
3029 if (ObjCMethod->isImplicit() && overridden->isImplicit())
3030 continue; // Conflicting properties are detected elsewhere.
3031
3032 // Check for overriding methods
3033 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
3034 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
3035 CheckConflictingOverridingMethod(ObjCMethod, overridden,
3036 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
3037
3038 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00003039 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
3040 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003041 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
3042 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00003043 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
3044 PrevE = overridden->param_end();
3045 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003046 assert(PrevI != overridden->param_end() && "Param mismatch");
3047 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
3048 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
3049 // If type of argument of method in this class does not match its
3050 // respective argument type in the super class method, issue warning;
3051 if (!Context.typesAreCompatible(T1, T2)) {
3052 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
3053 << T1 << T2;
3054 Diag(overridden->getLocation(), diag::note_previous_declaration);
3055 break;
3056 }
3057 }
3058 }
3059 }
3060
3061 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
3062}
3063
John McCalld226f652010-08-21 09:40:31 +00003064Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003065 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00003066 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003067 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00003068 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003069 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00003070 Selector Sel,
3071 // optional arguments. The number of types/arguments is obtained
3072 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00003073 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003074 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00003075 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00003076 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00003077 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003078 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00003079 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00003080 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00003081 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003082 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
3083 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00003084 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00003085
Douglas Gregore97179c2011-09-08 01:46:34 +00003086 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003087 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00003088 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003089 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00003090
Eli Friedmanddb5a392013-06-14 21:14:10 +00003091 if (CheckFunctionReturnType(resultDeclType, MethodLoc))
John McCalld226f652010-08-21 09:40:31 +00003092 return 0;
Eli Friedmanddb5a392013-06-14 21:14:10 +00003093
Douglas Gregore97179c2011-09-08 01:46:34 +00003094 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00003095 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003096 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00003097 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003098 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00003099 }
Mike Stump1eb44332009-09-09 15:08:12 +00003100
3101 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003102 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00003103 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00003104 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003105 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00003106 MethodType == tok::minus, isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +00003107 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00003108 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00003109 MethodDeclKind == tok::objc_optional
3110 ? ObjCMethodDecl::Optional
3111 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00003112 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00003113
Chris Lattner5f9e2722011-07-23 10:55:15 +00003114 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00003115
Chris Lattner7db638d2009-04-11 19:42:43 +00003116 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00003117 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00003118 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00003119
David Blaikie7247c882013-05-15 07:37:26 +00003120 if (!ArgInfo[i].Type) {
John McCall58e46772009-10-23 21:48:59 +00003121 ArgType = Context.getObjCIdType();
3122 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00003123 } else {
John McCall58e46772009-10-23 21:48:59 +00003124 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnere294d3f2009-04-11 18:57:04 +00003125 }
Mike Stump1eb44332009-09-09 15:08:12 +00003126
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003127 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
3128 LookupOrdinaryName, ForRedeclaration);
3129 LookupName(R, S);
3130 if (R.isSingleResult()) {
3131 NamedDecl *PrevDecl = R.getFoundDecl();
3132 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00003133 Diag(ArgInfo[i].NameLoc,
3134 (MethodDefinition ? diag::warn_method_param_redefinition
3135 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003136 << ArgInfo[i].Name;
3137 Diag(PrevDecl->getLocation(),
3138 diag::note_previous_declaration);
3139 }
3140 }
3141
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003142 SourceLocation StartLoc = DI
3143 ? DI->getTypeLoc().getBeginLoc()
3144 : ArgInfo[i].NameLoc;
3145
John McCall81ef3e62011-04-23 02:46:06 +00003146 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
3147 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003148 ArgType, DI, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00003149
John McCall70798862011-05-02 00:30:12 +00003150 Param->setObjCMethodScopeInfo(i);
3151
Chris Lattner0ed844b2008-04-04 06:12:32 +00003152 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00003153 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00003154
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00003155 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003156 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00003157
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00003158 if (Param->hasAttr<BlocksAttr>()) {
3159 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
3160 Param->setInvalidDecl();
3161 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003162 S->AddDecl(Param);
3163 IdResolver.AddDecl(Param);
3164
Chris Lattner0ed844b2008-04-04 06:12:32 +00003165 Params.push_back(Param);
3166 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00003167
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003168 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00003169 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003170 QualType ArgType = Param->getType();
3171 if (ArgType.isNull())
3172 ArgType = Context.getObjCIdType();
3173 else
3174 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00003175 ArgType = Context.getAdjustedParameterType(ArgType);
Eli Friedmanddb5a392013-06-14 21:14:10 +00003176
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003177 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00003178 Params.push_back(Param);
3179 }
3180
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00003181 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003182 ObjCMethod->setObjCDeclQualifier(
3183 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00003184
3185 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00003186 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00003187
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003188 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00003189 const ObjCMethodDecl *PrevMethod = 0;
3190 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00003191 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003192 PrevMethod = ImpDecl->getInstanceMethod(Sel);
3193 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003194 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003195 PrevMethod = ImpDecl->getClassMethod(Sel);
3196 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003197 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00003198
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00003199 ObjCMethodDecl *IMD = 0;
3200 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3201 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3202 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00003203 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00003204 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00003205 SourceLocation MethodLoc = IMD->getLocation();
3206 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
3207 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00003208 Diag(MethodLoc, diag::note_method_declared_at)
3209 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00003210 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00003211 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003212 } else {
3213 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003214 }
John McCall6c2c2502011-07-22 02:45:48 +00003215
Chris Lattner4d391482007-12-12 07:09:47 +00003216 if (PrevMethod) {
3217 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00003218 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00003219 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00003220 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Fariborz Jahanianfbff0c42013-05-13 17:27:00 +00003221 ObjCMethod->setInvalidDecl();
3222 return ObjCMethod;
Mike Stump1eb44332009-09-09 15:08:12 +00003223 }
John McCall54abf7d2009-11-04 02:18:39 +00003224
Douglas Gregor926df6c2011-06-11 01:09:30 +00003225 // If this Objective-C method does not have a related result type, but we
3226 // are allowed to infer related result types, try to do so based on the
3227 // method family.
3228 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3229 if (!CurrentClass) {
3230 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3231 CurrentClass = Cat->getClassInterface();
3232 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3233 CurrentClass = Impl->getClassInterface();
3234 else if (ObjCCategoryImplDecl *CatImpl
3235 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3236 CurrentClass = CatImpl->getClassInterface();
3237 }
John McCall6c2c2502011-07-22 02:45:48 +00003238
Douglas Gregore97179c2011-09-08 01:46:34 +00003239 ResultTypeCompatibilityKind RTC
3240 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00003241
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003242 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00003243
John McCallf85e1932011-06-15 23:02:42 +00003244 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00003245 if (getLangOpts().ObjCAutoRefCount)
John McCallb8463812013-04-04 01:38:37 +00003246 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCallf85e1932011-06-15 23:02:42 +00003247
Douglas Gregore97179c2011-09-08 01:46:34 +00003248 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003249 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00003250 !ObjCMethod->hasRelatedResultType() &&
3251 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00003252 bool InferRelatedResultType = false;
3253 switch (ObjCMethod->getMethodFamily()) {
3254 case OMF_None:
3255 case OMF_copy:
3256 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00003257 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003258 case OMF_mutableCopy:
3259 case OMF_release:
3260 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00003261 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003262 break;
3263
3264 case OMF_alloc:
3265 case OMF_new:
3266 InferRelatedResultType = ObjCMethod->isClassMethod();
3267 break;
3268
3269 case OMF_init:
3270 case OMF_autorelease:
3271 case OMF_retain:
3272 case OMF_self:
3273 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3274 break;
3275 }
3276
John McCall6c2c2502011-07-22 02:45:48 +00003277 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003278 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003279 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003280
3281 ActOnDocumentableDecl(ObjCMethod);
3282
John McCalld226f652010-08-21 09:40:31 +00003283 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003284}
3285
Chris Lattnercc98eac2008-12-17 07:13:27 +00003286bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003287 // Following is also an error. But it is caused by a missing @end
3288 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003289 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003290 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003291
3292 // If we switched context to translation unit while we are still lexically in
3293 // an objc container, it means the parser missed emitting an error.
3294 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3295 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003296
Anders Carlsson15281452008-11-04 16:57:32 +00003297 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3298 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003299
Anders Carlsson15281452008-11-04 16:57:32 +00003300 return true;
3301}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003302
James Dennett1dfbd922012-06-14 21:40:34 +00003303/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003304/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003305void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003306 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003307 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003308 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003309 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003310 if (!Class) {
3311 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3312 return;
3313 }
John McCall260611a2012-06-20 06:18:46 +00003314 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003315 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3316 return;
3317 }
Mike Stump1eb44332009-09-09 15:08:12 +00003318
Chris Lattnercc98eac2008-12-17 07:13:27 +00003319 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003320 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003321 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003322 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003323 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003324 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003325 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003326 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3327 /*FIXME: StartL=*/ID->getLocation(),
3328 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003329 ID->getIdentifier(), ID->getType(),
3330 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003331 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003332 }
Mike Stump1eb44332009-09-09 15:08:12 +00003333
Chris Lattnercc98eac2008-12-17 07:13:27 +00003334 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003335 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003336 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003337 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003338 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003339 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003340 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003341 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003342 }
3343}
3344
Douglas Gregor160b5632010-04-26 17:32:49 +00003345/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003346VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3347 SourceLocation StartLoc,
3348 SourceLocation IdLoc,
3349 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003350 bool Invalid) {
3351 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3352 // duration shall not be qualified by an address-space qualifier."
3353 // Since all parameters have automatic store duration, they can not have
3354 // an address space.
3355 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003356 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003357 Invalid = true;
3358 }
3359
3360 // An @catch parameter must be an unqualified object pointer type;
3361 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3362 if (Invalid) {
3363 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003364 } else if (T->isDependentType()) {
3365 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003366 } else if (!T->isObjCObjectPointerType()) {
3367 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003368 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003369 } else if (T->isObjCQualifiedIdType()) {
3370 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003371 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003372 }
3373
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003374 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003375 T, TInfo, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003376 New->setExceptionVariable(true);
3377
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003378 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003379 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003380 Invalid = true;
3381
Douglas Gregor160b5632010-04-26 17:32:49 +00003382 if (Invalid)
3383 New->setInvalidDecl();
3384 return New;
3385}
3386
John McCalld226f652010-08-21 09:40:31 +00003387Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003388 const DeclSpec &DS = D.getDeclSpec();
3389
3390 // We allow the "register" storage class on exception variables because
3391 // GCC did, but we drop it completely. Any other storage class is an error.
3392 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3393 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3394 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
Richard Smithec642442013-04-12 22:46:28 +00003395 } else if (DeclSpec::SCS SCS = DS.getStorageClassSpec()) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003396 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
Richard Smithec642442013-04-12 22:46:28 +00003397 << DeclSpec::getSpecifierName(SCS);
3398 }
3399 if (DeclSpec::TSCS TSCS = D.getDeclSpec().getThreadStorageClassSpec())
3400 Diag(D.getDeclSpec().getThreadStorageClassSpecLoc(),
3401 diag::err_invalid_thread)
3402 << DeclSpec::getSpecifierName(TSCS);
Douglas Gregor160b5632010-04-26 17:32:49 +00003403 D.getMutableDeclSpec().ClearStorageClassSpecs();
3404
Richard Smithc7f81162013-03-18 22:52:47 +00003405 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregor160b5632010-04-26 17:32:49 +00003406
3407 // Check that there are no default arguments inside the type of this
3408 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003409 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003410 CheckExtraCXXDefaultArguments(D);
3411
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003412 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003413 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003414
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003415 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3416 D.getSourceRange().getBegin(),
3417 D.getIdentifierLoc(),
3418 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003419 D.isInvalidType());
3420
3421 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3422 if (D.getCXXScopeSpec().isSet()) {
3423 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3424 << D.getCXXScopeSpec().getRange();
3425 New->setInvalidDecl();
3426 }
3427
3428 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003429 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003430 if (D.getIdentifier())
3431 IdResolver.AddDecl(New);
3432
3433 ProcessDeclAttributes(S, New, D);
3434
3435 if (New->hasAttr<BlocksAttr>())
3436 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003437 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003438}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003439
3440/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003441/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003442void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003443 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003444 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3445 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003446 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003447 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003448 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003449 }
3450}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003451
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003452void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003453 // Load referenced selectors from the external source.
3454 if (ExternalSource) {
3455 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3456 ExternalSource->ReadReferencedSelectors(Sels);
3457 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3458 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3459 }
3460
Fariborz Jahanianf98c6882013-05-30 21:48:58 +00003461 DiagnoseMismatchedMethodsInGlobalPool();
3462
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003463 // Warning will be issued only when selector table is
3464 // generated (which means there is at lease one implementation
3465 // in the TU). This is to match gcc's behavior.
3466 if (ReferencedSelectors.empty() ||
3467 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003468 return;
3469 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3470 ReferencedSelectors.begin(),
3471 E = ReferencedSelectors.end(); S != E; ++S) {
3472 Selector Sel = (*S).first;
3473 if (!LookupImplementedMethodInGlobalPool(Sel))
3474 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3475 }
3476 return;
3477}