blob: a4fec7aebf651a0a3768c5b2f389d7e6234d09f3 [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 Jahaniana1a32f72012-09-13 18:53:14 +0000285/// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer
286/// has explicit ownership attribute; false otherwise.
287static bool
288HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) {
289 QualType T = Param->getType();
290
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000291 if (const PointerType *PT = T->getAs<PointerType>()) {
292 T = PT->getPointeeType();
293 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
294 T = RT->getPointeeType();
295 } else {
296 return true;
297 }
298
299 // If we have a lifetime qualifier, but it's local, we must have
300 // inferred it. So, it is implicit.
301 return !T.getLocalQualifiers().hasObjCLifetime();
302}
303
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +0000304/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
305/// and user declared, in the method definition's AST.
306void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
307 assert((getCurMethodDecl() == 0) && "Methodparsing confused");
John McCalld226f652010-08-21 09:40:31 +0000308 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000309
Steve Naroff394f3f42008-07-25 17:57:26 +0000310 // If we don't have a valid method decl, simply return.
311 if (!MDecl)
312 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000313
Chris Lattner4d391482007-12-12 07:09:47 +0000314 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000315 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000316 PushFunctionScope();
317
Chris Lattner4d391482007-12-12 07:09:47 +0000318 // Create Decl objects for each parameter, entrring them in the scope for
319 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000320
321 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000322 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Daniel Dunbar451318c2008-08-26 06:07:48 +0000324 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
325 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000326
Chris Lattner8123a952008-04-10 02:22:51 +0000327 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000328 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000329 E = MDecl->param_end(); PI != E; ++PI) {
330 ParmVarDecl *Param = (*PI);
331 if (!Param->isInvalidDecl() &&
332 RequireCompleteType(Param->getLocation(), Param->getType(),
333 diag::err_typecheck_decl_incomplete_type))
334 Param->setInvalidDecl();
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000335 if (!Param->isInvalidDecl() &&
336 getLangOpts().ObjCAutoRefCount &&
337 !HasExplicitOwnershipAttr(*this, Param))
338 Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
339 Param->getType();
Fariborz Jahanian918546c2012-08-30 23:56:02 +0000340
Chris Lattner89951a82009-02-20 18:43:26 +0000341 if ((*PI)->getIdentifier())
342 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000343 }
John McCallf85e1932011-06-15 23:02:42 +0000344
345 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000346 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000347 switch (MDecl->getMethodFamily()) {
348 case OMF_retain:
349 case OMF_retainCount:
350 case OMF_release:
351 case OMF_autorelease:
352 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
353 << MDecl->getSelector();
354 break;
355
356 case OMF_None:
357 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000358 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000359 case OMF_alloc:
360 case OMF_init:
361 case OMF_mutableCopy:
362 case OMF_copy:
363 case OMF_new:
364 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000365 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000366 break;
367 }
368 }
369
Nico Weber9a1ecf02011-08-22 17:25:57 +0000370 // Warn on deprecated methods under -Wdeprecated-implementations,
371 // and prepare for warning on missing super calls.
372 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian84101132012-09-07 23:46:23 +0000373 ObjCMethodDecl *IMD =
374 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
375
376 if (IMD)
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000377 DiagnoseObjCImplementedDeprecations(*this,
378 dyn_cast<NamedDecl>(IMD),
379 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000380
Nico Weber80cb6e62011-08-28 22:35:17 +0000381 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000382 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
383 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
384 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000385 if (IC->getSuperClass()) {
Jordan Rose535a5d02012-10-19 16:05:26 +0000386 ObjCMethodFamily Family = MDecl->getMethodFamily();
387 if (Family == OMF_dealloc) {
388 if (!(getLangOpts().ObjCAutoRefCount ||
389 getLangOpts().getGC() == LangOptions::GCOnly))
390 getCurFunction()->ObjCShouldCallSuper = true;
391
392 } else if (Family == OMF_finalize) {
393 if (Context.getLangOpts().getGC() != LangOptions::NonGC)
394 getCurFunction()->ObjCShouldCallSuper = true;
395
396 } else {
397 const ObjCMethodDecl *SuperMethod =
398 IC->getSuperClass()->lookupMethod(MDecl->getSelector(),
399 MDecl->isInstanceMethod());
400 getCurFunction()->ObjCShouldCallSuper =
401 (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000402 }
Nico Weber80cb6e62011-08-28 22:35:17 +0000403 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000404 }
Chris Lattner4d391482007-12-12 07:09:47 +0000405}
406
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000407namespace {
408
409// Callback to only accept typo corrections that are Objective-C classes.
410// If an ObjCInterfaceDecl* is given to the constructor, then the validation
411// function will reject corrections to that class.
412class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
413 public:
414 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
415 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
416 : CurrentIDecl(IDecl) {}
417
418 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
419 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
420 return ID && !declaresSameEntity(ID, CurrentIDecl);
421 }
422
423 private:
424 ObjCInterfaceDecl *CurrentIDecl;
425};
426
427}
428
John McCalld226f652010-08-21 09:40:31 +0000429Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000430ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
431 IdentifierInfo *ClassName, SourceLocation ClassLoc,
432 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000433 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000434 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000435 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000436 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattner4d391482007-12-12 07:09:47 +0000438 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000439 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000440 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000441
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000442 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000443 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000444 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000445 }
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Douglas Gregor7723fec2011-12-15 20:29:51 +0000447 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000448 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000449 ObjCInterfaceDecl *IDecl
450 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000451 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000452
Douglas Gregor7723fec2011-12-15 20:29:51 +0000453 if (PrevIDecl) {
454 // Class already seen. Was it a definition?
455 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
456 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
457 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000458 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000459 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000460 }
Chris Lattner4d391482007-12-12 07:09:47 +0000461 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000462
463 if (AttrList)
464 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
465 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Douglas Gregor7723fec2011-12-15 20:29:51 +0000467 // Start the definition of this class. If we're in a redefinition case, there
468 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000469 if (!IDecl->hasDefinition())
470 IDecl->startDefinition();
471
Chris Lattner4d391482007-12-12 07:09:47 +0000472 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000473 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000474 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
475 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000476
477 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000478 // Try to correct for a typo in the superclass name without correcting
479 // to the class we're defining.
480 ObjCInterfaceValidatorCCC Validator(IDecl);
481 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000482 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000483 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000484 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
485 Diag(SuperLoc, diag::err_undef_superclass_suggest)
486 << SuperName << ClassName << PrevDecl->getDeclName();
487 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
488 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000489 }
490 }
491
Douglas Gregor60ef3082011-12-15 00:29:59 +0000492 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000493 Diag(SuperLoc, diag::err_recursive_superclass)
494 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000495 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000496 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000497 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000498 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000499
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000500 // Diagnose classes that inherit from deprecated classes.
501 if (SuperClassDecl)
502 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000504 if (PrevDecl && SuperClassDecl == 0) {
505 // The previous declaration was not a class decl. Check if we have a
506 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000507 if (const TypedefNameDecl *TDecl =
508 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000509 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000510 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000511 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
512 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000513 }
514 }
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000516 // This handles the following case:
517 //
518 // typedef int SuperClass;
519 // @interface MyClass : SuperClass {} @end
520 //
521 if (!SuperClassDecl) {
522 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
523 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000524 }
525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Richard Smith162e1c12011-04-15 14:24:37 +0000527 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000528 if (!SuperClassDecl)
529 Diag(SuperLoc, diag::err_undef_superclass)
530 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000531 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000532 Context.getObjCInterfaceType(SuperClassDecl),
533 diag::err_forward_superclass,
534 SuperClassDecl->getDeclName(),
535 ClassName,
536 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000537 SuperClassDecl = 0;
538 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000539 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000540 IDecl->setSuperClass(SuperClassDecl);
541 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000542 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000543 }
Chris Lattner4d391482007-12-12 07:09:47 +0000544 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000545 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000546 }
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Sebastian Redl0b17c612010-08-13 00:28:03 +0000548 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000549 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000550 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000551 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000552 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Anders Carlsson15281452008-11-04 16:57:32 +0000555 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000556 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000557}
558
Richard Smithde01b7a2012-08-08 23:32:13 +0000559/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000560/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000561Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
562 IdentifierInfo *AliasName,
563 SourceLocation AliasLocation,
564 IdentifierInfo *ClassName,
565 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000566 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000567 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000568 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000569 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000570 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000571 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000572 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000573 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000574 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000575 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000576 }
577 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000578 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000579 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000580 if (const TypedefNameDecl *TDecl =
581 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000582 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000583 if (T->isObjCObjectType()) {
584 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000585 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000586 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000587 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000588 }
589 }
590 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000591 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
592 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000593 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000594 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000595 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000596 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000597 }
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000599 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000600 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000601 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000602
Anders Carlsson15281452008-11-04 16:57:32 +0000603 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000604 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000605
John McCalld226f652010-08-21 09:40:31 +0000606 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000607}
608
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000609bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000610 IdentifierInfo *PName,
611 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000612 const ObjCList<ObjCProtocolDecl> &PList) {
613
614 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000615 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
616 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000617 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
618 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000619 if (PDecl->getIdentifier() == PName) {
620 Diag(Ploc, diag::err_protocol_has_circular_dependency);
621 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000622 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000623 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000624
625 if (!PDecl->hasDefinition())
626 continue;
627
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000628 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
629 PDecl->getLocation(), PDecl->getReferencedProtocols()))
630 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000631 }
632 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000633 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000634}
635
John McCalld226f652010-08-21 09:40:31 +0000636Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000637Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
638 IdentifierInfo *ProtocolName,
639 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000640 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000641 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000642 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000643 SourceLocation EndProtoLoc,
644 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000645 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000646 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000647 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000648 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
649 ForRedeclaration);
650 ObjCProtocolDecl *PDecl = 0;
651 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
652 // If we already have a definition, complain.
653 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
654 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Douglas Gregor27c6da22012-01-01 20:30:41 +0000656 // Create a new protocol that is completely distinct from previous
657 // declarations, and do not make this protocol available for name lookup.
658 // That way, we'll end up completely ignoring the duplicate.
659 // FIXME: Can we turn this into an error?
660 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
661 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000662 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000663 PDecl->startDefinition();
664 } else {
665 if (PrevDecl) {
666 // Check for circular dependencies among protocol declarations. This can
667 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000668 ObjCList<ObjCProtocolDecl> PList;
669 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
670 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000671 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000672 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000673
674 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000675 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000676 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000677 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000678
Douglas Gregor6e378de2009-04-23 23:18:26 +0000679 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000680 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000681 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000682
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000683 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000684 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000685
686 // Merge attributes from previous declarations.
687 if (PrevDecl)
688 mergeDeclAttributes(PDecl, PrevDecl);
689
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000690 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000691 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000692 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000693 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000694 }
Mike Stump1eb44332009-09-09 15:08:12 +0000695
696 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000697 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000698}
699
700/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000701/// issues an error if they are not declared. It returns list of
702/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000703void
Chris Lattnere13b9592008-07-26 04:03:38 +0000704Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000705 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000706 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000707 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000708 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000709 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
710 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000711 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000712 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000713 TypoCorrection Corrected = CorrectTypo(
714 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000715 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000716 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000717 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000718 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000719 Diag(PDecl->getLocation(), diag::note_previous_decl)
720 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000721 }
722 }
723
724 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000725 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000726 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000727 continue;
728 }
Mike Stump1eb44332009-09-09 15:08:12 +0000729
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000730 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000731
732 // If this is a forward declaration and we are supposed to warn in this
733 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000734 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000735 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000736 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000737 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000738 }
739}
740
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000741/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000742/// a class method in its extension.
743///
Mike Stump1eb44332009-09-09 15:08:12 +0000744void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000745 ObjCInterfaceDecl *ID) {
746 if (!ID)
747 return; // Possibly due to previous error
748
749 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000750 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
751 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000752 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000753 MethodMap[MD->getSelector()] = MD;
754 }
755
756 if (MethodMap.empty())
757 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000758 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
759 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000760 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000761 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
762 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
763 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
764 << Method->getDeclName();
765 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
766 }
767 }
768}
769
James Dennett1dfbd922012-06-14 21:40:34 +0000770/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000771Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000772Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000773 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000774 unsigned NumElts,
775 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000776 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000777 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000778 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000779 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
780 ForRedeclaration);
781 ObjCProtocolDecl *PDecl
782 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
783 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000784 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000785
786 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000787 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000788
Douglas Gregor3937f872012-01-01 20:33:24 +0000789 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000790 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000791
792 if (PrevDecl)
793 mergeDeclAttributes(PDecl, PrevDecl);
794
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000795 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000796 }
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000798 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000799}
800
John McCalld226f652010-08-21 09:40:31 +0000801Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000802ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
803 IdentifierInfo *ClassName, SourceLocation ClassLoc,
804 IdentifierInfo *CategoryName,
805 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000806 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000807 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000808 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000809 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000810 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000811 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000812
813 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000814
815 if (!IDecl
816 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000817 diag::err_category_forward_interface,
818 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000819 // Create an invalid ObjCCategoryDecl to serve as context for
820 // the enclosing method declarations. We mark the decl invalid
821 // to make it clear that this isn't a valid AST.
822 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000823 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000824 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000825 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000826
827 if (!IDecl)
828 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000829 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000830 }
831
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000832 if (!CategoryName && IDecl->getImplementation()) {
833 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
834 Diag(IDecl->getImplementation()->getLocation(),
835 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000836 }
837
Fariborz Jahanian25760612010-02-15 21:55:26 +0000838 if (CategoryName) {
839 /// Check for duplicate interface declaration for this category
840 ObjCCategoryDecl *CDeclChain;
841 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
842 CDeclChain = CDeclChain->getNextClassCategory()) {
843 if (CDeclChain->getIdentifier() == CategoryName) {
844 // Class extensions can be declared multiple times.
845 Diag(CategoryLoc, diag::warn_dup_category_def)
846 << ClassName << CategoryName;
847 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
848 break;
849 }
Chris Lattner70f19542009-02-16 21:26:43 +0000850 }
851 }
Chris Lattner70f19542009-02-16 21:26:43 +0000852
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000853 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
854 ClassLoc, CategoryLoc, CategoryName, IDecl);
855 // FIXME: PushOnScopeChains?
856 CurContext->addDecl(CDecl);
857
Chris Lattner4d391482007-12-12 07:09:47 +0000858 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000859 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000860 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000861 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000862 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000863 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000864 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000865 }
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Anders Carlsson15281452008-11-04 16:57:32 +0000867 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000868 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000869}
870
871/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000872/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000873/// object.
John McCalld226f652010-08-21 09:40:31 +0000874Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000875 SourceLocation AtCatImplLoc,
876 IdentifierInfo *ClassName, SourceLocation ClassLoc,
877 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000878 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000879 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000880 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000881 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
882 if (!CatIDecl) {
883 // Category @implementation with no corresponding @interface.
884 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000885 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
886 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000887 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000888 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000889 }
890 }
891
Mike Stump1eb44332009-09-09 15:08:12 +0000892 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000893 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000894 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000895 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000896 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000897 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000898 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000899 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
900 diag::err_undef_interface)) {
901 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000902 }
Chris Lattner4d391482007-12-12 07:09:47 +0000903
Douglas Gregord0434102009-01-09 00:49:46 +0000904 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000905 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000906
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000907 // If the interface is deprecated/unavailable, warn/error about it.
908 if (IDecl)
909 DiagnoseUseOfDecl(IDecl, ClassLoc);
910
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000911 /// Check that CatName, category name, is not used in another implementation.
912 if (CatIDecl) {
913 if (CatIDecl->getImplementation()) {
914 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
915 << CatName;
916 Diag(CatIDecl->getImplementation()->getLocation(),
917 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000918 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000919 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000920 // Warn on implementating category of deprecated class under
921 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000922 DiagnoseObjCImplementedDeprecations(*this,
923 dyn_cast<NamedDecl>(IDecl),
924 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000925 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
Anders Carlsson15281452008-11-04 16:57:32 +0000928 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000929 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000930}
931
John McCalld226f652010-08-21 09:40:31 +0000932Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000933 SourceLocation AtClassImplLoc,
934 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000935 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000936 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000937 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000938 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000939 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000940 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
941 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000942 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000943 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000944 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000945 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000946 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
947 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000948 } else {
949 // We did not find anything with the name ClassName; try to correct for
950 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000951 ObjCInterfaceValidatorCCC Validator;
952 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000953 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000954 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000955 // Suggest the (potentially) correct interface name. However, put the
956 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000957 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000958 // provide a code-modification hint or use the typo name for recovery,
959 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000960 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000961 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000962 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000963 << ClassName << CorrectedName;
964 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
965 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000966 IDecl = 0;
967 } else {
968 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
969 }
Chris Lattner4d391482007-12-12 07:09:47 +0000970 }
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Chris Lattner4d391482007-12-12 07:09:47 +0000972 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000973 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000974 if (SuperClassname) {
975 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000976 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
977 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000978 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000979 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
980 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000981 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000982 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000983 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000984 if (SDecl && !SDecl->hasDefinition())
985 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000986 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000987 Diag(SuperClassLoc, diag::err_undef_superclass)
988 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000989 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000990 // This implementation and its interface do not have the same
991 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000992 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000993 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000994 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000995 }
996 }
997 }
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Chris Lattner4d391482007-12-12 07:09:47 +0000999 if (!IDecl) {
1000 // Legacy case of @implementation with no corresponding @interface.
1001 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +00001002
Mike Stump390b4cc2009-05-16 07:39:55 +00001003 // FIXME: Do we support attributes on the @implementation? If so we should
1004 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001005 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001006 ClassName, /*PrevDecl=*/0, ClassLoc,
1007 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001008 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001009 if (SDecl) {
1010 IDecl->setSuperClass(SDecl);
1011 IDecl->setSuperClassLoc(SuperClassLoc);
1012 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1013 } else {
1014 IDecl->setEndOfDefinitionLoc(ClassLoc);
1015 }
1016
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001017 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001018 } else {
1019 // Mark the interface as being completed, even if it was just as
1020 // @class ....;
1021 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001022 if (!IDecl->hasDefinition())
1023 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001024 }
Mike Stump1eb44332009-09-09 15:08:12 +00001025
1026 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001027 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1028 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001029
Anders Carlsson15281452008-11-04 16:57:32 +00001030 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001031 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Chris Lattner4d391482007-12-12 07:09:47 +00001033 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001034 if (IDecl->getImplementation()) {
1035 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001036 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001037 Diag(IDecl->getImplementation()->getLocation(),
1038 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001039 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001040 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001041 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001042 // Warn on implementating deprecated class under
1043 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001044 DiagnoseObjCImplementedDeprecations(*this,
1045 dyn_cast<NamedDecl>(IDecl),
1046 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001047 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001048 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001049}
1050
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001051Sema::DeclGroupPtrTy
1052Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1053 SmallVector<Decl *, 64> DeclsInGroup;
1054 DeclsInGroup.reserve(Decls.size() + 1);
1055
1056 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1057 Decl *Dcl = Decls[i];
1058 if (!Dcl)
1059 continue;
1060 if (Dcl->getDeclContext()->isFileContext())
1061 Dcl->setTopLevelDeclInObjCContainer();
1062 DeclsInGroup.push_back(Dcl);
1063 }
1064
1065 DeclsInGroup.push_back(ObjCImpDecl);
1066
1067 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1068}
1069
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001070void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1071 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001072 SourceLocation RBrace) {
1073 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001074 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001075 if (!IDecl)
1076 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001077 /// Check case of non-existing \@interface decl.
1078 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001079 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001080 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001081 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001082 // Add ivar's to class's DeclContext.
1083 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001084 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001085 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001086 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001087 }
1088
Chris Lattner4d391482007-12-12 07:09:47 +00001089 return;
1090 }
1091 // If implementation has empty ivar list, just return.
1092 if (numIvars == 0)
1093 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Chris Lattner4d391482007-12-12 07:09:47 +00001095 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001096 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001097 if (ImpDecl->getSuperClass())
1098 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1099 for (unsigned i = 0; i < numIvars; i++) {
1100 ObjCIvarDecl* ImplIvar = ivars[i];
1101 if (const ObjCIvarDecl *ClsIvar =
1102 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1103 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1104 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1105 continue;
1106 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001107 // Instance ivar to Implementation's DeclContext.
1108 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001109 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001110 ImpDecl->addDecl(ImplIvar);
1111 }
1112 return;
1113 }
Chris Lattner4d391482007-12-12 07:09:47 +00001114 // Check interface's Ivar list against those in the implementation.
1115 // names and types must match.
1116 //
Chris Lattner4d391482007-12-12 07:09:47 +00001117 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001118 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001119 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1120 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001121 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001122 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001123 assert (ImplIvar && "missing implementation ivar");
1124 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001125
Steve Naroffca331292009-03-03 14:49:36 +00001126 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001127 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001128 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001129 << ImplIvar->getIdentifier()
1130 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001131 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001132 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1133 ImplIvar->getBitWidthValue(Context) !=
1134 ClsIvar->getBitWidthValue(Context)) {
1135 Diag(ImplIvar->getBitWidth()->getLocStart(),
1136 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1137 Diag(ClsIvar->getBitWidth()->getLocStart(),
1138 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001139 }
Steve Naroffca331292009-03-03 14:49:36 +00001140 // Make sure the names are identical.
1141 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001142 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001143 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001144 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001145 }
1146 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001147 }
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Chris Lattner609e4c72007-12-12 18:11:49 +00001149 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001150 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001151 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001152 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001153}
1154
Steve Naroff3c2eb662008-02-10 21:38:56 +00001155void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001156 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001157 // No point warning no definition of method which is 'unavailable'.
1158 if (method->hasAttr<UnavailableAttr>())
1159 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001160 if (!IncompleteImpl) {
1161 Diag(ImpLoc, diag::warn_incomplete_impl);
1162 IncompleteImpl = true;
1163 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001164 if (DiagID == diag::warn_unimplemented_protocol_method)
1165 Diag(ImpLoc, DiagID) << method->getDeclName();
1166 else
1167 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001168}
1169
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001170/// Determines if type B can be substituted for type A. Returns true if we can
1171/// guarantee that anything that the user will do to an object of type A can
1172/// also be done to an object of type B. This is trivially true if the two
1173/// types are the same, or if B is a subclass of A. It becomes more complex
1174/// in cases where protocols are involved.
1175///
1176/// Object types in Objective-C describe the minimum requirements for an
1177/// object, rather than providing a complete description of a type. For
1178/// example, if A is a subclass of B, then B* may refer to an instance of A.
1179/// The principle of substitutability means that we may use an instance of A
1180/// anywhere that we may use an instance of B - it will implement all of the
1181/// ivars of B and all of the methods of B.
1182///
1183/// This substitutability is important when type checking methods, because
1184/// the implementation may have stricter type definitions than the interface.
1185/// The interface specifies minimum requirements, but the implementation may
1186/// have more accurate ones. For example, a method may privately accept
1187/// instances of B, but only publish that it accepts instances of A. Any
1188/// object passed to it will be type checked against B, and so will implicitly
1189/// by a valid A*. Similarly, a method may return a subclass of the class that
1190/// it is declared as returning.
1191///
1192/// This is most important when considering subclassing. A method in a
1193/// subclass must accept any object as an argument that its superclass's
1194/// implementation accepts. It may, however, accept a more general type
1195/// without breaking substitutability (i.e. you can still use the subclass
1196/// anywhere that you can use the superclass, but not vice versa). The
1197/// converse requirement applies to return types: the return type for a
1198/// subclass method must be a valid object of the kind that the superclass
1199/// advertises, but it may be specified more accurately. This avoids the need
1200/// for explicit down-casting by callers.
1201///
1202/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001203static bool isObjCTypeSubstitutable(ASTContext &Context,
1204 const ObjCObjectPointerType *A,
1205 const ObjCObjectPointerType *B,
1206 bool rejectId) {
1207 // Reject a protocol-unqualified id.
1208 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001209
1210 // If B is a qualified id, then A must also be a qualified id and it must
1211 // implement all of the protocols in B. It may not be a qualified class.
1212 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1213 // stricter definition so it is not substitutable for id<A>.
1214 if (B->isObjCQualifiedIdType()) {
1215 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001216 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1217 QualType(B,0),
1218 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001219 }
1220
1221 /*
1222 // id is a special type that bypasses type checking completely. We want a
1223 // warning when it is used in one place but not another.
1224 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1225
1226
1227 // If B is a qualified id, then A must also be a qualified id (which it isn't
1228 // if we've got this far)
1229 if (B->isObjCQualifiedIdType()) return false;
1230 */
1231
1232 // Now we know that A and B are (potentially-qualified) class types. The
1233 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001234 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001235}
1236
John McCall10302c02010-10-28 02:34:38 +00001237static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1238 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1239}
1240
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001241static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001242 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001243 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001244 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001245 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001246 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001247 if (IsProtocolMethodDecl &&
1248 (MethodDecl->getObjCDeclQualifier() !=
1249 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001250 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001251 S.Diag(MethodImpl->getLocation(),
1252 (IsOverridingMode ?
1253 diag::warn_conflicting_overriding_ret_type_modifiers
1254 : diag::warn_conflicting_ret_type_modifiers))
1255 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001256 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1257 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1258 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1259 }
1260 else
1261 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001262 }
1263
John McCall10302c02010-10-28 02:34:38 +00001264 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001265 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001266 return true;
1267 if (!Warn)
1268 return false;
John McCall10302c02010-10-28 02:34:38 +00001269
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001270 unsigned DiagID =
1271 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1272 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001273
1274 // Mismatches between ObjC pointers go into a different warning
1275 // category, and sometimes they're even completely whitelisted.
1276 if (const ObjCObjectPointerType *ImplPtrTy =
1277 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1278 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001279 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001280 // Allow non-matching return types as long as they don't violate
1281 // the principle of substitutability. Specifically, we permit
1282 // return types that are subclasses of the declared return type,
1283 // or that are more-qualified versions of the declared type.
1284 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001285 return false;
John McCall10302c02010-10-28 02:34:38 +00001286
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001287 DiagID =
1288 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1289 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001290 }
1291 }
1292
1293 S.Diag(MethodImpl->getLocation(), DiagID)
1294 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001295 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001296 << MethodImpl->getResultType()
1297 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001298 S.Diag(MethodDecl->getLocation(),
1299 IsOverridingMode ? diag::note_previous_declaration
1300 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001301 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001302 return false;
John McCall10302c02010-10-28 02:34:38 +00001303}
1304
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001305static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001306 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001307 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001308 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001309 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001310 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001311 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001312 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001313 if (IsProtocolMethodDecl &&
1314 (ImplVar->getObjCDeclQualifier() !=
1315 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001316 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001317 if (IsOverridingMode)
1318 S.Diag(ImplVar->getLocation(),
1319 diag::warn_conflicting_overriding_param_modifiers)
1320 << getTypeRange(ImplVar->getTypeSourceInfo())
1321 << MethodImpl->getDeclName();
1322 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001323 diag::warn_conflicting_param_modifiers)
1324 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001325 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001326 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1327 << getTypeRange(IfaceVar->getTypeSourceInfo());
1328 }
1329 else
1330 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001331 }
1332
John McCall10302c02010-10-28 02:34:38 +00001333 QualType ImplTy = ImplVar->getType();
1334 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001335
John McCall10302c02010-10-28 02:34:38 +00001336 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001337 return true;
1338
1339 if (!Warn)
1340 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001341 unsigned DiagID =
1342 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1343 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001344
1345 // Mismatches between ObjC pointers go into a different warning
1346 // category, and sometimes they're even completely whitelisted.
1347 if (const ObjCObjectPointerType *ImplPtrTy =
1348 ImplTy->getAs<ObjCObjectPointerType>()) {
1349 if (const ObjCObjectPointerType *IfacePtrTy =
1350 IfaceTy->getAs<ObjCObjectPointerType>()) {
1351 // Allow non-matching argument types as long as they don't
1352 // violate the principle of substitutability. Specifically, the
1353 // implementation must accept any objects that the superclass
1354 // accepts, however it may also accept others.
1355 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001356 return false;
John McCall10302c02010-10-28 02:34:38 +00001357
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001358 DiagID =
1359 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1360 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001361 }
1362 }
1363
1364 S.Diag(ImplVar->getLocation(), DiagID)
1365 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001366 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1367 S.Diag(IfaceVar->getLocation(),
1368 (IsOverridingMode ? diag::note_previous_declaration
1369 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001370 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001371 return false;
John McCall10302c02010-10-28 02:34:38 +00001372}
John McCallf85e1932011-06-15 23:02:42 +00001373
1374/// In ARC, check whether the conventional meanings of the two methods
1375/// match. If they don't, it's a hard error.
1376static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1377 ObjCMethodDecl *decl) {
1378 ObjCMethodFamily implFamily = impl->getMethodFamily();
1379 ObjCMethodFamily declFamily = decl->getMethodFamily();
1380 if (implFamily == declFamily) return false;
1381
1382 // Since conventions are sorted by selector, the only possibility is
1383 // that the types differ enough to cause one selector or the other
1384 // to fall out of the family.
1385 assert(implFamily == OMF_None || declFamily == OMF_None);
1386
1387 // No further diagnostics required on invalid declarations.
1388 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1389
1390 const ObjCMethodDecl *unmatched = impl;
1391 ObjCMethodFamily family = declFamily;
1392 unsigned errorID = diag::err_arc_lost_method_convention;
1393 unsigned noteID = diag::note_arc_lost_method_convention;
1394 if (declFamily == OMF_None) {
1395 unmatched = decl;
1396 family = implFamily;
1397 errorID = diag::err_arc_gained_method_convention;
1398 noteID = diag::note_arc_gained_method_convention;
1399 }
1400
1401 // Indexes into a %select clause in the diagnostic.
1402 enum FamilySelector {
1403 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1404 };
1405 FamilySelector familySelector = FamilySelector();
1406
1407 switch (family) {
1408 case OMF_None: llvm_unreachable("logic error, no method convention");
1409 case OMF_retain:
1410 case OMF_release:
1411 case OMF_autorelease:
1412 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001413 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001414 case OMF_retainCount:
1415 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001416 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001417 // Mismatches for these methods don't change ownership
1418 // conventions, so we don't care.
1419 return false;
1420
1421 case OMF_init: familySelector = F_init; break;
1422 case OMF_alloc: familySelector = F_alloc; break;
1423 case OMF_copy: familySelector = F_copy; break;
1424 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1425 case OMF_new: familySelector = F_new; break;
1426 }
1427
1428 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1429 ReasonSelector reasonSelector;
1430
1431 // The only reason these methods don't fall within their families is
1432 // due to unusual result types.
1433 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1434 reasonSelector = R_UnrelatedReturn;
1435 } else {
1436 reasonSelector = R_NonObjectReturn;
1437 }
1438
1439 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1440 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1441
1442 return true;
1443}
John McCall10302c02010-10-28 02:34:38 +00001444
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001445void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001446 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001447 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001448 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001449 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1450 return;
1451
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001452 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001453 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001454 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Chris Lattner3aff9192009-04-11 19:58:42 +00001456 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001457 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1458 EF = MethodDecl->param_end();
1459 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001460 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001461 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001462 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001463
Fariborz Jahanian21121902011-08-08 18:03:17 +00001464 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001465 Diag(ImpMethodDecl->getLocation(),
1466 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001467 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001468 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001469}
1470
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001471void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1472 ObjCMethodDecl *Overridden,
1473 bool IsProtocolMethodDecl) {
1474
1475 CheckMethodOverrideReturn(*this, Method, Overridden,
1476 IsProtocolMethodDecl, true,
1477 true);
1478
1479 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001480 IF = Overridden->param_begin(), EM = Method->param_end(),
1481 EF = Overridden->param_end();
1482 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001483 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1484 IsProtocolMethodDecl, true, true);
1485 }
1486
1487 if (Method->isVariadic() != Overridden->isVariadic()) {
1488 Diag(Method->getLocation(),
1489 diag::warn_conflicting_overriding_variadic);
1490 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1491 }
1492}
1493
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001494/// WarnExactTypedMethods - This routine issues a warning if method
1495/// implementation declaration matches exactly that of its declaration.
1496void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1497 ObjCMethodDecl *MethodDecl,
1498 bool IsProtocolMethodDecl) {
1499 // don't issue warning when protocol method is optional because primary
1500 // class is not required to implement it and it is safe for protocol
1501 // to implement it.
1502 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1503 return;
1504 // don't issue warning when primary class's method is
1505 // depecated/unavailable.
1506 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1507 MethodDecl->hasAttr<DeprecatedAttr>())
1508 return;
1509
1510 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1511 IsProtocolMethodDecl, false, false);
1512 if (match)
1513 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001514 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1515 EF = MethodDecl->param_end();
1516 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001517 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1518 *IM, *IF,
1519 IsProtocolMethodDecl, false, false);
1520 if (!match)
1521 break;
1522 }
1523 if (match)
1524 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001525 if (match)
1526 match = !(MethodDecl->isClassMethod() &&
1527 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001528
1529 if (match) {
1530 Diag(ImpMethodDecl->getLocation(),
1531 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001532 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1533 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001534 }
1535}
1536
Mike Stump390b4cc2009-05-16 07:39:55 +00001537/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1538/// improve the efficiency of selector lookups and type checking by associating
1539/// with each protocol / interface / category the flattened instance tables. If
1540/// we used an immutable set to keep the table then it wouldn't add significant
1541/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001542
Steve Naroffefe7f362008-02-08 22:06:17 +00001543/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001544/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001545void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1546 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001547 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001548 const SelectorSet &InsMap,
1549 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001550 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001551 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1552 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1553 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001554 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1555
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001556 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001557 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001558 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001559 // check to see if class implements forwardInvocation method and objects
1560 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001561 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001562 // Under such conditions, which means that every method possible is
1563 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001564 // found" warnings.
1565 // FIXME: Use a general GetUnarySelector method for this.
1566 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1567 Selector fISelector = Context.Selectors.getSelector(1, &II);
1568 if (InsMap.count(fISelector))
1569 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1570 // need be implemented in the implementation.
1571 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1572 }
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001574 // If a method lookup fails locally we still need to look and see if
1575 // the method was implemented by a base class or an inherited
1576 // protocol. This lookup is slow, but occurs rarely in correct code
1577 // and otherwise would terminate in a warning.
1578
Chris Lattner4d391482007-12-12 07:09:47 +00001579 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001580 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001581 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001582 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001583 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001584 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001585 !method->isPropertyAccessor() &&
1586 !InsMap.count(method->getSelector()) &&
1587 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001588 // If a method is not implemented in the category implementation but
1589 // has been declared in its primary class, superclass,
1590 // or in one of their protocols, no need to issue the warning.
1591 // This is because method will be implemented in the primary class
1592 // or one of its super class implementation.
1593
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001594 // Ugly, but necessary. Method declared in protcol might have
1595 // have been synthesized due to a property declared in the class which
1596 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001597 if (ObjCMethodDecl *MethodInClass =
1598 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001599 true /*shallowCategoryLookup*/))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001600 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001601 continue;
1602 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1603 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1604 != DiagnosticsEngine::Ignored) {
1605 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001606 Diag(method->getLocation(), diag::note_method_declared_at)
1607 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001608 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1609 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001610 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001611 }
1612 }
Chris Lattner4d391482007-12-12 07:09:47 +00001613 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001614 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001615 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001616 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001617 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001618 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1619 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001620 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001621 // See above comment for instance method lookups.
1622 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001623 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001624 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001625 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001626 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1627 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001628 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001629 Diag(method->getLocation(), diag::note_method_declared_at)
1630 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001631 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1632 PDecl->getDeclName();
1633 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001634 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001635 }
Chris Lattner780f3292008-07-21 21:32:27 +00001636 // Check on this protocols's referenced protocols, recursively.
1637 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1638 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001639 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001640}
1641
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001642/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001643/// or protocol against those declared in their implementations.
1644///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001645void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1646 const SelectorSet &ClsMap,
1647 SelectorSet &InsMapSeen,
1648 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001649 ObjCImplDecl* IMPDecl,
1650 ObjCContainerDecl* CDecl,
1651 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001652 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001653 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001654 // Check and see if instance methods in class interface have been
1655 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001656 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1657 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001658 if (InsMapSeen.count((*I)->getSelector()))
1659 continue;
1660 InsMapSeen.insert((*I)->getSelector());
Jordan Rose1e4691b2012-10-10 16:42:25 +00001661 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001662 !InsMap.count((*I)->getSelector())) {
1663 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001664 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1665 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001666 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001667 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001668 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001669 IMPDecl->getInstanceMethod((*I)->getSelector());
1670 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1671 "Expected to find the method through lookup as well");
1672 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001673 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001674 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001675 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001676 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1677 isa<ObjCProtocolDecl>(CDecl));
Jordan Rose1e4691b2012-10-10 16:42:25 +00001678 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001679 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001680 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001681 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001682 }
1683 }
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001685 // Check and see if class methods in class interface have been
1686 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001687 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001688 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001689 if (ClsMapSeen.count((*I)->getSelector()))
1690 continue;
1691 ClsMapSeen.insert((*I)->getSelector());
1692 if (!ClsMap.count((*I)->getSelector())) {
1693 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001694 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1695 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001696 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001697 ObjCMethodDecl *ImpMethodDecl =
1698 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001699 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1700 "Expected to find the method through lookup as well");
1701 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001702 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001703 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1704 isa<ObjCProtocolDecl>(CDecl));
1705 else
1706 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001707 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001708 }
1709 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001710
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001711 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001712 // Also methods in class extensions need be looked at next.
1713 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1714 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1715 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1716 IMPDecl,
1717 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001718 IncompleteImpl, false,
1719 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001720
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001721 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001722 for (ObjCInterfaceDecl::all_protocol_iterator
1723 PI = I->all_referenced_protocol_begin(),
1724 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001725 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1726 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001727 (*PI), IncompleteImpl, false,
1728 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001729
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001730 // FIXME. For now, we are not checking for extact match of methods
1731 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001732 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001733 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001734 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001735 I->getSuperClass(), IncompleteImpl, false);
1736 }
1737}
1738
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001739/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1740/// category matches with those implemented in its primary class and
1741/// warns each time an exact match is found.
1742void Sema::CheckCategoryVsClassMethodMatches(
1743 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001744 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001745
1746 for (ObjCImplementationDecl::instmeth_iterator
1747 I = CatIMPDecl->instmeth_begin(),
1748 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1749 InsMap.insert((*I)->getSelector());
1750
1751 for (ObjCImplementationDecl::classmeth_iterator
1752 I = CatIMPDecl->classmeth_begin(),
1753 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1754 ClsMap.insert((*I)->getSelector());
1755 if (InsMap.empty() && ClsMap.empty())
1756 return;
1757
1758 // Get category's primary class.
1759 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1760 if (!CatDecl)
1761 return;
1762 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1763 if (!IDecl)
1764 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001765 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001766 bool IncompleteImpl = false;
1767 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1768 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001769 IncompleteImpl, false,
1770 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001771}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001772
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001773void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001774 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001775 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001776 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001777 // Check and see if instance methods in class interface have been
1778 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001779 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001780 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001781 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001782
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001783 // Check and see if properties declared in the interface have either 1)
1784 // an implementation or 2) there is a @synthesize/@dynamic implementation
1785 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001786 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001787 if (!(LangOpts.ObjCDefaultSynthProperties &&
1788 LangOpts.ObjCRuntime.isNonFragile()) ||
1789 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001790 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001791
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001792 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001793 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001794 I = IMPDecl->classmeth_begin(),
1795 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001796 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001797
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001798 // Check for type conflict of methods declared in a class/protocol and
1799 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001800 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001801 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1802 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001803 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001804
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001805 // check all methods implemented in category against those declared
1806 // in its primary class.
1807 if (ObjCCategoryImplDecl *CatDecl =
1808 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1809 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Chris Lattner4d391482007-12-12 07:09:47 +00001811 // Check the protocol list for unimplemented methods in the @implementation
1812 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001813 // Check and see if class methods in class interface have been
1814 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Chris Lattnercddc8882009-03-01 00:56:52 +00001816 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001817 for (ObjCInterfaceDecl::all_protocol_iterator
1818 PI = I->all_referenced_protocol_begin(),
1819 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001820 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001821 InsMap, ClsMap, I);
1822 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001823 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1824 Categories; Categories = Categories->getNextClassExtension())
1825 ImplMethodsVsClassMethods(S, IMPDecl,
1826 const_cast<ObjCCategoryDecl*>(Categories),
1827 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001828 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001829 // For extended class, unimplemented methods in its protocols will
1830 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001831 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001832 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1833 E = C->protocol_end(); PI != E; ++PI)
1834 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001835 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001836 // Report unimplemented properties in the category as well.
1837 // When reporting on missing setter/getters, do not report when
1838 // setter/getter is implemented in category's primary class
1839 // implementation.
1840 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1841 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1842 for (ObjCImplementationDecl::instmeth_iterator
1843 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1844 InsMap.insert((*I)->getSelector());
1845 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001846 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001847 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001848 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001849 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001850}
1851
Mike Stump1eb44332009-09-09 15:08:12 +00001852/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001853Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001854Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001855 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001856 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001857 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001858 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001859 for (unsigned i = 0; i != NumElts; ++i) {
1860 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001861 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001862 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001863 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001864 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001865 // Maybe we will complain about the shadowed template parameter.
1866 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1867 // Just pretend that we didn't see the previous declaration.
1868 PrevDecl = 0;
1869 }
1870
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001871 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001872 // GCC apparently allows the following idiom:
1873 //
1874 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1875 // @class XCElementToggler;
1876 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001877 // Here we have chosen to ignore the forward class declaration
1878 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001879 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001880 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001881 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001882 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001883 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001884 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001885 // to the underlying class. Just ignore the forward class with a warning
1886 // as this will force the intended behavior which is to lookup the typedef
1887 // name.
1888 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1889 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1890 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1891 continue;
1892 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001893 }
Chris Lattner4d391482007-12-12 07:09:47 +00001894 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001895
1896 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001897 ObjCInterfaceDecl *PrevIDecl
1898 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001899 ObjCInterfaceDecl *IDecl
1900 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001901 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001902 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001903
Douglas Gregor7723fec2011-12-15 20:29:51 +00001904 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001905 CheckObjCDeclScope(IDecl);
1906 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001907 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001908
1909 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001910}
1911
John McCall0f4c4c42011-06-16 01:15:19 +00001912static bool tryMatchRecordTypes(ASTContext &Context,
1913 Sema::MethodMatchStrategy strategy,
1914 const Type *left, const Type *right);
1915
John McCallf85e1932011-06-15 23:02:42 +00001916static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1917 QualType leftQT, QualType rightQT) {
1918 const Type *left =
1919 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1920 const Type *right =
1921 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1922
1923 if (left == right) return true;
1924
1925 // If we're doing a strict match, the types have to match exactly.
1926 if (strategy == Sema::MMS_strict) return false;
1927
1928 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1929
1930 // Otherwise, use this absurdly complicated algorithm to try to
1931 // validate the basic, low-level compatibility of the two types.
1932
1933 // As a minimum, require the sizes and alignments to match.
1934 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1935 return false;
1936
1937 // Consider all the kinds of non-dependent canonical types:
1938 // - functions and arrays aren't possible as return and parameter types
1939
1940 // - vector types of equal size can be arbitrarily mixed
1941 if (isa<VectorType>(left)) return isa<VectorType>(right);
1942 if (isa<VectorType>(right)) return false;
1943
1944 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001945 // - structs, unions, and Objective-C objects must match more-or-less
1946 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001947 // - everything else should be a scalar
1948 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001949 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001950
John McCall1d9b3b22011-09-09 05:25:32 +00001951 // Make scalars agree in kind, except count bools as chars, and group
1952 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001953 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1954 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1955 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1956 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001957 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1958 leftSK = Type::STK_ObjCObjectPointer;
1959 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1960 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001961
1962 // Note that data member pointers and function member pointers don't
1963 // intermix because of the size differences.
1964
1965 return (leftSK == rightSK);
1966}
Chris Lattner4d391482007-12-12 07:09:47 +00001967
John McCall0f4c4c42011-06-16 01:15:19 +00001968static bool tryMatchRecordTypes(ASTContext &Context,
1969 Sema::MethodMatchStrategy strategy,
1970 const Type *lt, const Type *rt) {
1971 assert(lt && rt && lt != rt);
1972
1973 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1974 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1975 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1976
1977 // Require union-hood to match.
1978 if (left->isUnion() != right->isUnion()) return false;
1979
1980 // Require an exact match if either is non-POD.
1981 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1982 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1983 return false;
1984
1985 // Require size and alignment to match.
1986 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1987
1988 // Require fields to match.
1989 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1990 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1991 for (; li != le && ri != re; ++li, ++ri) {
1992 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1993 return false;
1994 }
1995 return (li == le && ri == re);
1996}
1997
Chris Lattner4d391482007-12-12 07:09:47 +00001998/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1999/// returns true, or false, accordingly.
2000/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00002001bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2002 const ObjCMethodDecl *right,
2003 MethodMatchStrategy strategy) {
2004 if (!matchTypes(Context, strategy,
2005 left->getResultType(), right->getResultType()))
2006 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002007
David Blaikie4e4d0842012-03-11 07:00:24 +00002008 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002009 (left->hasAttr<NSReturnsRetainedAttr>()
2010 != right->hasAttr<NSReturnsRetainedAttr>() ||
2011 left->hasAttr<NSConsumesSelfAttr>()
2012 != right->hasAttr<NSConsumesSelfAttr>()))
2013 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002015 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002016 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2017 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002019 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002020 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002021 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002022
2023 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2024 return false;
2025
David Blaikie4e4d0842012-03-11 07:00:24 +00002026 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002027 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2028 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002029 }
2030 return true;
2031}
2032
Douglas Gregorff310c72012-05-01 23:37:00 +00002033void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002034 // If the list is empty, make it a singleton list.
2035 if (List->Method == 0) {
2036 List->Method = Method;
2037 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002038 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002039 }
2040
2041 // We've seen a method with this name, see if we have already seen this type
2042 // signature.
2043 ObjCMethodList *Previous = List;
2044 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002045 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002046 continue;
2047
2048 ObjCMethodDecl *PrevObjCMethod = List->Method;
2049
2050 // Propagate the 'defined' bit.
2051 if (Method->isDefined())
2052 PrevObjCMethod->setDefined(true);
2053
2054 // If a method is deprecated, push it in the global pool.
2055 // This is used for better diagnostics.
2056 if (Method->isDeprecated()) {
2057 if (!PrevObjCMethod->isDeprecated())
2058 List->Method = Method;
2059 }
2060 // If new method is unavailable, push it into global pool
2061 // unless previous one is deprecated.
2062 if (Method->isUnavailable()) {
2063 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2064 List->Method = Method;
2065 }
2066
Douglas Gregorff310c72012-05-01 23:37:00 +00002067 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002068 }
2069
2070 // We have a new signature for an existing method - add it.
2071 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002072 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002073 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2074}
2075
Sebastian Redldb9d2142010-08-02 23:18:59 +00002076/// \brief Read the contents of the method pool for a given selector from
2077/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002078void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002079 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002080 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002081}
2082
Douglas Gregorff310c72012-05-01 23:37:00 +00002083void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002084 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002085 // Ignore methods of invalid containers.
2086 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002087 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002088
Douglas Gregor0d266d62012-01-25 00:59:09 +00002089 if (ExternalSource)
2090 ReadMethodPool(Method->getSelector());
2091
Sebastian Redldb9d2142010-08-02 23:18:59 +00002092 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002093 if (Pos == MethodPool.end())
2094 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2095 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002096
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002097 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002098
Sebastian Redldb9d2142010-08-02 23:18:59 +00002099 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002100 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002101}
2102
John McCallf85e1932011-06-15 23:02:42 +00002103/// Determines if this is an "acceptable" loose mismatch in the global
2104/// method pool. This exists mostly as a hack to get around certain
2105/// global mismatches which we can't afford to make warnings / errors.
2106/// Really, what we want is a way to take a method out of the global
2107/// method pool.
2108static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2109 ObjCMethodDecl *other) {
2110 if (!chosen->isInstanceMethod())
2111 return false;
2112
2113 Selector sel = chosen->getSelector();
2114 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2115 return false;
2116
2117 // Don't complain about mismatches for -length if the method we
2118 // chose has an integral result type.
2119 return (chosen->getResultType()->isIntegerType());
2120}
2121
Sebastian Redldb9d2142010-08-02 23:18:59 +00002122ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002123 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002124 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002125 if (ExternalSource)
2126 ReadMethodPool(Sel);
2127
Sebastian Redldb9d2142010-08-02 23:18:59 +00002128 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002129 if (Pos == MethodPool.end())
2130 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002131
Sebastian Redldb9d2142010-08-02 23:18:59 +00002132 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002133
Sebastian Redldb9d2142010-08-02 23:18:59 +00002134 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002135 bool issueDiagnostic = false, issueError = false;
2136
2137 // We support a warning which complains about *any* difference in
2138 // method signature.
2139 bool strictSelectorMatch =
2140 (receiverIdOrClass && warn &&
2141 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2142 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002143 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002144 if (strictSelectorMatch)
2145 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002146 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2147 MMS_strict)) {
2148 issueDiagnostic = true;
2149 break;
2150 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002151 }
2152
John McCallf85e1932011-06-15 23:02:42 +00002153 // If we didn't see any strict differences, we won't see any loose
2154 // differences. In ARC, however, we also need to check for loose
2155 // mismatches, because most of them are errors.
2156 if (!strictSelectorMatch ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002157 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002158 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002159 // This checks if the methods differ in type mismatch.
2160 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2161 MMS_loose) &&
2162 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2163 issueDiagnostic = true;
David Blaikie4e4d0842012-03-11 07:00:24 +00002164 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002165 issueError = true;
2166 break;
2167 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002168 }
2169
John McCallf85e1932011-06-15 23:02:42 +00002170 if (issueDiagnostic) {
2171 if (issueError)
2172 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2173 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002174 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2175 else
2176 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002177
2178 Diag(MethList.Method->getLocStart(),
2179 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002180 << MethList.Method->getSourceRange();
2181 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2182 Diag(Next->Method->getLocStart(), diag::note_also_found)
2183 << Next->Method->getSourceRange();
2184 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002185 }
2186 return MethList.Method;
2187}
2188
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002189ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002190 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2191 if (Pos == MethodPool.end())
2192 return 0;
2193
2194 GlobalMethods &Methods = Pos->second;
2195
2196 if (Methods.first.Method && Methods.first.Method->isDefined())
2197 return Methods.first.Method;
2198 if (Methods.second.Method && Methods.second.Method->isDefined())
2199 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002200 return 0;
2201}
2202
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002203/// DiagnoseDuplicateIvars -
2204/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002205/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002206/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002207/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002208void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2209 ObjCInterfaceDecl *SID) {
2210 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2211 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002212 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002213 if (Ivar->isInvalidDecl())
2214 continue;
2215 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2216 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2217 if (prevIvar) {
2218 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2219 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2220 Ivar->setInvalidDecl();
2221 }
2222 }
2223 }
2224}
2225
Erik Verbruggend64251f2011-12-06 09:25:23 +00002226Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2227 switch (CurContext->getDeclKind()) {
2228 case Decl::ObjCInterface:
2229 return Sema::OCK_Interface;
2230 case Decl::ObjCProtocol:
2231 return Sema::OCK_Protocol;
2232 case Decl::ObjCCategory:
2233 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2234 return Sema::OCK_ClassExtension;
2235 else
2236 return Sema::OCK_Category;
2237 case Decl::ObjCImplementation:
2238 return Sema::OCK_Implementation;
2239 case Decl::ObjCCategoryImpl:
2240 return Sema::OCK_CategoryImplementation;
2241
2242 default:
2243 return Sema::OCK_None;
2244 }
2245}
2246
Steve Naroffa56f6162007-12-18 01:30:32 +00002247// Note: For class/category implemenations, allMethods/allProperties is
2248// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002249Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2250 Decl **allMethods, unsigned allNum,
2251 Decl **allProperties, unsigned pNum,
2252 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002253
Erik Verbruggend64251f2011-12-06 09:25:23 +00002254 if (getObjCContainerKind() == Sema::OCK_None)
2255 return 0;
2256
2257 assert(AtEnd.isValid() && "Invalid location for '@end'");
2258
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002259 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2260 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002261
Mike Stump1eb44332009-09-09 15:08:12 +00002262 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002263 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2264 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002265 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002266
Steve Naroff0701bbb2009-01-08 17:28:14 +00002267 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2268 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2269 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2270
Chris Lattner4d391482007-12-12 07:09:47 +00002271 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002272 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002273 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002274
2275 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002276 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002277 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002278 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002279 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002280 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002281 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002282 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002283 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002284 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002285 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002286 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002287 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002288 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002289 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002290 if (!Context.getSourceManager().isInSystemHeader(
2291 Method->getLocation()))
2292 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2293 << Method->getDeclName();
2294 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2295 }
Chris Lattner4d391482007-12-12 07:09:47 +00002296 InsMap[Method->getSelector()] = Method;
2297 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002298 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002299 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002300 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002301 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002302 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002303 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002304 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002305 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002306 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002307 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002308 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002309 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002310 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002311 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002312 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002313 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002314 if (!Context.getSourceManager().isInSystemHeader(
2315 Method->getLocation()))
2316 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2317 << Method->getDeclName();
2318 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2319 }
Chris Lattner4d391482007-12-12 07:09:47 +00002320 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002321 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002322 }
2323 }
2324 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002325 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002326 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002327 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002328 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002329 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002330 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002331 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002332 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002333 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002334
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002335 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002336 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002337 if (C->IsClassExtension()) {
2338 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2339 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002340 }
Chris Lattner4d391482007-12-12 07:09:47 +00002341 }
Steve Naroff09c47192009-01-09 15:36:25 +00002342 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002343 if (CDecl->getIdentifier())
2344 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2345 // user-defined setter/getter. It also synthesizes setter/getter methods
2346 // and adds them to the DeclContext and global method pools.
2347 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2348 E = CDecl->prop_end();
2349 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002350 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002351 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002352 }
2353 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002354 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002355 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002356 // Any property declared in a class extension might have user
2357 // declared setter or getter in current class extension or one
2358 // of the other class extensions. Mark them as synthesized as
2359 // property will be synthesized when property with same name is
2360 // seen in the @implementation.
2361 for (const ObjCCategoryDecl *ClsExtDecl =
2362 IDecl->getFirstClassExtension();
2363 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2364 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2365 E = ClsExtDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002366 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002367 // Skip over properties declared @dynamic
2368 if (const ObjCPropertyImplDecl *PIDecl
2369 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2370 if (PIDecl->getPropertyImplementation()
2371 == ObjCPropertyImplDecl::Dynamic)
2372 continue;
2373
2374 for (const ObjCCategoryDecl *CExtDecl =
2375 IDecl->getFirstClassExtension();
2376 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2377 if (ObjCMethodDecl *GetterMethod =
2378 CExtDecl->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002379 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002380 if (!Property->isReadOnly())
2381 if (ObjCMethodDecl *SetterMethod =
2382 CExtDecl->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002383 SetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002384 }
2385 }
2386 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002387 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002388 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002389 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002390
Patrick Beardb2f68202012-04-06 18:12:22 +00002391 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2392 if (IDecl->getSuperClass() == NULL) {
2393 // This class has no superclass, so check that it has been marked with
2394 // __attribute((objc_root_class)).
2395 if (!HasRootClassAttr) {
2396 SourceLocation DeclLoc(IDecl->getLocation());
2397 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2398 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2399 << IDecl->getIdentifier();
2400 // See if NSObject is in the current scope, and if it is, suggest
2401 // adding " : NSObject " to the class declaration.
2402 NamedDecl *IF = LookupSingleName(TUScope,
2403 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2404 DeclLoc, LookupOrdinaryName);
2405 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2406 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2407 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2408 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2409 } else {
2410 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2411 }
2412 }
2413 } else if (HasRootClassAttr) {
2414 // Complain that only root classes may have this attribute.
2415 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2416 }
2417
John McCall260611a2012-06-20 06:18:46 +00002418 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002419 while (IDecl->getSuperClass()) {
2420 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2421 IDecl = IDecl->getSuperClass();
2422 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002423 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002424 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002425 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002426 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002427 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002428 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002429
Chris Lattner4d391482007-12-12 07:09:47 +00002430 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002431 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002432 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002433 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002434 Categories; Categories = Categories->getNextClassCategory()) {
2435 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002436 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002437 break;
2438 }
2439 }
2440 }
2441 }
Chris Lattner682bf922009-03-29 16:50:03 +00002442 if (isInterfaceDeclKind) {
2443 // Reject invalid vardecls.
2444 for (unsigned i = 0; i != tuvNum; i++) {
2445 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2446 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2447 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002448 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002449 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002450 }
Chris Lattner682bf922009-03-29 16:50:03 +00002451 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002452 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002453 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002454
2455 for (unsigned i = 0; i != tuvNum; i++) {
2456 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002457 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2458 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002459 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2460 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002461
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002462 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002463 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002464}
2465
2466
2467/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2468/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002469static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002470CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002471 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002472}
2473
Ted Kremenek422bae72010-04-18 04:59:38 +00002474static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002475unsigned countAlignAttr(const AttrVec &A) {
2476 unsigned count=0;
2477 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2478 if ((*i)->getKind() == attr::Aligned)
2479 ++count;
2480 return count;
2481}
2482
2483static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002484bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2485 const AttrVec &A) {
2486 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002487 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002488 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002489 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002490
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002491 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002492 // But implementation has attributes. This is invalid.
2493 // Except when implementation has 'Align' attribute which is
2494 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002495 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002496 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002497
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002498 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002499
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002500 unsigned countAlignOnImpl = countAlignAttr(A);
2501 if (!countAlignOnImpl && (A.size() != D.size()))
2502 return true;
2503 else if (countAlignOnImpl) {
2504 unsigned countAlignOnDecl = countAlignAttr(D);
2505 if (countAlignOnDecl && (A.size() != D.size()))
2506 return true;
2507 else if (!countAlignOnDecl &&
2508 ((A.size()-countAlignOnImpl) != D.size()))
2509 return true;
2510 }
2511
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002512 // attributes on method declaration and definition must match exactly.
2513 // Note that we have at most a couple of attributes on methods, so this
2514 // n*n search is good enough.
2515 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002516 if ((*i)->getKind() == attr::Aligned)
2517 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002518 bool match = false;
2519 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2520 if ((*i)->getKind() == (*i1)->getKind()) {
2521 match = true;
2522 break;
2523 }
2524 }
2525 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002526 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002527 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002528
Sean Huntcf807c42010-08-18 23:23:40 +00002529 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002530}
2531
Douglas Gregor926df6c2011-06-11 01:09:30 +00002532/// \brief Check whether the declared result type of the given Objective-C
2533/// method declaration is compatible with the method's class.
2534///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002535static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002536CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2537 ObjCInterfaceDecl *CurrentClass) {
2538 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002539
2540 // If an Objective-C method inherits its related result type, then its
2541 // declared result type must be compatible with its own class type. The
2542 // declared result type is compatible if:
2543 if (const ObjCObjectPointerType *ResultObjectType
2544 = ResultType->getAs<ObjCObjectPointerType>()) {
2545 // - it is id or qualified id, or
2546 if (ResultObjectType->isObjCIdType() ||
2547 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002548 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002549
2550 if (CurrentClass) {
2551 if (ObjCInterfaceDecl *ResultClass
2552 = ResultObjectType->getInterfaceDecl()) {
2553 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002554 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002555 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002556
2557 // - it is a superclass of the method's class type
2558 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002559 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002560 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002561 } else {
2562 // Any Objective-C pointer type might be acceptable for a protocol
2563 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002564 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002565 }
2566 }
2567
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002568 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002569}
2570
John McCall6c2c2502011-07-22 02:45:48 +00002571namespace {
2572/// A helper class for searching for methods which a particular method
2573/// overrides.
2574class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002575public:
John McCall6c2c2502011-07-22 02:45:48 +00002576 Sema &S;
2577 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002578 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002579 bool Recursive;
2580
2581public:
2582 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2583 Selector selector = method->getSelector();
2584
2585 // Bypass this search if we've never seen an instance/class method
2586 // with this selector before.
2587 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2588 if (it == S.MethodPool.end()) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00002589 if (!S.getExternalSource()) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002590 S.ReadMethodPool(selector);
2591
2592 it = S.MethodPool.find(selector);
2593 if (it == S.MethodPool.end())
2594 return;
John McCall6c2c2502011-07-22 02:45:48 +00002595 }
2596 ObjCMethodList &list =
2597 method->isInstanceMethod() ? it->second.first : it->second.second;
2598 if (!list.Method) return;
2599
2600 ObjCContainerDecl *container
2601 = cast<ObjCContainerDecl>(method->getDeclContext());
2602
2603 // Prevent the search from reaching this container again. This is
2604 // important with categories, which override methods from the
2605 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002606 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2607 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002608 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2609 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002610 } else {
2611 searchFromContainer(container);
2612 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002613 }
John McCall6c2c2502011-07-22 02:45:48 +00002614
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002615 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002616 iterator begin() const { return Overridden.begin(); }
2617 iterator end() const { return Overridden.end(); }
2618
2619private:
2620 void searchFromContainer(ObjCContainerDecl *container) {
2621 if (container->isInvalidDecl()) return;
2622
2623 switch (container->getDeclKind()) {
2624#define OBJCCONTAINER(type, base) \
2625 case Decl::type: \
2626 searchFrom(cast<type##Decl>(container)); \
2627 break;
2628#define ABSTRACT_DECL(expansion)
2629#define DECL(type, base) \
2630 case Decl::type:
2631#include "clang/AST/DeclNodes.inc"
2632 llvm_unreachable("not an ObjC container!");
2633 }
2634 }
2635
2636 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002637 if (!protocol->hasDefinition())
2638 return;
2639
John McCall6c2c2502011-07-22 02:45:48 +00002640 // A method in a protocol declaration overrides declarations from
2641 // referenced ("parent") protocols.
2642 search(protocol->getReferencedProtocols());
2643 }
2644
2645 void searchFrom(ObjCCategoryDecl *category) {
2646 // A method in a category declaration overrides declarations from
2647 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002648 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002649 search(category->getReferencedProtocols());
2650 }
2651
2652 void searchFrom(ObjCCategoryImplDecl *impl) {
2653 // A method in a category definition that has a category
2654 // declaration overrides declarations from the category
2655 // declaration.
2656 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2657 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002658 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2659 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002660
2661 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002662 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2663 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002664 }
2665 }
2666
2667 void searchFrom(ObjCInterfaceDecl *iface) {
2668 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002669 if (!iface->hasDefinition())
2670 return;
2671
John McCall6c2c2502011-07-22 02:45:48 +00002672 // - categories,
2673 for (ObjCCategoryDecl *category = iface->getCategoryList();
2674 category; category = category->getNextClassCategory())
2675 search(category);
2676
2677 // - the super class, and
2678 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2679 search(super);
2680
2681 // - any referenced protocols.
2682 search(iface->getReferencedProtocols());
2683 }
2684
2685 void searchFrom(ObjCImplementationDecl *impl) {
2686 // A method in a class implementation overrides declarations from
2687 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002688 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2689 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002690 }
2691
2692
2693 void search(const ObjCProtocolList &protocols) {
2694 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2695 i != e; ++i)
2696 search(*i);
2697 }
2698
2699 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002700 // Check for a method in this container which matches this selector.
2701 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2702 Method->isInstanceMethod());
2703
2704 // If we find one, record it and bail out.
2705 if (meth) {
2706 Overridden.insert(meth);
2707 return;
2708 }
2709
2710 // Otherwise, search for methods that a hypothetical method here
2711 // would have overridden.
2712
2713 // Note that we're now in a recursive case.
2714 Recursive = true;
2715
2716 searchFromContainer(container);
2717 }
2718};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002719}
2720
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002721void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2722 ObjCInterfaceDecl *CurrentClass,
2723 ResultTypeCompatibilityKind RTC) {
2724 // Search for overridden methods and merge information down from them.
2725 OverrideSearch overrides(*this, ObjCMethod);
2726 // Keep track if the method overrides any method in the class's base classes,
2727 // its protocols, or its categories' protocols; we will keep that info
2728 // in the ObjCMethodDecl.
2729 // For this info, a method in an implementation is not considered as
2730 // overriding the same method in the interface or its categories.
2731 bool hasOverriddenMethodsInBaseOrProtocol = false;
2732 for (OverrideSearch::iterator
2733 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2734 ObjCMethodDecl *overridden = *i;
2735
2736 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2737 CurrentClass != overridden->getClassInterface() ||
2738 overridden->isOverriding())
2739 hasOverriddenMethodsInBaseOrProtocol = true;
2740
2741 // Propagate down the 'related result type' bit from overridden methods.
2742 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2743 ObjCMethod->SetRelatedResultType();
2744
2745 // Then merge the declarations.
2746 mergeObjCMethodDecls(ObjCMethod, overridden);
2747
2748 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2749 continue; // Conflicting properties are detected elsewhere.
2750
2751 // Check for overriding methods
2752 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2753 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2754 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2755 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2756
2757 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00002758 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
2759 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002760 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2761 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002762 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2763 PrevE = overridden->param_end();
2764 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002765 assert(PrevI != overridden->param_end() && "Param mismatch");
2766 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2767 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2768 // If type of argument of method in this class does not match its
2769 // respective argument type in the super class method, issue warning;
2770 if (!Context.typesAreCompatible(T1, T2)) {
2771 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2772 << T1 << T2;
2773 Diag(overridden->getLocation(), diag::note_previous_declaration);
2774 break;
2775 }
2776 }
2777 }
2778 }
2779
2780 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2781}
2782
John McCalld226f652010-08-21 09:40:31 +00002783Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002784 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002785 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002786 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002787 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002788 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002789 Selector Sel,
2790 // optional arguments. The number of types/arguments is obtained
2791 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002792 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002793 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002794 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002795 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002796 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002797 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002798 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002799 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002800 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002801 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2802 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002803 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002804
Douglas Gregore97179c2011-09-08 01:46:34 +00002805 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002806 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002807 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002808 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002809
Steve Naroffccef3712009-02-20 22:59:16 +00002810 // Methods cannot return interface types. All ObjC objects are
2811 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002812 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002813 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2814 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002815 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002816 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002817
2818 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002819 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002820 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002821 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002822 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002823 }
Mike Stump1eb44332009-09-09 15:08:12 +00002824
2825 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002826 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002827 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002828 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002829 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002830 MethodType == tok::minus, isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +00002831 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002832 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002833 MethodDeclKind == tok::objc_optional
2834 ? ObjCMethodDecl::Optional
2835 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002836 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002837
Chris Lattner5f9e2722011-07-23 10:55:15 +00002838 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002839
Chris Lattner7db638d2009-04-11 19:42:43 +00002840 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002841 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002842 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002843
Chris Lattnere294d3f2009-04-11 18:57:04 +00002844 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002845 ArgType = Context.getObjCIdType();
2846 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002847 } else {
John McCall58e46772009-10-23 21:48:59 +00002848 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002849 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002850 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002851 }
Mike Stump1eb44332009-09-09 15:08:12 +00002852
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002853 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2854 LookupOrdinaryName, ForRedeclaration);
2855 LookupName(R, S);
2856 if (R.isSingleResult()) {
2857 NamedDecl *PrevDecl = R.getFoundDecl();
2858 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002859 Diag(ArgInfo[i].NameLoc,
2860 (MethodDefinition ? diag::warn_method_param_redefinition
2861 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002862 << ArgInfo[i].Name;
2863 Diag(PrevDecl->getLocation(),
2864 diag::note_previous_declaration);
2865 }
2866 }
2867
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002868 SourceLocation StartLoc = DI
2869 ? DI->getTypeLoc().getBeginLoc()
2870 : ArgInfo[i].NameLoc;
2871
John McCall81ef3e62011-04-23 02:46:06 +00002872 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2873 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2874 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002875
John McCall70798862011-05-02 00:30:12 +00002876 Param->setObjCMethodScopeInfo(i);
2877
Chris Lattner0ed844b2008-04-04 06:12:32 +00002878 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002879 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002880
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002881 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002882 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002883
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002884 if (Param->hasAttr<BlocksAttr>()) {
2885 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2886 Param->setInvalidDecl();
2887 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002888 S->AddDecl(Param);
2889 IdResolver.AddDecl(Param);
2890
Chris Lattner0ed844b2008-04-04 06:12:32 +00002891 Params.push_back(Param);
2892 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002893
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002894 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002895 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002896 QualType ArgType = Param->getType();
2897 if (ArgType.isNull())
2898 ArgType = Context.getObjCIdType();
2899 else
2900 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002901 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002902 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002903 Diag(Param->getLocation(),
2904 diag::err_object_cannot_be_passed_returned_by_value)
2905 << 1 << ArgType;
2906 Param->setInvalidDecl();
2907 }
2908 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002909
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002910 Params.push_back(Param);
2911 }
2912
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002913 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002914 ObjCMethod->setObjCDeclQualifier(
2915 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002916
2917 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002918 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002919
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002920 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002921 const ObjCMethodDecl *PrevMethod = 0;
2922 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002923 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002924 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2925 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002926 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002927 PrevMethod = ImpDecl->getClassMethod(Sel);
2928 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002929 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002930
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002931 ObjCMethodDecl *IMD = 0;
2932 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2933 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2934 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002935 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002936 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002937 SourceLocation MethodLoc = IMD->getLocation();
2938 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2939 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002940 Diag(MethodLoc, diag::note_method_declared_at)
2941 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002942 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002943 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002944 } else {
2945 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002946 }
John McCall6c2c2502011-07-22 02:45:48 +00002947
Chris Lattner4d391482007-12-12 07:09:47 +00002948 if (PrevMethod) {
2949 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002950 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002951 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002952 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002953 }
John McCall54abf7d2009-11-04 02:18:39 +00002954
Douglas Gregor926df6c2011-06-11 01:09:30 +00002955 // If this Objective-C method does not have a related result type, but we
2956 // are allowed to infer related result types, try to do so based on the
2957 // method family.
2958 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2959 if (!CurrentClass) {
2960 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2961 CurrentClass = Cat->getClassInterface();
2962 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2963 CurrentClass = Impl->getClassInterface();
2964 else if (ObjCCategoryImplDecl *CatImpl
2965 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2966 CurrentClass = CatImpl->getClassInterface();
2967 }
John McCall6c2c2502011-07-22 02:45:48 +00002968
Douglas Gregore97179c2011-09-08 01:46:34 +00002969 ResultTypeCompatibilityKind RTC
2970 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002971
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002972 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00002973
John McCallf85e1932011-06-15 23:02:42 +00002974 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002975 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002976 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2977
Douglas Gregore97179c2011-09-08 01:46:34 +00002978 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002979 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00002980 !ObjCMethod->hasRelatedResultType() &&
2981 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002982 bool InferRelatedResultType = false;
2983 switch (ObjCMethod->getMethodFamily()) {
2984 case OMF_None:
2985 case OMF_copy:
2986 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002987 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002988 case OMF_mutableCopy:
2989 case OMF_release:
2990 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002991 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002992 break;
2993
2994 case OMF_alloc:
2995 case OMF_new:
2996 InferRelatedResultType = ObjCMethod->isClassMethod();
2997 break;
2998
2999 case OMF_init:
3000 case OMF_autorelease:
3001 case OMF_retain:
3002 case OMF_self:
3003 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3004 break;
3005 }
3006
John McCall6c2c2502011-07-22 02:45:48 +00003007 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003008 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003009 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003010
3011 ActOnDocumentableDecl(ObjCMethod);
3012
John McCalld226f652010-08-21 09:40:31 +00003013 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003014}
3015
Chris Lattnercc98eac2008-12-17 07:13:27 +00003016bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003017 // Following is also an error. But it is caused by a missing @end
3018 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003019 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003020 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003021
3022 // If we switched context to translation unit while we are still lexically in
3023 // an objc container, it means the parser missed emitting an error.
3024 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3025 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003026
Anders Carlsson15281452008-11-04 16:57:32 +00003027 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3028 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003029
Anders Carlsson15281452008-11-04 16:57:32 +00003030 return true;
3031}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003032
James Dennett1dfbd922012-06-14 21:40:34 +00003033/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003034/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003035void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003036 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003037 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003038 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003039 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003040 if (!Class) {
3041 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3042 return;
3043 }
John McCall260611a2012-06-20 06:18:46 +00003044 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003045 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3046 return;
3047 }
Mike Stump1eb44332009-09-09 15:08:12 +00003048
Chris Lattnercc98eac2008-12-17 07:13:27 +00003049 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003050 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003051 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003052 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003053 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003054 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003055 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003056 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3057 /*FIXME: StartL=*/ID->getLocation(),
3058 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003059 ID->getIdentifier(), ID->getType(),
3060 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003061 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003062 }
Mike Stump1eb44332009-09-09 15:08:12 +00003063
Chris Lattnercc98eac2008-12-17 07:13:27 +00003064 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003065 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003066 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003067 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003068 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003069 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003070 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003071 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003072 }
3073}
3074
Douglas Gregor160b5632010-04-26 17:32:49 +00003075/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003076VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3077 SourceLocation StartLoc,
3078 SourceLocation IdLoc,
3079 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003080 bool Invalid) {
3081 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3082 // duration shall not be qualified by an address-space qualifier."
3083 // Since all parameters have automatic store duration, they can not have
3084 // an address space.
3085 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003086 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003087 Invalid = true;
3088 }
3089
3090 // An @catch parameter must be an unqualified object pointer type;
3091 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3092 if (Invalid) {
3093 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003094 } else if (T->isDependentType()) {
3095 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003096 } else if (!T->isObjCObjectPointerType()) {
3097 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003098 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003099 } else if (T->isObjCQualifiedIdType()) {
3100 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003101 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003102 }
3103
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003104 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3105 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003106 New->setExceptionVariable(true);
3107
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003108 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003109 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003110 Invalid = true;
3111
Douglas Gregor160b5632010-04-26 17:32:49 +00003112 if (Invalid)
3113 New->setInvalidDecl();
3114 return New;
3115}
3116
John McCalld226f652010-08-21 09:40:31 +00003117Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003118 const DeclSpec &DS = D.getDeclSpec();
3119
3120 // We allow the "register" storage class on exception variables because
3121 // GCC did, but we drop it completely. Any other storage class is an error.
3122 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3123 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3124 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3125 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3126 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3127 << DS.getStorageClassSpec();
3128 }
3129 if (D.getDeclSpec().isThreadSpecified())
3130 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3131 D.getMutableDeclSpec().ClearStorageClassSpecs();
3132
3133 DiagnoseFunctionSpecifiers(D);
3134
3135 // Check that there are no default arguments inside the type of this
3136 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003137 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003138 CheckExtraCXXDefaultArguments(D);
3139
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003140 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003141 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003142
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003143 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3144 D.getSourceRange().getBegin(),
3145 D.getIdentifierLoc(),
3146 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003147 D.isInvalidType());
3148
3149 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3150 if (D.getCXXScopeSpec().isSet()) {
3151 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3152 << D.getCXXScopeSpec().getRange();
3153 New->setInvalidDecl();
3154 }
3155
3156 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003157 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003158 if (D.getIdentifier())
3159 IdResolver.AddDecl(New);
3160
3161 ProcessDeclAttributes(S, New, D);
3162
3163 if (New->hasAttr<BlocksAttr>())
3164 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003165 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003166}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003167
3168/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003169/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003170void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003171 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003172 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3173 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003174 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003175 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003176 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003177 }
3178}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003179
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003180void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003181 // Load referenced selectors from the external source.
3182 if (ExternalSource) {
3183 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3184 ExternalSource->ReadReferencedSelectors(Sels);
3185 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3186 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3187 }
3188
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003189 // Warning will be issued only when selector table is
3190 // generated (which means there is at lease one implementation
3191 // in the TU). This is to match gcc's behavior.
3192 if (ReferencedSelectors.empty() ||
3193 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003194 return;
3195 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3196 ReferencedSelectors.begin(),
3197 E = ReferencedSelectors.end(); S != E; ++S) {
3198 Selector Sel = (*S).first;
3199 if (!LookupImplementedMethodInGlobalPool(Sel))
3200 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3201 }
3202 return;
3203}