blob: e81429cc4c77538a19538926e1b300c68d639452 [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"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000017#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000019#include "clang/AST/ASTConsumer.h"
Steve Naroffca331292009-03-03 14:49:36 +000020#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/AST/ExprObjC.h"
Chris Lattner4d391482007-12-12 07:09:47 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +000024#include "clang/AST/ASTMutationListener.h"
John McCallf85e1932011-06-15 23:02:42 +000025#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
Patrick Beardb2f68202012-04-06 18:12:22 +000027#include "clang/Lex/Preprocessor.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 Gregor926df6c2011-06-11 01:09:30 +0000112 const ObjCMethodDecl *Overridden,
113 bool IsImplementation) {
114 if (Overridden->hasRelatedResultType() &&
115 !NewMethod->hasRelatedResultType()) {
116 // This can only happen when the method follows a naming convention that
117 // implies a related result type, and the original (overridden) method has
118 // a suitable return type, but the new (overriding) method does not have
119 // a suitable return type.
120 QualType ResultType = NewMethod->getResultType();
121 SourceRange ResultTypeRange;
122 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000123 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000124 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
125
126 // Figure out which class this method is part of, if any.
127 ObjCInterfaceDecl *CurrentClass
128 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
129 if (!CurrentClass) {
130 DeclContext *DC = NewMethod->getDeclContext();
131 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
132 CurrentClass = Cat->getClassInterface();
133 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
134 CurrentClass = Impl->getClassInterface();
135 else if (ObjCCategoryImplDecl *CatImpl
136 = dyn_cast<ObjCCategoryImplDecl>(DC))
137 CurrentClass = CatImpl->getClassInterface();
138 }
139
140 if (CurrentClass) {
141 Diag(NewMethod->getLocation(),
142 diag::warn_related_result_type_compatibility_class)
143 << Context.getObjCInterfaceType(CurrentClass)
144 << ResultType
145 << ResultTypeRange;
146 } else {
147 Diag(NewMethod->getLocation(),
148 diag::warn_related_result_type_compatibility_protocol)
149 << ResultType
150 << ResultTypeRange;
151 }
152
Douglas Gregore97179c2011-09-08 01:46:34 +0000153 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
154 Diag(Overridden->getLocation(),
155 diag::note_related_result_type_overridden_family)
156 << 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.
196static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
197 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:
210 if (!S.Context.hasSameType(method->getResultType(), S.Context.VoidTy)) {
211 SourceRange ResultTypeRange;
212 if (const TypeSourceInfo *ResultTypeInfo
213 = method->getResultTypeSourceInfo())
214 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
215 if (ResultTypeRange.isInvalid())
216 S.Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
217 << method->getResultType()
218 << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
219 else
220 S.Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
221 << 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.
229 if (S.checkInitMethod(method, QualType()))
230 return true;
231
232 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
233 S.Context));
234
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
252 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
253 S.Context));
254 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 Jahanian918546c2012-08-30 23:56:02 +0000285/// StrongPointerToObjCPointer - returns true when pointer to ObjC pointer
286/// is __strong, or when it is any other type. It returns false when
287/// pointer to ObjC pointer is not __strong.
288static bool
289StrongPointerToObjCPointer(Sema &S, ParmVarDecl *Param) {
290 QualType T = Param->getType();
291 if (!T->isObjCIndirectLifetimeType())
292 return true;
293 if (!T->isPointerType() && !T->isReferenceType())
294 return true;
295 T = T->isPointerType()
296 ? T->getAs<PointerType>()->getPointeeType()
297 : T->getAs<ReferenceType>()->getPointeeType();
298 if (T->isObjCLifetimeType()) {
299 Qualifiers::ObjCLifetime lifetime = T.getObjCLifetime();
300 return lifetime == Qualifiers::OCL_Strong;
301 }
302 return true;
303}
304
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +0000305/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
306/// and user declared, in the method definition's AST.
307void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
308 assert((getCurMethodDecl() == 0) && "Methodparsing confused");
John McCalld226f652010-08-21 09:40:31 +0000309 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000310
Steve Naroff394f3f42008-07-25 17:57:26 +0000311 // If we don't have a valid method decl, simply return.
312 if (!MDecl)
313 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000314
Chris Lattner4d391482007-12-12 07:09:47 +0000315 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000316 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000317 PushFunctionScope();
318
Chris Lattner4d391482007-12-12 07:09:47 +0000319 // Create Decl objects for each parameter, entrring them in the scope for
320 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000321
322 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000323 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Daniel Dunbar451318c2008-08-26 06:07:48 +0000325 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
326 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000327
Chris Lattner8123a952008-04-10 02:22:51 +0000328 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000329 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000330 E = MDecl->param_end(); PI != E; ++PI) {
331 ParmVarDecl *Param = (*PI);
332 if (!Param->isInvalidDecl() &&
333 RequireCompleteType(Param->getLocation(), Param->getType(),
334 diag::err_typecheck_decl_incomplete_type))
335 Param->setInvalidDecl();
Fariborz Jahanian918546c2012-08-30 23:56:02 +0000336 if (!Param->isInvalidDecl() &&
337 getLangOpts().ObjCAutoRefCount &&
338 !StrongPointerToObjCPointer(*this, Param))
339 Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
340 Param->getType();
341
Chris Lattner89951a82009-02-20 18:43:26 +0000342 if ((*PI)->getIdentifier())
343 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000344 }
John McCallf85e1932011-06-15 23:02:42 +0000345
346 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000347 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000348 switch (MDecl->getMethodFamily()) {
349 case OMF_retain:
350 case OMF_retainCount:
351 case OMF_release:
352 case OMF_autorelease:
353 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
354 << MDecl->getSelector();
355 break;
356
357 case OMF_None:
358 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000359 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000360 case OMF_alloc:
361 case OMF_init:
362 case OMF_mutableCopy:
363 case OMF_copy:
364 case OMF_new:
365 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000366 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000367 break;
368 }
369 }
370
Nico Weber9a1ecf02011-08-22 17:25:57 +0000371 // Warn on deprecated methods under -Wdeprecated-implementations,
372 // and prepare for warning on missing super calls.
373 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian84101132012-09-07 23:46:23 +0000374 ObjCMethodDecl *IMD =
375 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
376
377 if (IMD)
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000378 DiagnoseObjCImplementedDeprecations(*this,
379 dyn_cast<NamedDecl>(IMD),
380 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000381
Nico Weber80cb6e62011-08-28 22:35:17 +0000382 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000383 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
384 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
385 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000386 if (IC->getSuperClass()) {
Eli Friedman95aac152012-08-01 21:02:59 +0000387 getCurFunction()->ObjCShouldCallSuperDealloc =
David Blaikie4e4d0842012-03-11 07:00:24 +0000388 !(Context.getLangOpts().ObjCAutoRefCount ||
389 Context.getLangOpts().getGC() == LangOptions::GCOnly) &&
Fariborz Jahanian84101132012-09-07 23:46:23 +0000390 MDecl->getMethodFamily() == OMF_dealloc;
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000391 if (!getCurFunction()->ObjCShouldCallSuperDealloc) {
392 IMD = IC->getSuperClass()->lookupMethod(MDecl->getSelector(),
393 MDecl->isInstanceMethod());
Fariborz Jahanian84101132012-09-07 23:46:23 +0000394 getCurFunction()->ObjCShouldCallSuperDealloc =
395 (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000396 }
Eli Friedman95aac152012-08-01 21:02:59 +0000397 getCurFunction()->ObjCShouldCallSuperFinalize =
David Blaikie4e4d0842012-03-11 07:00:24 +0000398 Context.getLangOpts().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000399 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000400 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000401 }
Chris Lattner4d391482007-12-12 07:09:47 +0000402}
403
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000404namespace {
405
406// Callback to only accept typo corrections that are Objective-C classes.
407// If an ObjCInterfaceDecl* is given to the constructor, then the validation
408// function will reject corrections to that class.
409class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
410 public:
411 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
412 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
413 : CurrentIDecl(IDecl) {}
414
415 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
416 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
417 return ID && !declaresSameEntity(ID, CurrentIDecl);
418 }
419
420 private:
421 ObjCInterfaceDecl *CurrentIDecl;
422};
423
424}
425
John McCalld226f652010-08-21 09:40:31 +0000426Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000427ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
428 IdentifierInfo *ClassName, SourceLocation ClassLoc,
429 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000430 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000431 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000432 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000433 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Chris Lattner4d391482007-12-12 07:09:47 +0000435 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000436 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000437 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000438
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000439 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000440 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000441 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000442 }
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Douglas Gregor7723fec2011-12-15 20:29:51 +0000444 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000445 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000446 ObjCInterfaceDecl *IDecl
447 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000448 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000449
Douglas Gregor7723fec2011-12-15 20:29:51 +0000450 if (PrevIDecl) {
451 // Class already seen. Was it a definition?
452 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
453 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
454 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000455 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000456 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000457 }
Chris Lattner4d391482007-12-12 07:09:47 +0000458 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000459
460 if (AttrList)
461 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
462 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Douglas Gregor7723fec2011-12-15 20:29:51 +0000464 // Start the definition of this class. If we're in a redefinition case, there
465 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000466 if (!IDecl->hasDefinition())
467 IDecl->startDefinition();
468
Chris Lattner4d391482007-12-12 07:09:47 +0000469 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000470 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000471 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
472 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000473
474 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000475 // Try to correct for a typo in the superclass name without correcting
476 // to the class we're defining.
477 ObjCInterfaceValidatorCCC Validator(IDecl);
478 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000479 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000480 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000481 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
482 Diag(SuperLoc, diag::err_undef_superclass_suggest)
483 << SuperName << ClassName << PrevDecl->getDeclName();
484 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
485 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000486 }
487 }
488
Douglas Gregor60ef3082011-12-15 00:29:59 +0000489 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000490 Diag(SuperLoc, diag::err_recursive_superclass)
491 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000492 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000493 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000494 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000495 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000496
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000497 // Diagnose classes that inherit from deprecated classes.
498 if (SuperClassDecl)
499 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000501 if (PrevDecl && SuperClassDecl == 0) {
502 // The previous declaration was not a class decl. Check if we have a
503 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000504 if (const TypedefNameDecl *TDecl =
505 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000506 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000507 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000508 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
509 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000510 }
511 }
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000513 // This handles the following case:
514 //
515 // typedef int SuperClass;
516 // @interface MyClass : SuperClass {} @end
517 //
518 if (!SuperClassDecl) {
519 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
520 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000521 }
522 }
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Richard Smith162e1c12011-04-15 14:24:37 +0000524 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000525 if (!SuperClassDecl)
526 Diag(SuperLoc, diag::err_undef_superclass)
527 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000528 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000529 Context.getObjCInterfaceType(SuperClassDecl),
530 diag::err_forward_superclass,
531 SuperClassDecl->getDeclName(),
532 ClassName,
533 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000534 SuperClassDecl = 0;
535 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000536 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000537 IDecl->setSuperClass(SuperClassDecl);
538 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000539 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000540 }
Chris Lattner4d391482007-12-12 07:09:47 +0000541 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000542 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Sebastian Redl0b17c612010-08-13 00:28:03 +0000545 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000546 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000547 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000548 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000549 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000550 }
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Anders Carlsson15281452008-11-04 16:57:32 +0000552 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000553 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000554}
555
Richard Smithde01b7a2012-08-08 23:32:13 +0000556/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000557/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000558Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
559 IdentifierInfo *AliasName,
560 SourceLocation AliasLocation,
561 IdentifierInfo *ClassName,
562 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000563 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000564 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000565 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000566 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000567 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000568 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000569 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000570 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000571 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000572 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000573 }
574 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000575 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000576 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000577 if (const TypedefNameDecl *TDecl =
578 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000579 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000580 if (T->isObjCObjectType()) {
581 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000582 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000583 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000584 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000585 }
586 }
587 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000588 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
589 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000590 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000591 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000592 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000593 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000594 }
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000596 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000597 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000598 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000599
Anders Carlsson15281452008-11-04 16:57:32 +0000600 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000601 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000602
John McCalld226f652010-08-21 09:40:31 +0000603 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000604}
605
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000606bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000607 IdentifierInfo *PName,
608 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000609 const ObjCList<ObjCProtocolDecl> &PList) {
610
611 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000612 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
613 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000614 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
615 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000616 if (PDecl->getIdentifier() == PName) {
617 Diag(Ploc, diag::err_protocol_has_circular_dependency);
618 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000619 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000620 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000621
622 if (!PDecl->hasDefinition())
623 continue;
624
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000625 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
626 PDecl->getLocation(), PDecl->getReferencedProtocols()))
627 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000628 }
629 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000630 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000631}
632
John McCalld226f652010-08-21 09:40:31 +0000633Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000634Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
635 IdentifierInfo *ProtocolName,
636 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000637 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000638 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000639 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000640 SourceLocation EndProtoLoc,
641 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000642 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000643 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000644 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000645 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
646 ForRedeclaration);
647 ObjCProtocolDecl *PDecl = 0;
648 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
649 // If we already have a definition, complain.
650 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
651 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Douglas Gregor27c6da22012-01-01 20:30:41 +0000653 // Create a new protocol that is completely distinct from previous
654 // declarations, and do not make this protocol available for name lookup.
655 // That way, we'll end up completely ignoring the duplicate.
656 // FIXME: Can we turn this into an error?
657 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
658 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000659 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000660 PDecl->startDefinition();
661 } else {
662 if (PrevDecl) {
663 // Check for circular dependencies among protocol declarations. This can
664 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000665 ObjCList<ObjCProtocolDecl> PList;
666 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
667 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000668 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000669 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000670
671 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000672 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000673 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000674 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000675
Douglas Gregor6e378de2009-04-23 23:18:26 +0000676 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000677 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000678 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000679
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000680 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000681 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000682
683 // Merge attributes from previous declarations.
684 if (PrevDecl)
685 mergeDeclAttributes(PDecl, PrevDecl);
686
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000687 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000688 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000689 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000690 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000691 }
Mike Stump1eb44332009-09-09 15:08:12 +0000692
693 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000694 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000695}
696
697/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000698/// issues an error if they are not declared. It returns list of
699/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000700void
Chris Lattnere13b9592008-07-26 04:03:38 +0000701Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000702 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000703 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000704 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000705 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000706 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
707 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000708 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000709 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000710 TypoCorrection Corrected = CorrectTypo(
711 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000712 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000713 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000714 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000715 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000716 Diag(PDecl->getLocation(), diag::note_previous_decl)
717 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000718 }
719 }
720
721 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000722 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000723 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000724 continue;
725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000727 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000728
729 // If this is a forward declaration and we are supposed to warn in this
730 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000731 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000732 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000733 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000734 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000735 }
736}
737
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000738/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000739/// a class method in its extension.
740///
Mike Stump1eb44332009-09-09 15:08:12 +0000741void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000742 ObjCInterfaceDecl *ID) {
743 if (!ID)
744 return; // Possibly due to previous error
745
746 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000747 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
748 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000749 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000750 MethodMap[MD->getSelector()] = MD;
751 }
752
753 if (MethodMap.empty())
754 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000755 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
756 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000757 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000758 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
759 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
760 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
761 << Method->getDeclName();
762 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
763 }
764 }
765}
766
James Dennett1dfbd922012-06-14 21:40:34 +0000767/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000768Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000769Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000770 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000771 unsigned NumElts,
772 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000773 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000774 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000775 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000776 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
777 ForRedeclaration);
778 ObjCProtocolDecl *PDecl
779 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
780 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000781 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000782
783 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000784 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000785
Douglas Gregor3937f872012-01-01 20:33:24 +0000786 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000787 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000788
789 if (PrevDecl)
790 mergeDeclAttributes(PDecl, PrevDecl);
791
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000792 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000793 }
Mike Stump1eb44332009-09-09 15:08:12 +0000794
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000795 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000796}
797
John McCalld226f652010-08-21 09:40:31 +0000798Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000799ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
800 IdentifierInfo *ClassName, SourceLocation ClassLoc,
801 IdentifierInfo *CategoryName,
802 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000803 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000804 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000805 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000806 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000807 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000808 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000809
810 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000811
812 if (!IDecl
813 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000814 diag::err_category_forward_interface,
815 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000816 // Create an invalid ObjCCategoryDecl to serve as context for
817 // the enclosing method declarations. We mark the decl invalid
818 // to make it clear that this isn't a valid AST.
819 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000820 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000821 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000822 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000823
824 if (!IDecl)
825 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000826 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000827 }
828
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000829 if (!CategoryName && IDecl->getImplementation()) {
830 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
831 Diag(IDecl->getImplementation()->getLocation(),
832 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000833 }
834
Fariborz Jahanian25760612010-02-15 21:55:26 +0000835 if (CategoryName) {
836 /// Check for duplicate interface declaration for this category
837 ObjCCategoryDecl *CDeclChain;
838 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
839 CDeclChain = CDeclChain->getNextClassCategory()) {
840 if (CDeclChain->getIdentifier() == CategoryName) {
841 // Class extensions can be declared multiple times.
842 Diag(CategoryLoc, diag::warn_dup_category_def)
843 << ClassName << CategoryName;
844 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
845 break;
846 }
Chris Lattner70f19542009-02-16 21:26:43 +0000847 }
848 }
Chris Lattner70f19542009-02-16 21:26:43 +0000849
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000850 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
851 ClassLoc, CategoryLoc, CategoryName, IDecl);
852 // FIXME: PushOnScopeChains?
853 CurContext->addDecl(CDecl);
854
Chris Lattner4d391482007-12-12 07:09:47 +0000855 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000856 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000857 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000858 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000859 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000860 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000861 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000862 }
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Anders Carlsson15281452008-11-04 16:57:32 +0000864 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000865 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000866}
867
868/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000869/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000870/// object.
John McCalld226f652010-08-21 09:40:31 +0000871Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000872 SourceLocation AtCatImplLoc,
873 IdentifierInfo *ClassName, SourceLocation ClassLoc,
874 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000875 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000876 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000877 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000878 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
879 if (!CatIDecl) {
880 // Category @implementation with no corresponding @interface.
881 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000882 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
883 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000884 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000885 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000886 }
887 }
888
Mike Stump1eb44332009-09-09 15:08:12 +0000889 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000890 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000891 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000892 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000893 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000894 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000895 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000896 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
897 diag::err_undef_interface)) {
898 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000899 }
Chris Lattner4d391482007-12-12 07:09:47 +0000900
Douglas Gregord0434102009-01-09 00:49:46 +0000901 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000902 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000903
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000904 // If the interface is deprecated/unavailable, warn/error about it.
905 if (IDecl)
906 DiagnoseUseOfDecl(IDecl, ClassLoc);
907
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000908 /// Check that CatName, category name, is not used in another implementation.
909 if (CatIDecl) {
910 if (CatIDecl->getImplementation()) {
911 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
912 << CatName;
913 Diag(CatIDecl->getImplementation()->getLocation(),
914 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000915 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000916 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000917 // Warn on implementating category of deprecated class under
918 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000919 DiagnoseObjCImplementedDeprecations(*this,
920 dyn_cast<NamedDecl>(IDecl),
921 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000922 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000923 }
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Anders Carlsson15281452008-11-04 16:57:32 +0000925 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000926 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000927}
928
John McCalld226f652010-08-21 09:40:31 +0000929Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000930 SourceLocation AtClassImplLoc,
931 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000932 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000933 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000934 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000935 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000936 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000937 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
938 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000939 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000940 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000941 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000942 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000943 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
944 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000945 } else {
946 // We did not find anything with the name ClassName; try to correct for
947 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000948 ObjCInterfaceValidatorCCC Validator;
949 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000950 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000951 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000952 // Suggest the (potentially) correct interface name. However, put the
953 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000954 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000955 // provide a code-modification hint or use the typo name for recovery,
956 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000957 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000958 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000959 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000960 << ClassName << CorrectedName;
961 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
962 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000963 IDecl = 0;
964 } else {
965 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
966 }
Chris Lattner4d391482007-12-12 07:09:47 +0000967 }
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Chris Lattner4d391482007-12-12 07:09:47 +0000969 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000970 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000971 if (SuperClassname) {
972 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000973 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
974 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000975 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000976 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
977 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000978 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000979 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000980 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000981 if (SDecl && !SDecl->hasDefinition())
982 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000983 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000984 Diag(SuperClassLoc, diag::err_undef_superclass)
985 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000986 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000987 // This implementation and its interface do not have the same
988 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000989 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000990 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000991 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000992 }
993 }
994 }
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Chris Lattner4d391482007-12-12 07:09:47 +0000996 if (!IDecl) {
997 // Legacy case of @implementation with no corresponding @interface.
998 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000999
Mike Stump390b4cc2009-05-16 07:39:55 +00001000 // FIXME: Do we support attributes on the @implementation? If so we should
1001 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001002 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001003 ClassName, /*PrevDecl=*/0, ClassLoc,
1004 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001005 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001006 if (SDecl) {
1007 IDecl->setSuperClass(SDecl);
1008 IDecl->setSuperClassLoc(SuperClassLoc);
1009 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1010 } else {
1011 IDecl->setEndOfDefinitionLoc(ClassLoc);
1012 }
1013
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001014 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001015 } else {
1016 // Mark the interface as being completed, even if it was just as
1017 // @class ....;
1018 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001019 if (!IDecl->hasDefinition())
1020 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001021 }
Mike Stump1eb44332009-09-09 15:08:12 +00001022
1023 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001024 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1025 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Anders Carlsson15281452008-11-04 16:57:32 +00001027 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001028 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Chris Lattner4d391482007-12-12 07:09:47 +00001030 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001031 if (IDecl->getImplementation()) {
1032 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001033 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001034 Diag(IDecl->getImplementation()->getLocation(),
1035 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001036 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001037 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001038 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001039 // Warn on implementating deprecated class under
1040 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001041 DiagnoseObjCImplementedDeprecations(*this,
1042 dyn_cast<NamedDecl>(IDecl),
1043 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001044 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001045 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001046}
1047
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001048Sema::DeclGroupPtrTy
1049Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1050 SmallVector<Decl *, 64> DeclsInGroup;
1051 DeclsInGroup.reserve(Decls.size() + 1);
1052
1053 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1054 Decl *Dcl = Decls[i];
1055 if (!Dcl)
1056 continue;
1057 if (Dcl->getDeclContext()->isFileContext())
1058 Dcl->setTopLevelDeclInObjCContainer();
1059 DeclsInGroup.push_back(Dcl);
1060 }
1061
1062 DeclsInGroup.push_back(ObjCImpDecl);
1063
1064 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1065}
1066
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001067void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1068 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001069 SourceLocation RBrace) {
1070 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001071 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001072 if (!IDecl)
1073 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001074 /// Check case of non-existing \@interface decl.
1075 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001076 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001077 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001078 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001079 // Add ivar's to class's DeclContext.
1080 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001081 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001082 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001083 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001084 }
1085
Chris Lattner4d391482007-12-12 07:09:47 +00001086 return;
1087 }
1088 // If implementation has empty ivar list, just return.
1089 if (numIvars == 0)
1090 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Chris Lattner4d391482007-12-12 07:09:47 +00001092 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001093 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001094 if (ImpDecl->getSuperClass())
1095 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1096 for (unsigned i = 0; i < numIvars; i++) {
1097 ObjCIvarDecl* ImplIvar = ivars[i];
1098 if (const ObjCIvarDecl *ClsIvar =
1099 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1100 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1101 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1102 continue;
1103 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001104 // Instance ivar to Implementation's DeclContext.
1105 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001106 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001107 ImpDecl->addDecl(ImplIvar);
1108 }
1109 return;
1110 }
Chris Lattner4d391482007-12-12 07:09:47 +00001111 // Check interface's Ivar list against those in the implementation.
1112 // names and types must match.
1113 //
Chris Lattner4d391482007-12-12 07:09:47 +00001114 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001115 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001116 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1117 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001118 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001119 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001120 assert (ImplIvar && "missing implementation ivar");
1121 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Steve Naroffca331292009-03-03 14:49:36 +00001123 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001124 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001125 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001126 << ImplIvar->getIdentifier()
1127 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001128 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001129 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1130 ImplIvar->getBitWidthValue(Context) !=
1131 ClsIvar->getBitWidthValue(Context)) {
1132 Diag(ImplIvar->getBitWidth()->getLocStart(),
1133 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1134 Diag(ClsIvar->getBitWidth()->getLocStart(),
1135 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001136 }
Steve Naroffca331292009-03-03 14:49:36 +00001137 // Make sure the names are identical.
1138 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001139 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001140 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001141 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001142 }
1143 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001144 }
Mike Stump1eb44332009-09-09 15:08:12 +00001145
Chris Lattner609e4c72007-12-12 18:11:49 +00001146 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001147 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001148 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001149 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001150}
1151
Steve Naroff3c2eb662008-02-10 21:38:56 +00001152void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001153 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001154 // No point warning no definition of method which is 'unavailable'.
1155 if (method->hasAttr<UnavailableAttr>())
1156 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001157 if (!IncompleteImpl) {
1158 Diag(ImpLoc, diag::warn_incomplete_impl);
1159 IncompleteImpl = true;
1160 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001161 if (DiagID == diag::warn_unimplemented_protocol_method)
1162 Diag(ImpLoc, DiagID) << method->getDeclName();
1163 else
1164 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001165}
1166
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001167/// Determines if type B can be substituted for type A. Returns true if we can
1168/// guarantee that anything that the user will do to an object of type A can
1169/// also be done to an object of type B. This is trivially true if the two
1170/// types are the same, or if B is a subclass of A. It becomes more complex
1171/// in cases where protocols are involved.
1172///
1173/// Object types in Objective-C describe the minimum requirements for an
1174/// object, rather than providing a complete description of a type. For
1175/// example, if A is a subclass of B, then B* may refer to an instance of A.
1176/// The principle of substitutability means that we may use an instance of A
1177/// anywhere that we may use an instance of B - it will implement all of the
1178/// ivars of B and all of the methods of B.
1179///
1180/// This substitutability is important when type checking methods, because
1181/// the implementation may have stricter type definitions than the interface.
1182/// The interface specifies minimum requirements, but the implementation may
1183/// have more accurate ones. For example, a method may privately accept
1184/// instances of B, but only publish that it accepts instances of A. Any
1185/// object passed to it will be type checked against B, and so will implicitly
1186/// by a valid A*. Similarly, a method may return a subclass of the class that
1187/// it is declared as returning.
1188///
1189/// This is most important when considering subclassing. A method in a
1190/// subclass must accept any object as an argument that its superclass's
1191/// implementation accepts. It may, however, accept a more general type
1192/// without breaking substitutability (i.e. you can still use the subclass
1193/// anywhere that you can use the superclass, but not vice versa). The
1194/// converse requirement applies to return types: the return type for a
1195/// subclass method must be a valid object of the kind that the superclass
1196/// advertises, but it may be specified more accurately. This avoids the need
1197/// for explicit down-casting by callers.
1198///
1199/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001200static bool isObjCTypeSubstitutable(ASTContext &Context,
1201 const ObjCObjectPointerType *A,
1202 const ObjCObjectPointerType *B,
1203 bool rejectId) {
1204 // Reject a protocol-unqualified id.
1205 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001206
1207 // If B is a qualified id, then A must also be a qualified id and it must
1208 // implement all of the protocols in B. It may not be a qualified class.
1209 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1210 // stricter definition so it is not substitutable for id<A>.
1211 if (B->isObjCQualifiedIdType()) {
1212 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001213 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1214 QualType(B,0),
1215 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001216 }
1217
1218 /*
1219 // id is a special type that bypasses type checking completely. We want a
1220 // warning when it is used in one place but not another.
1221 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1222
1223
1224 // If B is a qualified id, then A must also be a qualified id (which it isn't
1225 // if we've got this far)
1226 if (B->isObjCQualifiedIdType()) return false;
1227 */
1228
1229 // Now we know that A and B are (potentially-qualified) class types. The
1230 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001231 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001232}
1233
John McCall10302c02010-10-28 02:34:38 +00001234static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1235 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1236}
1237
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001238static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001239 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001240 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001241 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001242 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001243 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001244 if (IsProtocolMethodDecl &&
1245 (MethodDecl->getObjCDeclQualifier() !=
1246 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001247 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001248 S.Diag(MethodImpl->getLocation(),
1249 (IsOverridingMode ?
1250 diag::warn_conflicting_overriding_ret_type_modifiers
1251 : diag::warn_conflicting_ret_type_modifiers))
1252 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001253 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1254 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1255 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1256 }
1257 else
1258 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001259 }
1260
John McCall10302c02010-10-28 02:34:38 +00001261 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001262 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001263 return true;
1264 if (!Warn)
1265 return false;
John McCall10302c02010-10-28 02:34:38 +00001266
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001267 unsigned DiagID =
1268 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1269 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001270
1271 // Mismatches between ObjC pointers go into a different warning
1272 // category, and sometimes they're even completely whitelisted.
1273 if (const ObjCObjectPointerType *ImplPtrTy =
1274 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1275 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001276 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001277 // Allow non-matching return types as long as they don't violate
1278 // the principle of substitutability. Specifically, we permit
1279 // return types that are subclasses of the declared return type,
1280 // or that are more-qualified versions of the declared type.
1281 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001282 return false;
John McCall10302c02010-10-28 02:34:38 +00001283
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001284 DiagID =
1285 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1286 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001287 }
1288 }
1289
1290 S.Diag(MethodImpl->getLocation(), DiagID)
1291 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001292 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001293 << MethodImpl->getResultType()
1294 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001295 S.Diag(MethodDecl->getLocation(),
1296 IsOverridingMode ? diag::note_previous_declaration
1297 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001298 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001299 return false;
John McCall10302c02010-10-28 02:34:38 +00001300}
1301
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001302static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001303 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001304 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001305 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001306 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001307 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001308 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001309 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001310 if (IsProtocolMethodDecl &&
1311 (ImplVar->getObjCDeclQualifier() !=
1312 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001313 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001314 if (IsOverridingMode)
1315 S.Diag(ImplVar->getLocation(),
1316 diag::warn_conflicting_overriding_param_modifiers)
1317 << getTypeRange(ImplVar->getTypeSourceInfo())
1318 << MethodImpl->getDeclName();
1319 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001320 diag::warn_conflicting_param_modifiers)
1321 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001322 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001323 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1324 << getTypeRange(IfaceVar->getTypeSourceInfo());
1325 }
1326 else
1327 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001328 }
1329
John McCall10302c02010-10-28 02:34:38 +00001330 QualType ImplTy = ImplVar->getType();
1331 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001332
John McCall10302c02010-10-28 02:34:38 +00001333 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001334 return true;
1335
1336 if (!Warn)
1337 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001338 unsigned DiagID =
1339 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1340 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001341
1342 // Mismatches between ObjC pointers go into a different warning
1343 // category, and sometimes they're even completely whitelisted.
1344 if (const ObjCObjectPointerType *ImplPtrTy =
1345 ImplTy->getAs<ObjCObjectPointerType>()) {
1346 if (const ObjCObjectPointerType *IfacePtrTy =
1347 IfaceTy->getAs<ObjCObjectPointerType>()) {
1348 // Allow non-matching argument types as long as they don't
1349 // violate the principle of substitutability. Specifically, the
1350 // implementation must accept any objects that the superclass
1351 // accepts, however it may also accept others.
1352 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001353 return false;
John McCall10302c02010-10-28 02:34:38 +00001354
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001355 DiagID =
1356 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1357 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001358 }
1359 }
1360
1361 S.Diag(ImplVar->getLocation(), DiagID)
1362 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001363 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1364 S.Diag(IfaceVar->getLocation(),
1365 (IsOverridingMode ? diag::note_previous_declaration
1366 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001367 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001368 return false;
John McCall10302c02010-10-28 02:34:38 +00001369}
John McCallf85e1932011-06-15 23:02:42 +00001370
1371/// In ARC, check whether the conventional meanings of the two methods
1372/// match. If they don't, it's a hard error.
1373static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1374 ObjCMethodDecl *decl) {
1375 ObjCMethodFamily implFamily = impl->getMethodFamily();
1376 ObjCMethodFamily declFamily = decl->getMethodFamily();
1377 if (implFamily == declFamily) return false;
1378
1379 // Since conventions are sorted by selector, the only possibility is
1380 // that the types differ enough to cause one selector or the other
1381 // to fall out of the family.
1382 assert(implFamily == OMF_None || declFamily == OMF_None);
1383
1384 // No further diagnostics required on invalid declarations.
1385 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1386
1387 const ObjCMethodDecl *unmatched = impl;
1388 ObjCMethodFamily family = declFamily;
1389 unsigned errorID = diag::err_arc_lost_method_convention;
1390 unsigned noteID = diag::note_arc_lost_method_convention;
1391 if (declFamily == OMF_None) {
1392 unmatched = decl;
1393 family = implFamily;
1394 errorID = diag::err_arc_gained_method_convention;
1395 noteID = diag::note_arc_gained_method_convention;
1396 }
1397
1398 // Indexes into a %select clause in the diagnostic.
1399 enum FamilySelector {
1400 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1401 };
1402 FamilySelector familySelector = FamilySelector();
1403
1404 switch (family) {
1405 case OMF_None: llvm_unreachable("logic error, no method convention");
1406 case OMF_retain:
1407 case OMF_release:
1408 case OMF_autorelease:
1409 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001410 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001411 case OMF_retainCount:
1412 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001413 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001414 // Mismatches for these methods don't change ownership
1415 // conventions, so we don't care.
1416 return false;
1417
1418 case OMF_init: familySelector = F_init; break;
1419 case OMF_alloc: familySelector = F_alloc; break;
1420 case OMF_copy: familySelector = F_copy; break;
1421 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1422 case OMF_new: familySelector = F_new; break;
1423 }
1424
1425 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1426 ReasonSelector reasonSelector;
1427
1428 // The only reason these methods don't fall within their families is
1429 // due to unusual result types.
1430 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1431 reasonSelector = R_UnrelatedReturn;
1432 } else {
1433 reasonSelector = R_NonObjectReturn;
1434 }
1435
1436 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1437 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1438
1439 return true;
1440}
John McCall10302c02010-10-28 02:34:38 +00001441
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001442void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001443 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001444 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001445 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001446 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1447 return;
1448
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001449 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001450 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001451 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Chris Lattner3aff9192009-04-11 19:58:42 +00001453 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001454 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1455 EF = MethodDecl->param_end();
1456 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001457 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001458 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001459 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001460
Fariborz Jahanian21121902011-08-08 18:03:17 +00001461 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001462 Diag(ImpMethodDecl->getLocation(),
1463 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001464 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001465 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001466}
1467
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001468void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1469 ObjCMethodDecl *Overridden,
1470 bool IsProtocolMethodDecl) {
1471
1472 CheckMethodOverrideReturn(*this, Method, Overridden,
1473 IsProtocolMethodDecl, true,
1474 true);
1475
1476 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001477 IF = Overridden->param_begin(), EM = Method->param_end(),
1478 EF = Overridden->param_end();
1479 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001480 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1481 IsProtocolMethodDecl, true, true);
1482 }
1483
1484 if (Method->isVariadic() != Overridden->isVariadic()) {
1485 Diag(Method->getLocation(),
1486 diag::warn_conflicting_overriding_variadic);
1487 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1488 }
1489}
1490
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001491/// WarnExactTypedMethods - This routine issues a warning if method
1492/// implementation declaration matches exactly that of its declaration.
1493void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1494 ObjCMethodDecl *MethodDecl,
1495 bool IsProtocolMethodDecl) {
1496 // don't issue warning when protocol method is optional because primary
1497 // class is not required to implement it and it is safe for protocol
1498 // to implement it.
1499 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1500 return;
1501 // don't issue warning when primary class's method is
1502 // depecated/unavailable.
1503 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1504 MethodDecl->hasAttr<DeprecatedAttr>())
1505 return;
1506
1507 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1508 IsProtocolMethodDecl, false, false);
1509 if (match)
1510 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001511 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1512 EF = MethodDecl->param_end();
1513 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001514 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1515 *IM, *IF,
1516 IsProtocolMethodDecl, false, false);
1517 if (!match)
1518 break;
1519 }
1520 if (match)
1521 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001522 if (match)
1523 match = !(MethodDecl->isClassMethod() &&
1524 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001525
1526 if (match) {
1527 Diag(ImpMethodDecl->getLocation(),
1528 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001529 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1530 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001531 }
1532}
1533
Mike Stump390b4cc2009-05-16 07:39:55 +00001534/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1535/// improve the efficiency of selector lookups and type checking by associating
1536/// with each protocol / interface / category the flattened instance tables. If
1537/// we used an immutable set to keep the table then it wouldn't add significant
1538/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001539
Steve Naroffefe7f362008-02-08 22:06:17 +00001540/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001541/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001542void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1543 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001544 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001545 const SelectorSet &InsMap,
1546 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001547 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001548 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1549 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1550 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001551 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1552
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001553 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001554 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001555 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001556 // check to see if class implements forwardInvocation method and objects
1557 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001558 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001559 // Under such conditions, which means that every method possible is
1560 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001561 // found" warnings.
1562 // FIXME: Use a general GetUnarySelector method for this.
1563 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1564 Selector fISelector = Context.Selectors.getSelector(1, &II);
1565 if (InsMap.count(fISelector))
1566 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1567 // need be implemented in the implementation.
1568 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1569 }
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001571 // If a method lookup fails locally we still need to look and see if
1572 // the method was implemented by a base class or an inherited
1573 // protocol. This lookup is slow, but occurs rarely in correct code
1574 // and otherwise would terminate in a warning.
1575
Chris Lattner4d391482007-12-12 07:09:47 +00001576 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001577 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001578 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001579 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001580 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001581 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001582 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001583 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001584 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001585 // If a method is not implemented in the category implementation but
1586 // has been declared in its primary class, superclass,
1587 // or in one of their protocols, no need to issue the warning.
1588 // This is because method will be implemented in the primary class
1589 // or one of its super class implementation.
1590
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001591 // Ugly, but necessary. Method declared in protcol might have
1592 // have been synthesized due to a property declared in the class which
1593 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001594 if (ObjCMethodDecl *MethodInClass =
1595 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001596 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001597 if (C || MethodInClass->isSynthesized())
1598 continue;
1599 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1600 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1601 != DiagnosticsEngine::Ignored) {
1602 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001603 Diag(method->getLocation(), diag::note_method_declared_at)
1604 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001605 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1606 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001607 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001608 }
1609 }
Chris Lattner4d391482007-12-12 07:09:47 +00001610 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001611 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001612 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001613 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001614 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001615 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1616 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001617 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001618 // See above comment for instance method lookups.
1619 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001620 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001621 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001622 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001623 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1624 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001625 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001626 Diag(method->getLocation(), diag::note_method_declared_at)
1627 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001628 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1629 PDecl->getDeclName();
1630 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001631 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001632 }
Chris Lattner780f3292008-07-21 21:32:27 +00001633 // Check on this protocols's referenced protocols, recursively.
1634 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1635 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001636 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001637}
1638
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001639/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001640/// or protocol against those declared in their implementations.
1641///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001642void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1643 const SelectorSet &ClsMap,
1644 SelectorSet &InsMapSeen,
1645 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001646 ObjCImplDecl* IMPDecl,
1647 ObjCContainerDecl* CDecl,
1648 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001649 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001650 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001651 // Check and see if instance methods in class interface have been
1652 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001653 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1654 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001655 if (InsMapSeen.count((*I)->getSelector()))
1656 continue;
1657 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001658 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001659 !InsMap.count((*I)->getSelector())) {
1660 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001661 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1662 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001663 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001664 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001665 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001666 IMPDecl->getInstanceMethod((*I)->getSelector());
1667 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1668 "Expected to find the method through lookup as well");
1669 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001670 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001671 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001672 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001673 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1674 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001675 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001676 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001677 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001678 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001679 }
1680 }
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001682 // Check and see if class methods in class interface have been
1683 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001684 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001685 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001686 if (ClsMapSeen.count((*I)->getSelector()))
1687 continue;
1688 ClsMapSeen.insert((*I)->getSelector());
1689 if (!ClsMap.count((*I)->getSelector())) {
1690 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001691 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1692 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001693 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001694 ObjCMethodDecl *ImpMethodDecl =
1695 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001696 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1697 "Expected to find the method through lookup as well");
1698 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001699 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001700 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1701 isa<ObjCProtocolDecl>(CDecl));
1702 else
1703 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001704 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001705 }
1706 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001707
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001708 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001709 // Also methods in class extensions need be looked at next.
1710 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1711 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1712 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1713 IMPDecl,
1714 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001715 IncompleteImpl, false,
1716 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001717
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001718 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001719 for (ObjCInterfaceDecl::all_protocol_iterator
1720 PI = I->all_referenced_protocol_begin(),
1721 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001722 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1723 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001724 (*PI), IncompleteImpl, false,
1725 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001726
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001727 // FIXME. For now, we are not checking for extact match of methods
1728 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001729 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001730 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001731 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001732 I->getSuperClass(), IncompleteImpl, false);
1733 }
1734}
1735
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001736/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1737/// category matches with those implemented in its primary class and
1738/// warns each time an exact match is found.
1739void Sema::CheckCategoryVsClassMethodMatches(
1740 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001741 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001742
1743 for (ObjCImplementationDecl::instmeth_iterator
1744 I = CatIMPDecl->instmeth_begin(),
1745 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1746 InsMap.insert((*I)->getSelector());
1747
1748 for (ObjCImplementationDecl::classmeth_iterator
1749 I = CatIMPDecl->classmeth_begin(),
1750 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1751 ClsMap.insert((*I)->getSelector());
1752 if (InsMap.empty() && ClsMap.empty())
1753 return;
1754
1755 // Get category's primary class.
1756 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1757 if (!CatDecl)
1758 return;
1759 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1760 if (!IDecl)
1761 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001762 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001763 bool IncompleteImpl = false;
1764 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1765 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001766 IncompleteImpl, false,
1767 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001768}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001769
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001770void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001771 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001772 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001773 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001774 // Check and see if instance methods in class interface have been
1775 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001776 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001777 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001778 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001779
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001780 // Check and see if properties declared in the interface have either 1)
1781 // an implementation or 2) there is a @synthesize/@dynamic implementation
1782 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001783 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001784 if (!(LangOpts.ObjCDefaultSynthProperties &&
1785 LangOpts.ObjCRuntime.isNonFragile()) ||
1786 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001787 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001788
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001789 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001790 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001791 I = IMPDecl->classmeth_begin(),
1792 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001793 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001795 // Check for type conflict of methods declared in a class/protocol and
1796 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001797 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001798 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1799 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001800 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001801
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001802 // check all methods implemented in category against those declared
1803 // in its primary class.
1804 if (ObjCCategoryImplDecl *CatDecl =
1805 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1806 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Chris Lattner4d391482007-12-12 07:09:47 +00001808 // Check the protocol list for unimplemented methods in the @implementation
1809 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001810 // Check and see if class methods in class interface have been
1811 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Chris Lattnercddc8882009-03-01 00:56:52 +00001813 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001814 for (ObjCInterfaceDecl::all_protocol_iterator
1815 PI = I->all_referenced_protocol_begin(),
1816 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001817 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001818 InsMap, ClsMap, I);
1819 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001820 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1821 Categories; Categories = Categories->getNextClassExtension())
1822 ImplMethodsVsClassMethods(S, IMPDecl,
1823 const_cast<ObjCCategoryDecl*>(Categories),
1824 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001825 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001826 // For extended class, unimplemented methods in its protocols will
1827 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001828 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001829 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1830 E = C->protocol_end(); PI != E; ++PI)
1831 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001832 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001833 // Report unimplemented properties in the category as well.
1834 // When reporting on missing setter/getters, do not report when
1835 // setter/getter is implemented in category's primary class
1836 // implementation.
1837 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1838 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1839 for (ObjCImplementationDecl::instmeth_iterator
1840 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1841 InsMap.insert((*I)->getSelector());
1842 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001843 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001844 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001845 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001846 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001847}
1848
Mike Stump1eb44332009-09-09 15:08:12 +00001849/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001850Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001851Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001852 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001853 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001854 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001855 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001856 for (unsigned i = 0; i != NumElts; ++i) {
1857 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001858 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001859 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001860 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001861 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001862 // Maybe we will complain about the shadowed template parameter.
1863 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1864 // Just pretend that we didn't see the previous declaration.
1865 PrevDecl = 0;
1866 }
1867
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001868 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001869 // GCC apparently allows the following idiom:
1870 //
1871 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1872 // @class XCElementToggler;
1873 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001874 // Here we have chosen to ignore the forward class declaration
1875 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001876 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001877 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001878 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001879 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001880 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001881 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001882 // to the underlying class. Just ignore the forward class with a warning
1883 // as this will force the intended behavior which is to lookup the typedef
1884 // name.
1885 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1886 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1887 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1888 continue;
1889 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001890 }
Chris Lattner4d391482007-12-12 07:09:47 +00001891 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001892
1893 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001894 ObjCInterfaceDecl *PrevIDecl
1895 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001896 ObjCInterfaceDecl *IDecl
1897 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001898 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001899 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001900
Douglas Gregor7723fec2011-12-15 20:29:51 +00001901 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001902 CheckObjCDeclScope(IDecl);
1903 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001904 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001905
1906 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001907}
1908
John McCall0f4c4c42011-06-16 01:15:19 +00001909static bool tryMatchRecordTypes(ASTContext &Context,
1910 Sema::MethodMatchStrategy strategy,
1911 const Type *left, const Type *right);
1912
John McCallf85e1932011-06-15 23:02:42 +00001913static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1914 QualType leftQT, QualType rightQT) {
1915 const Type *left =
1916 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1917 const Type *right =
1918 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1919
1920 if (left == right) return true;
1921
1922 // If we're doing a strict match, the types have to match exactly.
1923 if (strategy == Sema::MMS_strict) return false;
1924
1925 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1926
1927 // Otherwise, use this absurdly complicated algorithm to try to
1928 // validate the basic, low-level compatibility of the two types.
1929
1930 // As a minimum, require the sizes and alignments to match.
1931 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1932 return false;
1933
1934 // Consider all the kinds of non-dependent canonical types:
1935 // - functions and arrays aren't possible as return and parameter types
1936
1937 // - vector types of equal size can be arbitrarily mixed
1938 if (isa<VectorType>(left)) return isa<VectorType>(right);
1939 if (isa<VectorType>(right)) return false;
1940
1941 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001942 // - structs, unions, and Objective-C objects must match more-or-less
1943 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001944 // - everything else should be a scalar
1945 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001946 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001947
John McCall1d9b3b22011-09-09 05:25:32 +00001948 // Make scalars agree in kind, except count bools as chars, and group
1949 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001950 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1951 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1952 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1953 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001954 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1955 leftSK = Type::STK_ObjCObjectPointer;
1956 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1957 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001958
1959 // Note that data member pointers and function member pointers don't
1960 // intermix because of the size differences.
1961
1962 return (leftSK == rightSK);
1963}
Chris Lattner4d391482007-12-12 07:09:47 +00001964
John McCall0f4c4c42011-06-16 01:15:19 +00001965static bool tryMatchRecordTypes(ASTContext &Context,
1966 Sema::MethodMatchStrategy strategy,
1967 const Type *lt, const Type *rt) {
1968 assert(lt && rt && lt != rt);
1969
1970 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1971 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1972 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1973
1974 // Require union-hood to match.
1975 if (left->isUnion() != right->isUnion()) return false;
1976
1977 // Require an exact match if either is non-POD.
1978 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1979 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1980 return false;
1981
1982 // Require size and alignment to match.
1983 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1984
1985 // Require fields to match.
1986 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1987 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1988 for (; li != le && ri != re; ++li, ++ri) {
1989 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1990 return false;
1991 }
1992 return (li == le && ri == re);
1993}
1994
Chris Lattner4d391482007-12-12 07:09:47 +00001995/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1996/// returns true, or false, accordingly.
1997/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001998bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1999 const ObjCMethodDecl *right,
2000 MethodMatchStrategy strategy) {
2001 if (!matchTypes(Context, strategy,
2002 left->getResultType(), right->getResultType()))
2003 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002004
David Blaikie4e4d0842012-03-11 07:00:24 +00002005 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002006 (left->hasAttr<NSReturnsRetainedAttr>()
2007 != right->hasAttr<NSReturnsRetainedAttr>() ||
2008 left->hasAttr<NSConsumesSelfAttr>()
2009 != right->hasAttr<NSConsumesSelfAttr>()))
2010 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002012 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002013 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2014 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002015
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002016 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002017 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002018 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002019
2020 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2021 return false;
2022
David Blaikie4e4d0842012-03-11 07:00:24 +00002023 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002024 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2025 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002026 }
2027 return true;
2028}
2029
Douglas Gregorff310c72012-05-01 23:37:00 +00002030void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002031 // If the list is empty, make it a singleton list.
2032 if (List->Method == 0) {
2033 List->Method = Method;
2034 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002035 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002036 }
2037
2038 // We've seen a method with this name, see if we have already seen this type
2039 // signature.
2040 ObjCMethodList *Previous = List;
2041 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002042 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002043 continue;
2044
2045 ObjCMethodDecl *PrevObjCMethod = List->Method;
2046
2047 // Propagate the 'defined' bit.
2048 if (Method->isDefined())
2049 PrevObjCMethod->setDefined(true);
2050
2051 // If a method is deprecated, push it in the global pool.
2052 // This is used for better diagnostics.
2053 if (Method->isDeprecated()) {
2054 if (!PrevObjCMethod->isDeprecated())
2055 List->Method = Method;
2056 }
2057 // If new method is unavailable, push it into global pool
2058 // unless previous one is deprecated.
2059 if (Method->isUnavailable()) {
2060 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2061 List->Method = Method;
2062 }
2063
Douglas Gregorff310c72012-05-01 23:37:00 +00002064 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002065 }
2066
2067 // We have a new signature for an existing method - add it.
2068 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002069 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002070 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2071}
2072
Sebastian Redldb9d2142010-08-02 23:18:59 +00002073/// \brief Read the contents of the method pool for a given selector from
2074/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002075void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002076 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002077 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002078}
2079
Douglas Gregorff310c72012-05-01 23:37:00 +00002080void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002081 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002082 // Ignore methods of invalid containers.
2083 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002084 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002085
Douglas Gregor0d266d62012-01-25 00:59:09 +00002086 if (ExternalSource)
2087 ReadMethodPool(Method->getSelector());
2088
Sebastian Redldb9d2142010-08-02 23:18:59 +00002089 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002090 if (Pos == MethodPool.end())
2091 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2092 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002093
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002094 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002095
Sebastian Redldb9d2142010-08-02 23:18:59 +00002096 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002097 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002098}
2099
John McCallf85e1932011-06-15 23:02:42 +00002100/// Determines if this is an "acceptable" loose mismatch in the global
2101/// method pool. This exists mostly as a hack to get around certain
2102/// global mismatches which we can't afford to make warnings / errors.
2103/// Really, what we want is a way to take a method out of the global
2104/// method pool.
2105static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2106 ObjCMethodDecl *other) {
2107 if (!chosen->isInstanceMethod())
2108 return false;
2109
2110 Selector sel = chosen->getSelector();
2111 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2112 return false;
2113
2114 // Don't complain about mismatches for -length if the method we
2115 // chose has an integral result type.
2116 return (chosen->getResultType()->isIntegerType());
2117}
2118
Sebastian Redldb9d2142010-08-02 23:18:59 +00002119ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002120 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002121 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002122 if (ExternalSource)
2123 ReadMethodPool(Sel);
2124
Sebastian Redldb9d2142010-08-02 23:18:59 +00002125 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002126 if (Pos == MethodPool.end())
2127 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002128
Sebastian Redldb9d2142010-08-02 23:18:59 +00002129 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002130
Sebastian Redldb9d2142010-08-02 23:18:59 +00002131 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002132 bool issueDiagnostic = false, issueError = false;
2133
2134 // We support a warning which complains about *any* difference in
2135 // method signature.
2136 bool strictSelectorMatch =
2137 (receiverIdOrClass && warn &&
2138 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2139 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002140 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002141 if (strictSelectorMatch)
2142 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002143 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2144 MMS_strict)) {
2145 issueDiagnostic = true;
2146 break;
2147 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002148 }
2149
John McCallf85e1932011-06-15 23:02:42 +00002150 // If we didn't see any strict differences, we won't see any loose
2151 // differences. In ARC, however, we also need to check for loose
2152 // mismatches, because most of them are errors.
2153 if (!strictSelectorMatch ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002154 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002155 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002156 // This checks if the methods differ in type mismatch.
2157 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2158 MMS_loose) &&
2159 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2160 issueDiagnostic = true;
David Blaikie4e4d0842012-03-11 07:00:24 +00002161 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002162 issueError = true;
2163 break;
2164 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002165 }
2166
John McCallf85e1932011-06-15 23:02:42 +00002167 if (issueDiagnostic) {
2168 if (issueError)
2169 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2170 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002171 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2172 else
2173 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002174
2175 Diag(MethList.Method->getLocStart(),
2176 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002177 << MethList.Method->getSourceRange();
2178 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2179 Diag(Next->Method->getLocStart(), diag::note_also_found)
2180 << Next->Method->getSourceRange();
2181 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002182 }
2183 return MethList.Method;
2184}
2185
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002186ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002187 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2188 if (Pos == MethodPool.end())
2189 return 0;
2190
2191 GlobalMethods &Methods = Pos->second;
2192
2193 if (Methods.first.Method && Methods.first.Method->isDefined())
2194 return Methods.first.Method;
2195 if (Methods.second.Method && Methods.second.Method->isDefined())
2196 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002197 return 0;
2198}
2199
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002200/// DiagnoseDuplicateIvars -
2201/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002202/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002203/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002204/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002205void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2206 ObjCInterfaceDecl *SID) {
2207 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2208 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002209 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002210 if (Ivar->isInvalidDecl())
2211 continue;
2212 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2213 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2214 if (prevIvar) {
2215 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2216 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2217 Ivar->setInvalidDecl();
2218 }
2219 }
2220 }
2221}
2222
Erik Verbruggend64251f2011-12-06 09:25:23 +00002223Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2224 switch (CurContext->getDeclKind()) {
2225 case Decl::ObjCInterface:
2226 return Sema::OCK_Interface;
2227 case Decl::ObjCProtocol:
2228 return Sema::OCK_Protocol;
2229 case Decl::ObjCCategory:
2230 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2231 return Sema::OCK_ClassExtension;
2232 else
2233 return Sema::OCK_Category;
2234 case Decl::ObjCImplementation:
2235 return Sema::OCK_Implementation;
2236 case Decl::ObjCCategoryImpl:
2237 return Sema::OCK_CategoryImplementation;
2238
2239 default:
2240 return Sema::OCK_None;
2241 }
2242}
2243
Steve Naroffa56f6162007-12-18 01:30:32 +00002244// Note: For class/category implemenations, allMethods/allProperties is
2245// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002246Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2247 Decl **allMethods, unsigned allNum,
2248 Decl **allProperties, unsigned pNum,
2249 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002250
Erik Verbruggend64251f2011-12-06 09:25:23 +00002251 if (getObjCContainerKind() == Sema::OCK_None)
2252 return 0;
2253
2254 assert(AtEnd.isValid() && "Invalid location for '@end'");
2255
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002256 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2257 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002258
Mike Stump1eb44332009-09-09 15:08:12 +00002259 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002260 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2261 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002262 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002263
Steve Naroff0701bbb2009-01-08 17:28:14 +00002264 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2265 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2266 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2267
Chris Lattner4d391482007-12-12 07:09:47 +00002268 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002269 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002270 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002271
2272 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002273 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002274 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002275 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002276 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002277 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002278 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002279 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002280 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002281 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002282 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002283 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002284 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002285 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002286 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002287 if (!Context.getSourceManager().isInSystemHeader(
2288 Method->getLocation()))
2289 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2290 << Method->getDeclName();
2291 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2292 }
Chris Lattner4d391482007-12-12 07:09:47 +00002293 InsMap[Method->getSelector()] = Method;
2294 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002295 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002296 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002297 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002298 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002299 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002300 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002301 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002302 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002303 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002304 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002305 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002306 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002307 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002308 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002309 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002310 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002311 if (!Context.getSourceManager().isInSystemHeader(
2312 Method->getLocation()))
2313 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2314 << Method->getDeclName();
2315 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2316 }
Chris Lattner4d391482007-12-12 07:09:47 +00002317 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002318 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002319 }
2320 }
2321 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002322 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002323 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002324 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002325 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002326 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002327 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002328 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002329 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002330 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002331
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002332 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002333 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002334 if (C->IsClassExtension()) {
2335 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2336 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002337 }
Chris Lattner4d391482007-12-12 07:09:47 +00002338 }
Steve Naroff09c47192009-01-09 15:36:25 +00002339 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002340 if (CDecl->getIdentifier())
2341 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2342 // user-defined setter/getter. It also synthesizes setter/getter methods
2343 // and adds them to the DeclContext and global method pools.
2344 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2345 E = CDecl->prop_end();
2346 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002347 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002348 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002349 }
2350 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002351 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002352 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002353 // Any property declared in a class extension might have user
2354 // declared setter or getter in current class extension or one
2355 // of the other class extensions. Mark them as synthesized as
2356 // property will be synthesized when property with same name is
2357 // seen in the @implementation.
2358 for (const ObjCCategoryDecl *ClsExtDecl =
2359 IDecl->getFirstClassExtension();
2360 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2361 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2362 E = ClsExtDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002363 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002364 // Skip over properties declared @dynamic
2365 if (const ObjCPropertyImplDecl *PIDecl
2366 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2367 if (PIDecl->getPropertyImplementation()
2368 == ObjCPropertyImplDecl::Dynamic)
2369 continue;
2370
2371 for (const ObjCCategoryDecl *CExtDecl =
2372 IDecl->getFirstClassExtension();
2373 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2374 if (ObjCMethodDecl *GetterMethod =
2375 CExtDecl->getInstanceMethod(Property->getGetterName()))
2376 GetterMethod->setSynthesized(true);
2377 if (!Property->isReadOnly())
2378 if (ObjCMethodDecl *SetterMethod =
2379 CExtDecl->getInstanceMethod(Property->getSetterName()))
2380 SetterMethod->setSynthesized(true);
2381 }
2382 }
2383 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002384 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002385 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002386 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002387
Patrick Beardb2f68202012-04-06 18:12:22 +00002388 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2389 if (IDecl->getSuperClass() == NULL) {
2390 // This class has no superclass, so check that it has been marked with
2391 // __attribute((objc_root_class)).
2392 if (!HasRootClassAttr) {
2393 SourceLocation DeclLoc(IDecl->getLocation());
2394 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2395 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2396 << IDecl->getIdentifier();
2397 // See if NSObject is in the current scope, and if it is, suggest
2398 // adding " : NSObject " to the class declaration.
2399 NamedDecl *IF = LookupSingleName(TUScope,
2400 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2401 DeclLoc, LookupOrdinaryName);
2402 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2403 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2404 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2405 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2406 } else {
2407 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2408 }
2409 }
2410 } else if (HasRootClassAttr) {
2411 // Complain that only root classes may have this attribute.
2412 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2413 }
2414
John McCall260611a2012-06-20 06:18:46 +00002415 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002416 while (IDecl->getSuperClass()) {
2417 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2418 IDecl = IDecl->getSuperClass();
2419 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002420 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002421 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002422 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002423 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002424 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002425 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002426
Chris Lattner4d391482007-12-12 07:09:47 +00002427 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002428 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002429 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002430 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002431 Categories; Categories = Categories->getNextClassCategory()) {
2432 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002433 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002434 break;
2435 }
2436 }
2437 }
2438 }
Chris Lattner682bf922009-03-29 16:50:03 +00002439 if (isInterfaceDeclKind) {
2440 // Reject invalid vardecls.
2441 for (unsigned i = 0; i != tuvNum; i++) {
2442 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2443 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2444 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002445 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002446 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002447 }
Chris Lattner682bf922009-03-29 16:50:03 +00002448 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002449 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002450 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002451
2452 for (unsigned i = 0; i != tuvNum; i++) {
2453 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002454 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2455 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002456 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2457 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002458
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002459 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002460 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002461}
2462
2463
2464/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2465/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002466static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002467CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002468 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002469}
2470
Ted Kremenek422bae72010-04-18 04:59:38 +00002471static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002472unsigned countAlignAttr(const AttrVec &A) {
2473 unsigned count=0;
2474 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2475 if ((*i)->getKind() == attr::Aligned)
2476 ++count;
2477 return count;
2478}
2479
2480static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002481bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2482 const AttrVec &A) {
2483 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002484 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002485 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002486 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002487
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002488 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002489 // But implementation has attributes. This is invalid.
2490 // Except when implementation has 'Align' attribute which is
2491 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002492 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002493 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002494
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002495 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002496
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002497 unsigned countAlignOnImpl = countAlignAttr(A);
2498 if (!countAlignOnImpl && (A.size() != D.size()))
2499 return true;
2500 else if (countAlignOnImpl) {
2501 unsigned countAlignOnDecl = countAlignAttr(D);
2502 if (countAlignOnDecl && (A.size() != D.size()))
2503 return true;
2504 else if (!countAlignOnDecl &&
2505 ((A.size()-countAlignOnImpl) != D.size()))
2506 return true;
2507 }
2508
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002509 // attributes on method declaration and definition must match exactly.
2510 // Note that we have at most a couple of attributes on methods, so this
2511 // n*n search is good enough.
2512 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002513 if ((*i)->getKind() == attr::Aligned)
2514 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002515 bool match = false;
2516 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2517 if ((*i)->getKind() == (*i1)->getKind()) {
2518 match = true;
2519 break;
2520 }
2521 }
2522 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002523 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002524 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002525
Sean Huntcf807c42010-08-18 23:23:40 +00002526 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002527}
2528
Douglas Gregor926df6c2011-06-11 01:09:30 +00002529/// \brief Check whether the declared result type of the given Objective-C
2530/// method declaration is compatible with the method's class.
2531///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002532static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002533CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2534 ObjCInterfaceDecl *CurrentClass) {
2535 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002536
2537 // If an Objective-C method inherits its related result type, then its
2538 // declared result type must be compatible with its own class type. The
2539 // declared result type is compatible if:
2540 if (const ObjCObjectPointerType *ResultObjectType
2541 = ResultType->getAs<ObjCObjectPointerType>()) {
2542 // - it is id or qualified id, or
2543 if (ResultObjectType->isObjCIdType() ||
2544 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002545 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002546
2547 if (CurrentClass) {
2548 if (ObjCInterfaceDecl *ResultClass
2549 = ResultObjectType->getInterfaceDecl()) {
2550 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002551 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002552 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002553
2554 // - it is a superclass of the method's class type
2555 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002556 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002557 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002558 } else {
2559 // Any Objective-C pointer type might be acceptable for a protocol
2560 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002561 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002562 }
2563 }
2564
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002565 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002566}
2567
John McCall6c2c2502011-07-22 02:45:48 +00002568namespace {
2569/// A helper class for searching for methods which a particular method
2570/// overrides.
2571class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002572public:
John McCall6c2c2502011-07-22 02:45:48 +00002573 Sema &S;
2574 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002575 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002576 bool Recursive;
2577
2578public:
2579 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2580 Selector selector = method->getSelector();
2581
2582 // Bypass this search if we've never seen an instance/class method
2583 // with this selector before.
2584 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2585 if (it == S.MethodPool.end()) {
2586 if (!S.ExternalSource) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002587 S.ReadMethodPool(selector);
2588
2589 it = S.MethodPool.find(selector);
2590 if (it == S.MethodPool.end())
2591 return;
John McCall6c2c2502011-07-22 02:45:48 +00002592 }
2593 ObjCMethodList &list =
2594 method->isInstanceMethod() ? it->second.first : it->second.second;
2595 if (!list.Method) return;
2596
2597 ObjCContainerDecl *container
2598 = cast<ObjCContainerDecl>(method->getDeclContext());
2599
2600 // Prevent the search from reaching this container again. This is
2601 // important with categories, which override methods from the
2602 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002603 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2604 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002605 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2606 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002607 } else {
2608 searchFromContainer(container);
2609 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002610 }
John McCall6c2c2502011-07-22 02:45:48 +00002611
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002612 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002613 iterator begin() const { return Overridden.begin(); }
2614 iterator end() const { return Overridden.end(); }
2615
2616private:
2617 void searchFromContainer(ObjCContainerDecl *container) {
2618 if (container->isInvalidDecl()) return;
2619
2620 switch (container->getDeclKind()) {
2621#define OBJCCONTAINER(type, base) \
2622 case Decl::type: \
2623 searchFrom(cast<type##Decl>(container)); \
2624 break;
2625#define ABSTRACT_DECL(expansion)
2626#define DECL(type, base) \
2627 case Decl::type:
2628#include "clang/AST/DeclNodes.inc"
2629 llvm_unreachable("not an ObjC container!");
2630 }
2631 }
2632
2633 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002634 if (!protocol->hasDefinition())
2635 return;
2636
John McCall6c2c2502011-07-22 02:45:48 +00002637 // A method in a protocol declaration overrides declarations from
2638 // referenced ("parent") protocols.
2639 search(protocol->getReferencedProtocols());
2640 }
2641
2642 void searchFrom(ObjCCategoryDecl *category) {
2643 // A method in a category declaration overrides declarations from
2644 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002645 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002646 search(category->getReferencedProtocols());
2647 }
2648
2649 void searchFrom(ObjCCategoryImplDecl *impl) {
2650 // A method in a category definition that has a category
2651 // declaration overrides declarations from the category
2652 // declaration.
2653 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2654 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002655 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2656 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002657
2658 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002659 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2660 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002661 }
2662 }
2663
2664 void searchFrom(ObjCInterfaceDecl *iface) {
2665 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002666 if (!iface->hasDefinition())
2667 return;
2668
John McCall6c2c2502011-07-22 02:45:48 +00002669 // - categories,
2670 for (ObjCCategoryDecl *category = iface->getCategoryList();
2671 category; category = category->getNextClassCategory())
2672 search(category);
2673
2674 // - the super class, and
2675 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2676 search(super);
2677
2678 // - any referenced protocols.
2679 search(iface->getReferencedProtocols());
2680 }
2681
2682 void searchFrom(ObjCImplementationDecl *impl) {
2683 // A method in a class implementation overrides declarations from
2684 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002685 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2686 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002687 }
2688
2689
2690 void search(const ObjCProtocolList &protocols) {
2691 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2692 i != e; ++i)
2693 search(*i);
2694 }
2695
2696 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002697 // Check for a method in this container which matches this selector.
2698 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2699 Method->isInstanceMethod());
2700
2701 // If we find one, record it and bail out.
2702 if (meth) {
2703 Overridden.insert(meth);
2704 return;
2705 }
2706
2707 // Otherwise, search for methods that a hypothetical method here
2708 // would have overridden.
2709
2710 // Note that we're now in a recursive case.
2711 Recursive = true;
2712
2713 searchFromContainer(container);
2714 }
2715};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002716}
2717
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002718void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2719 ObjCInterfaceDecl *CurrentClass,
2720 ResultTypeCompatibilityKind RTC) {
2721 // Search for overridden methods and merge information down from them.
2722 OverrideSearch overrides(*this, ObjCMethod);
2723 // Keep track if the method overrides any method in the class's base classes,
2724 // its protocols, or its categories' protocols; we will keep that info
2725 // in the ObjCMethodDecl.
2726 // For this info, a method in an implementation is not considered as
2727 // overriding the same method in the interface or its categories.
2728 bool hasOverriddenMethodsInBaseOrProtocol = false;
2729 for (OverrideSearch::iterator
2730 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2731 ObjCMethodDecl *overridden = *i;
2732
2733 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2734 CurrentClass != overridden->getClassInterface() ||
2735 overridden->isOverriding())
2736 hasOverriddenMethodsInBaseOrProtocol = true;
2737
2738 // Propagate down the 'related result type' bit from overridden methods.
2739 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2740 ObjCMethod->SetRelatedResultType();
2741
2742 // Then merge the declarations.
2743 mergeObjCMethodDecls(ObjCMethod, overridden);
2744
2745 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2746 continue; // Conflicting properties are detected elsewhere.
2747
2748 // Check for overriding methods
2749 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2750 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2751 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2752 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2753
2754 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00002755 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
2756 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002757 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2758 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002759 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2760 PrevE = overridden->param_end();
2761 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002762 assert(PrevI != overridden->param_end() && "Param mismatch");
2763 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2764 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2765 // If type of argument of method in this class does not match its
2766 // respective argument type in the super class method, issue warning;
2767 if (!Context.typesAreCompatible(T1, T2)) {
2768 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2769 << T1 << T2;
2770 Diag(overridden->getLocation(), diag::note_previous_declaration);
2771 break;
2772 }
2773 }
2774 }
2775 }
2776
2777 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2778}
2779
John McCalld226f652010-08-21 09:40:31 +00002780Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002781 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002782 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002783 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002784 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002785 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002786 Selector Sel,
2787 // optional arguments. The number of types/arguments is obtained
2788 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002789 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002790 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002791 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002792 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002793 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002794 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002795 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002796 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002797 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002798 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2799 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002800 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002801
Douglas Gregore97179c2011-09-08 01:46:34 +00002802 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002803 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002804 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002805 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002806
Steve Naroffccef3712009-02-20 22:59:16 +00002807 // Methods cannot return interface types. All ObjC objects are
2808 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002809 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002810 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2811 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002812 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002813 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002814
2815 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002816 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002817 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002818 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002819 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002820 }
Mike Stump1eb44332009-09-09 15:08:12 +00002821
2822 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002823 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002824 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002825 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002826 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002827 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002828 /*isSynthesized=*/false,
2829 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002830 MethodDeclKind == tok::objc_optional
2831 ? ObjCMethodDecl::Optional
2832 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002833 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002834
Chris Lattner5f9e2722011-07-23 10:55:15 +00002835 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002836
Chris Lattner7db638d2009-04-11 19:42:43 +00002837 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002838 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002839 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002840
Chris Lattnere294d3f2009-04-11 18:57:04 +00002841 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002842 ArgType = Context.getObjCIdType();
2843 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002844 } else {
John McCall58e46772009-10-23 21:48:59 +00002845 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002846 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002847 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002848 }
Mike Stump1eb44332009-09-09 15:08:12 +00002849
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002850 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2851 LookupOrdinaryName, ForRedeclaration);
2852 LookupName(R, S);
2853 if (R.isSingleResult()) {
2854 NamedDecl *PrevDecl = R.getFoundDecl();
2855 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002856 Diag(ArgInfo[i].NameLoc,
2857 (MethodDefinition ? diag::warn_method_param_redefinition
2858 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002859 << ArgInfo[i].Name;
2860 Diag(PrevDecl->getLocation(),
2861 diag::note_previous_declaration);
2862 }
2863 }
2864
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002865 SourceLocation StartLoc = DI
2866 ? DI->getTypeLoc().getBeginLoc()
2867 : ArgInfo[i].NameLoc;
2868
John McCall81ef3e62011-04-23 02:46:06 +00002869 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2870 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2871 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002872
John McCall70798862011-05-02 00:30:12 +00002873 Param->setObjCMethodScopeInfo(i);
2874
Chris Lattner0ed844b2008-04-04 06:12:32 +00002875 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002876 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002877
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002878 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002879 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002880
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002881 if (Param->hasAttr<BlocksAttr>()) {
2882 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2883 Param->setInvalidDecl();
2884 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002885 S->AddDecl(Param);
2886 IdResolver.AddDecl(Param);
2887
Chris Lattner0ed844b2008-04-04 06:12:32 +00002888 Params.push_back(Param);
2889 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002890
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002891 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002892 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002893 QualType ArgType = Param->getType();
2894 if (ArgType.isNull())
2895 ArgType = Context.getObjCIdType();
2896 else
2897 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002898 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002899 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002900 Diag(Param->getLocation(),
2901 diag::err_object_cannot_be_passed_returned_by_value)
2902 << 1 << ArgType;
2903 Param->setInvalidDecl();
2904 }
2905 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002906
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002907 Params.push_back(Param);
2908 }
2909
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002910 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002911 ObjCMethod->setObjCDeclQualifier(
2912 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002913
2914 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002915 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002916
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002917 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002918 const ObjCMethodDecl *PrevMethod = 0;
2919 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002920 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002921 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2922 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002923 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002924 PrevMethod = ImpDecl->getClassMethod(Sel);
2925 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002926 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002927
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002928 ObjCMethodDecl *IMD = 0;
2929 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2930 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2931 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002932 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002933 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002934 SourceLocation MethodLoc = IMD->getLocation();
2935 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2936 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002937 Diag(MethodLoc, diag::note_method_declared_at)
2938 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002939 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002940 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002941 } else {
2942 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002943 }
John McCall6c2c2502011-07-22 02:45:48 +00002944
Chris Lattner4d391482007-12-12 07:09:47 +00002945 if (PrevMethod) {
2946 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002947 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002948 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002949 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002950 }
John McCall54abf7d2009-11-04 02:18:39 +00002951
Douglas Gregor926df6c2011-06-11 01:09:30 +00002952 // If this Objective-C method does not have a related result type, but we
2953 // are allowed to infer related result types, try to do so based on the
2954 // method family.
2955 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2956 if (!CurrentClass) {
2957 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2958 CurrentClass = Cat->getClassInterface();
2959 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2960 CurrentClass = Impl->getClassInterface();
2961 else if (ObjCCategoryImplDecl *CatImpl
2962 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2963 CurrentClass = CatImpl->getClassInterface();
2964 }
John McCall6c2c2502011-07-22 02:45:48 +00002965
Douglas Gregore97179c2011-09-08 01:46:34 +00002966 ResultTypeCompatibilityKind RTC
2967 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002968
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002969 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00002970
John McCallf85e1932011-06-15 23:02:42 +00002971 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002972 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002973 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2974
Douglas Gregore97179c2011-09-08 01:46:34 +00002975 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002976 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00002977 !ObjCMethod->hasRelatedResultType() &&
2978 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002979 bool InferRelatedResultType = false;
2980 switch (ObjCMethod->getMethodFamily()) {
2981 case OMF_None:
2982 case OMF_copy:
2983 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002984 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002985 case OMF_mutableCopy:
2986 case OMF_release:
2987 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002988 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002989 break;
2990
2991 case OMF_alloc:
2992 case OMF_new:
2993 InferRelatedResultType = ObjCMethod->isClassMethod();
2994 break;
2995
2996 case OMF_init:
2997 case OMF_autorelease:
2998 case OMF_retain:
2999 case OMF_self:
3000 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3001 break;
3002 }
3003
John McCall6c2c2502011-07-22 02:45:48 +00003004 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003005 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003006 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003007
3008 ActOnDocumentableDecl(ObjCMethod);
3009
John McCalld226f652010-08-21 09:40:31 +00003010 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003011}
3012
Chris Lattnercc98eac2008-12-17 07:13:27 +00003013bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003014 // Following is also an error. But it is caused by a missing @end
3015 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003016 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003017 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003018
3019 // If we switched context to translation unit while we are still lexically in
3020 // an objc container, it means the parser missed emitting an error.
3021 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3022 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003023
Anders Carlsson15281452008-11-04 16:57:32 +00003024 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3025 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003026
Anders Carlsson15281452008-11-04 16:57:32 +00003027 return true;
3028}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003029
James Dennett1dfbd922012-06-14 21:40:34 +00003030/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003031/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003032void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003033 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003034 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003035 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003036 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003037 if (!Class) {
3038 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3039 return;
3040 }
John McCall260611a2012-06-20 06:18:46 +00003041 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003042 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3043 return;
3044 }
Mike Stump1eb44332009-09-09 15:08:12 +00003045
Chris Lattnercc98eac2008-12-17 07:13:27 +00003046 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003047 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003048 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003049 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003050 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003051 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003052 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003053 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3054 /*FIXME: StartL=*/ID->getLocation(),
3055 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003056 ID->getIdentifier(), ID->getType(),
3057 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003058 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003059 }
Mike Stump1eb44332009-09-09 15:08:12 +00003060
Chris Lattnercc98eac2008-12-17 07:13:27 +00003061 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003062 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003063 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003064 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003065 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003066 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003067 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003068 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003069 }
3070}
3071
Douglas Gregor160b5632010-04-26 17:32:49 +00003072/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003073VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3074 SourceLocation StartLoc,
3075 SourceLocation IdLoc,
3076 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003077 bool Invalid) {
3078 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3079 // duration shall not be qualified by an address-space qualifier."
3080 // Since all parameters have automatic store duration, they can not have
3081 // an address space.
3082 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003083 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003084 Invalid = true;
3085 }
3086
3087 // An @catch parameter must be an unqualified object pointer type;
3088 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3089 if (Invalid) {
3090 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003091 } else if (T->isDependentType()) {
3092 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003093 } else if (!T->isObjCObjectPointerType()) {
3094 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003095 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003096 } else if (T->isObjCQualifiedIdType()) {
3097 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003098 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003099 }
3100
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003101 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3102 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003103 New->setExceptionVariable(true);
3104
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003105 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003106 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003107 Invalid = true;
3108
Douglas Gregor160b5632010-04-26 17:32:49 +00003109 if (Invalid)
3110 New->setInvalidDecl();
3111 return New;
3112}
3113
John McCalld226f652010-08-21 09:40:31 +00003114Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003115 const DeclSpec &DS = D.getDeclSpec();
3116
3117 // We allow the "register" storage class on exception variables because
3118 // GCC did, but we drop it completely. Any other storage class is an error.
3119 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3120 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3121 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3122 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3123 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3124 << DS.getStorageClassSpec();
3125 }
3126 if (D.getDeclSpec().isThreadSpecified())
3127 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3128 D.getMutableDeclSpec().ClearStorageClassSpecs();
3129
3130 DiagnoseFunctionSpecifiers(D);
3131
3132 // Check that there are no default arguments inside the type of this
3133 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003134 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003135 CheckExtraCXXDefaultArguments(D);
3136
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003137 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003138 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003139
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003140 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3141 D.getSourceRange().getBegin(),
3142 D.getIdentifierLoc(),
3143 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003144 D.isInvalidType());
3145
3146 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3147 if (D.getCXXScopeSpec().isSet()) {
3148 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3149 << D.getCXXScopeSpec().getRange();
3150 New->setInvalidDecl();
3151 }
3152
3153 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003154 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003155 if (D.getIdentifier())
3156 IdResolver.AddDecl(New);
3157
3158 ProcessDeclAttributes(S, New, D);
3159
3160 if (New->hasAttr<BlocksAttr>())
3161 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003162 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003163}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003164
3165/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003166/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003167void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003168 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003169 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3170 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003171 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003172 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003173 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003174 }
3175}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003176
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003177void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003178 // Load referenced selectors from the external source.
3179 if (ExternalSource) {
3180 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3181 ExternalSource->ReadReferencedSelectors(Sels);
3182 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3183 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3184 }
3185
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003186 // Warning will be issued only when selector table is
3187 // generated (which means there is at lease one implementation
3188 // in the TU). This is to match gcc's behavior.
3189 if (ReferencedSelectors.empty() ||
3190 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003191 return;
3192 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3193 ReferencedSelectors.begin(),
3194 E = ReferencedSelectors.end(); S != E; ++S) {
3195 Selector Sel = (*S).first;
3196 if (!LookupImplementedMethodInGlobalPool(Sel))
3197 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3198 }
3199 return;
3200}