blob: 709a868582696b5df36230696b21ef3eb441aa8d [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;
391 if (!getCurFunction()->ObjCShouldCallSuperDealloc)
392 getCurFunction()->ObjCShouldCallSuperDealloc =
393 (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>());
Eli Friedman95aac152012-08-01 21:02:59 +0000394 getCurFunction()->ObjCShouldCallSuperFinalize =
David Blaikie4e4d0842012-03-11 07:00:24 +0000395 Context.getLangOpts().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000396 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000397 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000398 }
Chris Lattner4d391482007-12-12 07:09:47 +0000399}
400
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000401namespace {
402
403// Callback to only accept typo corrections that are Objective-C classes.
404// If an ObjCInterfaceDecl* is given to the constructor, then the validation
405// function will reject corrections to that class.
406class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
407 public:
408 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
409 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
410 : CurrentIDecl(IDecl) {}
411
412 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
413 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
414 return ID && !declaresSameEntity(ID, CurrentIDecl);
415 }
416
417 private:
418 ObjCInterfaceDecl *CurrentIDecl;
419};
420
421}
422
John McCalld226f652010-08-21 09:40:31 +0000423Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000424ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
425 IdentifierInfo *ClassName, SourceLocation ClassLoc,
426 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000427 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000428 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000429 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000430 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000431
Chris Lattner4d391482007-12-12 07:09:47 +0000432 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000433 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000434 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000435
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000436 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000437 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000438 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000439 }
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Douglas Gregor7723fec2011-12-15 20:29:51 +0000441 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000442 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000443 ObjCInterfaceDecl *IDecl
444 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000445 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000446
Douglas Gregor7723fec2011-12-15 20:29:51 +0000447 if (PrevIDecl) {
448 // Class already seen. Was it a definition?
449 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
450 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
451 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000452 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000453 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000454 }
Chris Lattner4d391482007-12-12 07:09:47 +0000455 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000456
457 if (AttrList)
458 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
459 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Douglas Gregor7723fec2011-12-15 20:29:51 +0000461 // Start the definition of this class. If we're in a redefinition case, there
462 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000463 if (!IDecl->hasDefinition())
464 IDecl->startDefinition();
465
Chris Lattner4d391482007-12-12 07:09:47 +0000466 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000467 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000468 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
469 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000470
471 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000472 // Try to correct for a typo in the superclass name without correcting
473 // to the class we're defining.
474 ObjCInterfaceValidatorCCC Validator(IDecl);
475 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000476 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000477 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000478 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
479 Diag(SuperLoc, diag::err_undef_superclass_suggest)
480 << SuperName << ClassName << PrevDecl->getDeclName();
481 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
482 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000483 }
484 }
485
Douglas Gregor60ef3082011-12-15 00:29:59 +0000486 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000487 Diag(SuperLoc, diag::err_recursive_superclass)
488 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000489 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000490 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000491 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000492 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000493
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000494 // Diagnose classes that inherit from deprecated classes.
495 if (SuperClassDecl)
496 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000497
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000498 if (PrevDecl && SuperClassDecl == 0) {
499 // The previous declaration was not a class decl. Check if we have a
500 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000501 if (const TypedefNameDecl *TDecl =
502 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000503 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000504 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000505 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
506 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000507 }
508 }
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000510 // This handles the following case:
511 //
512 // typedef int SuperClass;
513 // @interface MyClass : SuperClass {} @end
514 //
515 if (!SuperClassDecl) {
516 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
517 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000518 }
519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Richard Smith162e1c12011-04-15 14:24:37 +0000521 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000522 if (!SuperClassDecl)
523 Diag(SuperLoc, diag::err_undef_superclass)
524 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000525 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000526 Context.getObjCInterfaceType(SuperClassDecl),
527 diag::err_forward_superclass,
528 SuperClassDecl->getDeclName(),
529 ClassName,
530 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000531 SuperClassDecl = 0;
532 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000533 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000534 IDecl->setSuperClass(SuperClassDecl);
535 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000536 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000537 }
Chris Lattner4d391482007-12-12 07:09:47 +0000538 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000539 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000540 }
Mike Stump1eb44332009-09-09 15:08:12 +0000541
Sebastian Redl0b17c612010-08-13 00:28:03 +0000542 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000543 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000544 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000545 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000546 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000547 }
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Anders Carlsson15281452008-11-04 16:57:32 +0000549 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000550 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000551}
552
Richard Smithde01b7a2012-08-08 23:32:13 +0000553/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000554/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000555Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
556 IdentifierInfo *AliasName,
557 SourceLocation AliasLocation,
558 IdentifierInfo *ClassName,
559 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000560 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000561 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000562 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000563 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000564 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000565 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000566 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000567 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000568 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000569 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000570 }
571 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000572 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000573 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000574 if (const TypedefNameDecl *TDecl =
575 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000576 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000577 if (T->isObjCObjectType()) {
578 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000579 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000580 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000581 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000582 }
583 }
584 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000585 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
586 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000587 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000588 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000589 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000590 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000591 }
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000593 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000594 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000595 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Anders Carlsson15281452008-11-04 16:57:32 +0000597 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000598 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000599
John McCalld226f652010-08-21 09:40:31 +0000600 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000601}
602
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000603bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000604 IdentifierInfo *PName,
605 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000606 const ObjCList<ObjCProtocolDecl> &PList) {
607
608 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000609 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
610 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000611 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
612 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000613 if (PDecl->getIdentifier() == PName) {
614 Diag(Ploc, diag::err_protocol_has_circular_dependency);
615 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000616 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000617 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000618
619 if (!PDecl->hasDefinition())
620 continue;
621
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000622 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
623 PDecl->getLocation(), PDecl->getReferencedProtocols()))
624 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000625 }
626 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000627 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000628}
629
John McCalld226f652010-08-21 09:40:31 +0000630Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000631Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
632 IdentifierInfo *ProtocolName,
633 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000634 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000635 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000636 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000637 SourceLocation EndProtoLoc,
638 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000639 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000640 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000641 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000642 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
643 ForRedeclaration);
644 ObjCProtocolDecl *PDecl = 0;
645 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
646 // If we already have a definition, complain.
647 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
648 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Douglas Gregor27c6da22012-01-01 20:30:41 +0000650 // Create a new protocol that is completely distinct from previous
651 // declarations, and do not make this protocol available for name lookup.
652 // That way, we'll end up completely ignoring the duplicate.
653 // FIXME: Can we turn this into an error?
654 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
655 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000656 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000657 PDecl->startDefinition();
658 } else {
659 if (PrevDecl) {
660 // Check for circular dependencies among protocol declarations. This can
661 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000662 ObjCList<ObjCProtocolDecl> PList;
663 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
664 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000665 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000666 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000667
668 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000669 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000670 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000671 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000672
Douglas Gregor6e378de2009-04-23 23:18:26 +0000673 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000674 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000675 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000676
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000677 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000678 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000679
680 // Merge attributes from previous declarations.
681 if (PrevDecl)
682 mergeDeclAttributes(PDecl, PrevDecl);
683
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000684 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000685 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000686 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000687 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000688 }
Mike Stump1eb44332009-09-09 15:08:12 +0000689
690 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000691 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000692}
693
694/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000695/// issues an error if they are not declared. It returns list of
696/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000697void
Chris Lattnere13b9592008-07-26 04:03:38 +0000698Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000699 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000700 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000701 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000702 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000703 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
704 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000705 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000706 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000707 TypoCorrection Corrected = CorrectTypo(
708 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000709 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000710 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000711 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000712 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000713 Diag(PDecl->getLocation(), diag::note_previous_decl)
714 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000715 }
716 }
717
718 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000719 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000720 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000721 continue;
722 }
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000724 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000725
726 // If this is a forward declaration and we are supposed to warn in this
727 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000728 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000729 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000730 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000731 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000732 }
733}
734
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000735/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000736/// a class method in its extension.
737///
Mike Stump1eb44332009-09-09 15:08:12 +0000738void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000739 ObjCInterfaceDecl *ID) {
740 if (!ID)
741 return; // Possibly due to previous error
742
743 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000744 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
745 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000746 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000747 MethodMap[MD->getSelector()] = MD;
748 }
749
750 if (MethodMap.empty())
751 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000752 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
753 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000754 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000755 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
756 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
757 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
758 << Method->getDeclName();
759 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
760 }
761 }
762}
763
James Dennett1dfbd922012-06-14 21:40:34 +0000764/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000765Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000766Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000767 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000768 unsigned NumElts,
769 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000770 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000771 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000772 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000773 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
774 ForRedeclaration);
775 ObjCProtocolDecl *PDecl
776 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
777 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000778 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000779
780 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000781 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000782
Douglas Gregor3937f872012-01-01 20:33:24 +0000783 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000784 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000785
786 if (PrevDecl)
787 mergeDeclAttributes(PDecl, PrevDecl);
788
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000789 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000790 }
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000792 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000793}
794
John McCalld226f652010-08-21 09:40:31 +0000795Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000796ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
797 IdentifierInfo *ClassName, SourceLocation ClassLoc,
798 IdentifierInfo *CategoryName,
799 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000800 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000801 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000802 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000803 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000804 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000805 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000806
807 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000808
809 if (!IDecl
810 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000811 diag::err_category_forward_interface,
812 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000813 // Create an invalid ObjCCategoryDecl to serve as context for
814 // the enclosing method declarations. We mark the decl invalid
815 // to make it clear that this isn't a valid AST.
816 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000817 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000818 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000819 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000820
821 if (!IDecl)
822 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000823 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000824 }
825
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000826 if (!CategoryName && IDecl->getImplementation()) {
827 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
828 Diag(IDecl->getImplementation()->getLocation(),
829 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000830 }
831
Fariborz Jahanian25760612010-02-15 21:55:26 +0000832 if (CategoryName) {
833 /// Check for duplicate interface declaration for this category
834 ObjCCategoryDecl *CDeclChain;
835 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
836 CDeclChain = CDeclChain->getNextClassCategory()) {
837 if (CDeclChain->getIdentifier() == CategoryName) {
838 // Class extensions can be declared multiple times.
839 Diag(CategoryLoc, diag::warn_dup_category_def)
840 << ClassName << CategoryName;
841 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
842 break;
843 }
Chris Lattner70f19542009-02-16 21:26:43 +0000844 }
845 }
Chris Lattner70f19542009-02-16 21:26:43 +0000846
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000847 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
848 ClassLoc, CategoryLoc, CategoryName, IDecl);
849 // FIXME: PushOnScopeChains?
850 CurContext->addDecl(CDecl);
851
Chris Lattner4d391482007-12-12 07:09:47 +0000852 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000853 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000854 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000855 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000856 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000857 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000858 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000859 }
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Anders Carlsson15281452008-11-04 16:57:32 +0000861 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000862 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000863}
864
865/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000866/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000867/// object.
John McCalld226f652010-08-21 09:40:31 +0000868Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000869 SourceLocation AtCatImplLoc,
870 IdentifierInfo *ClassName, SourceLocation ClassLoc,
871 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000872 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000873 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000874 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000875 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
876 if (!CatIDecl) {
877 // Category @implementation with no corresponding @interface.
878 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000879 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
880 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000881 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000882 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000883 }
884 }
885
Mike Stump1eb44332009-09-09 15:08:12 +0000886 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000887 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000888 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000889 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000890 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000891 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000892 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000893 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
894 diag::err_undef_interface)) {
895 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000896 }
Chris Lattner4d391482007-12-12 07:09:47 +0000897
Douglas Gregord0434102009-01-09 00:49:46 +0000898 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000899 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000900
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000901 // If the interface is deprecated/unavailable, warn/error about it.
902 if (IDecl)
903 DiagnoseUseOfDecl(IDecl, ClassLoc);
904
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000905 /// Check that CatName, category name, is not used in another implementation.
906 if (CatIDecl) {
907 if (CatIDecl->getImplementation()) {
908 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
909 << CatName;
910 Diag(CatIDecl->getImplementation()->getLocation(),
911 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000912 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000913 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000914 // Warn on implementating category of deprecated class under
915 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000916 DiagnoseObjCImplementedDeprecations(*this,
917 dyn_cast<NamedDecl>(IDecl),
918 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000919 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000920 }
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Anders Carlsson15281452008-11-04 16:57:32 +0000922 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000923 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000924}
925
John McCalld226f652010-08-21 09:40:31 +0000926Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000927 SourceLocation AtClassImplLoc,
928 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000929 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000930 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000931 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000932 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000933 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000934 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
935 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000936 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000937 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000938 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000939 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000940 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
941 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000942 } else {
943 // We did not find anything with the name ClassName; try to correct for
944 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000945 ObjCInterfaceValidatorCCC Validator;
946 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000947 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000948 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000949 // Suggest the (potentially) correct interface name. However, put the
950 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000951 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000952 // provide a code-modification hint or use the typo name for recovery,
953 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000954 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000955 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000956 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000957 << ClassName << CorrectedName;
958 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
959 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000960 IDecl = 0;
961 } else {
962 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
963 }
Chris Lattner4d391482007-12-12 07:09:47 +0000964 }
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Chris Lattner4d391482007-12-12 07:09:47 +0000966 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000967 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000968 if (SuperClassname) {
969 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000970 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
971 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000972 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000973 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
974 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000975 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000976 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000977 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000978 if (SDecl && !SDecl->hasDefinition())
979 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000980 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000981 Diag(SuperClassLoc, diag::err_undef_superclass)
982 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000983 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000984 // This implementation and its interface do not have the same
985 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000986 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000987 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000988 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000989 }
990 }
991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Chris Lattner4d391482007-12-12 07:09:47 +0000993 if (!IDecl) {
994 // Legacy case of @implementation with no corresponding @interface.
995 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000996
Mike Stump390b4cc2009-05-16 07:39:55 +0000997 // FIXME: Do we support attributes on the @implementation? If so we should
998 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000999 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001000 ClassName, /*PrevDecl=*/0, ClassLoc,
1001 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001002 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001003 if (SDecl) {
1004 IDecl->setSuperClass(SDecl);
1005 IDecl->setSuperClassLoc(SuperClassLoc);
1006 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1007 } else {
1008 IDecl->setEndOfDefinitionLoc(ClassLoc);
1009 }
1010
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001011 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001012 } else {
1013 // Mark the interface as being completed, even if it was just as
1014 // @class ....;
1015 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001016 if (!IDecl->hasDefinition())
1017 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001018 }
Mike Stump1eb44332009-09-09 15:08:12 +00001019
1020 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001021 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1022 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001023
Anders Carlsson15281452008-11-04 16:57:32 +00001024 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001025 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Chris Lattner4d391482007-12-12 07:09:47 +00001027 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001028 if (IDecl->getImplementation()) {
1029 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001030 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001031 Diag(IDecl->getImplementation()->getLocation(),
1032 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001033 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001034 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001035 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001036 // Warn on implementating deprecated class under
1037 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001038 DiagnoseObjCImplementedDeprecations(*this,
1039 dyn_cast<NamedDecl>(IDecl),
1040 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001041 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001042 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001043}
1044
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001045Sema::DeclGroupPtrTy
1046Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1047 SmallVector<Decl *, 64> DeclsInGroup;
1048 DeclsInGroup.reserve(Decls.size() + 1);
1049
1050 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1051 Decl *Dcl = Decls[i];
1052 if (!Dcl)
1053 continue;
1054 if (Dcl->getDeclContext()->isFileContext())
1055 Dcl->setTopLevelDeclInObjCContainer();
1056 DeclsInGroup.push_back(Dcl);
1057 }
1058
1059 DeclsInGroup.push_back(ObjCImpDecl);
1060
1061 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1062}
1063
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001064void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1065 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001066 SourceLocation RBrace) {
1067 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001068 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001069 if (!IDecl)
1070 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001071 /// Check case of non-existing \@interface decl.
1072 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001073 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001074 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001075 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001076 // Add ivar's to class's DeclContext.
1077 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001078 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001079 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001080 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001081 }
1082
Chris Lattner4d391482007-12-12 07:09:47 +00001083 return;
1084 }
1085 // If implementation has empty ivar list, just return.
1086 if (numIvars == 0)
1087 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Chris Lattner4d391482007-12-12 07:09:47 +00001089 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001090 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001091 if (ImpDecl->getSuperClass())
1092 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1093 for (unsigned i = 0; i < numIvars; i++) {
1094 ObjCIvarDecl* ImplIvar = ivars[i];
1095 if (const ObjCIvarDecl *ClsIvar =
1096 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1097 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1098 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1099 continue;
1100 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001101 // Instance ivar to Implementation's DeclContext.
1102 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001103 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001104 ImpDecl->addDecl(ImplIvar);
1105 }
1106 return;
1107 }
Chris Lattner4d391482007-12-12 07:09:47 +00001108 // Check interface's Ivar list against those in the implementation.
1109 // names and types must match.
1110 //
Chris Lattner4d391482007-12-12 07:09:47 +00001111 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001112 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001113 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1114 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001115 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001116 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001117 assert (ImplIvar && "missing implementation ivar");
1118 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Steve Naroffca331292009-03-03 14:49:36 +00001120 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001121 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001122 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001123 << ImplIvar->getIdentifier()
1124 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001125 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001126 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1127 ImplIvar->getBitWidthValue(Context) !=
1128 ClsIvar->getBitWidthValue(Context)) {
1129 Diag(ImplIvar->getBitWidth()->getLocStart(),
1130 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1131 Diag(ClsIvar->getBitWidth()->getLocStart(),
1132 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001133 }
Steve Naroffca331292009-03-03 14:49:36 +00001134 // Make sure the names are identical.
1135 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001136 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001137 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001138 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001139 }
1140 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001141 }
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Chris Lattner609e4c72007-12-12 18:11:49 +00001143 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001144 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001145 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001146 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001147}
1148
Steve Naroff3c2eb662008-02-10 21:38:56 +00001149void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001150 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001151 // No point warning no definition of method which is 'unavailable'.
1152 if (method->hasAttr<UnavailableAttr>())
1153 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001154 if (!IncompleteImpl) {
1155 Diag(ImpLoc, diag::warn_incomplete_impl);
1156 IncompleteImpl = true;
1157 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001158 if (DiagID == diag::warn_unimplemented_protocol_method)
1159 Diag(ImpLoc, DiagID) << method->getDeclName();
1160 else
1161 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001162}
1163
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001164/// Determines if type B can be substituted for type A. Returns true if we can
1165/// guarantee that anything that the user will do to an object of type A can
1166/// also be done to an object of type B. This is trivially true if the two
1167/// types are the same, or if B is a subclass of A. It becomes more complex
1168/// in cases where protocols are involved.
1169///
1170/// Object types in Objective-C describe the minimum requirements for an
1171/// object, rather than providing a complete description of a type. For
1172/// example, if A is a subclass of B, then B* may refer to an instance of A.
1173/// The principle of substitutability means that we may use an instance of A
1174/// anywhere that we may use an instance of B - it will implement all of the
1175/// ivars of B and all of the methods of B.
1176///
1177/// This substitutability is important when type checking methods, because
1178/// the implementation may have stricter type definitions than the interface.
1179/// The interface specifies minimum requirements, but the implementation may
1180/// have more accurate ones. For example, a method may privately accept
1181/// instances of B, but only publish that it accepts instances of A. Any
1182/// object passed to it will be type checked against B, and so will implicitly
1183/// by a valid A*. Similarly, a method may return a subclass of the class that
1184/// it is declared as returning.
1185///
1186/// This is most important when considering subclassing. A method in a
1187/// subclass must accept any object as an argument that its superclass's
1188/// implementation accepts. It may, however, accept a more general type
1189/// without breaking substitutability (i.e. you can still use the subclass
1190/// anywhere that you can use the superclass, but not vice versa). The
1191/// converse requirement applies to return types: the return type for a
1192/// subclass method must be a valid object of the kind that the superclass
1193/// advertises, but it may be specified more accurately. This avoids the need
1194/// for explicit down-casting by callers.
1195///
1196/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001197static bool isObjCTypeSubstitutable(ASTContext &Context,
1198 const ObjCObjectPointerType *A,
1199 const ObjCObjectPointerType *B,
1200 bool rejectId) {
1201 // Reject a protocol-unqualified id.
1202 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001203
1204 // If B is a qualified id, then A must also be a qualified id and it must
1205 // implement all of the protocols in B. It may not be a qualified class.
1206 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1207 // stricter definition so it is not substitutable for id<A>.
1208 if (B->isObjCQualifiedIdType()) {
1209 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001210 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1211 QualType(B,0),
1212 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001213 }
1214
1215 /*
1216 // id is a special type that bypasses type checking completely. We want a
1217 // warning when it is used in one place but not another.
1218 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1219
1220
1221 // If B is a qualified id, then A must also be a qualified id (which it isn't
1222 // if we've got this far)
1223 if (B->isObjCQualifiedIdType()) return false;
1224 */
1225
1226 // Now we know that A and B are (potentially-qualified) class types. The
1227 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001228 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001229}
1230
John McCall10302c02010-10-28 02:34:38 +00001231static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1232 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1233}
1234
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001235static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001236 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001237 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001238 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001239 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001240 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001241 if (IsProtocolMethodDecl &&
1242 (MethodDecl->getObjCDeclQualifier() !=
1243 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001244 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001245 S.Diag(MethodImpl->getLocation(),
1246 (IsOverridingMode ?
1247 diag::warn_conflicting_overriding_ret_type_modifiers
1248 : diag::warn_conflicting_ret_type_modifiers))
1249 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001250 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1251 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1252 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1253 }
1254 else
1255 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001256 }
1257
John McCall10302c02010-10-28 02:34:38 +00001258 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001259 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001260 return true;
1261 if (!Warn)
1262 return false;
John McCall10302c02010-10-28 02:34:38 +00001263
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001264 unsigned DiagID =
1265 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1266 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001267
1268 // Mismatches between ObjC pointers go into a different warning
1269 // category, and sometimes they're even completely whitelisted.
1270 if (const ObjCObjectPointerType *ImplPtrTy =
1271 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1272 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001273 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001274 // Allow non-matching return types as long as they don't violate
1275 // the principle of substitutability. Specifically, we permit
1276 // return types that are subclasses of the declared return type,
1277 // or that are more-qualified versions of the declared type.
1278 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001279 return false;
John McCall10302c02010-10-28 02:34:38 +00001280
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001281 DiagID =
1282 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1283 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001284 }
1285 }
1286
1287 S.Diag(MethodImpl->getLocation(), DiagID)
1288 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001289 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001290 << MethodImpl->getResultType()
1291 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001292 S.Diag(MethodDecl->getLocation(),
1293 IsOverridingMode ? diag::note_previous_declaration
1294 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001295 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001296 return false;
John McCall10302c02010-10-28 02:34:38 +00001297}
1298
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001299static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001300 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001301 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001302 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001303 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001304 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001305 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001306 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001307 if (IsProtocolMethodDecl &&
1308 (ImplVar->getObjCDeclQualifier() !=
1309 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001310 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001311 if (IsOverridingMode)
1312 S.Diag(ImplVar->getLocation(),
1313 diag::warn_conflicting_overriding_param_modifiers)
1314 << getTypeRange(ImplVar->getTypeSourceInfo())
1315 << MethodImpl->getDeclName();
1316 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001317 diag::warn_conflicting_param_modifiers)
1318 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001319 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001320 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1321 << getTypeRange(IfaceVar->getTypeSourceInfo());
1322 }
1323 else
1324 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001325 }
1326
John McCall10302c02010-10-28 02:34:38 +00001327 QualType ImplTy = ImplVar->getType();
1328 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001329
John McCall10302c02010-10-28 02:34:38 +00001330 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001331 return true;
1332
1333 if (!Warn)
1334 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001335 unsigned DiagID =
1336 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1337 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001338
1339 // Mismatches between ObjC pointers go into a different warning
1340 // category, and sometimes they're even completely whitelisted.
1341 if (const ObjCObjectPointerType *ImplPtrTy =
1342 ImplTy->getAs<ObjCObjectPointerType>()) {
1343 if (const ObjCObjectPointerType *IfacePtrTy =
1344 IfaceTy->getAs<ObjCObjectPointerType>()) {
1345 // Allow non-matching argument types as long as they don't
1346 // violate the principle of substitutability. Specifically, the
1347 // implementation must accept any objects that the superclass
1348 // accepts, however it may also accept others.
1349 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001350 return false;
John McCall10302c02010-10-28 02:34:38 +00001351
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001352 DiagID =
1353 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1354 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001355 }
1356 }
1357
1358 S.Diag(ImplVar->getLocation(), DiagID)
1359 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001360 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1361 S.Diag(IfaceVar->getLocation(),
1362 (IsOverridingMode ? diag::note_previous_declaration
1363 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001364 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001365 return false;
John McCall10302c02010-10-28 02:34:38 +00001366}
John McCallf85e1932011-06-15 23:02:42 +00001367
1368/// In ARC, check whether the conventional meanings of the two methods
1369/// match. If they don't, it's a hard error.
1370static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1371 ObjCMethodDecl *decl) {
1372 ObjCMethodFamily implFamily = impl->getMethodFamily();
1373 ObjCMethodFamily declFamily = decl->getMethodFamily();
1374 if (implFamily == declFamily) return false;
1375
1376 // Since conventions are sorted by selector, the only possibility is
1377 // that the types differ enough to cause one selector or the other
1378 // to fall out of the family.
1379 assert(implFamily == OMF_None || declFamily == OMF_None);
1380
1381 // No further diagnostics required on invalid declarations.
1382 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1383
1384 const ObjCMethodDecl *unmatched = impl;
1385 ObjCMethodFamily family = declFamily;
1386 unsigned errorID = diag::err_arc_lost_method_convention;
1387 unsigned noteID = diag::note_arc_lost_method_convention;
1388 if (declFamily == OMF_None) {
1389 unmatched = decl;
1390 family = implFamily;
1391 errorID = diag::err_arc_gained_method_convention;
1392 noteID = diag::note_arc_gained_method_convention;
1393 }
1394
1395 // Indexes into a %select clause in the diagnostic.
1396 enum FamilySelector {
1397 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1398 };
1399 FamilySelector familySelector = FamilySelector();
1400
1401 switch (family) {
1402 case OMF_None: llvm_unreachable("logic error, no method convention");
1403 case OMF_retain:
1404 case OMF_release:
1405 case OMF_autorelease:
1406 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001407 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001408 case OMF_retainCount:
1409 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001410 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001411 // Mismatches for these methods don't change ownership
1412 // conventions, so we don't care.
1413 return false;
1414
1415 case OMF_init: familySelector = F_init; break;
1416 case OMF_alloc: familySelector = F_alloc; break;
1417 case OMF_copy: familySelector = F_copy; break;
1418 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1419 case OMF_new: familySelector = F_new; break;
1420 }
1421
1422 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1423 ReasonSelector reasonSelector;
1424
1425 // The only reason these methods don't fall within their families is
1426 // due to unusual result types.
1427 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1428 reasonSelector = R_UnrelatedReturn;
1429 } else {
1430 reasonSelector = R_NonObjectReturn;
1431 }
1432
1433 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1434 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1435
1436 return true;
1437}
John McCall10302c02010-10-28 02:34:38 +00001438
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001439void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001440 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001441 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001442 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001443 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1444 return;
1445
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001446 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001447 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001448 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001449
Chris Lattner3aff9192009-04-11 19:58:42 +00001450 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001451 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1452 EF = MethodDecl->param_end();
1453 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001454 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001455 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001456 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001457
Fariborz Jahanian21121902011-08-08 18:03:17 +00001458 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001459 Diag(ImpMethodDecl->getLocation(),
1460 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001461 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001462 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001463}
1464
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001465void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1466 ObjCMethodDecl *Overridden,
1467 bool IsProtocolMethodDecl) {
1468
1469 CheckMethodOverrideReturn(*this, Method, Overridden,
1470 IsProtocolMethodDecl, true,
1471 true);
1472
1473 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001474 IF = Overridden->param_begin(), EM = Method->param_end(),
1475 EF = Overridden->param_end();
1476 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001477 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1478 IsProtocolMethodDecl, true, true);
1479 }
1480
1481 if (Method->isVariadic() != Overridden->isVariadic()) {
1482 Diag(Method->getLocation(),
1483 diag::warn_conflicting_overriding_variadic);
1484 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1485 }
1486}
1487
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001488/// WarnExactTypedMethods - This routine issues a warning if method
1489/// implementation declaration matches exactly that of its declaration.
1490void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1491 ObjCMethodDecl *MethodDecl,
1492 bool IsProtocolMethodDecl) {
1493 // don't issue warning when protocol method is optional because primary
1494 // class is not required to implement it and it is safe for protocol
1495 // to implement it.
1496 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1497 return;
1498 // don't issue warning when primary class's method is
1499 // depecated/unavailable.
1500 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1501 MethodDecl->hasAttr<DeprecatedAttr>())
1502 return;
1503
1504 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1505 IsProtocolMethodDecl, false, false);
1506 if (match)
1507 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001508 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1509 EF = MethodDecl->param_end();
1510 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001511 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1512 *IM, *IF,
1513 IsProtocolMethodDecl, false, false);
1514 if (!match)
1515 break;
1516 }
1517 if (match)
1518 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001519 if (match)
1520 match = !(MethodDecl->isClassMethod() &&
1521 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001522
1523 if (match) {
1524 Diag(ImpMethodDecl->getLocation(),
1525 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001526 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1527 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001528 }
1529}
1530
Mike Stump390b4cc2009-05-16 07:39:55 +00001531/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1532/// improve the efficiency of selector lookups and type checking by associating
1533/// with each protocol / interface / category the flattened instance tables. If
1534/// we used an immutable set to keep the table then it wouldn't add significant
1535/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001536
Steve Naroffefe7f362008-02-08 22:06:17 +00001537/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001538/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001539void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1540 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001541 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001542 const SelectorSet &InsMap,
1543 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001544 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001545 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1546 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1547 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001548 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1549
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001550 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001551 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001552 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001553 // check to see if class implements forwardInvocation method and objects
1554 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001555 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001556 // Under such conditions, which means that every method possible is
1557 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001558 // found" warnings.
1559 // FIXME: Use a general GetUnarySelector method for this.
1560 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1561 Selector fISelector = Context.Selectors.getSelector(1, &II);
1562 if (InsMap.count(fISelector))
1563 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1564 // need be implemented in the implementation.
1565 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1566 }
Mike Stump1eb44332009-09-09 15:08:12 +00001567
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001568 // If a method lookup fails locally we still need to look and see if
1569 // the method was implemented by a base class or an inherited
1570 // protocol. This lookup is slow, but occurs rarely in correct code
1571 // and otherwise would terminate in a warning.
1572
Chris Lattner4d391482007-12-12 07:09:47 +00001573 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001574 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001575 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001576 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001577 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001578 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001579 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001580 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001581 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001582 // If a method is not implemented in the category implementation but
1583 // has been declared in its primary class, superclass,
1584 // or in one of their protocols, no need to issue the warning.
1585 // This is because method will be implemented in the primary class
1586 // or one of its super class implementation.
1587
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001588 // Ugly, but necessary. Method declared in protcol might have
1589 // have been synthesized due to a property declared in the class which
1590 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001591 if (ObjCMethodDecl *MethodInClass =
1592 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001593 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001594 if (C || MethodInClass->isSynthesized())
1595 continue;
1596 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1597 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1598 != DiagnosticsEngine::Ignored) {
1599 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001600 Diag(method->getLocation(), diag::note_method_declared_at)
1601 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001602 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1603 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001604 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001605 }
1606 }
Chris Lattner4d391482007-12-12 07:09:47 +00001607 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001608 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001609 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001610 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001611 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001612 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1613 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001614 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001615 // See above comment for instance method lookups.
1616 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001617 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001618 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001619 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001620 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1621 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001622 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001623 Diag(method->getLocation(), diag::note_method_declared_at)
1624 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001625 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1626 PDecl->getDeclName();
1627 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001628 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001629 }
Chris Lattner780f3292008-07-21 21:32:27 +00001630 // Check on this protocols's referenced protocols, recursively.
1631 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1632 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001633 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001634}
1635
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001636/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001637/// or protocol against those declared in their implementations.
1638///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001639void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1640 const SelectorSet &ClsMap,
1641 SelectorSet &InsMapSeen,
1642 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001643 ObjCImplDecl* IMPDecl,
1644 ObjCContainerDecl* CDecl,
1645 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001646 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001647 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001648 // Check and see if instance methods in class interface have been
1649 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001650 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1651 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001652 if (InsMapSeen.count((*I)->getSelector()))
1653 continue;
1654 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001655 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001656 !InsMap.count((*I)->getSelector())) {
1657 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001658 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1659 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001660 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001661 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001662 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001663 IMPDecl->getInstanceMethod((*I)->getSelector());
1664 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1665 "Expected to find the method through lookup as well");
1666 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001667 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001668 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001669 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001670 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1671 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001672 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001673 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001674 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001675 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001676 }
1677 }
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001679 // Check and see if class methods in class interface have been
1680 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001681 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001682 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001683 if (ClsMapSeen.count((*I)->getSelector()))
1684 continue;
1685 ClsMapSeen.insert((*I)->getSelector());
1686 if (!ClsMap.count((*I)->getSelector())) {
1687 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001688 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1689 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001690 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001691 ObjCMethodDecl *ImpMethodDecl =
1692 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001693 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1694 "Expected to find the method through lookup as well");
1695 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001696 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001697 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1698 isa<ObjCProtocolDecl>(CDecl));
1699 else
1700 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001701 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001702 }
1703 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001704
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001705 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001706 // Also methods in class extensions need be looked at next.
1707 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1708 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1709 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1710 IMPDecl,
1711 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001712 IncompleteImpl, false,
1713 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001714
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001715 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001716 for (ObjCInterfaceDecl::all_protocol_iterator
1717 PI = I->all_referenced_protocol_begin(),
1718 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001719 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1720 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001721 (*PI), IncompleteImpl, false,
1722 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001723
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001724 // FIXME. For now, we are not checking for extact match of methods
1725 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001726 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001727 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001728 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001729 I->getSuperClass(), IncompleteImpl, false);
1730 }
1731}
1732
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001733/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1734/// category matches with those implemented in its primary class and
1735/// warns each time an exact match is found.
1736void Sema::CheckCategoryVsClassMethodMatches(
1737 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001738 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001739
1740 for (ObjCImplementationDecl::instmeth_iterator
1741 I = CatIMPDecl->instmeth_begin(),
1742 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1743 InsMap.insert((*I)->getSelector());
1744
1745 for (ObjCImplementationDecl::classmeth_iterator
1746 I = CatIMPDecl->classmeth_begin(),
1747 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1748 ClsMap.insert((*I)->getSelector());
1749 if (InsMap.empty() && ClsMap.empty())
1750 return;
1751
1752 // Get category's primary class.
1753 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1754 if (!CatDecl)
1755 return;
1756 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1757 if (!IDecl)
1758 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001759 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001760 bool IncompleteImpl = false;
1761 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1762 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001763 IncompleteImpl, false,
1764 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001765}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001766
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001767void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001768 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001769 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001770 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001771 // Check and see if instance methods in class interface have been
1772 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001773 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001774 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001775 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001777 // Check and see if properties declared in the interface have either 1)
1778 // an implementation or 2) there is a @synthesize/@dynamic implementation
1779 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001780 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001781 if (!(LangOpts.ObjCDefaultSynthProperties &&
1782 LangOpts.ObjCRuntime.isNonFragile()) ||
1783 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001784 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001785
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001786 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001787 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001788 I = IMPDecl->classmeth_begin(),
1789 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001790 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001792 // Check for type conflict of methods declared in a class/protocol and
1793 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001794 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001795 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1796 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001797 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001798
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001799 // check all methods implemented in category against those declared
1800 // in its primary class.
1801 if (ObjCCategoryImplDecl *CatDecl =
1802 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1803 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Chris Lattner4d391482007-12-12 07:09:47 +00001805 // Check the protocol list for unimplemented methods in the @implementation
1806 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001807 // Check and see if class methods in class interface have been
1808 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Chris Lattnercddc8882009-03-01 00:56:52 +00001810 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001811 for (ObjCInterfaceDecl::all_protocol_iterator
1812 PI = I->all_referenced_protocol_begin(),
1813 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001814 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001815 InsMap, ClsMap, I);
1816 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001817 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1818 Categories; Categories = Categories->getNextClassExtension())
1819 ImplMethodsVsClassMethods(S, IMPDecl,
1820 const_cast<ObjCCategoryDecl*>(Categories),
1821 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001822 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001823 // For extended class, unimplemented methods in its protocols will
1824 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001825 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001826 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1827 E = C->protocol_end(); PI != E; ++PI)
1828 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001829 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001830 // Report unimplemented properties in the category as well.
1831 // When reporting on missing setter/getters, do not report when
1832 // setter/getter is implemented in category's primary class
1833 // implementation.
1834 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1835 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1836 for (ObjCImplementationDecl::instmeth_iterator
1837 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1838 InsMap.insert((*I)->getSelector());
1839 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001840 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001841 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001842 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001843 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001844}
1845
Mike Stump1eb44332009-09-09 15:08:12 +00001846/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001847Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001848Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001849 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001850 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001851 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001852 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001853 for (unsigned i = 0; i != NumElts; ++i) {
1854 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001855 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001856 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001857 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001858 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001859 // Maybe we will complain about the shadowed template parameter.
1860 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1861 // Just pretend that we didn't see the previous declaration.
1862 PrevDecl = 0;
1863 }
1864
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001865 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001866 // GCC apparently allows the following idiom:
1867 //
1868 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1869 // @class XCElementToggler;
1870 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001871 // Here we have chosen to ignore the forward class declaration
1872 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001873 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001874 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001875 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001876 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001877 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001878 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001879 // to the underlying class. Just ignore the forward class with a warning
1880 // as this will force the intended behavior which is to lookup the typedef
1881 // name.
1882 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1883 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1884 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1885 continue;
1886 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001887 }
Chris Lattner4d391482007-12-12 07:09:47 +00001888 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001889
1890 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001891 ObjCInterfaceDecl *PrevIDecl
1892 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001893 ObjCInterfaceDecl *IDecl
1894 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001895 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001896 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001897
Douglas Gregor7723fec2011-12-15 20:29:51 +00001898 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001899 CheckObjCDeclScope(IDecl);
1900 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001901 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001902
1903 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001904}
1905
John McCall0f4c4c42011-06-16 01:15:19 +00001906static bool tryMatchRecordTypes(ASTContext &Context,
1907 Sema::MethodMatchStrategy strategy,
1908 const Type *left, const Type *right);
1909
John McCallf85e1932011-06-15 23:02:42 +00001910static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1911 QualType leftQT, QualType rightQT) {
1912 const Type *left =
1913 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1914 const Type *right =
1915 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1916
1917 if (left == right) return true;
1918
1919 // If we're doing a strict match, the types have to match exactly.
1920 if (strategy == Sema::MMS_strict) return false;
1921
1922 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1923
1924 // Otherwise, use this absurdly complicated algorithm to try to
1925 // validate the basic, low-level compatibility of the two types.
1926
1927 // As a minimum, require the sizes and alignments to match.
1928 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1929 return false;
1930
1931 // Consider all the kinds of non-dependent canonical types:
1932 // - functions and arrays aren't possible as return and parameter types
1933
1934 // - vector types of equal size can be arbitrarily mixed
1935 if (isa<VectorType>(left)) return isa<VectorType>(right);
1936 if (isa<VectorType>(right)) return false;
1937
1938 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001939 // - structs, unions, and Objective-C objects must match more-or-less
1940 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001941 // - everything else should be a scalar
1942 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001943 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001944
John McCall1d9b3b22011-09-09 05:25:32 +00001945 // Make scalars agree in kind, except count bools as chars, and group
1946 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001947 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1948 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1949 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1950 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001951 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1952 leftSK = Type::STK_ObjCObjectPointer;
1953 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1954 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001955
1956 // Note that data member pointers and function member pointers don't
1957 // intermix because of the size differences.
1958
1959 return (leftSK == rightSK);
1960}
Chris Lattner4d391482007-12-12 07:09:47 +00001961
John McCall0f4c4c42011-06-16 01:15:19 +00001962static bool tryMatchRecordTypes(ASTContext &Context,
1963 Sema::MethodMatchStrategy strategy,
1964 const Type *lt, const Type *rt) {
1965 assert(lt && rt && lt != rt);
1966
1967 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1968 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1969 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1970
1971 // Require union-hood to match.
1972 if (left->isUnion() != right->isUnion()) return false;
1973
1974 // Require an exact match if either is non-POD.
1975 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1976 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1977 return false;
1978
1979 // Require size and alignment to match.
1980 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1981
1982 // Require fields to match.
1983 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1984 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1985 for (; li != le && ri != re; ++li, ++ri) {
1986 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1987 return false;
1988 }
1989 return (li == le && ri == re);
1990}
1991
Chris Lattner4d391482007-12-12 07:09:47 +00001992/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1993/// returns true, or false, accordingly.
1994/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001995bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1996 const ObjCMethodDecl *right,
1997 MethodMatchStrategy strategy) {
1998 if (!matchTypes(Context, strategy,
1999 left->getResultType(), right->getResultType()))
2000 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002001
David Blaikie4e4d0842012-03-11 07:00:24 +00002002 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002003 (left->hasAttr<NSReturnsRetainedAttr>()
2004 != right->hasAttr<NSReturnsRetainedAttr>() ||
2005 left->hasAttr<NSConsumesSelfAttr>()
2006 != right->hasAttr<NSConsumesSelfAttr>()))
2007 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002008
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002009 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002010 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2011 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002012
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002013 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002014 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002015 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002016
2017 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2018 return false;
2019
David Blaikie4e4d0842012-03-11 07:00:24 +00002020 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002021 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2022 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002023 }
2024 return true;
2025}
2026
Douglas Gregorff310c72012-05-01 23:37:00 +00002027void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002028 // If the list is empty, make it a singleton list.
2029 if (List->Method == 0) {
2030 List->Method = Method;
2031 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002032 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002033 }
2034
2035 // We've seen a method with this name, see if we have already seen this type
2036 // signature.
2037 ObjCMethodList *Previous = List;
2038 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002039 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002040 continue;
2041
2042 ObjCMethodDecl *PrevObjCMethod = List->Method;
2043
2044 // Propagate the 'defined' bit.
2045 if (Method->isDefined())
2046 PrevObjCMethod->setDefined(true);
2047
2048 // If a method is deprecated, push it in the global pool.
2049 // This is used for better diagnostics.
2050 if (Method->isDeprecated()) {
2051 if (!PrevObjCMethod->isDeprecated())
2052 List->Method = Method;
2053 }
2054 // If new method is unavailable, push it into global pool
2055 // unless previous one is deprecated.
2056 if (Method->isUnavailable()) {
2057 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2058 List->Method = Method;
2059 }
2060
Douglas Gregorff310c72012-05-01 23:37:00 +00002061 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002062 }
2063
2064 // We have a new signature for an existing method - add it.
2065 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002066 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002067 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2068}
2069
Sebastian Redldb9d2142010-08-02 23:18:59 +00002070/// \brief Read the contents of the method pool for a given selector from
2071/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002072void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002073 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002074 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002075}
2076
Douglas Gregorff310c72012-05-01 23:37:00 +00002077void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002078 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002079 // Ignore methods of invalid containers.
2080 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002081 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002082
Douglas Gregor0d266d62012-01-25 00:59:09 +00002083 if (ExternalSource)
2084 ReadMethodPool(Method->getSelector());
2085
Sebastian Redldb9d2142010-08-02 23:18:59 +00002086 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002087 if (Pos == MethodPool.end())
2088 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2089 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002090
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002091 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002092
Sebastian Redldb9d2142010-08-02 23:18:59 +00002093 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002094 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002095}
2096
John McCallf85e1932011-06-15 23:02:42 +00002097/// Determines if this is an "acceptable" loose mismatch in the global
2098/// method pool. This exists mostly as a hack to get around certain
2099/// global mismatches which we can't afford to make warnings / errors.
2100/// Really, what we want is a way to take a method out of the global
2101/// method pool.
2102static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2103 ObjCMethodDecl *other) {
2104 if (!chosen->isInstanceMethod())
2105 return false;
2106
2107 Selector sel = chosen->getSelector();
2108 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2109 return false;
2110
2111 // Don't complain about mismatches for -length if the method we
2112 // chose has an integral result type.
2113 return (chosen->getResultType()->isIntegerType());
2114}
2115
Sebastian Redldb9d2142010-08-02 23:18:59 +00002116ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002117 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002118 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002119 if (ExternalSource)
2120 ReadMethodPool(Sel);
2121
Sebastian Redldb9d2142010-08-02 23:18:59 +00002122 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002123 if (Pos == MethodPool.end())
2124 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002125
Sebastian Redldb9d2142010-08-02 23:18:59 +00002126 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002127
Sebastian Redldb9d2142010-08-02 23:18:59 +00002128 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002129 bool issueDiagnostic = false, issueError = false;
2130
2131 // We support a warning which complains about *any* difference in
2132 // method signature.
2133 bool strictSelectorMatch =
2134 (receiverIdOrClass && warn &&
2135 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2136 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002137 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002138 if (strictSelectorMatch)
2139 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002140 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2141 MMS_strict)) {
2142 issueDiagnostic = true;
2143 break;
2144 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002145 }
2146
John McCallf85e1932011-06-15 23:02:42 +00002147 // If we didn't see any strict differences, we won't see any loose
2148 // differences. In ARC, however, we also need to check for loose
2149 // mismatches, because most of them are errors.
2150 if (!strictSelectorMatch ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002151 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002152 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002153 // This checks if the methods differ in type mismatch.
2154 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2155 MMS_loose) &&
2156 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2157 issueDiagnostic = true;
David Blaikie4e4d0842012-03-11 07:00:24 +00002158 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002159 issueError = true;
2160 break;
2161 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002162 }
2163
John McCallf85e1932011-06-15 23:02:42 +00002164 if (issueDiagnostic) {
2165 if (issueError)
2166 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2167 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002168 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2169 else
2170 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002171
2172 Diag(MethList.Method->getLocStart(),
2173 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002174 << MethList.Method->getSourceRange();
2175 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2176 Diag(Next->Method->getLocStart(), diag::note_also_found)
2177 << Next->Method->getSourceRange();
2178 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002179 }
2180 return MethList.Method;
2181}
2182
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002183ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002184 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2185 if (Pos == MethodPool.end())
2186 return 0;
2187
2188 GlobalMethods &Methods = Pos->second;
2189
2190 if (Methods.first.Method && Methods.first.Method->isDefined())
2191 return Methods.first.Method;
2192 if (Methods.second.Method && Methods.second.Method->isDefined())
2193 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002194 return 0;
2195}
2196
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002197/// DiagnoseDuplicateIvars -
2198/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002199/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002200/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002201/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002202void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2203 ObjCInterfaceDecl *SID) {
2204 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2205 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002206 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002207 if (Ivar->isInvalidDecl())
2208 continue;
2209 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2210 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2211 if (prevIvar) {
2212 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2213 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2214 Ivar->setInvalidDecl();
2215 }
2216 }
2217 }
2218}
2219
Erik Verbruggend64251f2011-12-06 09:25:23 +00002220Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2221 switch (CurContext->getDeclKind()) {
2222 case Decl::ObjCInterface:
2223 return Sema::OCK_Interface;
2224 case Decl::ObjCProtocol:
2225 return Sema::OCK_Protocol;
2226 case Decl::ObjCCategory:
2227 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2228 return Sema::OCK_ClassExtension;
2229 else
2230 return Sema::OCK_Category;
2231 case Decl::ObjCImplementation:
2232 return Sema::OCK_Implementation;
2233 case Decl::ObjCCategoryImpl:
2234 return Sema::OCK_CategoryImplementation;
2235
2236 default:
2237 return Sema::OCK_None;
2238 }
2239}
2240
Steve Naroffa56f6162007-12-18 01:30:32 +00002241// Note: For class/category implemenations, allMethods/allProperties is
2242// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002243Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2244 Decl **allMethods, unsigned allNum,
2245 Decl **allProperties, unsigned pNum,
2246 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002247
Erik Verbruggend64251f2011-12-06 09:25:23 +00002248 if (getObjCContainerKind() == Sema::OCK_None)
2249 return 0;
2250
2251 assert(AtEnd.isValid() && "Invalid location for '@end'");
2252
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002253 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2254 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002255
Mike Stump1eb44332009-09-09 15:08:12 +00002256 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002257 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2258 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002259 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002260
Steve Naroff0701bbb2009-01-08 17:28:14 +00002261 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2262 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2263 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2264
Chris Lattner4d391482007-12-12 07:09:47 +00002265 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002266 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002267 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002268
2269 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002270 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002271 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002272 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002273 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002274 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002275 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002276 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002277 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002278 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002279 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002280 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002281 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002282 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002283 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002284 if (!Context.getSourceManager().isInSystemHeader(
2285 Method->getLocation()))
2286 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2287 << Method->getDeclName();
2288 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2289 }
Chris Lattner4d391482007-12-12 07:09:47 +00002290 InsMap[Method->getSelector()] = Method;
2291 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002292 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002293 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002294 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002295 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002296 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002297 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002298 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002299 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002300 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002301 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002302 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002303 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002304 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002305 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002306 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002307 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002308 if (!Context.getSourceManager().isInSystemHeader(
2309 Method->getLocation()))
2310 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2311 << Method->getDeclName();
2312 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2313 }
Chris Lattner4d391482007-12-12 07:09:47 +00002314 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002315 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002316 }
2317 }
2318 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002319 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002320 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002321 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002322 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002323 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002324 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002325 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002326 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002327 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002328
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002329 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002330 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002331 if (C->IsClassExtension()) {
2332 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2333 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002334 }
Chris Lattner4d391482007-12-12 07:09:47 +00002335 }
Steve Naroff09c47192009-01-09 15:36:25 +00002336 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002337 if (CDecl->getIdentifier())
2338 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2339 // user-defined setter/getter. It also synthesizes setter/getter methods
2340 // and adds them to the DeclContext and global method pools.
2341 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2342 E = CDecl->prop_end();
2343 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002344 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002345 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002346 }
2347 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002348 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002349 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002350 // Any property declared in a class extension might have user
2351 // declared setter or getter in current class extension or one
2352 // of the other class extensions. Mark them as synthesized as
2353 // property will be synthesized when property with same name is
2354 // seen in the @implementation.
2355 for (const ObjCCategoryDecl *ClsExtDecl =
2356 IDecl->getFirstClassExtension();
2357 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2358 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2359 E = ClsExtDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002360 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002361 // Skip over properties declared @dynamic
2362 if (const ObjCPropertyImplDecl *PIDecl
2363 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2364 if (PIDecl->getPropertyImplementation()
2365 == ObjCPropertyImplDecl::Dynamic)
2366 continue;
2367
2368 for (const ObjCCategoryDecl *CExtDecl =
2369 IDecl->getFirstClassExtension();
2370 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2371 if (ObjCMethodDecl *GetterMethod =
2372 CExtDecl->getInstanceMethod(Property->getGetterName()))
2373 GetterMethod->setSynthesized(true);
2374 if (!Property->isReadOnly())
2375 if (ObjCMethodDecl *SetterMethod =
2376 CExtDecl->getInstanceMethod(Property->getSetterName()))
2377 SetterMethod->setSynthesized(true);
2378 }
2379 }
2380 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002381 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002382 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002383 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002384
Patrick Beardb2f68202012-04-06 18:12:22 +00002385 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2386 if (IDecl->getSuperClass() == NULL) {
2387 // This class has no superclass, so check that it has been marked with
2388 // __attribute((objc_root_class)).
2389 if (!HasRootClassAttr) {
2390 SourceLocation DeclLoc(IDecl->getLocation());
2391 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2392 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2393 << IDecl->getIdentifier();
2394 // See if NSObject is in the current scope, and if it is, suggest
2395 // adding " : NSObject " to the class declaration.
2396 NamedDecl *IF = LookupSingleName(TUScope,
2397 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2398 DeclLoc, LookupOrdinaryName);
2399 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2400 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2401 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2402 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2403 } else {
2404 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2405 }
2406 }
2407 } else if (HasRootClassAttr) {
2408 // Complain that only root classes may have this attribute.
2409 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2410 }
2411
John McCall260611a2012-06-20 06:18:46 +00002412 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002413 while (IDecl->getSuperClass()) {
2414 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2415 IDecl = IDecl->getSuperClass();
2416 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002417 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002418 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002419 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002420 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002421 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002422 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002423
Chris Lattner4d391482007-12-12 07:09:47 +00002424 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002425 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002426 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002427 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002428 Categories; Categories = Categories->getNextClassCategory()) {
2429 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002430 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002431 break;
2432 }
2433 }
2434 }
2435 }
Chris Lattner682bf922009-03-29 16:50:03 +00002436 if (isInterfaceDeclKind) {
2437 // Reject invalid vardecls.
2438 for (unsigned i = 0; i != tuvNum; i++) {
2439 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2440 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2441 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002442 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002443 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002444 }
Chris Lattner682bf922009-03-29 16:50:03 +00002445 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002446 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002447 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002448
2449 for (unsigned i = 0; i != tuvNum; i++) {
2450 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002451 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2452 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002453 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2454 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002455
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002456 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002457 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002458}
2459
2460
2461/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2462/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002463static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002464CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002465 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002466}
2467
Ted Kremenek422bae72010-04-18 04:59:38 +00002468static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002469unsigned countAlignAttr(const AttrVec &A) {
2470 unsigned count=0;
2471 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2472 if ((*i)->getKind() == attr::Aligned)
2473 ++count;
2474 return count;
2475}
2476
2477static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002478bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2479 const AttrVec &A) {
2480 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002481 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002482 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002483 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002484
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002485 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002486 // But implementation has attributes. This is invalid.
2487 // Except when implementation has 'Align' attribute which is
2488 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002489 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002490 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002491
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002492 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002493
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002494 unsigned countAlignOnImpl = countAlignAttr(A);
2495 if (!countAlignOnImpl && (A.size() != D.size()))
2496 return true;
2497 else if (countAlignOnImpl) {
2498 unsigned countAlignOnDecl = countAlignAttr(D);
2499 if (countAlignOnDecl && (A.size() != D.size()))
2500 return true;
2501 else if (!countAlignOnDecl &&
2502 ((A.size()-countAlignOnImpl) != D.size()))
2503 return true;
2504 }
2505
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002506 // attributes on method declaration and definition must match exactly.
2507 // Note that we have at most a couple of attributes on methods, so this
2508 // n*n search is good enough.
2509 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002510 if ((*i)->getKind() == attr::Aligned)
2511 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002512 bool match = false;
2513 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2514 if ((*i)->getKind() == (*i1)->getKind()) {
2515 match = true;
2516 break;
2517 }
2518 }
2519 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002520 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002521 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002522
Sean Huntcf807c42010-08-18 23:23:40 +00002523 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002524}
2525
Douglas Gregor926df6c2011-06-11 01:09:30 +00002526/// \brief Check whether the declared result type of the given Objective-C
2527/// method declaration is compatible with the method's class.
2528///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002529static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002530CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2531 ObjCInterfaceDecl *CurrentClass) {
2532 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002533
2534 // If an Objective-C method inherits its related result type, then its
2535 // declared result type must be compatible with its own class type. The
2536 // declared result type is compatible if:
2537 if (const ObjCObjectPointerType *ResultObjectType
2538 = ResultType->getAs<ObjCObjectPointerType>()) {
2539 // - it is id or qualified id, or
2540 if (ResultObjectType->isObjCIdType() ||
2541 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002542 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002543
2544 if (CurrentClass) {
2545 if (ObjCInterfaceDecl *ResultClass
2546 = ResultObjectType->getInterfaceDecl()) {
2547 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002548 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002549 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002550
2551 // - it is a superclass of the method's class type
2552 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002553 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002554 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002555 } else {
2556 // Any Objective-C pointer type might be acceptable for a protocol
2557 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002558 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002559 }
2560 }
2561
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002562 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002563}
2564
John McCall6c2c2502011-07-22 02:45:48 +00002565namespace {
2566/// A helper class for searching for methods which a particular method
2567/// overrides.
2568class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002569public:
John McCall6c2c2502011-07-22 02:45:48 +00002570 Sema &S;
2571 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002572 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002573 bool Recursive;
2574
2575public:
2576 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2577 Selector selector = method->getSelector();
2578
2579 // Bypass this search if we've never seen an instance/class method
2580 // with this selector before.
2581 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2582 if (it == S.MethodPool.end()) {
2583 if (!S.ExternalSource) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002584 S.ReadMethodPool(selector);
2585
2586 it = S.MethodPool.find(selector);
2587 if (it == S.MethodPool.end())
2588 return;
John McCall6c2c2502011-07-22 02:45:48 +00002589 }
2590 ObjCMethodList &list =
2591 method->isInstanceMethod() ? it->second.first : it->second.second;
2592 if (!list.Method) return;
2593
2594 ObjCContainerDecl *container
2595 = cast<ObjCContainerDecl>(method->getDeclContext());
2596
2597 // Prevent the search from reaching this container again. This is
2598 // important with categories, which override methods from the
2599 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002600 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2601 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002602 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2603 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002604 } else {
2605 searchFromContainer(container);
2606 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002607 }
John McCall6c2c2502011-07-22 02:45:48 +00002608
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002609 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002610 iterator begin() const { return Overridden.begin(); }
2611 iterator end() const { return Overridden.end(); }
2612
2613private:
2614 void searchFromContainer(ObjCContainerDecl *container) {
2615 if (container->isInvalidDecl()) return;
2616
2617 switch (container->getDeclKind()) {
2618#define OBJCCONTAINER(type, base) \
2619 case Decl::type: \
2620 searchFrom(cast<type##Decl>(container)); \
2621 break;
2622#define ABSTRACT_DECL(expansion)
2623#define DECL(type, base) \
2624 case Decl::type:
2625#include "clang/AST/DeclNodes.inc"
2626 llvm_unreachable("not an ObjC container!");
2627 }
2628 }
2629
2630 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002631 if (!protocol->hasDefinition())
2632 return;
2633
John McCall6c2c2502011-07-22 02:45:48 +00002634 // A method in a protocol declaration overrides declarations from
2635 // referenced ("parent") protocols.
2636 search(protocol->getReferencedProtocols());
2637 }
2638
2639 void searchFrom(ObjCCategoryDecl *category) {
2640 // A method in a category declaration overrides declarations from
2641 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002642 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002643 search(category->getReferencedProtocols());
2644 }
2645
2646 void searchFrom(ObjCCategoryImplDecl *impl) {
2647 // A method in a category definition that has a category
2648 // declaration overrides declarations from the category
2649 // declaration.
2650 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2651 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002652 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2653 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002654
2655 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002656 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2657 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002658 }
2659 }
2660
2661 void searchFrom(ObjCInterfaceDecl *iface) {
2662 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002663 if (!iface->hasDefinition())
2664 return;
2665
John McCall6c2c2502011-07-22 02:45:48 +00002666 // - categories,
2667 for (ObjCCategoryDecl *category = iface->getCategoryList();
2668 category; category = category->getNextClassCategory())
2669 search(category);
2670
2671 // - the super class, and
2672 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2673 search(super);
2674
2675 // - any referenced protocols.
2676 search(iface->getReferencedProtocols());
2677 }
2678
2679 void searchFrom(ObjCImplementationDecl *impl) {
2680 // A method in a class implementation overrides declarations from
2681 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002682 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2683 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002684 }
2685
2686
2687 void search(const ObjCProtocolList &protocols) {
2688 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2689 i != e; ++i)
2690 search(*i);
2691 }
2692
2693 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002694 // Check for a method in this container which matches this selector.
2695 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2696 Method->isInstanceMethod());
2697
2698 // If we find one, record it and bail out.
2699 if (meth) {
2700 Overridden.insert(meth);
2701 return;
2702 }
2703
2704 // Otherwise, search for methods that a hypothetical method here
2705 // would have overridden.
2706
2707 // Note that we're now in a recursive case.
2708 Recursive = true;
2709
2710 searchFromContainer(container);
2711 }
2712};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002713}
2714
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002715void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2716 ObjCInterfaceDecl *CurrentClass,
2717 ResultTypeCompatibilityKind RTC) {
2718 // Search for overridden methods and merge information down from them.
2719 OverrideSearch overrides(*this, ObjCMethod);
2720 // Keep track if the method overrides any method in the class's base classes,
2721 // its protocols, or its categories' protocols; we will keep that info
2722 // in the ObjCMethodDecl.
2723 // For this info, a method in an implementation is not considered as
2724 // overriding the same method in the interface or its categories.
2725 bool hasOverriddenMethodsInBaseOrProtocol = false;
2726 for (OverrideSearch::iterator
2727 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2728 ObjCMethodDecl *overridden = *i;
2729
2730 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2731 CurrentClass != overridden->getClassInterface() ||
2732 overridden->isOverriding())
2733 hasOverriddenMethodsInBaseOrProtocol = true;
2734
2735 // Propagate down the 'related result type' bit from overridden methods.
2736 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2737 ObjCMethod->SetRelatedResultType();
2738
2739 // Then merge the declarations.
2740 mergeObjCMethodDecls(ObjCMethod, overridden);
2741
2742 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2743 continue; // Conflicting properties are detected elsewhere.
2744
2745 // Check for overriding methods
2746 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2747 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2748 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2749 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2750
2751 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00002752 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
2753 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002754 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2755 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002756 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2757 PrevE = overridden->param_end();
2758 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002759 assert(PrevI != overridden->param_end() && "Param mismatch");
2760 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2761 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2762 // If type of argument of method in this class does not match its
2763 // respective argument type in the super class method, issue warning;
2764 if (!Context.typesAreCompatible(T1, T2)) {
2765 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2766 << T1 << T2;
2767 Diag(overridden->getLocation(), diag::note_previous_declaration);
2768 break;
2769 }
2770 }
2771 }
2772 }
2773
2774 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2775}
2776
John McCalld226f652010-08-21 09:40:31 +00002777Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002778 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002779 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002780 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002781 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002782 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002783 Selector Sel,
2784 // optional arguments. The number of types/arguments is obtained
2785 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002786 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002787 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002788 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002789 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002790 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002791 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002792 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002793 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002794 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002795 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2796 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002797 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002798
Douglas Gregore97179c2011-09-08 01:46:34 +00002799 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002800 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002801 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002802 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002803
Steve Naroffccef3712009-02-20 22:59:16 +00002804 // Methods cannot return interface types. All ObjC objects are
2805 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002806 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002807 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2808 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002809 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002810 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002811
2812 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002813 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002814 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002815 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002816 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002817 }
Mike Stump1eb44332009-09-09 15:08:12 +00002818
2819 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002820 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002821 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002822 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002823 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002824 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002825 /*isSynthesized=*/false,
2826 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002827 MethodDeclKind == tok::objc_optional
2828 ? ObjCMethodDecl::Optional
2829 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002830 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002831
Chris Lattner5f9e2722011-07-23 10:55:15 +00002832 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002833
Chris Lattner7db638d2009-04-11 19:42:43 +00002834 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002835 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002836 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002837
Chris Lattnere294d3f2009-04-11 18:57:04 +00002838 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002839 ArgType = Context.getObjCIdType();
2840 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002841 } else {
John McCall58e46772009-10-23 21:48:59 +00002842 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002843 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002844 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002845 }
Mike Stump1eb44332009-09-09 15:08:12 +00002846
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002847 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2848 LookupOrdinaryName, ForRedeclaration);
2849 LookupName(R, S);
2850 if (R.isSingleResult()) {
2851 NamedDecl *PrevDecl = R.getFoundDecl();
2852 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002853 Diag(ArgInfo[i].NameLoc,
2854 (MethodDefinition ? diag::warn_method_param_redefinition
2855 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002856 << ArgInfo[i].Name;
2857 Diag(PrevDecl->getLocation(),
2858 diag::note_previous_declaration);
2859 }
2860 }
2861
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002862 SourceLocation StartLoc = DI
2863 ? DI->getTypeLoc().getBeginLoc()
2864 : ArgInfo[i].NameLoc;
2865
John McCall81ef3e62011-04-23 02:46:06 +00002866 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2867 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2868 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002869
John McCall70798862011-05-02 00:30:12 +00002870 Param->setObjCMethodScopeInfo(i);
2871
Chris Lattner0ed844b2008-04-04 06:12:32 +00002872 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002873 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002874
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002875 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002876 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002877
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002878 if (Param->hasAttr<BlocksAttr>()) {
2879 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2880 Param->setInvalidDecl();
2881 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002882 S->AddDecl(Param);
2883 IdResolver.AddDecl(Param);
2884
Chris Lattner0ed844b2008-04-04 06:12:32 +00002885 Params.push_back(Param);
2886 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002887
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002888 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002889 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002890 QualType ArgType = Param->getType();
2891 if (ArgType.isNull())
2892 ArgType = Context.getObjCIdType();
2893 else
2894 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002895 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002896 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002897 Diag(Param->getLocation(),
2898 diag::err_object_cannot_be_passed_returned_by_value)
2899 << 1 << ArgType;
2900 Param->setInvalidDecl();
2901 }
2902 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002903
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002904 Params.push_back(Param);
2905 }
2906
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002907 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002908 ObjCMethod->setObjCDeclQualifier(
2909 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002910
2911 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002912 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002913
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002914 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002915 const ObjCMethodDecl *PrevMethod = 0;
2916 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002917 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002918 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2919 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002920 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002921 PrevMethod = ImpDecl->getClassMethod(Sel);
2922 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002923 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002924
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002925 ObjCMethodDecl *IMD = 0;
2926 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2927 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2928 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002929 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002930 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002931 SourceLocation MethodLoc = IMD->getLocation();
2932 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2933 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002934 Diag(MethodLoc, diag::note_method_declared_at)
2935 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002936 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002937 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002938 } else {
2939 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002940 }
John McCall6c2c2502011-07-22 02:45:48 +00002941
Chris Lattner4d391482007-12-12 07:09:47 +00002942 if (PrevMethod) {
2943 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002944 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002945 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002946 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002947 }
John McCall54abf7d2009-11-04 02:18:39 +00002948
Douglas Gregor926df6c2011-06-11 01:09:30 +00002949 // If this Objective-C method does not have a related result type, but we
2950 // are allowed to infer related result types, try to do so based on the
2951 // method family.
2952 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2953 if (!CurrentClass) {
2954 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2955 CurrentClass = Cat->getClassInterface();
2956 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2957 CurrentClass = Impl->getClassInterface();
2958 else if (ObjCCategoryImplDecl *CatImpl
2959 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2960 CurrentClass = CatImpl->getClassInterface();
2961 }
John McCall6c2c2502011-07-22 02:45:48 +00002962
Douglas Gregore97179c2011-09-08 01:46:34 +00002963 ResultTypeCompatibilityKind RTC
2964 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002965
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002966 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00002967
John McCallf85e1932011-06-15 23:02:42 +00002968 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002969 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002970 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2971
Douglas Gregore97179c2011-09-08 01:46:34 +00002972 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002973 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00002974 !ObjCMethod->hasRelatedResultType() &&
2975 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002976 bool InferRelatedResultType = false;
2977 switch (ObjCMethod->getMethodFamily()) {
2978 case OMF_None:
2979 case OMF_copy:
2980 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002981 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002982 case OMF_mutableCopy:
2983 case OMF_release:
2984 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002985 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002986 break;
2987
2988 case OMF_alloc:
2989 case OMF_new:
2990 InferRelatedResultType = ObjCMethod->isClassMethod();
2991 break;
2992
2993 case OMF_init:
2994 case OMF_autorelease:
2995 case OMF_retain:
2996 case OMF_self:
2997 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2998 break;
2999 }
3000
John McCall6c2c2502011-07-22 02:45:48 +00003001 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003002 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003003 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003004
3005 ActOnDocumentableDecl(ObjCMethod);
3006
John McCalld226f652010-08-21 09:40:31 +00003007 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003008}
3009
Chris Lattnercc98eac2008-12-17 07:13:27 +00003010bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003011 // Following is also an error. But it is caused by a missing @end
3012 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003013 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003014 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003015
3016 // If we switched context to translation unit while we are still lexically in
3017 // an objc container, it means the parser missed emitting an error.
3018 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3019 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003020
Anders Carlsson15281452008-11-04 16:57:32 +00003021 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3022 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003023
Anders Carlsson15281452008-11-04 16:57:32 +00003024 return true;
3025}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003026
James Dennett1dfbd922012-06-14 21:40:34 +00003027/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003028/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003029void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003030 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003031 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003032 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003033 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003034 if (!Class) {
3035 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3036 return;
3037 }
John McCall260611a2012-06-20 06:18:46 +00003038 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003039 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3040 return;
3041 }
Mike Stump1eb44332009-09-09 15:08:12 +00003042
Chris Lattnercc98eac2008-12-17 07:13:27 +00003043 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003044 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003045 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003046 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003047 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003048 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003049 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003050 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3051 /*FIXME: StartL=*/ID->getLocation(),
3052 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003053 ID->getIdentifier(), ID->getType(),
3054 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003055 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003056 }
Mike Stump1eb44332009-09-09 15:08:12 +00003057
Chris Lattnercc98eac2008-12-17 07:13:27 +00003058 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003059 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003060 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003061 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003062 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003063 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003064 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003065 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003066 }
3067}
3068
Douglas Gregor160b5632010-04-26 17:32:49 +00003069/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003070VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3071 SourceLocation StartLoc,
3072 SourceLocation IdLoc,
3073 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003074 bool Invalid) {
3075 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3076 // duration shall not be qualified by an address-space qualifier."
3077 // Since all parameters have automatic store duration, they can not have
3078 // an address space.
3079 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003080 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003081 Invalid = true;
3082 }
3083
3084 // An @catch parameter must be an unqualified object pointer type;
3085 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3086 if (Invalid) {
3087 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003088 } else if (T->isDependentType()) {
3089 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003090 } else if (!T->isObjCObjectPointerType()) {
3091 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003092 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003093 } else if (T->isObjCQualifiedIdType()) {
3094 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003095 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003096 }
3097
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003098 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3099 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003100 New->setExceptionVariable(true);
3101
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003102 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003103 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003104 Invalid = true;
3105
Douglas Gregor160b5632010-04-26 17:32:49 +00003106 if (Invalid)
3107 New->setInvalidDecl();
3108 return New;
3109}
3110
John McCalld226f652010-08-21 09:40:31 +00003111Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003112 const DeclSpec &DS = D.getDeclSpec();
3113
3114 // We allow the "register" storage class on exception variables because
3115 // GCC did, but we drop it completely. Any other storage class is an error.
3116 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3117 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3118 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3119 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3120 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3121 << DS.getStorageClassSpec();
3122 }
3123 if (D.getDeclSpec().isThreadSpecified())
3124 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3125 D.getMutableDeclSpec().ClearStorageClassSpecs();
3126
3127 DiagnoseFunctionSpecifiers(D);
3128
3129 // Check that there are no default arguments inside the type of this
3130 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003131 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003132 CheckExtraCXXDefaultArguments(D);
3133
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003134 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003135 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003136
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003137 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3138 D.getSourceRange().getBegin(),
3139 D.getIdentifierLoc(),
3140 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003141 D.isInvalidType());
3142
3143 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3144 if (D.getCXXScopeSpec().isSet()) {
3145 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3146 << D.getCXXScopeSpec().getRange();
3147 New->setInvalidDecl();
3148 }
3149
3150 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003151 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003152 if (D.getIdentifier())
3153 IdResolver.AddDecl(New);
3154
3155 ProcessDeclAttributes(S, New, D);
3156
3157 if (New->hasAttr<BlocksAttr>())
3158 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003159 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003160}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003161
3162/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003163/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003164void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003165 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003166 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3167 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003168 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003169 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003170 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003171 }
3172}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003173
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003174void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003175 // Load referenced selectors from the external source.
3176 if (ExternalSource) {
3177 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3178 ExternalSource->ReadReferencedSelectors(Sels);
3179 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3180 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3181 }
3182
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003183 // Warning will be issued only when selector table is
3184 // generated (which means there is at lease one implementation
3185 // in the TU). This is to match gcc's behavior.
3186 if (ReferencedSelectors.empty() ||
3187 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003188 return;
3189 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3190 ReferencedSelectors.begin(),
3191 E = ReferencedSelectors.end(); S != E; ++S) {
3192 Selector Sel = (*S).first;
3193 if (!LookupImplementedMethodInGlobalPool(Sel))
3194 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3195 }
3196 return;
3197}