blob: f4d63faaafabe886301bf80ec1ae449c8bc6a1a3 [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 Jahanian5ac96d52011-02-15 17:49:58 +0000374 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000375 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000376 DiagnoseObjCImplementedDeprecations(*this,
377 dyn_cast<NamedDecl>(IMD),
378 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000379
Nico Weber80cb6e62011-08-28 22:35:17 +0000380 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000381 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
382 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
383 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000384 if (IC->getSuperClass()) {
Eli Friedman95aac152012-08-01 21:02:59 +0000385 getCurFunction()->ObjCShouldCallSuperDealloc =
David Blaikie4e4d0842012-03-11 07:00:24 +0000386 !(Context.getLangOpts().ObjCAutoRefCount ||
387 Context.getLangOpts().getGC() == LangOptions::GCOnly) &&
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000388 MDecl->getMethodFamily() == OMF_dealloc;
Eli Friedman95aac152012-08-01 21:02:59 +0000389 getCurFunction()->ObjCShouldCallSuperFinalize =
David Blaikie4e4d0842012-03-11 07:00:24 +0000390 Context.getLangOpts().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000391 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000392 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000393 }
Chris Lattner4d391482007-12-12 07:09:47 +0000394}
395
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000396namespace {
397
398// Callback to only accept typo corrections that are Objective-C classes.
399// If an ObjCInterfaceDecl* is given to the constructor, then the validation
400// function will reject corrections to that class.
401class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
402 public:
403 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
404 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
405 : CurrentIDecl(IDecl) {}
406
407 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
408 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
409 return ID && !declaresSameEntity(ID, CurrentIDecl);
410 }
411
412 private:
413 ObjCInterfaceDecl *CurrentIDecl;
414};
415
416}
417
John McCalld226f652010-08-21 09:40:31 +0000418Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000419ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
420 IdentifierInfo *ClassName, SourceLocation ClassLoc,
421 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000422 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000423 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000424 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000425 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000426
Chris Lattner4d391482007-12-12 07:09:47 +0000427 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000428 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000429 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000430
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000431 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000432 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000433 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000434 }
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Douglas Gregor7723fec2011-12-15 20:29:51 +0000436 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000437 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000438 ObjCInterfaceDecl *IDecl
439 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000440 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000441
Douglas Gregor7723fec2011-12-15 20:29:51 +0000442 if (PrevIDecl) {
443 // Class already seen. Was it a definition?
444 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
445 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
446 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000447 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000448 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000449 }
Chris Lattner4d391482007-12-12 07:09:47 +0000450 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000451
452 if (AttrList)
453 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
454 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Douglas Gregor7723fec2011-12-15 20:29:51 +0000456 // Start the definition of this class. If we're in a redefinition case, there
457 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000458 if (!IDecl->hasDefinition())
459 IDecl->startDefinition();
460
Chris Lattner4d391482007-12-12 07:09:47 +0000461 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000462 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000463 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
464 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000465
466 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000467 // Try to correct for a typo in the superclass name without correcting
468 // to the class we're defining.
469 ObjCInterfaceValidatorCCC Validator(IDecl);
470 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000471 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000472 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000473 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
474 Diag(SuperLoc, diag::err_undef_superclass_suggest)
475 << SuperName << ClassName << PrevDecl->getDeclName();
476 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
477 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000478 }
479 }
480
Douglas Gregor60ef3082011-12-15 00:29:59 +0000481 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000482 Diag(SuperLoc, diag::err_recursive_superclass)
483 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000484 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000485 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000486 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000487 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000488
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000489 // Diagnose classes that inherit from deprecated classes.
490 if (SuperClassDecl)
491 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000493 if (PrevDecl && SuperClassDecl == 0) {
494 // The previous declaration was not a class decl. Check if we have a
495 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000496 if (const TypedefNameDecl *TDecl =
497 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000498 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000499 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000500 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
501 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000502 }
503 }
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000505 // This handles the following case:
506 //
507 // typedef int SuperClass;
508 // @interface MyClass : SuperClass {} @end
509 //
510 if (!SuperClassDecl) {
511 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
512 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000513 }
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Richard Smith162e1c12011-04-15 14:24:37 +0000516 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000517 if (!SuperClassDecl)
518 Diag(SuperLoc, diag::err_undef_superclass)
519 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000520 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000521 Context.getObjCInterfaceType(SuperClassDecl),
522 diag::err_forward_superclass,
523 SuperClassDecl->getDeclName(),
524 ClassName,
525 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000526 SuperClassDecl = 0;
527 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000528 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000529 IDecl->setSuperClass(SuperClassDecl);
530 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000531 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000532 }
Chris Lattner4d391482007-12-12 07:09:47 +0000533 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000534 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000535 }
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Sebastian Redl0b17c612010-08-13 00:28:03 +0000537 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000538 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000539 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000540 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000541 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000542 }
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Anders Carlsson15281452008-11-04 16:57:32 +0000544 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000545 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000546}
547
Richard Smithde01b7a2012-08-08 23:32:13 +0000548/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000549/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000550Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
551 IdentifierInfo *AliasName,
552 SourceLocation AliasLocation,
553 IdentifierInfo *ClassName,
554 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000555 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000556 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000557 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000558 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000559 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000560 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000561 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000562 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000563 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000564 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000565 }
566 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000567 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000568 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000569 if (const TypedefNameDecl *TDecl =
570 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000571 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000572 if (T->isObjCObjectType()) {
573 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000574 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000575 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000576 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000577 }
578 }
579 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000580 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
581 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000582 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000583 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000584 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000585 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000588 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000589 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000590 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Anders Carlsson15281452008-11-04 16:57:32 +0000592 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000593 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000594
John McCalld226f652010-08-21 09:40:31 +0000595 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000596}
597
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000598bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000599 IdentifierInfo *PName,
600 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000601 const ObjCList<ObjCProtocolDecl> &PList) {
602
603 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000604 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
605 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000606 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
607 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000608 if (PDecl->getIdentifier() == PName) {
609 Diag(Ploc, diag::err_protocol_has_circular_dependency);
610 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000611 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000612 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000613
614 if (!PDecl->hasDefinition())
615 continue;
616
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000617 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
618 PDecl->getLocation(), PDecl->getReferencedProtocols()))
619 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000620 }
621 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000622 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000623}
624
John McCalld226f652010-08-21 09:40:31 +0000625Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000626Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
627 IdentifierInfo *ProtocolName,
628 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000629 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000630 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000631 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000632 SourceLocation EndProtoLoc,
633 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000634 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000635 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000636 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000637 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
638 ForRedeclaration);
639 ObjCProtocolDecl *PDecl = 0;
640 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
641 // If we already have a definition, complain.
642 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
643 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000644
Douglas Gregor27c6da22012-01-01 20:30:41 +0000645 // Create a new protocol that is completely distinct from previous
646 // declarations, and do not make this protocol available for name lookup.
647 // That way, we'll end up completely ignoring the duplicate.
648 // FIXME: Can we turn this into an error?
649 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
650 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000651 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000652 PDecl->startDefinition();
653 } else {
654 if (PrevDecl) {
655 // Check for circular dependencies among protocol declarations. This can
656 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000657 ObjCList<ObjCProtocolDecl> PList;
658 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
659 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000660 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000661 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000662
663 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000664 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000665 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000666 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000667
Douglas Gregor6e378de2009-04-23 23:18:26 +0000668 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000669 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000670 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000671
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000672 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000673 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000674
675 // Merge attributes from previous declarations.
676 if (PrevDecl)
677 mergeDeclAttributes(PDecl, PrevDecl);
678
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000679 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000680 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000681 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
682 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000683 }
Mike Stump1eb44332009-09-09 15:08:12 +0000684
685 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000686 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000687}
688
689/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000690/// issues an error if they are not declared. It returns list of
691/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000692void
Chris Lattnere13b9592008-07-26 04:03:38 +0000693Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000694 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000695 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000696 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000697 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000698 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
699 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000700 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000701 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000702 TypoCorrection Corrected = CorrectTypo(
703 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000704 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000705 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000706 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000707 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000708 Diag(PDecl->getLocation(), diag::note_previous_decl)
709 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000710 }
711 }
712
713 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000714 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000715 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000716 continue;
717 }
Mike Stump1eb44332009-09-09 15:08:12 +0000718
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000719 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000720
721 // If this is a forward declaration and we are supposed to warn in this
722 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000723 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000724 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000725 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000726 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000727 }
728}
729
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000730/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000731/// a class method in its extension.
732///
Mike Stump1eb44332009-09-09 15:08:12 +0000733void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000734 ObjCInterfaceDecl *ID) {
735 if (!ID)
736 return; // Possibly due to previous error
737
738 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000739 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
740 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000741 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000742 MethodMap[MD->getSelector()] = MD;
743 }
744
745 if (MethodMap.empty())
746 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000747 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
748 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000749 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000750 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
751 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
752 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
753 << Method->getDeclName();
754 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
755 }
756 }
757}
758
James Dennett1dfbd922012-06-14 21:40:34 +0000759/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000760Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000761Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000762 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000763 unsigned NumElts,
764 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000765 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000766 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000767 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000768 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
769 ForRedeclaration);
770 ObjCProtocolDecl *PDecl
771 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
772 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000773 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000774
775 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000776 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000777
Douglas Gregor3937f872012-01-01 20:33:24 +0000778 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000779 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000780
781 if (PrevDecl)
782 mergeDeclAttributes(PDecl, PrevDecl);
783
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000784 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000787 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000788}
789
John McCalld226f652010-08-21 09:40:31 +0000790Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000791ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
792 IdentifierInfo *ClassName, SourceLocation ClassLoc,
793 IdentifierInfo *CategoryName,
794 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000795 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000796 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000797 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000798 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000799 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000800 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000801
802 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000803
804 if (!IDecl
805 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000806 diag::err_category_forward_interface,
807 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000808 // Create an invalid ObjCCategoryDecl to serve as context for
809 // the enclosing method declarations. We mark the decl invalid
810 // to make it clear that this isn't a valid AST.
811 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000812 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000813 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000814 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000815
816 if (!IDecl)
817 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000818 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000819 }
820
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000821 if (!CategoryName && IDecl->getImplementation()) {
822 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
823 Diag(IDecl->getImplementation()->getLocation(),
824 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000825 }
826
Fariborz Jahanian25760612010-02-15 21:55:26 +0000827 if (CategoryName) {
828 /// Check for duplicate interface declaration for this category
829 ObjCCategoryDecl *CDeclChain;
830 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
831 CDeclChain = CDeclChain->getNextClassCategory()) {
832 if (CDeclChain->getIdentifier() == CategoryName) {
833 // Class extensions can be declared multiple times.
834 Diag(CategoryLoc, diag::warn_dup_category_def)
835 << ClassName << CategoryName;
836 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
837 break;
838 }
Chris Lattner70f19542009-02-16 21:26:43 +0000839 }
840 }
Chris Lattner70f19542009-02-16 21:26:43 +0000841
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000842 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
843 ClassLoc, CategoryLoc, CategoryName, IDecl);
844 // FIXME: PushOnScopeChains?
845 CurContext->addDecl(CDecl);
846
Chris Lattner4d391482007-12-12 07:09:47 +0000847 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000848 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000849 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000850 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000851 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000852 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000853 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000854 }
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Anders Carlsson15281452008-11-04 16:57:32 +0000856 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000857 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000858}
859
860/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000861/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000862/// object.
John McCalld226f652010-08-21 09:40:31 +0000863Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000864 SourceLocation AtCatImplLoc,
865 IdentifierInfo *ClassName, SourceLocation ClassLoc,
866 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000867 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000868 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000869 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000870 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
871 if (!CatIDecl) {
872 // Category @implementation with no corresponding @interface.
873 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000874 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
875 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000876 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000877 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000878 }
879 }
880
Mike Stump1eb44332009-09-09 15:08:12 +0000881 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000882 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000883 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000884 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000885 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000886 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000887 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000888 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
889 diag::err_undef_interface)) {
890 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000891 }
Chris Lattner4d391482007-12-12 07:09:47 +0000892
Douglas Gregord0434102009-01-09 00:49:46 +0000893 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000894 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000895
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000896 // If the interface is deprecated/unavailable, warn/error about it.
897 if (IDecl)
898 DiagnoseUseOfDecl(IDecl, ClassLoc);
899
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000900 /// Check that CatName, category name, is not used in another implementation.
901 if (CatIDecl) {
902 if (CatIDecl->getImplementation()) {
903 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
904 << CatName;
905 Diag(CatIDecl->getImplementation()->getLocation(),
906 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000907 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000908 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000909 // Warn on implementating category of deprecated class under
910 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000911 DiagnoseObjCImplementedDeprecations(*this,
912 dyn_cast<NamedDecl>(IDecl),
913 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000914 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000915 }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Anders Carlsson15281452008-11-04 16:57:32 +0000917 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000918 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000919}
920
John McCalld226f652010-08-21 09:40:31 +0000921Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000922 SourceLocation AtClassImplLoc,
923 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000924 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000925 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000926 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000927 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000928 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000929 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
930 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000931 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000932 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000933 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000934 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000935 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
936 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000937 } else {
938 // We did not find anything with the name ClassName; try to correct for
939 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000940 ObjCInterfaceValidatorCCC Validator;
941 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000942 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000943 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000944 // Suggest the (potentially) correct interface name. However, put the
945 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000946 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000947 // provide a code-modification hint or use the typo name for recovery,
948 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000949 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000950 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000951 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000952 << ClassName << CorrectedName;
953 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
954 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000955 IDecl = 0;
956 } else {
957 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
958 }
Chris Lattner4d391482007-12-12 07:09:47 +0000959 }
Mike Stump1eb44332009-09-09 15:08:12 +0000960
Chris Lattner4d391482007-12-12 07:09:47 +0000961 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000962 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000963 if (SuperClassname) {
964 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000965 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
966 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000967 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000968 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
969 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000970 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000971 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000972 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000973 if (SDecl && !SDecl->hasDefinition())
974 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000975 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000976 Diag(SuperClassLoc, diag::err_undef_superclass)
977 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000978 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000979 // This implementation and its interface do not have the same
980 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000981 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000982 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000983 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000984 }
985 }
986 }
Mike Stump1eb44332009-09-09 15:08:12 +0000987
Chris Lattner4d391482007-12-12 07:09:47 +0000988 if (!IDecl) {
989 // Legacy case of @implementation with no corresponding @interface.
990 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000991
Mike Stump390b4cc2009-05-16 07:39:55 +0000992 // FIXME: Do we support attributes on the @implementation? If so we should
993 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000994 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000995 ClassName, /*PrevDecl=*/0, ClassLoc,
996 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000997 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000998 if (SDecl) {
999 IDecl->setSuperClass(SDecl);
1000 IDecl->setSuperClassLoc(SuperClassLoc);
1001 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1002 } else {
1003 IDecl->setEndOfDefinitionLoc(ClassLoc);
1004 }
1005
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001006 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001007 } else {
1008 // Mark the interface as being completed, even if it was just as
1009 // @class ....;
1010 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001011 if (!IDecl->hasDefinition())
1012 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001013 }
Mike Stump1eb44332009-09-09 15:08:12 +00001014
1015 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001016 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1017 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Anders Carlsson15281452008-11-04 16:57:32 +00001019 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001020 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Chris Lattner4d391482007-12-12 07:09:47 +00001022 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001023 if (IDecl->getImplementation()) {
1024 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001025 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001026 Diag(IDecl->getImplementation()->getLocation(),
1027 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001028 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001029 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001030 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001031 // Warn on implementating deprecated class under
1032 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001033 DiagnoseObjCImplementedDeprecations(*this,
1034 dyn_cast<NamedDecl>(IDecl),
1035 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001036 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001037 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001038}
1039
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001040Sema::DeclGroupPtrTy
1041Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1042 SmallVector<Decl *, 64> DeclsInGroup;
1043 DeclsInGroup.reserve(Decls.size() + 1);
1044
1045 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1046 Decl *Dcl = Decls[i];
1047 if (!Dcl)
1048 continue;
1049 if (Dcl->getDeclContext()->isFileContext())
1050 Dcl->setTopLevelDeclInObjCContainer();
1051 DeclsInGroup.push_back(Dcl);
1052 }
1053
1054 DeclsInGroup.push_back(ObjCImpDecl);
1055
1056 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1057}
1058
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001059void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1060 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001061 SourceLocation RBrace) {
1062 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001063 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001064 if (!IDecl)
1065 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001066 /// Check case of non-existing \@interface decl.
1067 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001068 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001069 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001070 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001071 // Add ivar's to class's DeclContext.
1072 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001073 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001074 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001075 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001076 }
1077
Chris Lattner4d391482007-12-12 07:09:47 +00001078 return;
1079 }
1080 // If implementation has empty ivar list, just return.
1081 if (numIvars == 0)
1082 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001083
Chris Lattner4d391482007-12-12 07:09:47 +00001084 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001085 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001086 if (ImpDecl->getSuperClass())
1087 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1088 for (unsigned i = 0; i < numIvars; i++) {
1089 ObjCIvarDecl* ImplIvar = ivars[i];
1090 if (const ObjCIvarDecl *ClsIvar =
1091 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1092 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1093 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1094 continue;
1095 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001096 // Instance ivar to Implementation's DeclContext.
1097 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001098 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001099 ImpDecl->addDecl(ImplIvar);
1100 }
1101 return;
1102 }
Chris Lattner4d391482007-12-12 07:09:47 +00001103 // Check interface's Ivar list against those in the implementation.
1104 // names and types must match.
1105 //
Chris Lattner4d391482007-12-12 07:09:47 +00001106 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001107 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001108 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1109 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001110 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001111 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001112 assert (ImplIvar && "missing implementation ivar");
1113 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001114
Steve Naroffca331292009-03-03 14:49:36 +00001115 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001116 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001117 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001118 << ImplIvar->getIdentifier()
1119 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001120 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001121 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1122 ImplIvar->getBitWidthValue(Context) !=
1123 ClsIvar->getBitWidthValue(Context)) {
1124 Diag(ImplIvar->getBitWidth()->getLocStart(),
1125 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1126 Diag(ClsIvar->getBitWidth()->getLocStart(),
1127 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001128 }
Steve Naroffca331292009-03-03 14:49:36 +00001129 // Make sure the names are identical.
1130 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001131 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001132 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001133 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001134 }
1135 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001136 }
Mike Stump1eb44332009-09-09 15:08:12 +00001137
Chris Lattner609e4c72007-12-12 18:11:49 +00001138 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001139 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001140 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001141 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001142}
1143
Steve Naroff3c2eb662008-02-10 21:38:56 +00001144void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001145 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001146 // No point warning no definition of method which is 'unavailable'.
1147 if (method->hasAttr<UnavailableAttr>())
1148 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001149 if (!IncompleteImpl) {
1150 Diag(ImpLoc, diag::warn_incomplete_impl);
1151 IncompleteImpl = true;
1152 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001153 if (DiagID == diag::warn_unimplemented_protocol_method)
1154 Diag(ImpLoc, DiagID) << method->getDeclName();
1155 else
1156 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001157}
1158
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001159/// Determines if type B can be substituted for type A. Returns true if we can
1160/// guarantee that anything that the user will do to an object of type A can
1161/// also be done to an object of type B. This is trivially true if the two
1162/// types are the same, or if B is a subclass of A. It becomes more complex
1163/// in cases where protocols are involved.
1164///
1165/// Object types in Objective-C describe the minimum requirements for an
1166/// object, rather than providing a complete description of a type. For
1167/// example, if A is a subclass of B, then B* may refer to an instance of A.
1168/// The principle of substitutability means that we may use an instance of A
1169/// anywhere that we may use an instance of B - it will implement all of the
1170/// ivars of B and all of the methods of B.
1171///
1172/// This substitutability is important when type checking methods, because
1173/// the implementation may have stricter type definitions than the interface.
1174/// The interface specifies minimum requirements, but the implementation may
1175/// have more accurate ones. For example, a method may privately accept
1176/// instances of B, but only publish that it accepts instances of A. Any
1177/// object passed to it will be type checked against B, and so will implicitly
1178/// by a valid A*. Similarly, a method may return a subclass of the class that
1179/// it is declared as returning.
1180///
1181/// This is most important when considering subclassing. A method in a
1182/// subclass must accept any object as an argument that its superclass's
1183/// implementation accepts. It may, however, accept a more general type
1184/// without breaking substitutability (i.e. you can still use the subclass
1185/// anywhere that you can use the superclass, but not vice versa). The
1186/// converse requirement applies to return types: the return type for a
1187/// subclass method must be a valid object of the kind that the superclass
1188/// advertises, but it may be specified more accurately. This avoids the need
1189/// for explicit down-casting by callers.
1190///
1191/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001192static bool isObjCTypeSubstitutable(ASTContext &Context,
1193 const ObjCObjectPointerType *A,
1194 const ObjCObjectPointerType *B,
1195 bool rejectId) {
1196 // Reject a protocol-unqualified id.
1197 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001198
1199 // If B is a qualified id, then A must also be a qualified id and it must
1200 // implement all of the protocols in B. It may not be a qualified class.
1201 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1202 // stricter definition so it is not substitutable for id<A>.
1203 if (B->isObjCQualifiedIdType()) {
1204 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001205 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1206 QualType(B,0),
1207 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001208 }
1209
1210 /*
1211 // id is a special type that bypasses type checking completely. We want a
1212 // warning when it is used in one place but not another.
1213 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1214
1215
1216 // If B is a qualified id, then A must also be a qualified id (which it isn't
1217 // if we've got this far)
1218 if (B->isObjCQualifiedIdType()) return false;
1219 */
1220
1221 // Now we know that A and B are (potentially-qualified) class types. The
1222 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001223 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001224}
1225
John McCall10302c02010-10-28 02:34:38 +00001226static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1227 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1228}
1229
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001230static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001231 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001232 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001233 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001234 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001235 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001236 if (IsProtocolMethodDecl &&
1237 (MethodDecl->getObjCDeclQualifier() !=
1238 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001239 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001240 S.Diag(MethodImpl->getLocation(),
1241 (IsOverridingMode ?
1242 diag::warn_conflicting_overriding_ret_type_modifiers
1243 : diag::warn_conflicting_ret_type_modifiers))
1244 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001245 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1246 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1247 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1248 }
1249 else
1250 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001251 }
1252
John McCall10302c02010-10-28 02:34:38 +00001253 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001254 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001255 return true;
1256 if (!Warn)
1257 return false;
John McCall10302c02010-10-28 02:34:38 +00001258
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001259 unsigned DiagID =
1260 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1261 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001262
1263 // Mismatches between ObjC pointers go into a different warning
1264 // category, and sometimes they're even completely whitelisted.
1265 if (const ObjCObjectPointerType *ImplPtrTy =
1266 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1267 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001268 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001269 // Allow non-matching return types as long as they don't violate
1270 // the principle of substitutability. Specifically, we permit
1271 // return types that are subclasses of the declared return type,
1272 // or that are more-qualified versions of the declared type.
1273 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001274 return false;
John McCall10302c02010-10-28 02:34:38 +00001275
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001276 DiagID =
1277 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1278 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001279 }
1280 }
1281
1282 S.Diag(MethodImpl->getLocation(), DiagID)
1283 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001284 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001285 << MethodImpl->getResultType()
1286 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001287 S.Diag(MethodDecl->getLocation(),
1288 IsOverridingMode ? diag::note_previous_declaration
1289 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001290 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001291 return false;
John McCall10302c02010-10-28 02:34:38 +00001292}
1293
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001294static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001295 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001296 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001297 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001298 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001299 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001300 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001301 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001302 if (IsProtocolMethodDecl &&
1303 (ImplVar->getObjCDeclQualifier() !=
1304 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001305 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001306 if (IsOverridingMode)
1307 S.Diag(ImplVar->getLocation(),
1308 diag::warn_conflicting_overriding_param_modifiers)
1309 << getTypeRange(ImplVar->getTypeSourceInfo())
1310 << MethodImpl->getDeclName();
1311 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001312 diag::warn_conflicting_param_modifiers)
1313 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001314 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001315 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1316 << getTypeRange(IfaceVar->getTypeSourceInfo());
1317 }
1318 else
1319 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001320 }
1321
John McCall10302c02010-10-28 02:34:38 +00001322 QualType ImplTy = ImplVar->getType();
1323 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001324
John McCall10302c02010-10-28 02:34:38 +00001325 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001326 return true;
1327
1328 if (!Warn)
1329 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001330 unsigned DiagID =
1331 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1332 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001333
1334 // Mismatches between ObjC pointers go into a different warning
1335 // category, and sometimes they're even completely whitelisted.
1336 if (const ObjCObjectPointerType *ImplPtrTy =
1337 ImplTy->getAs<ObjCObjectPointerType>()) {
1338 if (const ObjCObjectPointerType *IfacePtrTy =
1339 IfaceTy->getAs<ObjCObjectPointerType>()) {
1340 // Allow non-matching argument types as long as they don't
1341 // violate the principle of substitutability. Specifically, the
1342 // implementation must accept any objects that the superclass
1343 // accepts, however it may also accept others.
1344 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001345 return false;
John McCall10302c02010-10-28 02:34:38 +00001346
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001347 DiagID =
1348 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1349 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001350 }
1351 }
1352
1353 S.Diag(ImplVar->getLocation(), DiagID)
1354 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001355 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1356 S.Diag(IfaceVar->getLocation(),
1357 (IsOverridingMode ? diag::note_previous_declaration
1358 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001359 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001360 return false;
John McCall10302c02010-10-28 02:34:38 +00001361}
John McCallf85e1932011-06-15 23:02:42 +00001362
1363/// In ARC, check whether the conventional meanings of the two methods
1364/// match. If they don't, it's a hard error.
1365static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1366 ObjCMethodDecl *decl) {
1367 ObjCMethodFamily implFamily = impl->getMethodFamily();
1368 ObjCMethodFamily declFamily = decl->getMethodFamily();
1369 if (implFamily == declFamily) return false;
1370
1371 // Since conventions are sorted by selector, the only possibility is
1372 // that the types differ enough to cause one selector or the other
1373 // to fall out of the family.
1374 assert(implFamily == OMF_None || declFamily == OMF_None);
1375
1376 // No further diagnostics required on invalid declarations.
1377 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1378
1379 const ObjCMethodDecl *unmatched = impl;
1380 ObjCMethodFamily family = declFamily;
1381 unsigned errorID = diag::err_arc_lost_method_convention;
1382 unsigned noteID = diag::note_arc_lost_method_convention;
1383 if (declFamily == OMF_None) {
1384 unmatched = decl;
1385 family = implFamily;
1386 errorID = diag::err_arc_gained_method_convention;
1387 noteID = diag::note_arc_gained_method_convention;
1388 }
1389
1390 // Indexes into a %select clause in the diagnostic.
1391 enum FamilySelector {
1392 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1393 };
1394 FamilySelector familySelector = FamilySelector();
1395
1396 switch (family) {
1397 case OMF_None: llvm_unreachable("logic error, no method convention");
1398 case OMF_retain:
1399 case OMF_release:
1400 case OMF_autorelease:
1401 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001402 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001403 case OMF_retainCount:
1404 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001405 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001406 // Mismatches for these methods don't change ownership
1407 // conventions, so we don't care.
1408 return false;
1409
1410 case OMF_init: familySelector = F_init; break;
1411 case OMF_alloc: familySelector = F_alloc; break;
1412 case OMF_copy: familySelector = F_copy; break;
1413 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1414 case OMF_new: familySelector = F_new; break;
1415 }
1416
1417 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1418 ReasonSelector reasonSelector;
1419
1420 // The only reason these methods don't fall within their families is
1421 // due to unusual result types.
1422 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1423 reasonSelector = R_UnrelatedReturn;
1424 } else {
1425 reasonSelector = R_NonObjectReturn;
1426 }
1427
1428 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1429 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1430
1431 return true;
1432}
John McCall10302c02010-10-28 02:34:38 +00001433
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001434void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001435 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001436 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001437 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001438 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1439 return;
1440
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001441 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001442 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001443 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001444
Chris Lattner3aff9192009-04-11 19:58:42 +00001445 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001446 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1447 EF = MethodDecl->param_end();
1448 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001449 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001450 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001451 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001452
Fariborz Jahanian21121902011-08-08 18:03:17 +00001453 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001454 Diag(ImpMethodDecl->getLocation(),
1455 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001456 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001457 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001458}
1459
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001460void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1461 ObjCMethodDecl *Overridden,
1462 bool IsProtocolMethodDecl) {
1463
1464 CheckMethodOverrideReturn(*this, Method, Overridden,
1465 IsProtocolMethodDecl, true,
1466 true);
1467
1468 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001469 IF = Overridden->param_begin(), EM = Method->param_end(),
1470 EF = Overridden->param_end();
1471 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001472 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1473 IsProtocolMethodDecl, true, true);
1474 }
1475
1476 if (Method->isVariadic() != Overridden->isVariadic()) {
1477 Diag(Method->getLocation(),
1478 diag::warn_conflicting_overriding_variadic);
1479 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1480 }
1481}
1482
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001483/// WarnExactTypedMethods - This routine issues a warning if method
1484/// implementation declaration matches exactly that of its declaration.
1485void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1486 ObjCMethodDecl *MethodDecl,
1487 bool IsProtocolMethodDecl) {
1488 // don't issue warning when protocol method is optional because primary
1489 // class is not required to implement it and it is safe for protocol
1490 // to implement it.
1491 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1492 return;
1493 // don't issue warning when primary class's method is
1494 // depecated/unavailable.
1495 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1496 MethodDecl->hasAttr<DeprecatedAttr>())
1497 return;
1498
1499 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1500 IsProtocolMethodDecl, false, false);
1501 if (match)
1502 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001503 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1504 EF = MethodDecl->param_end();
1505 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001506 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1507 *IM, *IF,
1508 IsProtocolMethodDecl, false, false);
1509 if (!match)
1510 break;
1511 }
1512 if (match)
1513 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001514 if (match)
1515 match = !(MethodDecl->isClassMethod() &&
1516 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001517
1518 if (match) {
1519 Diag(ImpMethodDecl->getLocation(),
1520 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001521 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1522 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001523 }
1524}
1525
Mike Stump390b4cc2009-05-16 07:39:55 +00001526/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1527/// improve the efficiency of selector lookups and type checking by associating
1528/// with each protocol / interface / category the flattened instance tables. If
1529/// we used an immutable set to keep the table then it wouldn't add significant
1530/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001531
Steve Naroffefe7f362008-02-08 22:06:17 +00001532/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001533/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001534void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1535 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001536 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001537 const SelectorSet &InsMap,
1538 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001539 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001540 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1541 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1542 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001543 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1544
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001545 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001546 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001547 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001548 // check to see if class implements forwardInvocation method and objects
1549 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001550 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001551 // Under such conditions, which means that every method possible is
1552 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001553 // found" warnings.
1554 // FIXME: Use a general GetUnarySelector method for this.
1555 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1556 Selector fISelector = Context.Selectors.getSelector(1, &II);
1557 if (InsMap.count(fISelector))
1558 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1559 // need be implemented in the implementation.
1560 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1561 }
Mike Stump1eb44332009-09-09 15:08:12 +00001562
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001563 // If a method lookup fails locally we still need to look and see if
1564 // the method was implemented by a base class or an inherited
1565 // protocol. This lookup is slow, but occurs rarely in correct code
1566 // and otherwise would terminate in a warning.
1567
Chris Lattner4d391482007-12-12 07:09:47 +00001568 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001569 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001570 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001571 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001572 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001573 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001574 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001575 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001576 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001577 // If a method is not implemented in the category implementation but
1578 // has been declared in its primary class, superclass,
1579 // or in one of their protocols, no need to issue the warning.
1580 // This is because method will be implemented in the primary class
1581 // or one of its super class implementation.
1582
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001583 // Ugly, but necessary. Method declared in protcol might have
1584 // have been synthesized due to a property declared in the class which
1585 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001586 if (ObjCMethodDecl *MethodInClass =
1587 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001588 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001589 if (C || MethodInClass->isSynthesized())
1590 continue;
1591 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1592 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1593 != DiagnosticsEngine::Ignored) {
1594 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001595 Diag(method->getLocation(), diag::note_method_declared_at)
1596 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001597 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1598 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001599 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001600 }
1601 }
Chris Lattner4d391482007-12-12 07:09:47 +00001602 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001603 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001604 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001605 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001606 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001607 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1608 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001609 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001610 // See above comment for instance method lookups.
1611 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001612 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001613 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001614 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001615 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1616 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001617 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001618 Diag(method->getLocation(), diag::note_method_declared_at)
1619 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001620 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1621 PDecl->getDeclName();
1622 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001623 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001624 }
Chris Lattner780f3292008-07-21 21:32:27 +00001625 // Check on this protocols's referenced protocols, recursively.
1626 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1627 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001628 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001629}
1630
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001631/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001632/// or protocol against those declared in their implementations.
1633///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001634void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1635 const SelectorSet &ClsMap,
1636 SelectorSet &InsMapSeen,
1637 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001638 ObjCImplDecl* IMPDecl,
1639 ObjCContainerDecl* CDecl,
1640 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001641 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001642 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001643 // Check and see if instance methods in class interface have been
1644 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001645 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1646 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001647 if (InsMapSeen.count((*I)->getSelector()))
1648 continue;
1649 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001650 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001651 !InsMap.count((*I)->getSelector())) {
1652 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001653 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1654 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001655 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001656 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001657 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001658 IMPDecl->getInstanceMethod((*I)->getSelector());
1659 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1660 "Expected to find the method through lookup as well");
1661 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001662 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001663 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001664 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001665 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1666 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001667 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001668 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001669 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001670 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001671 }
1672 }
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001674 // Check and see if class methods in class interface have been
1675 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001676 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001677 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001678 if (ClsMapSeen.count((*I)->getSelector()))
1679 continue;
1680 ClsMapSeen.insert((*I)->getSelector());
1681 if (!ClsMap.count((*I)->getSelector())) {
1682 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001683 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1684 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001685 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001686 ObjCMethodDecl *ImpMethodDecl =
1687 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001688 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1689 "Expected to find the method through lookup as well");
1690 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001691 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001692 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1693 isa<ObjCProtocolDecl>(CDecl));
1694 else
1695 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001696 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001697 }
1698 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001699
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001700 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001701 // Also methods in class extensions need be looked at next.
1702 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1703 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1704 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1705 IMPDecl,
1706 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001707 IncompleteImpl, false,
1708 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001709
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001710 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001711 for (ObjCInterfaceDecl::all_protocol_iterator
1712 PI = I->all_referenced_protocol_begin(),
1713 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001714 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1715 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001716 (*PI), IncompleteImpl, false,
1717 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001718
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001719 // FIXME. For now, we are not checking for extact match of methods
1720 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001721 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001722 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001723 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001724 I->getSuperClass(), IncompleteImpl, false);
1725 }
1726}
1727
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001728/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1729/// category matches with those implemented in its primary class and
1730/// warns each time an exact match is found.
1731void Sema::CheckCategoryVsClassMethodMatches(
1732 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001733 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001734
1735 for (ObjCImplementationDecl::instmeth_iterator
1736 I = CatIMPDecl->instmeth_begin(),
1737 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1738 InsMap.insert((*I)->getSelector());
1739
1740 for (ObjCImplementationDecl::classmeth_iterator
1741 I = CatIMPDecl->classmeth_begin(),
1742 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1743 ClsMap.insert((*I)->getSelector());
1744 if (InsMap.empty() && ClsMap.empty())
1745 return;
1746
1747 // Get category's primary class.
1748 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1749 if (!CatDecl)
1750 return;
1751 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1752 if (!IDecl)
1753 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001754 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001755 bool IncompleteImpl = false;
1756 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1757 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001758 IncompleteImpl, false,
1759 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001760}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001761
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001762void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001763 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001764 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001765 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001766 // Check and see if instance methods in class interface have been
1767 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001768 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001769 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001770 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001771
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001772 // Check and see if properties declared in the interface have either 1)
1773 // an implementation or 2) there is a @synthesize/@dynamic implementation
1774 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001775 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001776 if (!(LangOpts.ObjCDefaultSynthProperties &&
1777 LangOpts.ObjCRuntime.isNonFragile()) ||
1778 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001779 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001780
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001781 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001782 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001783 I = IMPDecl->classmeth_begin(),
1784 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001785 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001786
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001787 // Check for type conflict of methods declared in a class/protocol and
1788 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001789 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001790 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1791 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001792 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001793
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001794 // check all methods implemented in category against those declared
1795 // in its primary class.
1796 if (ObjCCategoryImplDecl *CatDecl =
1797 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1798 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001799
Chris Lattner4d391482007-12-12 07:09:47 +00001800 // Check the protocol list for unimplemented methods in the @implementation
1801 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001802 // Check and see if class methods in class interface have been
1803 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Chris Lattnercddc8882009-03-01 00:56:52 +00001805 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001806 for (ObjCInterfaceDecl::all_protocol_iterator
1807 PI = I->all_referenced_protocol_begin(),
1808 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001809 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001810 InsMap, ClsMap, I);
1811 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001812 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1813 Categories; Categories = Categories->getNextClassExtension())
1814 ImplMethodsVsClassMethods(S, IMPDecl,
1815 const_cast<ObjCCategoryDecl*>(Categories),
1816 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001817 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001818 // For extended class, unimplemented methods in its protocols will
1819 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001820 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001821 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1822 E = C->protocol_end(); PI != E; ++PI)
1823 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001824 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001825 // Report unimplemented properties in the category as well.
1826 // When reporting on missing setter/getters, do not report when
1827 // setter/getter is implemented in category's primary class
1828 // implementation.
1829 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1830 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1831 for (ObjCImplementationDecl::instmeth_iterator
1832 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1833 InsMap.insert((*I)->getSelector());
1834 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001835 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001836 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001837 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001838 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001839}
1840
Mike Stump1eb44332009-09-09 15:08:12 +00001841/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001842Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001843Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001844 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001845 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001846 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001847 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001848 for (unsigned i = 0; i != NumElts; ++i) {
1849 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001850 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001851 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001852 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001853 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001854 // Maybe we will complain about the shadowed template parameter.
1855 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1856 // Just pretend that we didn't see the previous declaration.
1857 PrevDecl = 0;
1858 }
1859
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001860 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001861 // GCC apparently allows the following idiom:
1862 //
1863 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1864 // @class XCElementToggler;
1865 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001866 // Here we have chosen to ignore the forward class declaration
1867 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001868 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001869 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001870 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001871 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001872 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001873 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001874 // to the underlying class. Just ignore the forward class with a warning
1875 // as this will force the intended behavior which is to lookup the typedef
1876 // name.
1877 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1878 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1879 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1880 continue;
1881 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001882 }
Chris Lattner4d391482007-12-12 07:09:47 +00001883 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001884
1885 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001886 ObjCInterfaceDecl *PrevIDecl
1887 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001888 ObjCInterfaceDecl *IDecl
1889 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001890 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001891 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001892
Douglas Gregor7723fec2011-12-15 20:29:51 +00001893 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001894 CheckObjCDeclScope(IDecl);
1895 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001896 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001897
1898 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001899}
1900
John McCall0f4c4c42011-06-16 01:15:19 +00001901static bool tryMatchRecordTypes(ASTContext &Context,
1902 Sema::MethodMatchStrategy strategy,
1903 const Type *left, const Type *right);
1904
John McCallf85e1932011-06-15 23:02:42 +00001905static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1906 QualType leftQT, QualType rightQT) {
1907 const Type *left =
1908 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1909 const Type *right =
1910 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1911
1912 if (left == right) return true;
1913
1914 // If we're doing a strict match, the types have to match exactly.
1915 if (strategy == Sema::MMS_strict) return false;
1916
1917 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1918
1919 // Otherwise, use this absurdly complicated algorithm to try to
1920 // validate the basic, low-level compatibility of the two types.
1921
1922 // As a minimum, require the sizes and alignments to match.
1923 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1924 return false;
1925
1926 // Consider all the kinds of non-dependent canonical types:
1927 // - functions and arrays aren't possible as return and parameter types
1928
1929 // - vector types of equal size can be arbitrarily mixed
1930 if (isa<VectorType>(left)) return isa<VectorType>(right);
1931 if (isa<VectorType>(right)) return false;
1932
1933 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001934 // - structs, unions, and Objective-C objects must match more-or-less
1935 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001936 // - everything else should be a scalar
1937 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001938 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001939
John McCall1d9b3b22011-09-09 05:25:32 +00001940 // Make scalars agree in kind, except count bools as chars, and group
1941 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001942 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1943 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1944 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1945 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001946 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1947 leftSK = Type::STK_ObjCObjectPointer;
1948 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1949 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001950
1951 // Note that data member pointers and function member pointers don't
1952 // intermix because of the size differences.
1953
1954 return (leftSK == rightSK);
1955}
Chris Lattner4d391482007-12-12 07:09:47 +00001956
John McCall0f4c4c42011-06-16 01:15:19 +00001957static bool tryMatchRecordTypes(ASTContext &Context,
1958 Sema::MethodMatchStrategy strategy,
1959 const Type *lt, const Type *rt) {
1960 assert(lt && rt && lt != rt);
1961
1962 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1963 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1964 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1965
1966 // Require union-hood to match.
1967 if (left->isUnion() != right->isUnion()) return false;
1968
1969 // Require an exact match if either is non-POD.
1970 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1971 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1972 return false;
1973
1974 // Require size and alignment to match.
1975 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1976
1977 // Require fields to match.
1978 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1979 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1980 for (; li != le && ri != re; ++li, ++ri) {
1981 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1982 return false;
1983 }
1984 return (li == le && ri == re);
1985}
1986
Chris Lattner4d391482007-12-12 07:09:47 +00001987/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1988/// returns true, or false, accordingly.
1989/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001990bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1991 const ObjCMethodDecl *right,
1992 MethodMatchStrategy strategy) {
1993 if (!matchTypes(Context, strategy,
1994 left->getResultType(), right->getResultType()))
1995 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001996
David Blaikie4e4d0842012-03-11 07:00:24 +00001997 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001998 (left->hasAttr<NSReturnsRetainedAttr>()
1999 != right->hasAttr<NSReturnsRetainedAttr>() ||
2000 left->hasAttr<NSConsumesSelfAttr>()
2001 != right->hasAttr<NSConsumesSelfAttr>()))
2002 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002003
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002004 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002005 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2006 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002007
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002008 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002009 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002010 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002011
2012 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2013 return false;
2014
David Blaikie4e4d0842012-03-11 07:00:24 +00002015 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002016 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2017 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002018 }
2019 return true;
2020}
2021
Douglas Gregorff310c72012-05-01 23:37:00 +00002022void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002023 // If the list is empty, make it a singleton list.
2024 if (List->Method == 0) {
2025 List->Method = Method;
2026 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002027 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002028 }
2029
2030 // We've seen a method with this name, see if we have already seen this type
2031 // signature.
2032 ObjCMethodList *Previous = List;
2033 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002034 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002035 continue;
2036
2037 ObjCMethodDecl *PrevObjCMethod = List->Method;
2038
2039 // Propagate the 'defined' bit.
2040 if (Method->isDefined())
2041 PrevObjCMethod->setDefined(true);
2042
2043 // If a method is deprecated, push it in the global pool.
2044 // This is used for better diagnostics.
2045 if (Method->isDeprecated()) {
2046 if (!PrevObjCMethod->isDeprecated())
2047 List->Method = Method;
2048 }
2049 // If new method is unavailable, push it into global pool
2050 // unless previous one is deprecated.
2051 if (Method->isUnavailable()) {
2052 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2053 List->Method = Method;
2054 }
2055
Douglas Gregorff310c72012-05-01 23:37:00 +00002056 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002057 }
2058
2059 // We have a new signature for an existing method - add it.
2060 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002061 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002062 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2063}
2064
Sebastian Redldb9d2142010-08-02 23:18:59 +00002065/// \brief Read the contents of the method pool for a given selector from
2066/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002067void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002068 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002069 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002070}
2071
Douglas Gregorff310c72012-05-01 23:37:00 +00002072void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002073 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002074 // Ignore methods of invalid containers.
2075 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002076 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002077
Douglas Gregor0d266d62012-01-25 00:59:09 +00002078 if (ExternalSource)
2079 ReadMethodPool(Method->getSelector());
2080
Sebastian Redldb9d2142010-08-02 23:18:59 +00002081 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002082 if (Pos == MethodPool.end())
2083 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2084 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002085
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002086 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002087
Sebastian Redldb9d2142010-08-02 23:18:59 +00002088 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002089 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002090}
2091
John McCallf85e1932011-06-15 23:02:42 +00002092/// Determines if this is an "acceptable" loose mismatch in the global
2093/// method pool. This exists mostly as a hack to get around certain
2094/// global mismatches which we can't afford to make warnings / errors.
2095/// Really, what we want is a way to take a method out of the global
2096/// method pool.
2097static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2098 ObjCMethodDecl *other) {
2099 if (!chosen->isInstanceMethod())
2100 return false;
2101
2102 Selector sel = chosen->getSelector();
2103 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2104 return false;
2105
2106 // Don't complain about mismatches for -length if the method we
2107 // chose has an integral result type.
2108 return (chosen->getResultType()->isIntegerType());
2109}
2110
Sebastian Redldb9d2142010-08-02 23:18:59 +00002111ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002112 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002113 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002114 if (ExternalSource)
2115 ReadMethodPool(Sel);
2116
Sebastian Redldb9d2142010-08-02 23:18:59 +00002117 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002118 if (Pos == MethodPool.end())
2119 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002120
Sebastian Redldb9d2142010-08-02 23:18:59 +00002121 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002122
Sebastian Redldb9d2142010-08-02 23:18:59 +00002123 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002124 bool issueDiagnostic = false, issueError = false;
2125
2126 // We support a warning which complains about *any* difference in
2127 // method signature.
2128 bool strictSelectorMatch =
2129 (receiverIdOrClass && warn &&
2130 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2131 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002132 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002133 if (strictSelectorMatch)
2134 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002135 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2136 MMS_strict)) {
2137 issueDiagnostic = true;
2138 break;
2139 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002140 }
2141
John McCallf85e1932011-06-15 23:02:42 +00002142 // If we didn't see any strict differences, we won't see any loose
2143 // differences. In ARC, however, we also need to check for loose
2144 // mismatches, because most of them are errors.
2145 if (!strictSelectorMatch ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002146 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002147 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002148 // This checks if the methods differ in type mismatch.
2149 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2150 MMS_loose) &&
2151 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2152 issueDiagnostic = true;
David Blaikie4e4d0842012-03-11 07:00:24 +00002153 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002154 issueError = true;
2155 break;
2156 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002157 }
2158
John McCallf85e1932011-06-15 23:02:42 +00002159 if (issueDiagnostic) {
2160 if (issueError)
2161 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2162 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002163 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2164 else
2165 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002166
2167 Diag(MethList.Method->getLocStart(),
2168 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002169 << MethList.Method->getSourceRange();
2170 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2171 Diag(Next->Method->getLocStart(), diag::note_also_found)
2172 << Next->Method->getSourceRange();
2173 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002174 }
2175 return MethList.Method;
2176}
2177
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002178ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002179 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2180 if (Pos == MethodPool.end())
2181 return 0;
2182
2183 GlobalMethods &Methods = Pos->second;
2184
2185 if (Methods.first.Method && Methods.first.Method->isDefined())
2186 return Methods.first.Method;
2187 if (Methods.second.Method && Methods.second.Method->isDefined())
2188 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002189 return 0;
2190}
2191
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002192/// DiagnoseDuplicateIvars -
2193/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002194/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002195/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002196/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002197void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2198 ObjCInterfaceDecl *SID) {
2199 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2200 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002201 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002202 if (Ivar->isInvalidDecl())
2203 continue;
2204 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2205 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2206 if (prevIvar) {
2207 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2208 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2209 Ivar->setInvalidDecl();
2210 }
2211 }
2212 }
2213}
2214
Erik Verbruggend64251f2011-12-06 09:25:23 +00002215Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2216 switch (CurContext->getDeclKind()) {
2217 case Decl::ObjCInterface:
2218 return Sema::OCK_Interface;
2219 case Decl::ObjCProtocol:
2220 return Sema::OCK_Protocol;
2221 case Decl::ObjCCategory:
2222 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2223 return Sema::OCK_ClassExtension;
2224 else
2225 return Sema::OCK_Category;
2226 case Decl::ObjCImplementation:
2227 return Sema::OCK_Implementation;
2228 case Decl::ObjCCategoryImpl:
2229 return Sema::OCK_CategoryImplementation;
2230
2231 default:
2232 return Sema::OCK_None;
2233 }
2234}
2235
Steve Naroffa56f6162007-12-18 01:30:32 +00002236// Note: For class/category implemenations, allMethods/allProperties is
2237// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002238Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2239 Decl **allMethods, unsigned allNum,
2240 Decl **allProperties, unsigned pNum,
2241 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002242
Erik Verbruggend64251f2011-12-06 09:25:23 +00002243 if (getObjCContainerKind() == Sema::OCK_None)
2244 return 0;
2245
2246 assert(AtEnd.isValid() && "Invalid location for '@end'");
2247
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002248 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2249 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002250
Mike Stump1eb44332009-09-09 15:08:12 +00002251 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002252 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2253 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002254 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002255
Steve Naroff0701bbb2009-01-08 17:28:14 +00002256 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2257 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2258 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2259
Chris Lattner4d391482007-12-12 07:09:47 +00002260 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002261 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002262 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002263
2264 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002265 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002266 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002267 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002268 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002269 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002270 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002271 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002272 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002273 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002274 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002275 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002276 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002277 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002278 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002279 if (!Context.getSourceManager().isInSystemHeader(
2280 Method->getLocation()))
2281 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2282 << Method->getDeclName();
2283 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2284 }
Chris Lattner4d391482007-12-12 07:09:47 +00002285 InsMap[Method->getSelector()] = Method;
2286 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002287 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002288 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002289 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002290 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002291 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002292 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002293 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002294 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002295 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002296 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002297 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002298 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002299 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002300 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002301 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002302 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002303 if (!Context.getSourceManager().isInSystemHeader(
2304 Method->getLocation()))
2305 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2306 << Method->getDeclName();
2307 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2308 }
Chris Lattner4d391482007-12-12 07:09:47 +00002309 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002310 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002311 }
2312 }
2313 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002314 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002315 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002316 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002317 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002318 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002319 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002320 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002321 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002322 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002323
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002324 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002325 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002326 if (C->IsClassExtension()) {
2327 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2328 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002329 }
Chris Lattner4d391482007-12-12 07:09:47 +00002330 }
Steve Naroff09c47192009-01-09 15:36:25 +00002331 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002332 if (CDecl->getIdentifier())
2333 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2334 // user-defined setter/getter. It also synthesizes setter/getter methods
2335 // and adds them to the DeclContext and global method pools.
2336 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2337 E = CDecl->prop_end();
2338 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002339 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002340 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002341 }
2342 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002343 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002344 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002345 // Any property declared in a class extension might have user
2346 // declared setter or getter in current class extension or one
2347 // of the other class extensions. Mark them as synthesized as
2348 // property will be synthesized when property with same name is
2349 // seen in the @implementation.
2350 for (const ObjCCategoryDecl *ClsExtDecl =
2351 IDecl->getFirstClassExtension();
2352 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2353 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2354 E = ClsExtDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002355 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002356 // Skip over properties declared @dynamic
2357 if (const ObjCPropertyImplDecl *PIDecl
2358 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2359 if (PIDecl->getPropertyImplementation()
2360 == ObjCPropertyImplDecl::Dynamic)
2361 continue;
2362
2363 for (const ObjCCategoryDecl *CExtDecl =
2364 IDecl->getFirstClassExtension();
2365 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2366 if (ObjCMethodDecl *GetterMethod =
2367 CExtDecl->getInstanceMethod(Property->getGetterName()))
2368 GetterMethod->setSynthesized(true);
2369 if (!Property->isReadOnly())
2370 if (ObjCMethodDecl *SetterMethod =
2371 CExtDecl->getInstanceMethod(Property->getSetterName()))
2372 SetterMethod->setSynthesized(true);
2373 }
2374 }
2375 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002376 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002377 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002378 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002379
Patrick Beardb2f68202012-04-06 18:12:22 +00002380 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2381 if (IDecl->getSuperClass() == NULL) {
2382 // This class has no superclass, so check that it has been marked with
2383 // __attribute((objc_root_class)).
2384 if (!HasRootClassAttr) {
2385 SourceLocation DeclLoc(IDecl->getLocation());
2386 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2387 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2388 << IDecl->getIdentifier();
2389 // See if NSObject is in the current scope, and if it is, suggest
2390 // adding " : NSObject " to the class declaration.
2391 NamedDecl *IF = LookupSingleName(TUScope,
2392 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2393 DeclLoc, LookupOrdinaryName);
2394 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2395 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2396 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2397 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2398 } else {
2399 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2400 }
2401 }
2402 } else if (HasRootClassAttr) {
2403 // Complain that only root classes may have this attribute.
2404 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2405 }
2406
John McCall260611a2012-06-20 06:18:46 +00002407 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002408 while (IDecl->getSuperClass()) {
2409 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2410 IDecl = IDecl->getSuperClass();
2411 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002412 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002413 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002414 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002415 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002416 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002417 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002418
Chris Lattner4d391482007-12-12 07:09:47 +00002419 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002420 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002421 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002422 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002423 Categories; Categories = Categories->getNextClassCategory()) {
2424 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002425 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002426 break;
2427 }
2428 }
2429 }
2430 }
Chris Lattner682bf922009-03-29 16:50:03 +00002431 if (isInterfaceDeclKind) {
2432 // Reject invalid vardecls.
2433 for (unsigned i = 0; i != tuvNum; i++) {
2434 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2435 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2436 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002437 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002438 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002439 }
Chris Lattner682bf922009-03-29 16:50:03 +00002440 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002441 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002442 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002443
2444 for (unsigned i = 0; i != tuvNum; i++) {
2445 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002446 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2447 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002448 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2449 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002450
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002451 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002452 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002453}
2454
2455
2456/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2457/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002458static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002459CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002460 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002461}
2462
Ted Kremenek422bae72010-04-18 04:59:38 +00002463static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002464unsigned countAlignAttr(const AttrVec &A) {
2465 unsigned count=0;
2466 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2467 if ((*i)->getKind() == attr::Aligned)
2468 ++count;
2469 return count;
2470}
2471
2472static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002473bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2474 const AttrVec &A) {
2475 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002476 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002477 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002478 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002479
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002480 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002481 // But implementation has attributes. This is invalid.
2482 // Except when implementation has 'Align' attribute which is
2483 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002484 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002485 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002486
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002487 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002488
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002489 unsigned countAlignOnImpl = countAlignAttr(A);
2490 if (!countAlignOnImpl && (A.size() != D.size()))
2491 return true;
2492 else if (countAlignOnImpl) {
2493 unsigned countAlignOnDecl = countAlignAttr(D);
2494 if (countAlignOnDecl && (A.size() != D.size()))
2495 return true;
2496 else if (!countAlignOnDecl &&
2497 ((A.size()-countAlignOnImpl) != D.size()))
2498 return true;
2499 }
2500
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002501 // attributes on method declaration and definition must match exactly.
2502 // Note that we have at most a couple of attributes on methods, so this
2503 // n*n search is good enough.
2504 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002505 if ((*i)->getKind() == attr::Aligned)
2506 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002507 bool match = false;
2508 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2509 if ((*i)->getKind() == (*i1)->getKind()) {
2510 match = true;
2511 break;
2512 }
2513 }
2514 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002515 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002516 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002517
Sean Huntcf807c42010-08-18 23:23:40 +00002518 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002519}
2520
Douglas Gregor926df6c2011-06-11 01:09:30 +00002521/// \brief Check whether the declared result type of the given Objective-C
2522/// method declaration is compatible with the method's class.
2523///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002524static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002525CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2526 ObjCInterfaceDecl *CurrentClass) {
2527 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002528
2529 // If an Objective-C method inherits its related result type, then its
2530 // declared result type must be compatible with its own class type. The
2531 // declared result type is compatible if:
2532 if (const ObjCObjectPointerType *ResultObjectType
2533 = ResultType->getAs<ObjCObjectPointerType>()) {
2534 // - it is id or qualified id, or
2535 if (ResultObjectType->isObjCIdType() ||
2536 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002537 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002538
2539 if (CurrentClass) {
2540 if (ObjCInterfaceDecl *ResultClass
2541 = ResultObjectType->getInterfaceDecl()) {
2542 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002543 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002544 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002545
2546 // - it is a superclass of the method's class type
2547 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002548 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002549 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002550 } else {
2551 // Any Objective-C pointer type might be acceptable for a protocol
2552 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002553 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002554 }
2555 }
2556
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002557 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002558}
2559
John McCall6c2c2502011-07-22 02:45:48 +00002560namespace {
2561/// A helper class for searching for methods which a particular method
2562/// overrides.
2563class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002564public:
John McCall6c2c2502011-07-22 02:45:48 +00002565 Sema &S;
2566 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002567 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002568 bool Recursive;
2569
2570public:
2571 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2572 Selector selector = method->getSelector();
2573
2574 // Bypass this search if we've never seen an instance/class method
2575 // with this selector before.
2576 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2577 if (it == S.MethodPool.end()) {
2578 if (!S.ExternalSource) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002579 S.ReadMethodPool(selector);
2580
2581 it = S.MethodPool.find(selector);
2582 if (it == S.MethodPool.end())
2583 return;
John McCall6c2c2502011-07-22 02:45:48 +00002584 }
2585 ObjCMethodList &list =
2586 method->isInstanceMethod() ? it->second.first : it->second.second;
2587 if (!list.Method) return;
2588
2589 ObjCContainerDecl *container
2590 = cast<ObjCContainerDecl>(method->getDeclContext());
2591
2592 // Prevent the search from reaching this container again. This is
2593 // important with categories, which override methods from the
2594 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002595 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2596 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002597 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2598 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002599 } else {
2600 searchFromContainer(container);
2601 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002602 }
John McCall6c2c2502011-07-22 02:45:48 +00002603
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002604 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002605 iterator begin() const { return Overridden.begin(); }
2606 iterator end() const { return Overridden.end(); }
2607
2608private:
2609 void searchFromContainer(ObjCContainerDecl *container) {
2610 if (container->isInvalidDecl()) return;
2611
2612 switch (container->getDeclKind()) {
2613#define OBJCCONTAINER(type, base) \
2614 case Decl::type: \
2615 searchFrom(cast<type##Decl>(container)); \
2616 break;
2617#define ABSTRACT_DECL(expansion)
2618#define DECL(type, base) \
2619 case Decl::type:
2620#include "clang/AST/DeclNodes.inc"
2621 llvm_unreachable("not an ObjC container!");
2622 }
2623 }
2624
2625 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002626 if (!protocol->hasDefinition())
2627 return;
2628
John McCall6c2c2502011-07-22 02:45:48 +00002629 // A method in a protocol declaration overrides declarations from
2630 // referenced ("parent") protocols.
2631 search(protocol->getReferencedProtocols());
2632 }
2633
2634 void searchFrom(ObjCCategoryDecl *category) {
2635 // A method in a category declaration overrides declarations from
2636 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002637 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002638 search(category->getReferencedProtocols());
2639 }
2640
2641 void searchFrom(ObjCCategoryImplDecl *impl) {
2642 // A method in a category definition that has a category
2643 // declaration overrides declarations from the category
2644 // declaration.
2645 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2646 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002647 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2648 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002649
2650 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002651 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2652 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002653 }
2654 }
2655
2656 void searchFrom(ObjCInterfaceDecl *iface) {
2657 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002658 if (!iface->hasDefinition())
2659 return;
2660
John McCall6c2c2502011-07-22 02:45:48 +00002661 // - categories,
2662 for (ObjCCategoryDecl *category = iface->getCategoryList();
2663 category; category = category->getNextClassCategory())
2664 search(category);
2665
2666 // - the super class, and
2667 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2668 search(super);
2669
2670 // - any referenced protocols.
2671 search(iface->getReferencedProtocols());
2672 }
2673
2674 void searchFrom(ObjCImplementationDecl *impl) {
2675 // A method in a class implementation overrides declarations from
2676 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002677 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2678 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002679 }
2680
2681
2682 void search(const ObjCProtocolList &protocols) {
2683 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2684 i != e; ++i)
2685 search(*i);
2686 }
2687
2688 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002689 // Check for a method in this container which matches this selector.
2690 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2691 Method->isInstanceMethod());
2692
2693 // If we find one, record it and bail out.
2694 if (meth) {
2695 Overridden.insert(meth);
2696 return;
2697 }
2698
2699 // Otherwise, search for methods that a hypothetical method here
2700 // would have overridden.
2701
2702 // Note that we're now in a recursive case.
2703 Recursive = true;
2704
2705 searchFromContainer(container);
2706 }
2707};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002708}
2709
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002710void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2711 ObjCInterfaceDecl *CurrentClass,
2712 ResultTypeCompatibilityKind RTC) {
2713 // Search for overridden methods and merge information down from them.
2714 OverrideSearch overrides(*this, ObjCMethod);
2715 // Keep track if the method overrides any method in the class's base classes,
2716 // its protocols, or its categories' protocols; we will keep that info
2717 // in the ObjCMethodDecl.
2718 // For this info, a method in an implementation is not considered as
2719 // overriding the same method in the interface or its categories.
2720 bool hasOverriddenMethodsInBaseOrProtocol = false;
2721 for (OverrideSearch::iterator
2722 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2723 ObjCMethodDecl *overridden = *i;
2724
2725 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2726 CurrentClass != overridden->getClassInterface() ||
2727 overridden->isOverriding())
2728 hasOverriddenMethodsInBaseOrProtocol = true;
2729
2730 // Propagate down the 'related result type' bit from overridden methods.
2731 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2732 ObjCMethod->SetRelatedResultType();
2733
2734 // Then merge the declarations.
2735 mergeObjCMethodDecls(ObjCMethod, overridden);
2736
2737 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2738 continue; // Conflicting properties are detected elsewhere.
2739
2740 // Check for overriding methods
2741 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2742 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2743 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2744 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2745
2746 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00002747 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
2748 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002749 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2750 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002751 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2752 PrevE = overridden->param_end();
2753 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002754 assert(PrevI != overridden->param_end() && "Param mismatch");
2755 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2756 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2757 // If type of argument of method in this class does not match its
2758 // respective argument type in the super class method, issue warning;
2759 if (!Context.typesAreCompatible(T1, T2)) {
2760 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2761 << T1 << T2;
2762 Diag(overridden->getLocation(), diag::note_previous_declaration);
2763 break;
2764 }
2765 }
2766 }
2767 }
2768
2769 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2770}
2771
John McCalld226f652010-08-21 09:40:31 +00002772Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002773 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002774 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002775 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002776 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002777 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002778 Selector Sel,
2779 // optional arguments. The number of types/arguments is obtained
2780 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002781 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002782 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002783 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002784 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002785 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002786 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002787 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002788 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002789 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002790 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2791 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002792 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002793
Douglas Gregore97179c2011-09-08 01:46:34 +00002794 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002795 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002796 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002797 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002798
Steve Naroffccef3712009-02-20 22:59:16 +00002799 // Methods cannot return interface types. All ObjC objects are
2800 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002801 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002802 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2803 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002804 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002805 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002806
2807 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002808 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002809 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002810 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002811 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002812 }
Mike Stump1eb44332009-09-09 15:08:12 +00002813
2814 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002815 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002816 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002817 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002818 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002819 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002820 /*isSynthesized=*/false,
2821 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002822 MethodDeclKind == tok::objc_optional
2823 ? ObjCMethodDecl::Optional
2824 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002825 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002826
Chris Lattner5f9e2722011-07-23 10:55:15 +00002827 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002828
Chris Lattner7db638d2009-04-11 19:42:43 +00002829 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002830 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002831 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002832
Chris Lattnere294d3f2009-04-11 18:57:04 +00002833 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002834 ArgType = Context.getObjCIdType();
2835 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002836 } else {
John McCall58e46772009-10-23 21:48:59 +00002837 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002838 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002839 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002840 }
Mike Stump1eb44332009-09-09 15:08:12 +00002841
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002842 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2843 LookupOrdinaryName, ForRedeclaration);
2844 LookupName(R, S);
2845 if (R.isSingleResult()) {
2846 NamedDecl *PrevDecl = R.getFoundDecl();
2847 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002848 Diag(ArgInfo[i].NameLoc,
2849 (MethodDefinition ? diag::warn_method_param_redefinition
2850 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002851 << ArgInfo[i].Name;
2852 Diag(PrevDecl->getLocation(),
2853 diag::note_previous_declaration);
2854 }
2855 }
2856
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002857 SourceLocation StartLoc = DI
2858 ? DI->getTypeLoc().getBeginLoc()
2859 : ArgInfo[i].NameLoc;
2860
John McCall81ef3e62011-04-23 02:46:06 +00002861 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2862 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2863 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002864
John McCall70798862011-05-02 00:30:12 +00002865 Param->setObjCMethodScopeInfo(i);
2866
Chris Lattner0ed844b2008-04-04 06:12:32 +00002867 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002868 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002869
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002870 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002871 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002872
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002873 if (Param->hasAttr<BlocksAttr>()) {
2874 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2875 Param->setInvalidDecl();
2876 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002877 S->AddDecl(Param);
2878 IdResolver.AddDecl(Param);
2879
Chris Lattner0ed844b2008-04-04 06:12:32 +00002880 Params.push_back(Param);
2881 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002882
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002883 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002884 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002885 QualType ArgType = Param->getType();
2886 if (ArgType.isNull())
2887 ArgType = Context.getObjCIdType();
2888 else
2889 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002890 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002891 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002892 Diag(Param->getLocation(),
2893 diag::err_object_cannot_be_passed_returned_by_value)
2894 << 1 << ArgType;
2895 Param->setInvalidDecl();
2896 }
2897 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002898
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002899 Params.push_back(Param);
2900 }
2901
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002902 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002903 ObjCMethod->setObjCDeclQualifier(
2904 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002905
2906 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002907 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002908
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002909 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002910 const ObjCMethodDecl *PrevMethod = 0;
2911 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002912 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002913 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2914 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002915 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002916 PrevMethod = ImpDecl->getClassMethod(Sel);
2917 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002918 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002919
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002920 ObjCMethodDecl *IMD = 0;
2921 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2922 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2923 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002924 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002925 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002926 SourceLocation MethodLoc = IMD->getLocation();
2927 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2928 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002929 Diag(MethodLoc, diag::note_method_declared_at)
2930 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002931 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002932 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002933 } else {
2934 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002935 }
John McCall6c2c2502011-07-22 02:45:48 +00002936
Chris Lattner4d391482007-12-12 07:09:47 +00002937 if (PrevMethod) {
2938 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002939 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002940 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002941 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002942 }
John McCall54abf7d2009-11-04 02:18:39 +00002943
Douglas Gregor926df6c2011-06-11 01:09:30 +00002944 // If this Objective-C method does not have a related result type, but we
2945 // are allowed to infer related result types, try to do so based on the
2946 // method family.
2947 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2948 if (!CurrentClass) {
2949 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2950 CurrentClass = Cat->getClassInterface();
2951 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2952 CurrentClass = Impl->getClassInterface();
2953 else if (ObjCCategoryImplDecl *CatImpl
2954 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2955 CurrentClass = CatImpl->getClassInterface();
2956 }
John McCall6c2c2502011-07-22 02:45:48 +00002957
Douglas Gregore97179c2011-09-08 01:46:34 +00002958 ResultTypeCompatibilityKind RTC
2959 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002960
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002961 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00002962
John McCallf85e1932011-06-15 23:02:42 +00002963 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002964 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002965 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2966
Douglas Gregore97179c2011-09-08 01:46:34 +00002967 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002968 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00002969 !ObjCMethod->hasRelatedResultType() &&
2970 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002971 bool InferRelatedResultType = false;
2972 switch (ObjCMethod->getMethodFamily()) {
2973 case OMF_None:
2974 case OMF_copy:
2975 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002976 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002977 case OMF_mutableCopy:
2978 case OMF_release:
2979 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002980 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002981 break;
2982
2983 case OMF_alloc:
2984 case OMF_new:
2985 InferRelatedResultType = ObjCMethod->isClassMethod();
2986 break;
2987
2988 case OMF_init:
2989 case OMF_autorelease:
2990 case OMF_retain:
2991 case OMF_self:
2992 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2993 break;
2994 }
2995
John McCall6c2c2502011-07-22 02:45:48 +00002996 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002997 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002998 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00002999
3000 ActOnDocumentableDecl(ObjCMethod);
3001
John McCalld226f652010-08-21 09:40:31 +00003002 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003003}
3004
Chris Lattnercc98eac2008-12-17 07:13:27 +00003005bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003006 // Following is also an error. But it is caused by a missing @end
3007 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003008 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003009 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003010
3011 // If we switched context to translation unit while we are still lexically in
3012 // an objc container, it means the parser missed emitting an error.
3013 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3014 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003015
Anders Carlsson15281452008-11-04 16:57:32 +00003016 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3017 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003018
Anders Carlsson15281452008-11-04 16:57:32 +00003019 return true;
3020}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003021
James Dennett1dfbd922012-06-14 21:40:34 +00003022/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003023/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003024void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003025 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003026 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003027 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003028 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003029 if (!Class) {
3030 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3031 return;
3032 }
John McCall260611a2012-06-20 06:18:46 +00003033 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003034 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3035 return;
3036 }
Mike Stump1eb44332009-09-09 15:08:12 +00003037
Chris Lattnercc98eac2008-12-17 07:13:27 +00003038 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003039 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003040 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003041 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003042 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003043 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003044 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003045 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3046 /*FIXME: StartL=*/ID->getLocation(),
3047 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003048 ID->getIdentifier(), ID->getType(),
3049 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003050 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003051 }
Mike Stump1eb44332009-09-09 15:08:12 +00003052
Chris Lattnercc98eac2008-12-17 07:13:27 +00003053 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003054 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003055 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003056 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003057 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003058 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003059 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003060 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003061 }
3062}
3063
Douglas Gregor160b5632010-04-26 17:32:49 +00003064/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003065VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3066 SourceLocation StartLoc,
3067 SourceLocation IdLoc,
3068 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003069 bool Invalid) {
3070 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3071 // duration shall not be qualified by an address-space qualifier."
3072 // Since all parameters have automatic store duration, they can not have
3073 // an address space.
3074 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003075 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003076 Invalid = true;
3077 }
3078
3079 // An @catch parameter must be an unqualified object pointer type;
3080 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3081 if (Invalid) {
3082 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003083 } else if (T->isDependentType()) {
3084 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003085 } else if (!T->isObjCObjectPointerType()) {
3086 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003087 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003088 } else if (T->isObjCQualifiedIdType()) {
3089 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003090 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003091 }
3092
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003093 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3094 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003095 New->setExceptionVariable(true);
3096
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003097 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003098 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003099 Invalid = true;
3100
Douglas Gregor160b5632010-04-26 17:32:49 +00003101 if (Invalid)
3102 New->setInvalidDecl();
3103 return New;
3104}
3105
John McCalld226f652010-08-21 09:40:31 +00003106Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003107 const DeclSpec &DS = D.getDeclSpec();
3108
3109 // We allow the "register" storage class on exception variables because
3110 // GCC did, but we drop it completely. Any other storage class is an error.
3111 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3112 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3113 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3114 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3115 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3116 << DS.getStorageClassSpec();
3117 }
3118 if (D.getDeclSpec().isThreadSpecified())
3119 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3120 D.getMutableDeclSpec().ClearStorageClassSpecs();
3121
3122 DiagnoseFunctionSpecifiers(D);
3123
3124 // Check that there are no default arguments inside the type of this
3125 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003126 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003127 CheckExtraCXXDefaultArguments(D);
3128
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003129 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003130 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003131
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003132 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3133 D.getSourceRange().getBegin(),
3134 D.getIdentifierLoc(),
3135 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003136 D.isInvalidType());
3137
3138 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3139 if (D.getCXXScopeSpec().isSet()) {
3140 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3141 << D.getCXXScopeSpec().getRange();
3142 New->setInvalidDecl();
3143 }
3144
3145 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003146 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003147 if (D.getIdentifier())
3148 IdResolver.AddDecl(New);
3149
3150 ProcessDeclAttributes(S, New, D);
3151
3152 if (New->hasAttr<BlocksAttr>())
3153 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003154 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003155}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003156
3157/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003158/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003159void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003160 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003161 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3162 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003163 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003164 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003165 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003166 }
3167}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003168
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003169void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003170 // Load referenced selectors from the external source.
3171 if (ExternalSource) {
3172 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3173 ExternalSource->ReadReferencedSelectors(Sels);
3174 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3175 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3176 }
3177
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003178 // Warning will be issued only when selector table is
3179 // generated (which means there is at lease one implementation
3180 // in the TU). This is to match gcc's behavior.
3181 if (ReferencedSelectors.empty() ||
3182 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003183 return;
3184 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3185 ReferencedSelectors.begin(),
3186 E = ReferencedSelectors.end(); S != E; ++S) {
3187 Selector Sel = (*S).first;
3188 if (!LookupImplementedMethodInGlobalPool(Sel))
3189 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3190 }
3191 return;
3192}