blob: cb5de9c84a2265c7bb4d4df55026e45d592b86c3 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
John McCallf85e1932011-06-15 23:02:42 +000015#include "clang/AST/ASTConsumer.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/ASTMutationListener.h"
18#include "clang/AST/DeclObjC.h"
Steve Naroffca331292009-03-03 14:49:36 +000019#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000020#include "clang/AST/ExprObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/Basic/SourceManager.h"
Patrick Beardb2f68202012-04-06 18:12:22 +000022#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/ExternalSemaSource.h"
25#include "clang/Sema/Lookup.h"
26#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
John McCall50df6ae2010-08-25 07:03:20 +000028#include "llvm/ADT/DenseSet.h"
29
Chris Lattner4d391482007-12-12 07:09:47 +000030using namespace clang;
31
John McCallf85e1932011-06-15 23:02:42 +000032/// Check whether the given method, which must be in the 'init'
33/// family, is a valid member of that family.
34///
35/// \param receiverTypeIfCall - if null, check this as if declaring it;
36/// if non-null, check this as if making a call to it with the given
37/// receiver type
38///
39/// \return true to indicate that there was an error and appropriate
40/// actions were taken
41bool Sema::checkInitMethod(ObjCMethodDecl *method,
42 QualType receiverTypeIfCall) {
43 if (method->isInvalidDecl()) return true;
44
45 // This castAs is safe: methods that don't return an object
46 // pointer won't be inferred as inits and will reject an explicit
47 // objc_method_family(init).
48
49 // We ignore protocols here. Should we? What about Class?
50
51 const ObjCObjectType *result = method->getResultType()
52 ->castAs<ObjCObjectPointerType>()->getObjectType();
53
54 if (result->isObjCId()) {
55 return false;
56 } else if (result->isObjCClass()) {
57 // fall through: always an error
58 } else {
59 ObjCInterfaceDecl *resultClass = result->getInterface();
60 assert(resultClass && "unexpected object type!");
61
62 // It's okay for the result type to still be a forward declaration
63 // if we're checking an interface declaration.
Douglas Gregor7723fec2011-12-15 20:29:51 +000064 if (!resultClass->hasDefinition()) {
John McCallf85e1932011-06-15 23:02:42 +000065 if (receiverTypeIfCall.isNull() &&
66 !isa<ObjCImplementationDecl>(method->getDeclContext()))
67 return false;
68
69 // Otherwise, we try to compare class types.
70 } else {
71 // If this method was declared in a protocol, we can't check
72 // anything unless we have a receiver type that's an interface.
73 const ObjCInterfaceDecl *receiverClass = 0;
74 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
75 if (receiverTypeIfCall.isNull())
76 return false;
77
78 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
79 ->getInterfaceDecl();
80
81 // This can be null for calls to e.g. id<Foo>.
82 if (!receiverClass) return false;
83 } else {
84 receiverClass = method->getClassInterface();
85 assert(receiverClass && "method not associated with a class!");
86 }
87
88 // If either class is a subclass of the other, it's fine.
89 if (receiverClass->isSuperClassOf(resultClass) ||
90 resultClass->isSuperClassOf(receiverClass))
91 return false;
92 }
93 }
94
95 SourceLocation loc = method->getLocation();
96
97 // If we're in a system header, and this is not a call, just make
98 // the method unusable.
99 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
100 method->addAttr(new (Context) UnavailableAttr(loc, Context,
101 "init method returns a type unrelated to its receiver type"));
102 return true;
103 }
104
105 // Otherwise, it's an error.
106 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
107 method->setInvalidDecl();
108 return true;
109}
110
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000111void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregorf4d918f2013-01-15 22:43:08 +0000112 const ObjCMethodDecl *Overridden) {
Douglas Gregor926df6c2011-06-11 01:09:30 +0000113 if (Overridden->hasRelatedResultType() &&
114 !NewMethod->hasRelatedResultType()) {
115 // This can only happen when the method follows a naming convention that
116 // implies a related result type, and the original (overridden) method has
117 // a suitable return type, but the new (overriding) method does not have
118 // a suitable return type.
119 QualType ResultType = NewMethod->getResultType();
120 SourceRange ResultTypeRange;
121 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000122 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000123 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
124
125 // Figure out which class this method is part of, if any.
126 ObjCInterfaceDecl *CurrentClass
127 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
128 if (!CurrentClass) {
129 DeclContext *DC = NewMethod->getDeclContext();
130 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
131 CurrentClass = Cat->getClassInterface();
132 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
133 CurrentClass = Impl->getClassInterface();
134 else if (ObjCCategoryImplDecl *CatImpl
135 = dyn_cast<ObjCCategoryImplDecl>(DC))
136 CurrentClass = CatImpl->getClassInterface();
137 }
138
139 if (CurrentClass) {
140 Diag(NewMethod->getLocation(),
141 diag::warn_related_result_type_compatibility_class)
142 << Context.getObjCInterfaceType(CurrentClass)
143 << ResultType
144 << ResultTypeRange;
145 } else {
146 Diag(NewMethod->getLocation(),
147 diag::warn_related_result_type_compatibility_protocol)
148 << ResultType
149 << ResultTypeRange;
150 }
151
Douglas Gregore97179c2011-09-08 01:46:34 +0000152 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
153 Diag(Overridden->getLocation(),
John McCall7cca8212013-03-19 07:04:25 +0000154 diag::note_related_result_type_family)
155 << /*overridden method*/ 0
Douglas Gregore97179c2011-09-08 01:46:34 +0000156 << Family;
157 else
158 Diag(Overridden->getLocation(),
159 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000160 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000161 if (getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000162 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
163 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
164 Diag(NewMethod->getLocation(),
165 diag::err_nsreturns_retained_attribute_mismatch) << 1;
166 Diag(Overridden->getLocation(), diag::note_previous_decl)
167 << "method";
168 }
169 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
170 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
171 Diag(NewMethod->getLocation(),
172 diag::err_nsreturns_retained_attribute_mismatch) << 0;
173 Diag(Overridden->getLocation(), diag::note_previous_decl)
174 << "method";
175 }
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000176 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
177 oe = Overridden->param_end();
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000178 for (ObjCMethodDecl::param_iterator
179 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000180 ni != ne && oi != oe; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000181 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000182 ParmVarDecl *newDecl = (*ni);
183 if (newDecl->hasAttr<NSConsumedAttr>() !=
184 oldDecl->hasAttr<NSConsumedAttr>()) {
185 Diag(newDecl->getLocation(),
186 diag::err_nsconsumed_attribute_mismatch);
187 Diag(oldDecl->getLocation(), diag::note_previous_decl)
188 << "parameter";
189 }
190 }
191 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000192}
193
John McCallf85e1932011-06-15 23:02:42 +0000194/// \brief Check a method declaration for compatibility with the Objective-C
195/// ARC conventions.
John McCallb8463812013-04-04 01:38:37 +0000196bool Sema::CheckARCMethodDecl(ObjCMethodDecl *method) {
John McCallf85e1932011-06-15 23:02:42 +0000197 ObjCMethodFamily family = method->getMethodFamily();
198 switch (family) {
199 case OMF_None:
Nico Weber80cb6e62011-08-28 22:35:17 +0000200 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000201 case OMF_retain:
202 case OMF_release:
203 case OMF_autorelease:
204 case OMF_retainCount:
205 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000206 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000207 return false;
208
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000209 case OMF_dealloc:
John McCallb8463812013-04-04 01:38:37 +0000210 if (!Context.hasSameType(method->getResultType(), Context.VoidTy)) {
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000211 SourceRange ResultTypeRange;
212 if (const TypeSourceInfo *ResultTypeInfo
213 = method->getResultTypeSourceInfo())
214 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
215 if (ResultTypeRange.isInvalid())
John McCallb8463812013-04-04 01:38:37 +0000216 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000217 << method->getResultType()
218 << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
219 else
John McCallb8463812013-04-04 01:38:37 +0000220 Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000221 << method->getResultType()
222 << FixItHint::CreateReplacement(ResultTypeRange, "void");
223 return true;
224 }
225 return false;
226
John McCallf85e1932011-06-15 23:02:42 +0000227 case OMF_init:
228 // If the method doesn't obey the init rules, don't bother annotating it.
John McCallb8463812013-04-04 01:38:37 +0000229 if (checkInitMethod(method, QualType()))
John McCallf85e1932011-06-15 23:02:42 +0000230 return true;
231
John McCallb8463812013-04-04 01:38:37 +0000232 method->addAttr(new (Context) NSConsumesSelfAttr(SourceLocation(),
233 Context));
John McCallf85e1932011-06-15 23:02:42 +0000234
235 // Don't add a second copy of this attribute, but otherwise don't
236 // let it be suppressed.
237 if (method->hasAttr<NSReturnsRetainedAttr>())
238 return false;
239 break;
240
241 case OMF_alloc:
242 case OMF_copy:
243 case OMF_mutableCopy:
244 case OMF_new:
245 if (method->hasAttr<NSReturnsRetainedAttr>() ||
246 method->hasAttr<NSReturnsNotRetainedAttr>() ||
247 method->hasAttr<NSReturnsAutoreleasedAttr>())
248 return false;
249 break;
250 }
251
John McCallb8463812013-04-04 01:38:37 +0000252 method->addAttr(new (Context) NSReturnsRetainedAttr(SourceLocation(),
253 Context));
John McCallf85e1932011-06-15 23:02:42 +0000254 return false;
255}
256
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000257static void DiagnoseObjCImplementedDeprecations(Sema &S,
258 NamedDecl *ND,
259 SourceLocation ImplLoc,
260 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000261 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000262 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000263 if (select == 0)
Ted Kremenek3306ec12012-02-27 22:55:11 +0000264 S.Diag(ND->getLocation(), diag::note_method_declared_at)
265 << ND->getDeclName();
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000266 else
267 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
268 }
269}
270
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000271/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
272/// pool.
273void Sema::AddAnyMethodToGlobalPool(Decl *D) {
274 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
275
276 // If we don't have a valid method decl, simply return.
277 if (!MDecl)
278 return;
279 if (MDecl->isInstanceMethod())
280 AddInstanceMethodToGlobalPool(MDecl, true);
281 else
282 AddFactoryMethodToGlobalPool(MDecl, true);
283}
284
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000285/// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer
286/// has explicit ownership attribute; false otherwise.
287static bool
288HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) {
289 QualType T = Param->getType();
290
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000291 if (const PointerType *PT = T->getAs<PointerType>()) {
292 T = PT->getPointeeType();
293 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
294 T = RT->getPointeeType();
295 } else {
296 return true;
297 }
298
299 // If we have a lifetime qualifier, but it's local, we must have
300 // inferred it. So, it is implicit.
301 return !T.getLocalQualifiers().hasObjCLifetime();
302}
303
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +0000304/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
305/// and user declared, in the method definition's AST.
306void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
307 assert((getCurMethodDecl() == 0) && "Methodparsing confused");
John McCalld226f652010-08-21 09:40:31 +0000308 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000309
Steve Naroff394f3f42008-07-25 17:57:26 +0000310 // If we don't have a valid method decl, simply return.
311 if (!MDecl)
312 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000313
Chris Lattner4d391482007-12-12 07:09:47 +0000314 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000315 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000316 PushFunctionScope();
317
Chris Lattner4d391482007-12-12 07:09:47 +0000318 // Create Decl objects for each parameter, entrring them in the scope for
319 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000320
321 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000322 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Daniel Dunbar451318c2008-08-26 06:07:48 +0000324 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
325 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000326
Chris Lattner8123a952008-04-10 02:22:51 +0000327 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000328 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000329 E = MDecl->param_end(); PI != E; ++PI) {
330 ParmVarDecl *Param = (*PI);
331 if (!Param->isInvalidDecl() &&
332 RequireCompleteType(Param->getLocation(), Param->getType(),
333 diag::err_typecheck_decl_incomplete_type))
334 Param->setInvalidDecl();
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000335 if (!Param->isInvalidDecl() &&
336 getLangOpts().ObjCAutoRefCount &&
337 !HasExplicitOwnershipAttr(*this, Param))
338 Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
339 Param->getType();
Fariborz Jahanian918546c2012-08-30 23:56:02 +0000340
Chris Lattner89951a82009-02-20 18:43:26 +0000341 if ((*PI)->getIdentifier())
342 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000343 }
John McCallf85e1932011-06-15 23:02:42 +0000344
345 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000346 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000347 switch (MDecl->getMethodFamily()) {
348 case OMF_retain:
349 case OMF_retainCount:
350 case OMF_release:
351 case OMF_autorelease:
352 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
353 << MDecl->getSelector();
354 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);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000462 ObjCInterfaceDecl *IDecl
463 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000464 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000465
Douglas Gregor7723fec2011-12-15 20:29:51 +0000466 if (PrevIDecl) {
467 // Class already seen. Was it a definition?
468 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
469 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
470 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000471 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000472 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000473 }
Chris Lattner4d391482007-12-12 07:09:47 +0000474 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000475
476 if (AttrList)
477 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
478 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregor7723fec2011-12-15 20:29:51 +0000480 // Start the definition of this class. If we're in a redefinition case, there
481 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000482 if (!IDecl->hasDefinition())
483 IDecl->startDefinition();
484
Chris Lattner4d391482007-12-12 07:09:47 +0000485 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000486 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000487 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
488 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000489
490 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000491 // Try to correct for a typo in the superclass name without correcting
492 // to the class we're defining.
493 ObjCInterfaceValidatorCCC Validator(IDecl);
494 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000495 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000496 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000497 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
498 Diag(SuperLoc, diag::err_undef_superclass_suggest)
499 << SuperName << ClassName << PrevDecl->getDeclName();
500 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
501 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000502 }
503 }
504
Douglas Gregor60ef3082011-12-15 00:29:59 +0000505 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000506 Diag(SuperLoc, diag::err_recursive_superclass)
507 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000508 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000509 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000510 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000511 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000512
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000513 // Diagnose classes that inherit from deprecated classes.
514 if (SuperClassDecl)
515 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000517 if (PrevDecl && SuperClassDecl == 0) {
518 // The previous declaration was not a class decl. Check if we have a
519 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000520 if (const TypedefNameDecl *TDecl =
521 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000522 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000523 if (T->isObjCObjectType()) {
Fariborz Jahanian740991b2013-04-04 18:45:52 +0000524 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000525 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanian740991b2013-04-04 18:45:52 +0000526 // This handles the following case:
527 // @interface NewI @end
528 // typedef NewI DeprI __attribute__((deprecated("blah")))
529 // @interface SI : DeprI /* warn here */ @end
530 (void)DiagnoseUseOfDecl(const_cast<TypedefNameDecl*>(TDecl), SuperLoc);
531 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000532 }
533 }
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000535 // This handles the following case:
536 //
537 // typedef int SuperClass;
538 // @interface MyClass : SuperClass {} @end
539 //
540 if (!SuperClassDecl) {
541 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
542 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000543 }
544 }
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Richard Smith162e1c12011-04-15 14:24:37 +0000546 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000547 if (!SuperClassDecl)
548 Diag(SuperLoc, diag::err_undef_superclass)
549 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000550 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000551 Context.getObjCInterfaceType(SuperClassDecl),
552 diag::err_forward_superclass,
553 SuperClassDecl->getDeclName(),
554 ClassName,
555 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000556 SuperClassDecl = 0;
557 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000558 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000559 IDecl->setSuperClass(SuperClassDecl);
560 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000561 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000562 }
Chris Lattner4d391482007-12-12 07:09:47 +0000563 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000564 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Sebastian Redl0b17c612010-08-13 00:28:03 +0000567 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000568 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000569 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000570 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000571 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Anders Carlsson15281452008-11-04 16:57:32 +0000574 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000575 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000576}
577
Richard Smithde01b7a2012-08-08 23:32:13 +0000578/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000579/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000580Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
581 IdentifierInfo *AliasName,
582 SourceLocation AliasLocation,
583 IdentifierInfo *ClassName,
584 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000585 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000586 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000587 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000588 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000589 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000590 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000591 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000592 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000593 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000594 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000595 }
596 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000597 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000598 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000599 if (const TypedefNameDecl *TDecl =
600 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000601 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000602 if (T->isObjCObjectType()) {
603 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000604 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000605 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000606 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000607 }
608 }
609 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000610 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
611 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000612 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000613 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000614 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000615 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000616 }
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000618 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000619 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000620 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Anders Carlsson15281452008-11-04 16:57:32 +0000622 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000623 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000624
John McCalld226f652010-08-21 09:40:31 +0000625 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000626}
627
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000628bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000629 IdentifierInfo *PName,
630 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000631 const ObjCList<ObjCProtocolDecl> &PList) {
632
633 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000634 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
635 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000636 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
637 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000638 if (PDecl->getIdentifier() == PName) {
639 Diag(Ploc, diag::err_protocol_has_circular_dependency);
640 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000641 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000642 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000643
644 if (!PDecl->hasDefinition())
645 continue;
646
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000647 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
648 PDecl->getLocation(), PDecl->getReferencedProtocols()))
649 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000650 }
651 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000652 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000653}
654
John McCalld226f652010-08-21 09:40:31 +0000655Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000656Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
657 IdentifierInfo *ProtocolName,
658 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000659 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000660 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000661 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000662 SourceLocation EndProtoLoc,
663 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000664 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000665 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000666 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000667 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
668 ForRedeclaration);
669 ObjCProtocolDecl *PDecl = 0;
670 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
671 // If we already have a definition, complain.
672 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
673 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Douglas Gregor27c6da22012-01-01 20:30:41 +0000675 // Create a new protocol that is completely distinct from previous
676 // declarations, and do not make this protocol available for name lookup.
677 // That way, we'll end up completely ignoring the duplicate.
678 // FIXME: Can we turn this into an error?
679 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
680 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000681 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000682 PDecl->startDefinition();
683 } else {
684 if (PrevDecl) {
685 // Check for circular dependencies among protocol declarations. This can
686 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000687 ObjCList<ObjCProtocolDecl> PList;
688 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
689 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000690 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000691 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000692
693 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000694 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000695 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000696 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000697
Douglas Gregor6e378de2009-04-23 23:18:26 +0000698 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000699 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000700 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000701
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000702 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000703 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000704
705 // Merge attributes from previous declarations.
706 if (PrevDecl)
707 mergeDeclAttributes(PDecl, PrevDecl);
708
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000709 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000710 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000711 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000712 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000713 }
Mike Stump1eb44332009-09-09 15:08:12 +0000714
715 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000716 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000717}
718
719/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000720/// issues an error if they are not declared. It returns list of
721/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000722void
Chris Lattnere13b9592008-07-26 04:03:38 +0000723Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000724 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000725 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000726 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000727 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000728 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
729 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000730 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000731 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000732 TypoCorrection Corrected = CorrectTypo(
733 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000734 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000735 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000736 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000737 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000738 Diag(PDecl->getLocation(), diag::note_previous_decl)
739 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000740 }
741 }
742
743 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000744 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000745 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000746 continue;
747 }
Fariborz Jahanian3c9a0242013-04-09 17:52:29 +0000748 // If this is a forward protocol declaration, get its definition.
749 if (!PDecl->isThisDeclarationADefinition() && PDecl->getDefinition())
750 PDecl = PDecl->getDefinition();
751
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000752 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000753
754 // If this is a forward declaration and we are supposed to warn in this
755 // case, do it.
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000756 // FIXME: Recover nicely in the hidden case.
757 if (WarnOnDeclarations &&
758 (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000759 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000760 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000761 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000762 }
763}
764
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000765/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000766/// a class method in its extension.
767///
Mike Stump1eb44332009-09-09 15:08:12 +0000768void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000769 ObjCInterfaceDecl *ID) {
770 if (!ID)
771 return; // Possibly due to previous error
772
773 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000774 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
775 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000776 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000777 MethodMap[MD->getSelector()] = MD;
778 }
779
780 if (MethodMap.empty())
781 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000782 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
783 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000784 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000785 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
786 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
787 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
788 << Method->getDeclName();
789 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
790 }
791 }
792}
793
James Dennett1dfbd922012-06-14 21:40:34 +0000794/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000795Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000796Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000797 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000798 unsigned NumElts,
799 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000800 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000801 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000802 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000803 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
804 ForRedeclaration);
805 ObjCProtocolDecl *PDecl
806 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
807 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000808 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000809
810 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000811 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000812
Douglas Gregor3937f872012-01-01 20:33:24 +0000813 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000814 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000815
816 if (PrevDecl)
817 mergeDeclAttributes(PDecl, PrevDecl);
818
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000819 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000822 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000823}
824
John McCalld226f652010-08-21 09:40:31 +0000825Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000826ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
827 IdentifierInfo *ClassName, SourceLocation ClassLoc,
828 IdentifierInfo *CategoryName,
829 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000830 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000831 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000832 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000833 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000834 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000835 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000836
837 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000838
839 if (!IDecl
840 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000841 diag::err_category_forward_interface,
842 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000843 // Create an invalid ObjCCategoryDecl to serve as context for
844 // the enclosing method declarations. We mark the decl invalid
845 // to make it clear that this isn't a valid AST.
846 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000847 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000848 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000849 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000850
851 if (!IDecl)
852 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000853 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000854 }
855
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000856 if (!CategoryName && IDecl->getImplementation()) {
857 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
858 Diag(IDecl->getImplementation()->getLocation(),
859 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000860 }
861
Fariborz Jahanian25760612010-02-15 21:55:26 +0000862 if (CategoryName) {
863 /// Check for duplicate interface declaration for this category
Douglas Gregord3297242013-01-16 23:00:23 +0000864 if (ObjCCategoryDecl *Previous
865 = IDecl->FindCategoryDeclaration(CategoryName)) {
866 // Class extensions can be declared multiple times, categories cannot.
867 Diag(CategoryLoc, diag::warn_dup_category_def)
868 << ClassName << CategoryName;
869 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner70f19542009-02-16 21:26:43 +0000870 }
871 }
Chris Lattner70f19542009-02-16 21:26:43 +0000872
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000873 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
874 ClassLoc, CategoryLoc, CategoryName, IDecl);
875 // FIXME: PushOnScopeChains?
876 CurContext->addDecl(CDecl);
877
Chris Lattner4d391482007-12-12 07:09:47 +0000878 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000879 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000880 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000881 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000882 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000883 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000884 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000885 }
Mike Stump1eb44332009-09-09 15:08:12 +0000886
Anders Carlsson15281452008-11-04 16:57:32 +0000887 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000888 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000889}
890
891/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000892/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000893/// object.
John McCalld226f652010-08-21 09:40:31 +0000894Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000895 SourceLocation AtCatImplLoc,
896 IdentifierInfo *ClassName, SourceLocation ClassLoc,
897 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000898 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000899 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000900 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000901 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
902 if (!CatIDecl) {
903 // Category @implementation with no corresponding @interface.
904 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000905 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
906 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000907 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000908 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000909 }
910 }
911
Mike Stump1eb44332009-09-09 15:08:12 +0000912 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000913 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000914 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000915 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000916 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000917 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000918 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000919 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
920 diag::err_undef_interface)) {
921 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000922 }
Chris Lattner4d391482007-12-12 07:09:47 +0000923
Douglas Gregord0434102009-01-09 00:49:46 +0000924 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000925 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000926
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000927 // If the interface is deprecated/unavailable, warn/error about it.
928 if (IDecl)
929 DiagnoseUseOfDecl(IDecl, ClassLoc);
930
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000931 /// Check that CatName, category name, is not used in another implementation.
932 if (CatIDecl) {
933 if (CatIDecl->getImplementation()) {
934 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
935 << CatName;
936 Diag(CatIDecl->getImplementation()->getLocation(),
937 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000938 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000939 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000940 // Warn on implementating category of deprecated class under
941 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000942 DiagnoseObjCImplementedDeprecations(*this,
943 dyn_cast<NamedDecl>(IDecl),
944 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000945 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000946 }
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Anders Carlsson15281452008-11-04 16:57:32 +0000948 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000949 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000950}
951
John McCalld226f652010-08-21 09:40:31 +0000952Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000953 SourceLocation AtClassImplLoc,
954 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000955 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000956 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000957 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000958 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000959 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000960 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
961 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000962 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000963 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000964 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000965 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000966 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
967 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000968 } else {
969 // We did not find anything with the name ClassName; try to correct for
970 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000971 ObjCInterfaceValidatorCCC Validator;
972 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000973 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000974 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000975 // Suggest the (potentially) correct interface name. However, put the
976 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000977 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000978 // provide a code-modification hint or use the typo name for recovery,
979 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000980 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000981 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000982 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000983 << ClassName << CorrectedName;
984 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
985 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000986 IDecl = 0;
987 } else {
988 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
989 }
Chris Lattner4d391482007-12-12 07:09:47 +0000990 }
Mike Stump1eb44332009-09-09 15:08:12 +0000991
Chris Lattner4d391482007-12-12 07:09:47 +0000992 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000993 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000994 if (SuperClassname) {
995 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000996 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
997 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000998 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000999 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
1000 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +00001001 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +00001002 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001003 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +00001004 if (SDecl && !SDecl->hasDefinition())
1005 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +00001006 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +00001007 Diag(SuperClassLoc, diag::err_undef_superclass)
1008 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +00001009 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001010 // This implementation and its interface do not have the same
1011 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +00001012 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +00001013 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001014 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001015 }
1016 }
1017 }
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Chris Lattner4d391482007-12-12 07:09:47 +00001019 if (!IDecl) {
1020 // Legacy case of @implementation with no corresponding @interface.
1021 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +00001022
Mike Stump390b4cc2009-05-16 07:39:55 +00001023 // FIXME: Do we support attributes on the @implementation? If so we should
1024 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001025 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001026 ClassName, /*PrevDecl=*/0, ClassLoc,
1027 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001028 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001029 if (SDecl) {
1030 IDecl->setSuperClass(SDecl);
1031 IDecl->setSuperClassLoc(SuperClassLoc);
1032 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1033 } else {
1034 IDecl->setEndOfDefinitionLoc(ClassLoc);
1035 }
1036
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001037 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001038 } else {
1039 // Mark the interface as being completed, even if it was just as
1040 // @class ....;
1041 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001042 if (!IDecl->hasDefinition())
1043 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001044 }
Mike Stump1eb44332009-09-09 15:08:12 +00001045
1046 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001047 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1048 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Anders Carlsson15281452008-11-04 16:57:32 +00001050 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001051 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Chris Lattner4d391482007-12-12 07:09:47 +00001053 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001054 if (IDecl->getImplementation()) {
1055 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001056 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001057 Diag(IDecl->getImplementation()->getLocation(),
1058 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001059 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001060 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001061 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001062 // Warn on implementating deprecated class under
1063 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001064 DiagnoseObjCImplementedDeprecations(*this,
1065 dyn_cast<NamedDecl>(IDecl),
1066 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001067 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001068 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001069}
1070
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001071Sema::DeclGroupPtrTy
1072Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1073 SmallVector<Decl *, 64> DeclsInGroup;
1074 DeclsInGroup.reserve(Decls.size() + 1);
1075
1076 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1077 Decl *Dcl = Decls[i];
1078 if (!Dcl)
1079 continue;
1080 if (Dcl->getDeclContext()->isFileContext())
1081 Dcl->setTopLevelDeclInObjCContainer();
1082 DeclsInGroup.push_back(Dcl);
1083 }
1084
1085 DeclsInGroup.push_back(ObjCImpDecl);
1086
1087 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1088}
1089
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001090void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1091 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001092 SourceLocation RBrace) {
1093 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001094 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001095 if (!IDecl)
1096 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001097 /// Check case of non-existing \@interface decl.
1098 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001099 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001100 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001101 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001102 // Add ivar's to class's DeclContext.
1103 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001104 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001105 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001106 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001107 }
1108
Chris Lattner4d391482007-12-12 07:09:47 +00001109 return;
1110 }
1111 // If implementation has empty ivar list, just return.
1112 if (numIvars == 0)
1113 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Chris Lattner4d391482007-12-12 07:09:47 +00001115 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001116 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001117 if (ImpDecl->getSuperClass())
1118 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1119 for (unsigned i = 0; i < numIvars; i++) {
1120 ObjCIvarDecl* ImplIvar = ivars[i];
1121 if (const ObjCIvarDecl *ClsIvar =
1122 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1123 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1124 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1125 continue;
1126 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001127 // Instance ivar to Implementation's DeclContext.
1128 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001129 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001130 ImpDecl->addDecl(ImplIvar);
1131 }
1132 return;
1133 }
Chris Lattner4d391482007-12-12 07:09:47 +00001134 // Check interface's Ivar list against those in the implementation.
1135 // names and types must match.
1136 //
Chris Lattner4d391482007-12-12 07:09:47 +00001137 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001138 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001139 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1140 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001141 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001142 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001143 assert (ImplIvar && "missing implementation ivar");
1144 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Steve Naroffca331292009-03-03 14:49:36 +00001146 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001147 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001148 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001149 << ImplIvar->getIdentifier()
1150 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001151 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001152 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1153 ImplIvar->getBitWidthValue(Context) !=
1154 ClsIvar->getBitWidthValue(Context)) {
1155 Diag(ImplIvar->getBitWidth()->getLocStart(),
1156 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1157 Diag(ClsIvar->getBitWidth()->getLocStart(),
1158 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001159 }
Steve Naroffca331292009-03-03 14:49:36 +00001160 // Make sure the names are identical.
1161 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001162 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001163 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001164 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001165 }
1166 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001167 }
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Chris Lattner609e4c72007-12-12 18:11:49 +00001169 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001170 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001171 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001172 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001173}
1174
Steve Naroff3c2eb662008-02-10 21:38:56 +00001175void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001176 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001177 // No point warning no definition of method which is 'unavailable'.
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001178 switch (method->getAvailability()) {
1179 case AR_Available:
1180 case AR_Deprecated:
1181 break;
1182
1183 // Don't warn about unavailable or not-yet-introduced methods.
1184 case AR_NotYetIntroduced:
1185 case AR_Unavailable:
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001186 return;
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001187 }
1188
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001189 // FIXME: For now ignore 'IncompleteImpl'.
1190 // Previously we grouped all unimplemented methods under a single
1191 // warning, but some users strongly voiced that they would prefer
1192 // separate warnings. We will give that approach a try, as that
1193 // matches what we do with protocols.
1194
1195 Diag(ImpLoc, DiagID) << method->getDeclName();
1196
1197 // Issue a note to the original declaration.
1198 SourceLocation MethodLoc = method->getLocStart();
1199 if (MethodLoc.isValid())
1200 Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001201}
1202
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001203/// Determines if type B can be substituted for type A. Returns true if we can
1204/// guarantee that anything that the user will do to an object of type A can
1205/// also be done to an object of type B. This is trivially true if the two
1206/// types are the same, or if B is a subclass of A. It becomes more complex
1207/// in cases where protocols are involved.
1208///
1209/// Object types in Objective-C describe the minimum requirements for an
1210/// object, rather than providing a complete description of a type. For
1211/// example, if A is a subclass of B, then B* may refer to an instance of A.
1212/// The principle of substitutability means that we may use an instance of A
1213/// anywhere that we may use an instance of B - it will implement all of the
1214/// ivars of B and all of the methods of B.
1215///
1216/// This substitutability is important when type checking methods, because
1217/// the implementation may have stricter type definitions than the interface.
1218/// The interface specifies minimum requirements, but the implementation may
1219/// have more accurate ones. For example, a method may privately accept
1220/// instances of B, but only publish that it accepts instances of A. Any
1221/// object passed to it will be type checked against B, and so will implicitly
1222/// by a valid A*. Similarly, a method may return a subclass of the class that
1223/// it is declared as returning.
1224///
1225/// This is most important when considering subclassing. A method in a
1226/// subclass must accept any object as an argument that its superclass's
1227/// implementation accepts. It may, however, accept a more general type
1228/// without breaking substitutability (i.e. you can still use the subclass
1229/// anywhere that you can use the superclass, but not vice versa). The
1230/// converse requirement applies to return types: the return type for a
1231/// subclass method must be a valid object of the kind that the superclass
1232/// advertises, but it may be specified more accurately. This avoids the need
1233/// for explicit down-casting by callers.
1234///
1235/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001236static bool isObjCTypeSubstitutable(ASTContext &Context,
1237 const ObjCObjectPointerType *A,
1238 const ObjCObjectPointerType *B,
1239 bool rejectId) {
1240 // Reject a protocol-unqualified id.
1241 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001242
1243 // If B is a qualified id, then A must also be a qualified id and it must
1244 // implement all of the protocols in B. It may not be a qualified class.
1245 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1246 // stricter definition so it is not substitutable for id<A>.
1247 if (B->isObjCQualifiedIdType()) {
1248 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001249 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1250 QualType(B,0),
1251 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001252 }
1253
1254 /*
1255 // id is a special type that bypasses type checking completely. We want a
1256 // warning when it is used in one place but not another.
1257 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1258
1259
1260 // If B is a qualified id, then A must also be a qualified id (which it isn't
1261 // if we've got this far)
1262 if (B->isObjCQualifiedIdType()) return false;
1263 */
1264
1265 // Now we know that A and B are (potentially-qualified) class types. The
1266 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001267 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001268}
1269
John McCall10302c02010-10-28 02:34:38 +00001270static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1271 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1272}
1273
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001274static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001275 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001276 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001277 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001278 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001279 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001280 if (IsProtocolMethodDecl &&
1281 (MethodDecl->getObjCDeclQualifier() !=
1282 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001283 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001284 S.Diag(MethodImpl->getLocation(),
1285 (IsOverridingMode ?
1286 diag::warn_conflicting_overriding_ret_type_modifiers
1287 : diag::warn_conflicting_ret_type_modifiers))
1288 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001289 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1290 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1291 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1292 }
1293 else
1294 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001295 }
1296
John McCall10302c02010-10-28 02:34:38 +00001297 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001298 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001299 return true;
1300 if (!Warn)
1301 return false;
John McCall10302c02010-10-28 02:34:38 +00001302
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001303 unsigned DiagID =
1304 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1305 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001306
1307 // Mismatches between ObjC pointers go into a different warning
1308 // category, and sometimes they're even completely whitelisted.
1309 if (const ObjCObjectPointerType *ImplPtrTy =
1310 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1311 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001312 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001313 // Allow non-matching return types as long as they don't violate
1314 // the principle of substitutability. Specifically, we permit
1315 // return types that are subclasses of the declared return type,
1316 // or that are more-qualified versions of the declared type.
1317 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001318 return false;
John McCall10302c02010-10-28 02:34:38 +00001319
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001320 DiagID =
1321 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1322 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001323 }
1324 }
1325
1326 S.Diag(MethodImpl->getLocation(), DiagID)
1327 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001328 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001329 << MethodImpl->getResultType()
1330 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001331 S.Diag(MethodDecl->getLocation(),
1332 IsOverridingMode ? diag::note_previous_declaration
1333 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001334 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001335 return false;
John McCall10302c02010-10-28 02:34:38 +00001336}
1337
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001338static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001339 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001340 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001341 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001342 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001343 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001344 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001345 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001346 if (IsProtocolMethodDecl &&
1347 (ImplVar->getObjCDeclQualifier() !=
1348 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001349 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001350 if (IsOverridingMode)
1351 S.Diag(ImplVar->getLocation(),
1352 diag::warn_conflicting_overriding_param_modifiers)
1353 << getTypeRange(ImplVar->getTypeSourceInfo())
1354 << MethodImpl->getDeclName();
1355 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001356 diag::warn_conflicting_param_modifiers)
1357 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001358 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001359 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1360 << getTypeRange(IfaceVar->getTypeSourceInfo());
1361 }
1362 else
1363 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001364 }
1365
John McCall10302c02010-10-28 02:34:38 +00001366 QualType ImplTy = ImplVar->getType();
1367 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001368
John McCall10302c02010-10-28 02:34:38 +00001369 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001370 return true;
1371
1372 if (!Warn)
1373 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001374 unsigned DiagID =
1375 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1376 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001377
1378 // Mismatches between ObjC pointers go into a different warning
1379 // category, and sometimes they're even completely whitelisted.
1380 if (const ObjCObjectPointerType *ImplPtrTy =
1381 ImplTy->getAs<ObjCObjectPointerType>()) {
1382 if (const ObjCObjectPointerType *IfacePtrTy =
1383 IfaceTy->getAs<ObjCObjectPointerType>()) {
1384 // Allow non-matching argument types as long as they don't
1385 // violate the principle of substitutability. Specifically, the
1386 // implementation must accept any objects that the superclass
1387 // accepts, however it may also accept others.
1388 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001389 return false;
John McCall10302c02010-10-28 02:34:38 +00001390
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001391 DiagID =
1392 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1393 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001394 }
1395 }
1396
1397 S.Diag(ImplVar->getLocation(), DiagID)
1398 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001399 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1400 S.Diag(IfaceVar->getLocation(),
1401 (IsOverridingMode ? diag::note_previous_declaration
1402 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001403 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001404 return false;
John McCall10302c02010-10-28 02:34:38 +00001405}
John McCallf85e1932011-06-15 23:02:42 +00001406
1407/// In ARC, check whether the conventional meanings of the two methods
1408/// match. If they don't, it's a hard error.
1409static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1410 ObjCMethodDecl *decl) {
1411 ObjCMethodFamily implFamily = impl->getMethodFamily();
1412 ObjCMethodFamily declFamily = decl->getMethodFamily();
1413 if (implFamily == declFamily) return false;
1414
1415 // Since conventions are sorted by selector, the only possibility is
1416 // that the types differ enough to cause one selector or the other
1417 // to fall out of the family.
1418 assert(implFamily == OMF_None || declFamily == OMF_None);
1419
1420 // No further diagnostics required on invalid declarations.
1421 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1422
1423 const ObjCMethodDecl *unmatched = impl;
1424 ObjCMethodFamily family = declFamily;
1425 unsigned errorID = diag::err_arc_lost_method_convention;
1426 unsigned noteID = diag::note_arc_lost_method_convention;
1427 if (declFamily == OMF_None) {
1428 unmatched = decl;
1429 family = implFamily;
1430 errorID = diag::err_arc_gained_method_convention;
1431 noteID = diag::note_arc_gained_method_convention;
1432 }
1433
1434 // Indexes into a %select clause in the diagnostic.
1435 enum FamilySelector {
1436 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1437 };
1438 FamilySelector familySelector = FamilySelector();
1439
1440 switch (family) {
1441 case OMF_None: llvm_unreachable("logic error, no method convention");
1442 case OMF_retain:
1443 case OMF_release:
1444 case OMF_autorelease:
1445 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001446 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001447 case OMF_retainCount:
1448 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001449 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001450 // Mismatches for these methods don't change ownership
1451 // conventions, so we don't care.
1452 return false;
1453
1454 case OMF_init: familySelector = F_init; break;
1455 case OMF_alloc: familySelector = F_alloc; break;
1456 case OMF_copy: familySelector = F_copy; break;
1457 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1458 case OMF_new: familySelector = F_new; break;
1459 }
1460
1461 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1462 ReasonSelector reasonSelector;
1463
1464 // The only reason these methods don't fall within their families is
1465 // due to unusual result types.
1466 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1467 reasonSelector = R_UnrelatedReturn;
1468 } else {
1469 reasonSelector = R_NonObjectReturn;
1470 }
1471
1472 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1473 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1474
1475 return true;
1476}
John McCall10302c02010-10-28 02:34:38 +00001477
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001478void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001479 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001480 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001481 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001482 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1483 return;
1484
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001485 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001486 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001487 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001488
Chris Lattner3aff9192009-04-11 19:58:42 +00001489 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001490 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1491 EF = MethodDecl->param_end();
1492 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001493 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001494 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001495 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001496
Fariborz Jahanian21121902011-08-08 18:03:17 +00001497 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001498 Diag(ImpMethodDecl->getLocation(),
1499 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001500 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001501 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001502}
1503
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001504void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1505 ObjCMethodDecl *Overridden,
1506 bool IsProtocolMethodDecl) {
1507
1508 CheckMethodOverrideReturn(*this, Method, Overridden,
1509 IsProtocolMethodDecl, true,
1510 true);
1511
1512 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001513 IF = Overridden->param_begin(), EM = Method->param_end(),
1514 EF = Overridden->param_end();
1515 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001516 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1517 IsProtocolMethodDecl, true, true);
1518 }
1519
1520 if (Method->isVariadic() != Overridden->isVariadic()) {
1521 Diag(Method->getLocation(),
1522 diag::warn_conflicting_overriding_variadic);
1523 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1524 }
1525}
1526
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001527/// WarnExactTypedMethods - This routine issues a warning if method
1528/// implementation declaration matches exactly that of its declaration.
1529void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1530 ObjCMethodDecl *MethodDecl,
1531 bool IsProtocolMethodDecl) {
1532 // don't issue warning when protocol method is optional because primary
1533 // class is not required to implement it and it is safe for protocol
1534 // to implement it.
1535 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1536 return;
1537 // don't issue warning when primary class's method is
1538 // depecated/unavailable.
1539 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1540 MethodDecl->hasAttr<DeprecatedAttr>())
1541 return;
1542
1543 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1544 IsProtocolMethodDecl, false, false);
1545 if (match)
1546 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001547 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1548 EF = MethodDecl->param_end();
1549 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001550 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1551 *IM, *IF,
1552 IsProtocolMethodDecl, false, false);
1553 if (!match)
1554 break;
1555 }
1556 if (match)
1557 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001558 if (match)
1559 match = !(MethodDecl->isClassMethod() &&
1560 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001561
1562 if (match) {
1563 Diag(ImpMethodDecl->getLocation(),
1564 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001565 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1566 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001567 }
1568}
1569
Mike Stump390b4cc2009-05-16 07:39:55 +00001570/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1571/// improve the efficiency of selector lookups and type checking by associating
1572/// with each protocol / interface / category the flattened instance tables. If
1573/// we used an immutable set to keep the table then it wouldn't add significant
1574/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001575
Steve Naroffefe7f362008-02-08 22:06:17 +00001576/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001577/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001578void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1579 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001580 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001581 const SelectorSet &InsMap,
1582 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001583 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001584 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1585 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1586 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001587 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1588
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001589 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001590 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001591 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001592 // check to see if class implements forwardInvocation method and objects
1593 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001594 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001595 // Under such conditions, which means that every method possible is
1596 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001597 // found" warnings.
1598 // FIXME: Use a general GetUnarySelector method for this.
1599 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1600 Selector fISelector = Context.Selectors.getSelector(1, &II);
1601 if (InsMap.count(fISelector))
1602 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1603 // need be implemented in the implementation.
1604 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1605 }
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001607 // If this is a forward protocol declaration, get its definition.
1608 if (!PDecl->isThisDeclarationADefinition() &&
1609 PDecl->getDefinition())
1610 PDecl = PDecl->getDefinition();
1611
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001612 // If a method lookup fails locally we still need to look and see if
1613 // the method was implemented by a base class or an inherited
1614 // protocol. This lookup is slow, but occurs rarely in correct code
1615 // and otherwise would terminate in a warning.
1616
Chris Lattner4d391482007-12-12 07:09:47 +00001617 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001618 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001619 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001620 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001621 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001622 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001623 !method->isPropertyAccessor() &&
1624 !InsMap.count(method->getSelector()) &&
1625 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001626 // If a method is not implemented in the category implementation but
1627 // has been declared in its primary class, superclass,
1628 // or in one of their protocols, no need to issue the warning.
1629 // This is because method will be implemented in the primary class
1630 // or one of its super class implementation.
1631
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001632 // Ugly, but necessary. Method declared in protcol might have
1633 // have been synthesized due to a property declared in the class which
1634 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001635 if (ObjCMethodDecl *MethodInClass =
1636 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001637 true /*shallowCategoryLookup*/))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001638 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001639 continue;
1640 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1641 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1642 != DiagnosticsEngine::Ignored) {
1643 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001644 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1645 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001646 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001647 }
1648 }
Chris Lattner4d391482007-12-12 07:09:47 +00001649 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001650 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001651 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001652 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001653 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001654 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1655 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001656 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001657 // See above comment for instance method lookups.
1658 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001659 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001660 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001661 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001662 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1663 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001664 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
1665 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1666 PDecl->getDeclName();
1667 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001668 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001669 }
Chris Lattner780f3292008-07-21 21:32:27 +00001670 // Check on this protocols's referenced protocols, recursively.
1671 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1672 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001673 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001674}
1675
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001676/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001677/// or protocol against those declared in their implementations.
1678///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001679void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1680 const SelectorSet &ClsMap,
1681 SelectorSet &InsMapSeen,
1682 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001683 ObjCImplDecl* IMPDecl,
1684 ObjCContainerDecl* CDecl,
1685 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001686 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001687 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001688 // Check and see if instance methods in class interface have been
1689 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001690 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1691 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001692 if (InsMapSeen.count((*I)->getSelector()))
1693 continue;
1694 InsMapSeen.insert((*I)->getSelector());
Jordan Rose1e4691b2012-10-10 16:42:25 +00001695 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001696 !InsMap.count((*I)->getSelector())) {
1697 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001698 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001699 diag::warn_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001700 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001701 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001702 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001703 IMPDecl->getInstanceMethod((*I)->getSelector());
1704 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1705 "Expected to find the method through lookup as well");
1706 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001707 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001708 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001709 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001710 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1711 isa<ObjCProtocolDecl>(CDecl));
Jordan Rose1e4691b2012-10-10 16:42:25 +00001712 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001713 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001714 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001715 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001716 }
1717 }
Mike Stump1eb44332009-09-09 15:08:12 +00001718
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001719 // Check and see if class methods in class interface have been
1720 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001721 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001722 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001723 if (ClsMapSeen.count((*I)->getSelector()))
1724 continue;
1725 ClsMapSeen.insert((*I)->getSelector());
1726 if (!ClsMap.count((*I)->getSelector())) {
1727 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001728 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001729 diag::warn_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001730 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001731 ObjCMethodDecl *ImpMethodDecl =
1732 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001733 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1734 "Expected to find the method through lookup as well");
1735 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001736 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001737 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1738 isa<ObjCProtocolDecl>(CDecl));
1739 else
1740 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001741 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001742 }
1743 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001744
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001745 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001746 // when checking that methods in implementation match their declaration,
1747 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1748 // extension; as well as those in categories.
Douglas Gregord3297242013-01-16 23:00:23 +00001749 if (!WarnCategoryMethodImpl) {
1750 for (ObjCInterfaceDecl::visible_categories_iterator
1751 Cat = I->visible_categories_begin(),
1752 CatEnd = I->visible_categories_end();
1753 Cat != CatEnd; ++Cat) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001754 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001755 IMPDecl, *Cat, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001756 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001757 }
1758 } else {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001759 // Also methods in class extensions need be looked at next.
Douglas Gregord3297242013-01-16 23:00:23 +00001760 for (ObjCInterfaceDecl::visible_extensions_iterator
1761 Ext = I->visible_extensions_begin(),
1762 ExtEnd = I->visible_extensions_end();
1763 Ext != ExtEnd; ++Ext) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001764 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001765 IMPDecl, *Ext, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001766 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001767 }
1768 }
1769
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001770 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001771 for (ObjCInterfaceDecl::all_protocol_iterator
1772 PI = I->all_referenced_protocol_begin(),
1773 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001774 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1775 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001776 (*PI), IncompleteImpl, false,
1777 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001778
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001779 // FIXME. For now, we are not checking for extact match of methods
1780 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001781 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001782 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001783 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001784 I->getSuperClass(), IncompleteImpl, false);
1785 }
1786}
1787
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001788/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1789/// category matches with those implemented in its primary class and
1790/// warns each time an exact match is found.
1791void Sema::CheckCategoryVsClassMethodMatches(
1792 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001793 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001794
1795 for (ObjCImplementationDecl::instmeth_iterator
1796 I = CatIMPDecl->instmeth_begin(),
1797 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1798 InsMap.insert((*I)->getSelector());
1799
1800 for (ObjCImplementationDecl::classmeth_iterator
1801 I = CatIMPDecl->classmeth_begin(),
1802 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1803 ClsMap.insert((*I)->getSelector());
1804 if (InsMap.empty() && ClsMap.empty())
1805 return;
1806
1807 // Get category's primary class.
1808 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1809 if (!CatDecl)
1810 return;
1811 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1812 if (!IDecl)
1813 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001814 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001815 bool IncompleteImpl = false;
1816 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1817 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001818 IncompleteImpl, false,
1819 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001820}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001821
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001822void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001823 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001824 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001825 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001826 // Check and see if instance methods in class interface have been
1827 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001828 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001829 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001830 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001831
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001832 // Check and see if properties declared in the interface have either 1)
1833 // an implementation or 2) there is a @synthesize/@dynamic implementation
1834 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001835 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001836 if (!(LangOpts.ObjCDefaultSynthProperties &&
1837 LangOpts.ObjCRuntime.isNonFragile()) ||
1838 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001839 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001840
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001841 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001842 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001843 I = IMPDecl->classmeth_begin(),
1844 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001845 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001846
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001847 // Check for type conflict of methods declared in a class/protocol and
1848 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001849 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001850 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1851 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001852 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001853
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001854 // check all methods implemented in category against those declared
1855 // in its primary class.
1856 if (ObjCCategoryImplDecl *CatDecl =
1857 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1858 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Chris Lattner4d391482007-12-12 07:09:47 +00001860 // Check the protocol list for unimplemented methods in the @implementation
1861 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001862 // Check and see if class methods in class interface have been
1863 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Chris Lattnercddc8882009-03-01 00:56:52 +00001865 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001866 for (ObjCInterfaceDecl::all_protocol_iterator
1867 PI = I->all_referenced_protocol_begin(),
1868 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001869 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001870 InsMap, ClsMap, I);
1871 // Check class extensions (unnamed categories)
Douglas Gregord3297242013-01-16 23:00:23 +00001872 for (ObjCInterfaceDecl::visible_extensions_iterator
1873 Ext = I->visible_extensions_begin(),
1874 ExtEnd = I->visible_extensions_end();
1875 Ext != ExtEnd; ++Ext) {
1876 ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
1877 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001878 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001879 // For extended class, unimplemented methods in its protocols will
1880 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001881 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001882 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1883 E = C->protocol_end(); PI != E; ++PI)
1884 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001885 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001886 // Report unimplemented properties in the category as well.
1887 // When reporting on missing setter/getters, do not report when
1888 // setter/getter is implemented in category's primary class
1889 // implementation.
1890 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1891 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1892 for (ObjCImplementationDecl::instmeth_iterator
1893 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1894 InsMap.insert((*I)->getSelector());
1895 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001896 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001897 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001898 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001899 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001900}
1901
Mike Stump1eb44332009-09-09 15:08:12 +00001902/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001903Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001904Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001905 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001906 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001907 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001908 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001909 for (unsigned i = 0; i != NumElts; ++i) {
1910 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001911 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001912 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001913 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001914 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001915 // Maybe we will complain about the shadowed template parameter.
1916 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1917 // Just pretend that we didn't see the previous declaration.
1918 PrevDecl = 0;
1919 }
1920
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001921 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001922 // GCC apparently allows the following idiom:
1923 //
1924 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1925 // @class XCElementToggler;
1926 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001927 // Here we have chosen to ignore the forward class declaration
1928 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001929 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001930 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001931 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001932 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001933 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001934 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001935 // to the underlying class. Just ignore the forward class with a warning
1936 // as this will force the intended behavior which is to lookup the typedef
1937 // name.
1938 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1939 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1940 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1941 continue;
1942 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001943 }
Chris Lattner4d391482007-12-12 07:09:47 +00001944 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001945
1946 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001947 ObjCInterfaceDecl *PrevIDecl
1948 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001949 ObjCInterfaceDecl *IDecl
1950 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001951 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001952 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001953
Douglas Gregor7723fec2011-12-15 20:29:51 +00001954 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001955 CheckObjCDeclScope(IDecl);
1956 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001957 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001958
1959 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001960}
1961
John McCall0f4c4c42011-06-16 01:15:19 +00001962static bool tryMatchRecordTypes(ASTContext &Context,
1963 Sema::MethodMatchStrategy strategy,
1964 const Type *left, const Type *right);
1965
John McCallf85e1932011-06-15 23:02:42 +00001966static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1967 QualType leftQT, QualType rightQT) {
1968 const Type *left =
1969 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1970 const Type *right =
1971 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1972
1973 if (left == right) return true;
1974
1975 // If we're doing a strict match, the types have to match exactly.
1976 if (strategy == Sema::MMS_strict) return false;
1977
1978 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1979
1980 // Otherwise, use this absurdly complicated algorithm to try to
1981 // validate the basic, low-level compatibility of the two types.
1982
1983 // As a minimum, require the sizes and alignments to match.
1984 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1985 return false;
1986
1987 // Consider all the kinds of non-dependent canonical types:
1988 // - functions and arrays aren't possible as return and parameter types
1989
1990 // - vector types of equal size can be arbitrarily mixed
1991 if (isa<VectorType>(left)) return isa<VectorType>(right);
1992 if (isa<VectorType>(right)) return false;
1993
1994 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001995 // - structs, unions, and Objective-C objects must match more-or-less
1996 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001997 // - everything else should be a scalar
1998 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001999 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00002000
John McCall1d9b3b22011-09-09 05:25:32 +00002001 // Make scalars agree in kind, except count bools as chars, and group
2002 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00002003 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
2004 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
2005 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
2006 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00002007 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
2008 leftSK = Type::STK_ObjCObjectPointer;
2009 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2010 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00002011
2012 // Note that data member pointers and function member pointers don't
2013 // intermix because of the size differences.
2014
2015 return (leftSK == rightSK);
2016}
Chris Lattner4d391482007-12-12 07:09:47 +00002017
John McCall0f4c4c42011-06-16 01:15:19 +00002018static bool tryMatchRecordTypes(ASTContext &Context,
2019 Sema::MethodMatchStrategy strategy,
2020 const Type *lt, const Type *rt) {
2021 assert(lt && rt && lt != rt);
2022
2023 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2024 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2025 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2026
2027 // Require union-hood to match.
2028 if (left->isUnion() != right->isUnion()) return false;
2029
2030 // Require an exact match if either is non-POD.
2031 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2032 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2033 return false;
2034
2035 // Require size and alignment to match.
2036 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2037
2038 // Require fields to match.
2039 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2040 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2041 for (; li != le && ri != re; ++li, ++ri) {
2042 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2043 return false;
2044 }
2045 return (li == le && ri == re);
2046}
2047
Chris Lattner4d391482007-12-12 07:09:47 +00002048/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2049/// returns true, or false, accordingly.
2050/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00002051bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2052 const ObjCMethodDecl *right,
2053 MethodMatchStrategy strategy) {
2054 if (!matchTypes(Context, strategy,
2055 left->getResultType(), right->getResultType()))
2056 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002057
Douglas Gregor7666b032013-02-07 19:13:24 +00002058 // If either is hidden, it is not considered to match.
2059 if (left->isHidden() || right->isHidden())
2060 return false;
2061
David Blaikie4e4d0842012-03-11 07:00:24 +00002062 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002063 (left->hasAttr<NSReturnsRetainedAttr>()
2064 != right->hasAttr<NSReturnsRetainedAttr>() ||
2065 left->hasAttr<NSConsumesSelfAttr>()
2066 != right->hasAttr<NSConsumesSelfAttr>()))
2067 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002068
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002069 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002070 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2071 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002073 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002074 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002075 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002076
2077 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2078 return false;
2079
David Blaikie4e4d0842012-03-11 07:00:24 +00002080 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002081 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2082 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002083 }
2084 return true;
2085}
2086
Douglas Gregorff310c72012-05-01 23:37:00 +00002087void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002088 // If the list is empty, make it a singleton list.
2089 if (List->Method == 0) {
2090 List->Method = Method;
2091 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002092 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002093 }
2094
2095 // We've seen a method with this name, see if we have already seen this type
2096 // signature.
2097 ObjCMethodList *Previous = List;
2098 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002099 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002100 continue;
2101
2102 ObjCMethodDecl *PrevObjCMethod = List->Method;
2103
2104 // Propagate the 'defined' bit.
2105 if (Method->isDefined())
2106 PrevObjCMethod->setDefined(true);
2107
2108 // If a method is deprecated, push it in the global pool.
2109 // This is used for better diagnostics.
2110 if (Method->isDeprecated()) {
2111 if (!PrevObjCMethod->isDeprecated())
2112 List->Method = Method;
2113 }
2114 // If new method is unavailable, push it into global pool
2115 // unless previous one is deprecated.
2116 if (Method->isUnavailable()) {
2117 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2118 List->Method = Method;
2119 }
2120
Douglas Gregorff310c72012-05-01 23:37:00 +00002121 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002122 }
2123
2124 // We have a new signature for an existing method - add it.
2125 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002126 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002127 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2128}
2129
Sebastian Redldb9d2142010-08-02 23:18:59 +00002130/// \brief Read the contents of the method pool for a given selector from
2131/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002132void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002133 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002134 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002135}
2136
Douglas Gregorff310c72012-05-01 23:37:00 +00002137void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002138 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002139 // Ignore methods of invalid containers.
2140 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002141 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002142
Douglas Gregor0d266d62012-01-25 00:59:09 +00002143 if (ExternalSource)
2144 ReadMethodPool(Method->getSelector());
2145
Sebastian Redldb9d2142010-08-02 23:18:59 +00002146 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002147 if (Pos == MethodPool.end())
2148 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2149 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002150
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002151 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002152
Sebastian Redldb9d2142010-08-02 23:18:59 +00002153 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002154 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002155}
2156
John McCallf85e1932011-06-15 23:02:42 +00002157/// Determines if this is an "acceptable" loose mismatch in the global
2158/// method pool. This exists mostly as a hack to get around certain
2159/// global mismatches which we can't afford to make warnings / errors.
2160/// Really, what we want is a way to take a method out of the global
2161/// method pool.
2162static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2163 ObjCMethodDecl *other) {
2164 if (!chosen->isInstanceMethod())
2165 return false;
2166
2167 Selector sel = chosen->getSelector();
2168 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2169 return false;
2170
2171 // Don't complain about mismatches for -length if the method we
2172 // chose has an integral result type.
2173 return (chosen->getResultType()->isIntegerType());
2174}
2175
Sebastian Redldb9d2142010-08-02 23:18:59 +00002176ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002177 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002178 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002179 if (ExternalSource)
2180 ReadMethodPool(Sel);
2181
Sebastian Redldb9d2142010-08-02 23:18:59 +00002182 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002183 if (Pos == MethodPool.end())
2184 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002185
Douglas Gregorf0e00042013-01-16 18:47:38 +00002186 // Gather the non-hidden methods.
Sebastian Redldb9d2142010-08-02 23:18:59 +00002187 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorf0e00042013-01-16 18:47:38 +00002188 llvm::SmallVector<ObjCMethodDecl *, 4> Methods;
2189 for (ObjCMethodList *M = &MethList; M; M = M->Next) {
2190 if (M->Method && !M->Method->isHidden()) {
2191 // If we're not supposed to warn about mismatches, we're done.
2192 if (!warn)
2193 return M->Method;
Mike Stump1eb44332009-09-09 15:08:12 +00002194
Douglas Gregorf0e00042013-01-16 18:47:38 +00002195 Methods.push_back(M->Method);
Sebastian Redldb9d2142010-08-02 23:18:59 +00002196 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002197 }
Douglas Gregorf0e00042013-01-16 18:47:38 +00002198
2199 // If there aren't any visible methods, we're done.
2200 // FIXME: Recover if there are any known-but-hidden methods?
2201 if (Methods.empty())
2202 return 0;
2203
2204 if (Methods.size() == 1)
2205 return Methods[0];
2206
2207 // We found multiple methods, so we may have to complain.
2208 bool issueDiagnostic = false, issueError = false;
2209
2210 // We support a warning which complains about *any* difference in
2211 // method signature.
2212 bool strictSelectorMatch =
2213 (receiverIdOrClass && warn &&
2214 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2215 R.getBegin())
2216 != DiagnosticsEngine::Ignored));
2217 if (strictSelectorMatch) {
2218 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2219 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2220 issueDiagnostic = true;
2221 break;
2222 }
2223 }
2224 }
2225
2226 // If we didn't see any strict differences, we won't see any loose
2227 // differences. In ARC, however, we also need to check for loose
2228 // mismatches, because most of them are errors.
2229 if (!strictSelectorMatch ||
2230 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2231 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2232 // This checks if the methods differ in type mismatch.
2233 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2234 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2235 issueDiagnostic = true;
2236 if (getLangOpts().ObjCAutoRefCount)
2237 issueError = true;
2238 break;
2239 }
2240 }
2241
2242 if (issueDiagnostic) {
2243 if (issueError)
2244 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2245 else if (strictSelectorMatch)
2246 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2247 else
2248 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2249
2250 Diag(Methods[0]->getLocStart(),
2251 issueError ? diag::note_possibility : diag::note_using)
2252 << Methods[0]->getSourceRange();
2253 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2254 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2255 << Methods[I]->getSourceRange();
2256 }
2257 }
2258 return Methods[0];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002259}
2260
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002261ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002262 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2263 if (Pos == MethodPool.end())
2264 return 0;
2265
2266 GlobalMethods &Methods = Pos->second;
2267
2268 if (Methods.first.Method && Methods.first.Method->isDefined())
2269 return Methods.first.Method;
2270 if (Methods.second.Method && Methods.second.Method->isDefined())
2271 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002272 return 0;
2273}
2274
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002275/// DiagnoseDuplicateIvars -
2276/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002277/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002278/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002279/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002280void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2281 ObjCInterfaceDecl *SID) {
2282 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2283 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002284 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002285 if (Ivar->isInvalidDecl())
2286 continue;
2287 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2288 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2289 if (prevIvar) {
2290 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2291 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2292 Ivar->setInvalidDecl();
2293 }
2294 }
2295 }
2296}
2297
Erik Verbruggend64251f2011-12-06 09:25:23 +00002298Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2299 switch (CurContext->getDeclKind()) {
2300 case Decl::ObjCInterface:
2301 return Sema::OCK_Interface;
2302 case Decl::ObjCProtocol:
2303 return Sema::OCK_Protocol;
2304 case Decl::ObjCCategory:
2305 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2306 return Sema::OCK_ClassExtension;
2307 else
2308 return Sema::OCK_Category;
2309 case Decl::ObjCImplementation:
2310 return Sema::OCK_Implementation;
2311 case Decl::ObjCCategoryImpl:
2312 return Sema::OCK_CategoryImplementation;
2313
2314 default:
2315 return Sema::OCK_None;
2316 }
2317}
2318
Steve Naroffa56f6162007-12-18 01:30:32 +00002319// Note: For class/category implemenations, allMethods/allProperties is
2320// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002321Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2322 Decl **allMethods, unsigned allNum,
2323 Decl **allProperties, unsigned pNum,
2324 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002325
Erik Verbruggend64251f2011-12-06 09:25:23 +00002326 if (getObjCContainerKind() == Sema::OCK_None)
2327 return 0;
2328
2329 assert(AtEnd.isValid() && "Invalid location for '@end'");
2330
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002331 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2332 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002333
Mike Stump1eb44332009-09-09 15:08:12 +00002334 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002335 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2336 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002337 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002338
Steve Naroff0701bbb2009-01-08 17:28:14 +00002339 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2340 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2341 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2342
Chris Lattner4d391482007-12-12 07:09:47 +00002343 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002344 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002345 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002346
2347 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002348 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002349 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002350 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002351 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002352 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002353 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002354 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002355 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002356 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002357 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002358 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002359 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002360 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002361 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002362 if (!Context.getSourceManager().isInSystemHeader(
2363 Method->getLocation()))
2364 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2365 << Method->getDeclName();
2366 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2367 }
Chris Lattner4d391482007-12-12 07:09:47 +00002368 InsMap[Method->getSelector()] = Method;
2369 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002370 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002371 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002372 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002373 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002374 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002375 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002376 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002377 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002378 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002379 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002380 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002381 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002382 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002383 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002384 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002385 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002386 if (!Context.getSourceManager().isInSystemHeader(
2387 Method->getLocation()))
2388 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2389 << Method->getDeclName();
2390 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2391 }
Chris Lattner4d391482007-12-12 07:09:47 +00002392 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002393 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002394 }
2395 }
2396 }
Douglas Gregorb892d702013-01-21 19:42:21 +00002397 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2398 // Nothing to do here.
Steve Naroff09c47192009-01-09 15:36:25 +00002399 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002400 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002401 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002402 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002403
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002404 if (C->IsClassExtension()) {
2405 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2406 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002407 }
Chris Lattner4d391482007-12-12 07:09:47 +00002408 }
Steve Naroff09c47192009-01-09 15:36:25 +00002409 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002410 if (CDecl->getIdentifier())
2411 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2412 // user-defined setter/getter. It also synthesizes setter/getter methods
2413 // and adds them to the DeclContext and global method pools.
2414 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2415 E = CDecl->prop_end();
2416 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002417 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002418 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002419 }
2420 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002421 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002422 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002423 // Any property declared in a class extension might have user
2424 // declared setter or getter in current class extension or one
2425 // of the other class extensions. Mark them as synthesized as
2426 // property will be synthesized when property with same name is
2427 // seen in the @implementation.
Douglas Gregord3297242013-01-16 23:00:23 +00002428 for (ObjCInterfaceDecl::visible_extensions_iterator
2429 Ext = IDecl->visible_extensions_begin(),
2430 ExtEnd = IDecl->visible_extensions_end();
2431 Ext != ExtEnd; ++Ext) {
2432 for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
2433 E = Ext->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002434 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002435 // Skip over properties declared @dynamic
2436 if (const ObjCPropertyImplDecl *PIDecl
2437 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2438 if (PIDecl->getPropertyImplementation()
2439 == ObjCPropertyImplDecl::Dynamic)
2440 continue;
Douglas Gregord3297242013-01-16 23:00:23 +00002441
2442 for (ObjCInterfaceDecl::visible_extensions_iterator
2443 Ext = IDecl->visible_extensions_begin(),
2444 ExtEnd = IDecl->visible_extensions_end();
2445 Ext != ExtEnd; ++Ext) {
2446 if (ObjCMethodDecl *GetterMethod
2447 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002448 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002449 if (!Property->isReadOnly())
Douglas Gregord3297242013-01-16 23:00:23 +00002450 if (ObjCMethodDecl *SetterMethod
2451 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002452 SetterMethod->setPropertyAccessor(true);
Douglas Gregord3297242013-01-16 23:00:23 +00002453 }
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002454 }
2455 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002456 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002457 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002458 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002459
Patrick Beardb2f68202012-04-06 18:12:22 +00002460 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2461 if (IDecl->getSuperClass() == NULL) {
2462 // This class has no superclass, so check that it has been marked with
2463 // __attribute((objc_root_class)).
2464 if (!HasRootClassAttr) {
2465 SourceLocation DeclLoc(IDecl->getLocation());
2466 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2467 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2468 << IDecl->getIdentifier();
2469 // See if NSObject is in the current scope, and if it is, suggest
2470 // adding " : NSObject " to the class declaration.
2471 NamedDecl *IF = LookupSingleName(TUScope,
2472 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2473 DeclLoc, LookupOrdinaryName);
2474 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2475 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2476 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2477 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2478 } else {
2479 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2480 }
2481 }
2482 } else if (HasRootClassAttr) {
2483 // Complain that only root classes may have this attribute.
2484 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2485 }
2486
John McCall260611a2012-06-20 06:18:46 +00002487 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002488 while (IDecl->getSuperClass()) {
2489 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2490 IDecl = IDecl->getSuperClass();
2491 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002492 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002493 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002494 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002495 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002496 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002497 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002498
Chris Lattner4d391482007-12-12 07:09:47 +00002499 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002500 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002501 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregord3297242013-01-16 23:00:23 +00002502 if (ObjCCategoryDecl *Cat
2503 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2504 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattner4d391482007-12-12 07:09:47 +00002505 }
2506 }
2507 }
Chris Lattner682bf922009-03-29 16:50:03 +00002508 if (isInterfaceDeclKind) {
2509 // Reject invalid vardecls.
2510 for (unsigned i = 0; i != tuvNum; i++) {
2511 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2512 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2513 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002514 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002515 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002516 }
Chris Lattner682bf922009-03-29 16:50:03 +00002517 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002518 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002519 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002520
2521 for (unsigned i = 0; i != tuvNum; i++) {
2522 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002523 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2524 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002525 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2526 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002527
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002528 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002529 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002530}
2531
2532
2533/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2534/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002535static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002536CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002537 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002538}
2539
Ted Kremenek422bae72010-04-18 04:59:38 +00002540static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002541unsigned countAlignAttr(const AttrVec &A) {
2542 unsigned count=0;
2543 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2544 if ((*i)->getKind() == attr::Aligned)
2545 ++count;
2546 return count;
2547}
2548
2549static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002550bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2551 const AttrVec &A) {
2552 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002553 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002554 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002555 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002556
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002557 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002558 // But implementation has attributes. This is invalid.
2559 // Except when implementation has 'Align' attribute which is
2560 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002561 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002562 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002563
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002564 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002565
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002566 unsigned countAlignOnImpl = countAlignAttr(A);
2567 if (!countAlignOnImpl && (A.size() != D.size()))
2568 return true;
2569 else if (countAlignOnImpl) {
2570 unsigned countAlignOnDecl = countAlignAttr(D);
2571 if (countAlignOnDecl && (A.size() != D.size()))
2572 return true;
2573 else if (!countAlignOnDecl &&
2574 ((A.size()-countAlignOnImpl) != D.size()))
2575 return true;
2576 }
2577
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002578 // attributes on method declaration and definition must match exactly.
2579 // Note that we have at most a couple of attributes on methods, so this
2580 // n*n search is good enough.
2581 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002582 if ((*i)->getKind() == attr::Aligned)
2583 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002584 bool match = false;
2585 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2586 if ((*i)->getKind() == (*i1)->getKind()) {
2587 match = true;
2588 break;
2589 }
2590 }
2591 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002592 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002593 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002594
Sean Huntcf807c42010-08-18 23:23:40 +00002595 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002596}
2597
Douglas Gregor926df6c2011-06-11 01:09:30 +00002598/// \brief Check whether the declared result type of the given Objective-C
2599/// method declaration is compatible with the method's class.
2600///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002601static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002602CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2603 ObjCInterfaceDecl *CurrentClass) {
2604 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002605
2606 // If an Objective-C method inherits its related result type, then its
2607 // declared result type must be compatible with its own class type. The
2608 // declared result type is compatible if:
2609 if (const ObjCObjectPointerType *ResultObjectType
2610 = ResultType->getAs<ObjCObjectPointerType>()) {
2611 // - it is id or qualified id, or
2612 if (ResultObjectType->isObjCIdType() ||
2613 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002614 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002615
2616 if (CurrentClass) {
2617 if (ObjCInterfaceDecl *ResultClass
2618 = ResultObjectType->getInterfaceDecl()) {
2619 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002620 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002621 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002622
2623 // - it is a superclass of the method's class type
2624 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002625 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002626 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002627 } else {
2628 // Any Objective-C pointer type might be acceptable for a protocol
2629 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002630 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002631 }
2632 }
2633
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002634 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002635}
2636
John McCall6c2c2502011-07-22 02:45:48 +00002637namespace {
2638/// A helper class for searching for methods which a particular method
2639/// overrides.
2640class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002641public:
John McCall6c2c2502011-07-22 02:45:48 +00002642 Sema &S;
2643 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002644 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002645 bool Recursive;
2646
2647public:
2648 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2649 Selector selector = method->getSelector();
2650
2651 // Bypass this search if we've never seen an instance/class method
2652 // with this selector before.
2653 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2654 if (it == S.MethodPool.end()) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00002655 if (!S.getExternalSource()) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002656 S.ReadMethodPool(selector);
2657
2658 it = S.MethodPool.find(selector);
2659 if (it == S.MethodPool.end())
2660 return;
John McCall6c2c2502011-07-22 02:45:48 +00002661 }
2662 ObjCMethodList &list =
2663 method->isInstanceMethod() ? it->second.first : it->second.second;
2664 if (!list.Method) return;
2665
2666 ObjCContainerDecl *container
2667 = cast<ObjCContainerDecl>(method->getDeclContext());
2668
2669 // Prevent the search from reaching this container again. This is
2670 // important with categories, which override methods from the
2671 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002672 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2673 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002674 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2675 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002676 } else {
2677 searchFromContainer(container);
2678 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002679 }
John McCall6c2c2502011-07-22 02:45:48 +00002680
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002681 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002682 iterator begin() const { return Overridden.begin(); }
2683 iterator end() const { return Overridden.end(); }
2684
2685private:
2686 void searchFromContainer(ObjCContainerDecl *container) {
2687 if (container->isInvalidDecl()) return;
2688
2689 switch (container->getDeclKind()) {
2690#define OBJCCONTAINER(type, base) \
2691 case Decl::type: \
2692 searchFrom(cast<type##Decl>(container)); \
2693 break;
2694#define ABSTRACT_DECL(expansion)
2695#define DECL(type, base) \
2696 case Decl::type:
2697#include "clang/AST/DeclNodes.inc"
2698 llvm_unreachable("not an ObjC container!");
2699 }
2700 }
2701
2702 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002703 if (!protocol->hasDefinition())
2704 return;
2705
John McCall6c2c2502011-07-22 02:45:48 +00002706 // A method in a protocol declaration overrides declarations from
2707 // referenced ("parent") protocols.
2708 search(protocol->getReferencedProtocols());
2709 }
2710
2711 void searchFrom(ObjCCategoryDecl *category) {
2712 // A method in a category declaration overrides declarations from
2713 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002714 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002715 search(category->getReferencedProtocols());
2716 }
2717
2718 void searchFrom(ObjCCategoryImplDecl *impl) {
2719 // A method in a category definition that has a category
2720 // declaration overrides declarations from the category
2721 // declaration.
2722 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2723 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002724 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2725 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002726
2727 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002728 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2729 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002730 }
2731 }
2732
2733 void searchFrom(ObjCInterfaceDecl *iface) {
2734 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002735 if (!iface->hasDefinition())
2736 return;
2737
John McCall6c2c2502011-07-22 02:45:48 +00002738 // - categories,
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002739 for (ObjCInterfaceDecl::known_categories_iterator
2740 cat = iface->known_categories_begin(),
2741 catEnd = iface->known_categories_end();
Douglas Gregord3297242013-01-16 23:00:23 +00002742 cat != catEnd; ++cat) {
2743 search(*cat);
2744 }
John McCall6c2c2502011-07-22 02:45:48 +00002745
2746 // - the super class, and
2747 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2748 search(super);
2749
2750 // - any referenced protocols.
2751 search(iface->getReferencedProtocols());
2752 }
2753
2754 void searchFrom(ObjCImplementationDecl *impl) {
2755 // A method in a class implementation overrides declarations from
2756 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002757 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2758 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002759 }
2760
2761
2762 void search(const ObjCProtocolList &protocols) {
2763 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2764 i != e; ++i)
2765 search(*i);
2766 }
2767
2768 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002769 // Check for a method in this container which matches this selector.
2770 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002771 Method->isInstanceMethod(),
2772 /*AllowHidden=*/true);
John McCall6c2c2502011-07-22 02:45:48 +00002773
2774 // If we find one, record it and bail out.
2775 if (meth) {
2776 Overridden.insert(meth);
2777 return;
2778 }
2779
2780 // Otherwise, search for methods that a hypothetical method here
2781 // would have overridden.
2782
2783 // Note that we're now in a recursive case.
2784 Recursive = true;
2785
2786 searchFromContainer(container);
2787 }
2788};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002789}
2790
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002791void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2792 ObjCInterfaceDecl *CurrentClass,
2793 ResultTypeCompatibilityKind RTC) {
2794 // Search for overridden methods and merge information down from them.
2795 OverrideSearch overrides(*this, ObjCMethod);
2796 // Keep track if the method overrides any method in the class's base classes,
2797 // its protocols, or its categories' protocols; we will keep that info
2798 // in the ObjCMethodDecl.
2799 // For this info, a method in an implementation is not considered as
2800 // overriding the same method in the interface or its categories.
2801 bool hasOverriddenMethodsInBaseOrProtocol = false;
2802 for (OverrideSearch::iterator
2803 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2804 ObjCMethodDecl *overridden = *i;
2805
2806 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2807 CurrentClass != overridden->getClassInterface() ||
2808 overridden->isOverriding())
2809 hasOverriddenMethodsInBaseOrProtocol = true;
2810
2811 // Propagate down the 'related result type' bit from overridden methods.
2812 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2813 ObjCMethod->SetRelatedResultType();
2814
2815 // Then merge the declarations.
2816 mergeObjCMethodDecls(ObjCMethod, overridden);
2817
2818 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2819 continue; // Conflicting properties are detected elsewhere.
2820
2821 // Check for overriding methods
2822 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2823 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2824 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2825 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2826
2827 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00002828 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
2829 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002830 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2831 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002832 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2833 PrevE = overridden->param_end();
2834 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002835 assert(PrevI != overridden->param_end() && "Param mismatch");
2836 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2837 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2838 // If type of argument of method in this class does not match its
2839 // respective argument type in the super class method, issue warning;
2840 if (!Context.typesAreCompatible(T1, T2)) {
2841 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2842 << T1 << T2;
2843 Diag(overridden->getLocation(), diag::note_previous_declaration);
2844 break;
2845 }
2846 }
2847 }
2848 }
2849
2850 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2851}
2852
John McCalld226f652010-08-21 09:40:31 +00002853Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002854 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002855 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002856 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002857 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002858 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002859 Selector Sel,
2860 // optional arguments. The number of types/arguments is obtained
2861 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002862 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002863 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002864 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002865 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002866 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002867 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002868 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002869 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002870 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002871 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2872 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002873 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002874
Douglas Gregore97179c2011-09-08 01:46:34 +00002875 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002876 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002877 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002878 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002879
Steve Naroffccef3712009-02-20 22:59:16 +00002880 // Methods cannot return interface types. All ObjC objects are
2881 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002882 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002883 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2884 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002885 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002886 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002887
2888 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002889 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002890 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002891 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002892 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002893 }
Mike Stump1eb44332009-09-09 15:08:12 +00002894
2895 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002896 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002897 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002898 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002899 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002900 MethodType == tok::minus, isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +00002901 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002902 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002903 MethodDeclKind == tok::objc_optional
2904 ? ObjCMethodDecl::Optional
2905 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002906 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002907
Chris Lattner5f9e2722011-07-23 10:55:15 +00002908 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002909
Chris Lattner7db638d2009-04-11 19:42:43 +00002910 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002911 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002912 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002913
Chris Lattnere294d3f2009-04-11 18:57:04 +00002914 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002915 ArgType = Context.getObjCIdType();
2916 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002917 } else {
John McCall58e46772009-10-23 21:48:59 +00002918 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002919 }
Mike Stump1eb44332009-09-09 15:08:12 +00002920
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002921 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2922 LookupOrdinaryName, ForRedeclaration);
2923 LookupName(R, S);
2924 if (R.isSingleResult()) {
2925 NamedDecl *PrevDecl = R.getFoundDecl();
2926 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002927 Diag(ArgInfo[i].NameLoc,
2928 (MethodDefinition ? diag::warn_method_param_redefinition
2929 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002930 << ArgInfo[i].Name;
2931 Diag(PrevDecl->getLocation(),
2932 diag::note_previous_declaration);
2933 }
2934 }
2935
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002936 SourceLocation StartLoc = DI
2937 ? DI->getTypeLoc().getBeginLoc()
2938 : ArgInfo[i].NameLoc;
2939
John McCall81ef3e62011-04-23 02:46:06 +00002940 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2941 ArgInfo[i].NameLoc, ArgInfo[i].Name,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00002942 ArgType, DI, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002943
John McCall70798862011-05-02 00:30:12 +00002944 Param->setObjCMethodScopeInfo(i);
2945
Chris Lattner0ed844b2008-04-04 06:12:32 +00002946 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002947 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002948
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002949 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002950 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002951
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002952 if (Param->hasAttr<BlocksAttr>()) {
2953 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2954 Param->setInvalidDecl();
2955 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002956 S->AddDecl(Param);
2957 IdResolver.AddDecl(Param);
2958
Chris Lattner0ed844b2008-04-04 06:12:32 +00002959 Params.push_back(Param);
2960 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002961
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002962 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002963 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002964 QualType ArgType = Param->getType();
2965 if (ArgType.isNull())
2966 ArgType = Context.getObjCIdType();
2967 else
2968 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002969 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002970 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002971 Diag(Param->getLocation(),
2972 diag::err_object_cannot_be_passed_returned_by_value)
2973 << 1 << ArgType;
2974 Param->setInvalidDecl();
2975 }
2976 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002977
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002978 Params.push_back(Param);
2979 }
2980
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002981 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002982 ObjCMethod->setObjCDeclQualifier(
2983 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002984
2985 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002986 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002987
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002988 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002989 const ObjCMethodDecl *PrevMethod = 0;
2990 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002991 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002992 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2993 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002994 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002995 PrevMethod = ImpDecl->getClassMethod(Sel);
2996 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002997 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002998
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002999 ObjCMethodDecl *IMD = 0;
3000 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
3001 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
3002 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00003003 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00003004 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00003005 SourceLocation MethodLoc = IMD->getLocation();
3006 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
3007 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00003008 Diag(MethodLoc, diag::note_method_declared_at)
3009 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00003010 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00003011 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003012 } else {
3013 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003014 }
John McCall6c2c2502011-07-22 02:45:48 +00003015
Chris Lattner4d391482007-12-12 07:09:47 +00003016 if (PrevMethod) {
3017 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00003018 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00003019 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00003020 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00003021 }
John McCall54abf7d2009-11-04 02:18:39 +00003022
Douglas Gregor926df6c2011-06-11 01:09:30 +00003023 // If this Objective-C method does not have a related result type, but we
3024 // are allowed to infer related result types, try to do so based on the
3025 // method family.
3026 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3027 if (!CurrentClass) {
3028 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3029 CurrentClass = Cat->getClassInterface();
3030 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3031 CurrentClass = Impl->getClassInterface();
3032 else if (ObjCCategoryImplDecl *CatImpl
3033 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3034 CurrentClass = CatImpl->getClassInterface();
3035 }
John McCall6c2c2502011-07-22 02:45:48 +00003036
Douglas Gregore97179c2011-09-08 01:46:34 +00003037 ResultTypeCompatibilityKind RTC
3038 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00003039
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003040 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00003041
John McCallf85e1932011-06-15 23:02:42 +00003042 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00003043 if (getLangOpts().ObjCAutoRefCount)
John McCallb8463812013-04-04 01:38:37 +00003044 ARCError = CheckARCMethodDecl(ObjCMethod);
John McCallf85e1932011-06-15 23:02:42 +00003045
Douglas Gregore97179c2011-09-08 01:46:34 +00003046 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003047 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00003048 !ObjCMethod->hasRelatedResultType() &&
3049 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00003050 bool InferRelatedResultType = false;
3051 switch (ObjCMethod->getMethodFamily()) {
3052 case OMF_None:
3053 case OMF_copy:
3054 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00003055 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003056 case OMF_mutableCopy:
3057 case OMF_release:
3058 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00003059 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003060 break;
3061
3062 case OMF_alloc:
3063 case OMF_new:
3064 InferRelatedResultType = ObjCMethod->isClassMethod();
3065 break;
3066
3067 case OMF_init:
3068 case OMF_autorelease:
3069 case OMF_retain:
3070 case OMF_self:
3071 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3072 break;
3073 }
3074
John McCall6c2c2502011-07-22 02:45:48 +00003075 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003076 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003077 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003078
3079 ActOnDocumentableDecl(ObjCMethod);
3080
John McCalld226f652010-08-21 09:40:31 +00003081 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003082}
3083
Chris Lattnercc98eac2008-12-17 07:13:27 +00003084bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003085 // Following is also an error. But it is caused by a missing @end
3086 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003087 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003088 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003089
3090 // If we switched context to translation unit while we are still lexically in
3091 // an objc container, it means the parser missed emitting an error.
3092 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3093 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003094
Anders Carlsson15281452008-11-04 16:57:32 +00003095 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3096 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003097
Anders Carlsson15281452008-11-04 16:57:32 +00003098 return true;
3099}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003100
James Dennett1dfbd922012-06-14 21:40:34 +00003101/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003102/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003103void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003104 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003105 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003106 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003107 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003108 if (!Class) {
3109 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3110 return;
3111 }
John McCall260611a2012-06-20 06:18:46 +00003112 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003113 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3114 return;
3115 }
Mike Stump1eb44332009-09-09 15:08:12 +00003116
Chris Lattnercc98eac2008-12-17 07:13:27 +00003117 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003118 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003119 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003120 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003121 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003122 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003123 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003124 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3125 /*FIXME: StartL=*/ID->getLocation(),
3126 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003127 ID->getIdentifier(), ID->getType(),
3128 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003129 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003130 }
Mike Stump1eb44332009-09-09 15:08:12 +00003131
Chris Lattnercc98eac2008-12-17 07:13:27 +00003132 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003133 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003134 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003135 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003136 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003137 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003138 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003139 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003140 }
3141}
3142
Douglas Gregor160b5632010-04-26 17:32:49 +00003143/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003144VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3145 SourceLocation StartLoc,
3146 SourceLocation IdLoc,
3147 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003148 bool Invalid) {
3149 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3150 // duration shall not be qualified by an address-space qualifier."
3151 // Since all parameters have automatic store duration, they can not have
3152 // an address space.
3153 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003154 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003155 Invalid = true;
3156 }
3157
3158 // An @catch parameter must be an unqualified object pointer type;
3159 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3160 if (Invalid) {
3161 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003162 } else if (T->isDependentType()) {
3163 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003164 } else if (!T->isObjCObjectPointerType()) {
3165 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003166 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003167 } else if (T->isObjCQualifiedIdType()) {
3168 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003169 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003170 }
3171
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003172 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
Rafael Espindolad2615cc2013-04-03 19:27:57 +00003173 T, TInfo, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003174 New->setExceptionVariable(true);
3175
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003176 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003177 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003178 Invalid = true;
3179
Douglas Gregor160b5632010-04-26 17:32:49 +00003180 if (Invalid)
3181 New->setInvalidDecl();
3182 return New;
3183}
3184
John McCalld226f652010-08-21 09:40:31 +00003185Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003186 const DeclSpec &DS = D.getDeclSpec();
3187
3188 // We allow the "register" storage class on exception variables because
3189 // GCC did, but we drop it completely. Any other storage class is an error.
3190 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3191 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3192 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3193 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3194 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3195 << DS.getStorageClassSpec();
3196 }
3197 if (D.getDeclSpec().isThreadSpecified())
3198 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3199 D.getMutableDeclSpec().ClearStorageClassSpecs();
3200
Richard Smithc7f81162013-03-18 22:52:47 +00003201 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregor160b5632010-04-26 17:32:49 +00003202
3203 // Check that there are no default arguments inside the type of this
3204 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003205 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003206 CheckExtraCXXDefaultArguments(D);
3207
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003208 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003209 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003210
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003211 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3212 D.getSourceRange().getBegin(),
3213 D.getIdentifierLoc(),
3214 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003215 D.isInvalidType());
3216
3217 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3218 if (D.getCXXScopeSpec().isSet()) {
3219 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3220 << D.getCXXScopeSpec().getRange();
3221 New->setInvalidDecl();
3222 }
3223
3224 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003225 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003226 if (D.getIdentifier())
3227 IdResolver.AddDecl(New);
3228
3229 ProcessDeclAttributes(S, New, D);
3230
3231 if (New->hasAttr<BlocksAttr>())
3232 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003233 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003234}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003235
3236/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003237/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003238void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003239 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003240 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3241 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003242 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003243 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003244 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003245 }
3246}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003247
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003248void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003249 // Load referenced selectors from the external source.
3250 if (ExternalSource) {
3251 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3252 ExternalSource->ReadReferencedSelectors(Sels);
3253 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3254 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3255 }
3256
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003257 // Warning will be issued only when selector table is
3258 // generated (which means there is at lease one implementation
3259 // in the TU). This is to match gcc's behavior.
3260 if (ReferencedSelectors.empty() ||
3261 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003262 return;
3263 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3264 ReferencedSelectors.begin(),
3265 E = ReferencedSelectors.end(); S != E; ++S) {
3266 Selector Sel = (*S).first;
3267 if (!LookupImplementedMethodInGlobalPool(Sel))
3268 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3269 }
3270 return;
3271}