blob: d22d7a14f60e4a7fc4c2655aedddd1d944246f2c [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
John McCallf85e1932011-06-15 23:02:42 +000015#include "clang/AST/ASTConsumer.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000016#include "clang/AST/ASTContext.h"
17#include "clang/AST/ASTMutationListener.h"
18#include "clang/AST/DeclObjC.h"
Steve Naroffca331292009-03-03 14:49:36 +000019#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000020#include "clang/AST/ExprObjC.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/Basic/SourceManager.h"
Patrick Beardb2f68202012-04-06 18:12:22 +000022#include "clang/Lex/Preprocessor.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/DeclSpec.h"
24#include "clang/Sema/ExternalSemaSource.h"
25#include "clang/Sema/Lookup.h"
26#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
John McCall50df6ae2010-08-25 07:03:20 +000028#include "llvm/ADT/DenseSet.h"
29
Chris Lattner4d391482007-12-12 07:09:47 +000030using namespace clang;
31
John McCallf85e1932011-06-15 23:02:42 +000032/// Check whether the given method, which must be in the 'init'
33/// family, is a valid member of that family.
34///
35/// \param receiverTypeIfCall - if null, check this as if declaring it;
36/// if non-null, check this as if making a call to it with the given
37/// receiver type
38///
39/// \return true to indicate that there was an error and appropriate
40/// actions were taken
41bool Sema::checkInitMethod(ObjCMethodDecl *method,
42 QualType receiverTypeIfCall) {
43 if (method->isInvalidDecl()) return true;
44
45 // This castAs is safe: methods that don't return an object
46 // pointer won't be inferred as inits and will reject an explicit
47 // objc_method_family(init).
48
49 // We ignore protocols here. Should we? What about Class?
50
51 const ObjCObjectType *result = method->getResultType()
52 ->castAs<ObjCObjectPointerType>()->getObjectType();
53
54 if (result->isObjCId()) {
55 return false;
56 } else if (result->isObjCClass()) {
57 // fall through: always an error
58 } else {
59 ObjCInterfaceDecl *resultClass = result->getInterface();
60 assert(resultClass && "unexpected object type!");
61
62 // It's okay for the result type to still be a forward declaration
63 // if we're checking an interface declaration.
Douglas Gregor7723fec2011-12-15 20:29:51 +000064 if (!resultClass->hasDefinition()) {
John McCallf85e1932011-06-15 23:02:42 +000065 if (receiverTypeIfCall.isNull() &&
66 !isa<ObjCImplementationDecl>(method->getDeclContext()))
67 return false;
68
69 // Otherwise, we try to compare class types.
70 } else {
71 // If this method was declared in a protocol, we can't check
72 // anything unless we have a receiver type that's an interface.
73 const ObjCInterfaceDecl *receiverClass = 0;
74 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
75 if (receiverTypeIfCall.isNull())
76 return false;
77
78 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
79 ->getInterfaceDecl();
80
81 // This can be null for calls to e.g. id<Foo>.
82 if (!receiverClass) return false;
83 } else {
84 receiverClass = method->getClassInterface();
85 assert(receiverClass && "method not associated with a class!");
86 }
87
88 // If either class is a subclass of the other, it's fine.
89 if (receiverClass->isSuperClassOf(resultClass) ||
90 resultClass->isSuperClassOf(receiverClass))
91 return false;
92 }
93 }
94
95 SourceLocation loc = method->getLocation();
96
97 // If we're in a system header, and this is not a call, just make
98 // the method unusable.
99 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
100 method->addAttr(new (Context) UnavailableAttr(loc, Context,
101 "init method returns a type unrelated to its receiver type"));
102 return true;
103 }
104
105 // Otherwise, it's an error.
106 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
107 method->setInvalidDecl();
108 return true;
109}
110
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000111void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas 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
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000376 if (IMD) {
377 ObjCImplDecl *ImplDeclOfMethodDef =
378 dyn_cast<ObjCImplDecl>(MDecl->getDeclContext());
379 ObjCContainerDecl *ContDeclOfMethodDecl =
380 dyn_cast<ObjCContainerDecl>(IMD->getDeclContext());
381 ObjCImplDecl *ImplDeclOfMethodDecl = 0;
382 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
383 ImplDeclOfMethodDecl = OID->getImplementation();
384 else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl))
385 ImplDeclOfMethodDecl = CD->getImplementation();
386 // No need to issue deprecated warning if deprecated mehod in class/category
387 // is being implemented in its own implementation (no overriding is involved).
388 if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef)
389 DiagnoseObjCImplementedDeprecations(*this,
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000390 dyn_cast<NamedDecl>(IMD),
391 MDecl->getLocation(), 0);
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000392 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000393
Nico Weber80cb6e62011-08-28 22:35:17 +0000394 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000395 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
396 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
397 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000398 if (IC->getSuperClass()) {
Jordan Rose535a5d02012-10-19 16:05:26 +0000399 ObjCMethodFamily Family = MDecl->getMethodFamily();
400 if (Family == OMF_dealloc) {
401 if (!(getLangOpts().ObjCAutoRefCount ||
402 getLangOpts().getGC() == LangOptions::GCOnly))
403 getCurFunction()->ObjCShouldCallSuper = true;
404
405 } else if (Family == OMF_finalize) {
406 if (Context.getLangOpts().getGC() != LangOptions::NonGC)
407 getCurFunction()->ObjCShouldCallSuper = true;
408
409 } else {
410 const ObjCMethodDecl *SuperMethod =
411 IC->getSuperClass()->lookupMethod(MDecl->getSelector(),
412 MDecl->isInstanceMethod());
413 getCurFunction()->ObjCShouldCallSuper =
414 (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000415 }
Nico Weber80cb6e62011-08-28 22:35:17 +0000416 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000417 }
Chris Lattner4d391482007-12-12 07:09:47 +0000418}
419
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000420namespace {
421
422// Callback to only accept typo corrections that are Objective-C classes.
423// If an ObjCInterfaceDecl* is given to the constructor, then the validation
424// function will reject corrections to that class.
425class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
426 public:
427 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
428 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
429 : CurrentIDecl(IDecl) {}
430
431 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
432 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
433 return ID && !declaresSameEntity(ID, CurrentIDecl);
434 }
435
436 private:
437 ObjCInterfaceDecl *CurrentIDecl;
438};
439
440}
441
John McCalld226f652010-08-21 09:40:31 +0000442Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000443ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
444 IdentifierInfo *ClassName, SourceLocation ClassLoc,
445 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000446 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000447 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000448 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000449 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Chris Lattner4d391482007-12-12 07:09:47 +0000451 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000452 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000453 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000454
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000455 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000456 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000457 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Douglas Gregor7723fec2011-12-15 20:29:51 +0000460 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000461 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000462 ObjCInterfaceDecl *IDecl
463 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000464 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000465
Douglas Gregor7723fec2011-12-15 20:29:51 +0000466 if (PrevIDecl) {
467 // Class already seen. Was it a definition?
468 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
469 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
470 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000471 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000472 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000473 }
Chris Lattner4d391482007-12-12 07:09:47 +0000474 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000475
476 if (AttrList)
477 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
478 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregor7723fec2011-12-15 20:29:51 +0000480 // Start the definition of this class. If we're in a redefinition case, there
481 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000482 if (!IDecl->hasDefinition())
483 IDecl->startDefinition();
484
Chris Lattner4d391482007-12-12 07:09:47 +0000485 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000486 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000487 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
488 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000489
490 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000491 // Try to correct for a typo in the superclass name without correcting
492 // to the class we're defining.
493 ObjCInterfaceValidatorCCC Validator(IDecl);
494 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000495 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000496 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000497 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
498 Diag(SuperLoc, diag::err_undef_superclass_suggest)
499 << SuperName << ClassName << PrevDecl->getDeclName();
500 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
501 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000502 }
503 }
504
Douglas Gregor60ef3082011-12-15 00:29:59 +0000505 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000506 Diag(SuperLoc, diag::err_recursive_superclass)
507 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000508 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000509 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000510 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000511 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000512
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000513 // Diagnose classes that inherit from deprecated classes.
514 if (SuperClassDecl)
515 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000517 if (PrevDecl && SuperClassDecl == 0) {
518 // The previous declaration was not a class decl. Check if we have a
519 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000520 if (const TypedefNameDecl *TDecl =
521 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000522 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000523 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000524 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
525 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000526 }
527 }
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000529 // This handles the following case:
530 //
531 // typedef int SuperClass;
532 // @interface MyClass : SuperClass {} @end
533 //
534 if (!SuperClassDecl) {
535 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
536 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000537 }
538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Richard Smith162e1c12011-04-15 14:24:37 +0000540 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000541 if (!SuperClassDecl)
542 Diag(SuperLoc, diag::err_undef_superclass)
543 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000544 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000545 Context.getObjCInterfaceType(SuperClassDecl),
546 diag::err_forward_superclass,
547 SuperClassDecl->getDeclName(),
548 ClassName,
549 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000550 SuperClassDecl = 0;
551 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000552 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000553 IDecl->setSuperClass(SuperClassDecl);
554 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000555 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000556 }
Chris Lattner4d391482007-12-12 07:09:47 +0000557 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000558 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Sebastian Redl0b17c612010-08-13 00:28:03 +0000561 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000562 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000563 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000564 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000565 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Anders Carlsson15281452008-11-04 16:57:32 +0000568 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000569 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000570}
571
Richard Smithde01b7a2012-08-08 23:32:13 +0000572/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000573/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000574Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
575 IdentifierInfo *AliasName,
576 SourceLocation AliasLocation,
577 IdentifierInfo *ClassName,
578 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000579 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000580 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000581 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000582 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000583 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000584 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000585 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000586 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000587 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000588 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000589 }
590 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000591 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000592 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000593 if (const TypedefNameDecl *TDecl =
594 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000595 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000596 if (T->isObjCObjectType()) {
597 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000598 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000599 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000600 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000601 }
602 }
603 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000604 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
605 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000606 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000607 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000608 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000609 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000610 }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000612 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000613 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000614 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Anders Carlsson15281452008-11-04 16:57:32 +0000616 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000617 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000618
John McCalld226f652010-08-21 09:40:31 +0000619 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000620}
621
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000622bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000623 IdentifierInfo *PName,
624 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000625 const ObjCList<ObjCProtocolDecl> &PList) {
626
627 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000628 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
629 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000630 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
631 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000632 if (PDecl->getIdentifier() == PName) {
633 Diag(Ploc, diag::err_protocol_has_circular_dependency);
634 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000635 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000636 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000637
638 if (!PDecl->hasDefinition())
639 continue;
640
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000641 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
642 PDecl->getLocation(), PDecl->getReferencedProtocols()))
643 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000644 }
645 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000646 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000647}
648
John McCalld226f652010-08-21 09:40:31 +0000649Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000650Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
651 IdentifierInfo *ProtocolName,
652 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000653 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000654 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000655 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000656 SourceLocation EndProtoLoc,
657 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000658 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000659 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000660 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000661 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
662 ForRedeclaration);
663 ObjCProtocolDecl *PDecl = 0;
664 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
665 // If we already have a definition, complain.
666 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
667 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Douglas Gregor27c6da22012-01-01 20:30:41 +0000669 // Create a new protocol that is completely distinct from previous
670 // declarations, and do not make this protocol available for name lookup.
671 // That way, we'll end up completely ignoring the duplicate.
672 // FIXME: Can we turn this into an error?
673 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
674 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000675 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000676 PDecl->startDefinition();
677 } else {
678 if (PrevDecl) {
679 // Check for circular dependencies among protocol declarations. This can
680 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000681 ObjCList<ObjCProtocolDecl> PList;
682 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
683 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000684 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000685 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000686
687 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000688 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000689 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000690 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000691
Douglas Gregor6e378de2009-04-23 23:18:26 +0000692 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000693 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000694 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000695
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000696 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000697 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000698
699 // Merge attributes from previous declarations.
700 if (PrevDecl)
701 mergeDeclAttributes(PDecl, PrevDecl);
702
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000703 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000704 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000705 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000706 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000707 }
Mike Stump1eb44332009-09-09 15:08:12 +0000708
709 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000710 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000711}
712
713/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000714/// issues an error if they are not declared. It returns list of
715/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000716void
Chris Lattnere13b9592008-07-26 04:03:38 +0000717Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000718 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000719 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000720 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000721 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000722 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
723 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000724 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000725 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000726 TypoCorrection Corrected = CorrectTypo(
727 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000728 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000729 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000730 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000731 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000732 Diag(PDecl->getLocation(), diag::note_previous_decl)
733 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000734 }
735 }
736
737 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000738 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000739 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000740 continue;
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000743 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000744
745 // If this is a forward declaration and we are supposed to warn in this
746 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000747 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000748 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000749 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000750 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000751 }
752}
753
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000754/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000755/// a class method in its extension.
756///
Mike Stump1eb44332009-09-09 15:08:12 +0000757void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000758 ObjCInterfaceDecl *ID) {
759 if (!ID)
760 return; // Possibly due to previous error
761
762 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000763 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
764 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000765 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000766 MethodMap[MD->getSelector()] = MD;
767 }
768
769 if (MethodMap.empty())
770 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000771 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
772 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000773 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000774 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
775 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
776 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
777 << Method->getDeclName();
778 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
779 }
780 }
781}
782
James Dennett1dfbd922012-06-14 21:40:34 +0000783/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000784Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000785Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000786 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000787 unsigned NumElts,
788 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000789 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000790 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000791 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000792 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
793 ForRedeclaration);
794 ObjCProtocolDecl *PDecl
795 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
796 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000797 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000798
799 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000800 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000801
Douglas Gregor3937f872012-01-01 20:33:24 +0000802 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000803 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000804
805 if (PrevDecl)
806 mergeDeclAttributes(PDecl, PrevDecl);
807
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000808 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000809 }
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000811 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000812}
813
John McCalld226f652010-08-21 09:40:31 +0000814Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000815ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
816 IdentifierInfo *ClassName, SourceLocation ClassLoc,
817 IdentifierInfo *CategoryName,
818 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000819 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000820 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000821 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000822 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000823 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000824 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000825
826 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000827
828 if (!IDecl
829 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000830 diag::err_category_forward_interface,
831 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000832 // Create an invalid ObjCCategoryDecl to serve as context for
833 // the enclosing method declarations. We mark the decl invalid
834 // to make it clear that this isn't a valid AST.
835 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000836 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000837 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000838 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000839
840 if (!IDecl)
841 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000842 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000843 }
844
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000845 if (!CategoryName && IDecl->getImplementation()) {
846 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
847 Diag(IDecl->getImplementation()->getLocation(),
848 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000849 }
850
Fariborz Jahanian25760612010-02-15 21:55:26 +0000851 if (CategoryName) {
852 /// Check for duplicate interface declaration for this category
853 ObjCCategoryDecl *CDeclChain;
854 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
855 CDeclChain = CDeclChain->getNextClassCategory()) {
856 if (CDeclChain->getIdentifier() == CategoryName) {
857 // Class extensions can be declared multiple times.
858 Diag(CategoryLoc, diag::warn_dup_category_def)
859 << ClassName << CategoryName;
860 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
861 break;
862 }
Chris Lattner70f19542009-02-16 21:26:43 +0000863 }
864 }
Chris Lattner70f19542009-02-16 21:26:43 +0000865
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000866 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
867 ClassLoc, CategoryLoc, CategoryName, IDecl);
868 // FIXME: PushOnScopeChains?
869 CurContext->addDecl(CDecl);
870
Chris Lattner4d391482007-12-12 07:09:47 +0000871 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000872 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000873 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000874 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000875 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000876 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000877 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Anders Carlsson15281452008-11-04 16:57:32 +0000880 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000881 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000882}
883
884/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000885/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000886/// object.
John McCalld226f652010-08-21 09:40:31 +0000887Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000888 SourceLocation AtCatImplLoc,
889 IdentifierInfo *ClassName, SourceLocation ClassLoc,
890 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000891 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000892 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000893 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000894 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
895 if (!CatIDecl) {
896 // Category @implementation with no corresponding @interface.
897 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000898 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
899 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000900 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000901 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000902 }
903 }
904
Mike Stump1eb44332009-09-09 15:08:12 +0000905 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000906 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000907 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000908 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000909 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000910 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000911 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000912 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
913 diag::err_undef_interface)) {
914 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000915 }
Chris Lattner4d391482007-12-12 07:09:47 +0000916
Douglas Gregord0434102009-01-09 00:49:46 +0000917 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000918 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000919
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000920 // If the interface is deprecated/unavailable, warn/error about it.
921 if (IDecl)
922 DiagnoseUseOfDecl(IDecl, ClassLoc);
923
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000924 /// Check that CatName, category name, is not used in another implementation.
925 if (CatIDecl) {
926 if (CatIDecl->getImplementation()) {
927 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
928 << CatName;
929 Diag(CatIDecl->getImplementation()->getLocation(),
930 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000931 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000932 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000933 // Warn on implementating category of deprecated class under
934 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000935 DiagnoseObjCImplementedDeprecations(*this,
936 dyn_cast<NamedDecl>(IDecl),
937 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000938 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000939 }
Mike Stump1eb44332009-09-09 15:08:12 +0000940
Anders Carlsson15281452008-11-04 16:57:32 +0000941 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000942 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000943}
944
John McCalld226f652010-08-21 09:40:31 +0000945Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000946 SourceLocation AtClassImplLoc,
947 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000948 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000949 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000950 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000951 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000952 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000953 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
954 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000955 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000956 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000957 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000958 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000959 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
960 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000961 } else {
962 // We did not find anything with the name ClassName; try to correct for
963 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000964 ObjCInterfaceValidatorCCC Validator;
965 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000966 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000967 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000968 // Suggest the (potentially) correct interface name. However, put the
969 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000970 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000971 // provide a code-modification hint or use the typo name for recovery,
972 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000973 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000974 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000975 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000976 << ClassName << CorrectedName;
977 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
978 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000979 IDecl = 0;
980 } else {
981 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
982 }
Chris Lattner4d391482007-12-12 07:09:47 +0000983 }
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Chris Lattner4d391482007-12-12 07:09:47 +0000985 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000987 if (SuperClassname) {
988 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000989 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
990 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000991 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000992 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
993 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000994 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000995 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000996 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000997 if (SDecl && !SDecl->hasDefinition())
998 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000999 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +00001000 Diag(SuperClassLoc, diag::err_undef_superclass)
1001 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +00001002 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001003 // This implementation and its interface do not have the same
1004 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +00001005 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +00001006 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001007 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001008 }
1009 }
1010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Chris Lattner4d391482007-12-12 07:09:47 +00001012 if (!IDecl) {
1013 // Legacy case of @implementation with no corresponding @interface.
1014 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +00001015
Mike Stump390b4cc2009-05-16 07:39:55 +00001016 // FIXME: Do we support attributes on the @implementation? If so we should
1017 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001018 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001019 ClassName, /*PrevDecl=*/0, ClassLoc,
1020 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001021 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001022 if (SDecl) {
1023 IDecl->setSuperClass(SDecl);
1024 IDecl->setSuperClassLoc(SuperClassLoc);
1025 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1026 } else {
1027 IDecl->setEndOfDefinitionLoc(ClassLoc);
1028 }
1029
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001030 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001031 } else {
1032 // Mark the interface as being completed, even if it was just as
1033 // @class ....;
1034 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001035 if (!IDecl->hasDefinition())
1036 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001037 }
Mike Stump1eb44332009-09-09 15:08:12 +00001038
1039 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001040 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1041 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Anders Carlsson15281452008-11-04 16:57:32 +00001043 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001044 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Chris Lattner4d391482007-12-12 07:09:47 +00001046 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001047 if (IDecl->getImplementation()) {
1048 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001049 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001050 Diag(IDecl->getImplementation()->getLocation(),
1051 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001052 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001053 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001054 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001055 // Warn on implementating deprecated class under
1056 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001057 DiagnoseObjCImplementedDeprecations(*this,
1058 dyn_cast<NamedDecl>(IDecl),
1059 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001060 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001061 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001062}
1063
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001064Sema::DeclGroupPtrTy
1065Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1066 SmallVector<Decl *, 64> DeclsInGroup;
1067 DeclsInGroup.reserve(Decls.size() + 1);
1068
1069 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1070 Decl *Dcl = Decls[i];
1071 if (!Dcl)
1072 continue;
1073 if (Dcl->getDeclContext()->isFileContext())
1074 Dcl->setTopLevelDeclInObjCContainer();
1075 DeclsInGroup.push_back(Dcl);
1076 }
1077
1078 DeclsInGroup.push_back(ObjCImpDecl);
1079
1080 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1081}
1082
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001083void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1084 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001085 SourceLocation RBrace) {
1086 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001087 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001088 if (!IDecl)
1089 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001090 /// Check case of non-existing \@interface decl.
1091 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001092 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001093 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001094 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001095 // Add ivar's to class's DeclContext.
1096 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001097 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001098 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001099 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001100 }
1101
Chris Lattner4d391482007-12-12 07:09:47 +00001102 return;
1103 }
1104 // If implementation has empty ivar list, just return.
1105 if (numIvars == 0)
1106 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Chris Lattner4d391482007-12-12 07:09:47 +00001108 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001109 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001110 if (ImpDecl->getSuperClass())
1111 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1112 for (unsigned i = 0; i < numIvars; i++) {
1113 ObjCIvarDecl* ImplIvar = ivars[i];
1114 if (const ObjCIvarDecl *ClsIvar =
1115 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1116 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1117 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1118 continue;
1119 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001120 // Instance ivar to Implementation's DeclContext.
1121 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001122 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001123 ImpDecl->addDecl(ImplIvar);
1124 }
1125 return;
1126 }
Chris Lattner4d391482007-12-12 07:09:47 +00001127 // Check interface's Ivar list against those in the implementation.
1128 // names and types must match.
1129 //
Chris Lattner4d391482007-12-12 07:09:47 +00001130 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001131 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001132 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1133 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001134 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001135 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001136 assert (ImplIvar && "missing implementation ivar");
1137 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001138
Steve Naroffca331292009-03-03 14:49:36 +00001139 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001140 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001141 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001142 << ImplIvar->getIdentifier()
1143 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001144 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001145 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1146 ImplIvar->getBitWidthValue(Context) !=
1147 ClsIvar->getBitWidthValue(Context)) {
1148 Diag(ImplIvar->getBitWidth()->getLocStart(),
1149 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1150 Diag(ClsIvar->getBitWidth()->getLocStart(),
1151 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001152 }
Steve Naroffca331292009-03-03 14:49:36 +00001153 // Make sure the names are identical.
1154 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001155 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001156 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001157 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001158 }
1159 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001160 }
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Chris Lattner609e4c72007-12-12 18:11:49 +00001162 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001163 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001164 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001165 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001166}
1167
Steve Naroff3c2eb662008-02-10 21:38:56 +00001168void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001169 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001170 // No point warning no definition of method which is 'unavailable'.
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001171 switch (method->getAvailability()) {
1172 case AR_Available:
1173 case AR_Deprecated:
1174 break;
1175
1176 // Don't warn about unavailable or not-yet-introduced methods.
1177 case AR_NotYetIntroduced:
1178 case AR_Unavailable:
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001179 return;
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001180 }
1181
Steve Naroff3c2eb662008-02-10 21:38:56 +00001182 if (!IncompleteImpl) {
1183 Diag(ImpLoc, diag::warn_incomplete_impl);
1184 IncompleteImpl = true;
1185 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001186 if (DiagID == diag::warn_unimplemented_protocol_method)
1187 Diag(ImpLoc, DiagID) << method->getDeclName();
1188 else
1189 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001190}
1191
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001192/// Determines if type B can be substituted for type A. Returns true if we can
1193/// guarantee that anything that the user will do to an object of type A can
1194/// also be done to an object of type B. This is trivially true if the two
1195/// types are the same, or if B is a subclass of A. It becomes more complex
1196/// in cases where protocols are involved.
1197///
1198/// Object types in Objective-C describe the minimum requirements for an
1199/// object, rather than providing a complete description of a type. For
1200/// example, if A is a subclass of B, then B* may refer to an instance of A.
1201/// The principle of substitutability means that we may use an instance of A
1202/// anywhere that we may use an instance of B - it will implement all of the
1203/// ivars of B and all of the methods of B.
1204///
1205/// This substitutability is important when type checking methods, because
1206/// the implementation may have stricter type definitions than the interface.
1207/// The interface specifies minimum requirements, but the implementation may
1208/// have more accurate ones. For example, a method may privately accept
1209/// instances of B, but only publish that it accepts instances of A. Any
1210/// object passed to it will be type checked against B, and so will implicitly
1211/// by a valid A*. Similarly, a method may return a subclass of the class that
1212/// it is declared as returning.
1213///
1214/// This is most important when considering subclassing. A method in a
1215/// subclass must accept any object as an argument that its superclass's
1216/// implementation accepts. It may, however, accept a more general type
1217/// without breaking substitutability (i.e. you can still use the subclass
1218/// anywhere that you can use the superclass, but not vice versa). The
1219/// converse requirement applies to return types: the return type for a
1220/// subclass method must be a valid object of the kind that the superclass
1221/// advertises, but it may be specified more accurately. This avoids the need
1222/// for explicit down-casting by callers.
1223///
1224/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001225static bool isObjCTypeSubstitutable(ASTContext &Context,
1226 const ObjCObjectPointerType *A,
1227 const ObjCObjectPointerType *B,
1228 bool rejectId) {
1229 // Reject a protocol-unqualified id.
1230 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001231
1232 // If B is a qualified id, then A must also be a qualified id and it must
1233 // implement all of the protocols in B. It may not be a qualified class.
1234 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1235 // stricter definition so it is not substitutable for id<A>.
1236 if (B->isObjCQualifiedIdType()) {
1237 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001238 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1239 QualType(B,0),
1240 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001241 }
1242
1243 /*
1244 // id is a special type that bypasses type checking completely. We want a
1245 // warning when it is used in one place but not another.
1246 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1247
1248
1249 // If B is a qualified id, then A must also be a qualified id (which it isn't
1250 // if we've got this far)
1251 if (B->isObjCQualifiedIdType()) return false;
1252 */
1253
1254 // Now we know that A and B are (potentially-qualified) class types. The
1255 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001256 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001257}
1258
John McCall10302c02010-10-28 02:34:38 +00001259static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1260 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1261}
1262
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001263static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001264 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001265 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001266 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001267 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001268 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001269 if (IsProtocolMethodDecl &&
1270 (MethodDecl->getObjCDeclQualifier() !=
1271 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001272 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001273 S.Diag(MethodImpl->getLocation(),
1274 (IsOverridingMode ?
1275 diag::warn_conflicting_overriding_ret_type_modifiers
1276 : diag::warn_conflicting_ret_type_modifiers))
1277 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001278 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1279 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1280 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1281 }
1282 else
1283 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001284 }
1285
John McCall10302c02010-10-28 02:34:38 +00001286 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001287 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001288 return true;
1289 if (!Warn)
1290 return false;
John McCall10302c02010-10-28 02:34:38 +00001291
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001292 unsigned DiagID =
1293 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1294 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001295
1296 // Mismatches between ObjC pointers go into a different warning
1297 // category, and sometimes they're even completely whitelisted.
1298 if (const ObjCObjectPointerType *ImplPtrTy =
1299 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1300 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001301 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001302 // Allow non-matching return types as long as they don't violate
1303 // the principle of substitutability. Specifically, we permit
1304 // return types that are subclasses of the declared return type,
1305 // or that are more-qualified versions of the declared type.
1306 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001307 return false;
John McCall10302c02010-10-28 02:34:38 +00001308
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001309 DiagID =
1310 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1311 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001312 }
1313 }
1314
1315 S.Diag(MethodImpl->getLocation(), DiagID)
1316 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001317 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001318 << MethodImpl->getResultType()
1319 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001320 S.Diag(MethodDecl->getLocation(),
1321 IsOverridingMode ? diag::note_previous_declaration
1322 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001323 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001324 return false;
John McCall10302c02010-10-28 02:34:38 +00001325}
1326
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001327static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001328 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001329 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001330 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001331 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001332 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001333 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001334 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001335 if (IsProtocolMethodDecl &&
1336 (ImplVar->getObjCDeclQualifier() !=
1337 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001338 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001339 if (IsOverridingMode)
1340 S.Diag(ImplVar->getLocation(),
1341 diag::warn_conflicting_overriding_param_modifiers)
1342 << getTypeRange(ImplVar->getTypeSourceInfo())
1343 << MethodImpl->getDeclName();
1344 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001345 diag::warn_conflicting_param_modifiers)
1346 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001347 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001348 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1349 << getTypeRange(IfaceVar->getTypeSourceInfo());
1350 }
1351 else
1352 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001353 }
1354
John McCall10302c02010-10-28 02:34:38 +00001355 QualType ImplTy = ImplVar->getType();
1356 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001357
John McCall10302c02010-10-28 02:34:38 +00001358 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001359 return true;
1360
1361 if (!Warn)
1362 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001363 unsigned DiagID =
1364 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1365 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001366
1367 // Mismatches between ObjC pointers go into a different warning
1368 // category, and sometimes they're even completely whitelisted.
1369 if (const ObjCObjectPointerType *ImplPtrTy =
1370 ImplTy->getAs<ObjCObjectPointerType>()) {
1371 if (const ObjCObjectPointerType *IfacePtrTy =
1372 IfaceTy->getAs<ObjCObjectPointerType>()) {
1373 // Allow non-matching argument types as long as they don't
1374 // violate the principle of substitutability. Specifically, the
1375 // implementation must accept any objects that the superclass
1376 // accepts, however it may also accept others.
1377 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001378 return false;
John McCall10302c02010-10-28 02:34:38 +00001379
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001380 DiagID =
1381 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1382 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001383 }
1384 }
1385
1386 S.Diag(ImplVar->getLocation(), DiagID)
1387 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001388 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1389 S.Diag(IfaceVar->getLocation(),
1390 (IsOverridingMode ? diag::note_previous_declaration
1391 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001392 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001393 return false;
John McCall10302c02010-10-28 02:34:38 +00001394}
John McCallf85e1932011-06-15 23:02:42 +00001395
1396/// In ARC, check whether the conventional meanings of the two methods
1397/// match. If they don't, it's a hard error.
1398static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1399 ObjCMethodDecl *decl) {
1400 ObjCMethodFamily implFamily = impl->getMethodFamily();
1401 ObjCMethodFamily declFamily = decl->getMethodFamily();
1402 if (implFamily == declFamily) return false;
1403
1404 // Since conventions are sorted by selector, the only possibility is
1405 // that the types differ enough to cause one selector or the other
1406 // to fall out of the family.
1407 assert(implFamily == OMF_None || declFamily == OMF_None);
1408
1409 // No further diagnostics required on invalid declarations.
1410 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1411
1412 const ObjCMethodDecl *unmatched = impl;
1413 ObjCMethodFamily family = declFamily;
1414 unsigned errorID = diag::err_arc_lost_method_convention;
1415 unsigned noteID = diag::note_arc_lost_method_convention;
1416 if (declFamily == OMF_None) {
1417 unmatched = decl;
1418 family = implFamily;
1419 errorID = diag::err_arc_gained_method_convention;
1420 noteID = diag::note_arc_gained_method_convention;
1421 }
1422
1423 // Indexes into a %select clause in the diagnostic.
1424 enum FamilySelector {
1425 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1426 };
1427 FamilySelector familySelector = FamilySelector();
1428
1429 switch (family) {
1430 case OMF_None: llvm_unreachable("logic error, no method convention");
1431 case OMF_retain:
1432 case OMF_release:
1433 case OMF_autorelease:
1434 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001435 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001436 case OMF_retainCount:
1437 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001438 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001439 // Mismatches for these methods don't change ownership
1440 // conventions, so we don't care.
1441 return false;
1442
1443 case OMF_init: familySelector = F_init; break;
1444 case OMF_alloc: familySelector = F_alloc; break;
1445 case OMF_copy: familySelector = F_copy; break;
1446 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1447 case OMF_new: familySelector = F_new; break;
1448 }
1449
1450 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1451 ReasonSelector reasonSelector;
1452
1453 // The only reason these methods don't fall within their families is
1454 // due to unusual result types.
1455 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1456 reasonSelector = R_UnrelatedReturn;
1457 } else {
1458 reasonSelector = R_NonObjectReturn;
1459 }
1460
1461 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1462 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1463
1464 return true;
1465}
John McCall10302c02010-10-28 02:34:38 +00001466
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001467void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001468 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001469 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001470 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001471 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1472 return;
1473
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001474 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001475 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001476 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001477
Chris Lattner3aff9192009-04-11 19:58:42 +00001478 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001479 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1480 EF = MethodDecl->param_end();
1481 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001482 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001483 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001484 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001485
Fariborz Jahanian21121902011-08-08 18:03:17 +00001486 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001487 Diag(ImpMethodDecl->getLocation(),
1488 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001489 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001490 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001491}
1492
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001493void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1494 ObjCMethodDecl *Overridden,
1495 bool IsProtocolMethodDecl) {
1496
1497 CheckMethodOverrideReturn(*this, Method, Overridden,
1498 IsProtocolMethodDecl, true,
1499 true);
1500
1501 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001502 IF = Overridden->param_begin(), EM = Method->param_end(),
1503 EF = Overridden->param_end();
1504 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001505 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1506 IsProtocolMethodDecl, true, true);
1507 }
1508
1509 if (Method->isVariadic() != Overridden->isVariadic()) {
1510 Diag(Method->getLocation(),
1511 diag::warn_conflicting_overriding_variadic);
1512 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1513 }
1514}
1515
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001516/// WarnExactTypedMethods - This routine issues a warning if method
1517/// implementation declaration matches exactly that of its declaration.
1518void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1519 ObjCMethodDecl *MethodDecl,
1520 bool IsProtocolMethodDecl) {
1521 // don't issue warning when protocol method is optional because primary
1522 // class is not required to implement it and it is safe for protocol
1523 // to implement it.
1524 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1525 return;
1526 // don't issue warning when primary class's method is
1527 // depecated/unavailable.
1528 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1529 MethodDecl->hasAttr<DeprecatedAttr>())
1530 return;
1531
1532 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1533 IsProtocolMethodDecl, false, false);
1534 if (match)
1535 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001536 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1537 EF = MethodDecl->param_end();
1538 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001539 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1540 *IM, *IF,
1541 IsProtocolMethodDecl, false, false);
1542 if (!match)
1543 break;
1544 }
1545 if (match)
1546 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001547 if (match)
1548 match = !(MethodDecl->isClassMethod() &&
1549 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001550
1551 if (match) {
1552 Diag(ImpMethodDecl->getLocation(),
1553 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001554 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1555 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001556 }
1557}
1558
Mike Stump390b4cc2009-05-16 07:39:55 +00001559/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1560/// improve the efficiency of selector lookups and type checking by associating
1561/// with each protocol / interface / category the flattened instance tables. If
1562/// we used an immutable set to keep the table then it wouldn't add significant
1563/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001564
Steve Naroffefe7f362008-02-08 22:06:17 +00001565/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001566/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001567void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1568 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001569 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001570 const SelectorSet &InsMap,
1571 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001572 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001573 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1574 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1575 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001576 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1577
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001578 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001579 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001580 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001581 // check to see if class implements forwardInvocation method and objects
1582 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001583 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001584 // Under such conditions, which means that every method possible is
1585 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001586 // found" warnings.
1587 // FIXME: Use a general GetUnarySelector method for this.
1588 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1589 Selector fISelector = Context.Selectors.getSelector(1, &II);
1590 if (InsMap.count(fISelector))
1591 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1592 // need be implemented in the implementation.
1593 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1594 }
Mike Stump1eb44332009-09-09 15:08:12 +00001595
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001596 // If this is a forward protocol declaration, get its definition.
1597 if (!PDecl->isThisDeclarationADefinition() &&
1598 PDecl->getDefinition())
1599 PDecl = PDecl->getDefinition();
1600
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001601 // If a method lookup fails locally we still need to look and see if
1602 // the method was implemented by a base class or an inherited
1603 // protocol. This lookup is slow, but occurs rarely in correct code
1604 // and otherwise would terminate in a warning.
1605
Chris Lattner4d391482007-12-12 07:09:47 +00001606 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001607 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001608 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001609 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001610 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001611 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001612 !method->isPropertyAccessor() &&
1613 !InsMap.count(method->getSelector()) &&
1614 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001615 // If a method is not implemented in the category implementation but
1616 // has been declared in its primary class, superclass,
1617 // or in one of their protocols, no need to issue the warning.
1618 // This is because method will be implemented in the primary class
1619 // or one of its super class implementation.
1620
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001621 // Ugly, but necessary. Method declared in protcol might have
1622 // have been synthesized due to a property declared in the class which
1623 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001624 if (ObjCMethodDecl *MethodInClass =
1625 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001626 true /*shallowCategoryLookup*/))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001627 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001628 continue;
1629 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1630 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1631 != DiagnosticsEngine::Ignored) {
1632 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001633 Diag(method->getLocation(), diag::note_method_declared_at)
1634 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001635 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1636 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001637 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001638 }
1639 }
Chris Lattner4d391482007-12-12 07:09:47 +00001640 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001641 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001642 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001643 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001644 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001645 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1646 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001647 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001648 // See above comment for instance method lookups.
1649 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001650 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001651 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001652 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001653 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1654 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001655 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001656 Diag(method->getLocation(), diag::note_method_declared_at)
1657 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001658 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1659 PDecl->getDeclName();
1660 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001661 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001662 }
Chris Lattner780f3292008-07-21 21:32:27 +00001663 // Check on this protocols's referenced protocols, recursively.
1664 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1665 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001666 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001667}
1668
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001669/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001670/// or protocol against those declared in their implementations.
1671///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001672void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1673 const SelectorSet &ClsMap,
1674 SelectorSet &InsMapSeen,
1675 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001676 ObjCImplDecl* IMPDecl,
1677 ObjCContainerDecl* CDecl,
1678 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001679 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001680 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001681 // Check and see if instance methods in class interface have been
1682 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001683 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1684 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001685 if (InsMapSeen.count((*I)->getSelector()))
1686 continue;
1687 InsMapSeen.insert((*I)->getSelector());
Jordan Rose1e4691b2012-10-10 16:42:25 +00001688 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001689 !InsMap.count((*I)->getSelector())) {
1690 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001691 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1692 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001693 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001694 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001695 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001696 IMPDecl->getInstanceMethod((*I)->getSelector());
1697 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1698 "Expected to find the method through lookup as well");
1699 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001700 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001701 if (ImpMethodDecl) {
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));
Jordan Rose1e4691b2012-10-10 16:42:25 +00001705 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001706 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001707 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001708 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001709 }
1710 }
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001712 // Check and see if class methods in class interface have been
1713 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001714 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001715 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001716 if (ClsMapSeen.count((*I)->getSelector()))
1717 continue;
1718 ClsMapSeen.insert((*I)->getSelector());
1719 if (!ClsMap.count((*I)->getSelector())) {
1720 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001721 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1722 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001723 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001724 ObjCMethodDecl *ImpMethodDecl =
1725 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001726 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1727 "Expected to find the method through lookup as well");
1728 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001729 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001730 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1731 isa<ObjCProtocolDecl>(CDecl));
1732 else
1733 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001734 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001735 }
1736 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001737
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001738 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001739 // when checking that methods in implementation match their declaration,
1740 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1741 // extension; as well as those in categories.
1742 if (!WarnCategoryMethodImpl)
1743 for (const ObjCCategoryDecl *CDeclChain = I->getCategoryList();
1744 CDeclChain; CDeclChain = CDeclChain->getNextClassCategory())
1745 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1746 IMPDecl,
1747 const_cast<ObjCCategoryDecl *>(CDeclChain),
1748 IncompleteImpl, false,
1749 WarnCategoryMethodImpl);
1750 else
1751 // Also methods in class extensions need be looked at next.
1752 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1753 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1754 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1755 IMPDecl,
1756 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
1757 IncompleteImpl, false,
1758 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001759
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001760 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001761 for (ObjCInterfaceDecl::all_protocol_iterator
1762 PI = I->all_referenced_protocol_begin(),
1763 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001764 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1765 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001766 (*PI), IncompleteImpl, false,
1767 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001768
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001769 // FIXME. For now, we are not checking for extact match of methods
1770 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001771 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001772 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001773 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001774 I->getSuperClass(), IncompleteImpl, false);
1775 }
1776}
1777
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001778/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1779/// category matches with those implemented in its primary class and
1780/// warns each time an exact match is found.
1781void Sema::CheckCategoryVsClassMethodMatches(
1782 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001783 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001784
1785 for (ObjCImplementationDecl::instmeth_iterator
1786 I = CatIMPDecl->instmeth_begin(),
1787 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1788 InsMap.insert((*I)->getSelector());
1789
1790 for (ObjCImplementationDecl::classmeth_iterator
1791 I = CatIMPDecl->classmeth_begin(),
1792 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1793 ClsMap.insert((*I)->getSelector());
1794 if (InsMap.empty() && ClsMap.empty())
1795 return;
1796
1797 // Get category's primary class.
1798 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1799 if (!CatDecl)
1800 return;
1801 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1802 if (!IDecl)
1803 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001804 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001805 bool IncompleteImpl = false;
1806 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1807 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001808 IncompleteImpl, false,
1809 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001810}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001811
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001812void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001813 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001814 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001815 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001816 // Check and see if instance methods in class interface have been
1817 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001818 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001819 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001820 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001821
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001822 // Check and see if properties declared in the interface have either 1)
1823 // an implementation or 2) there is a @synthesize/@dynamic implementation
1824 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001825 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001826 if (!(LangOpts.ObjCDefaultSynthProperties &&
1827 LangOpts.ObjCRuntime.isNonFragile()) ||
1828 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001829 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001830
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001831 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001832 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001833 I = IMPDecl->classmeth_begin(),
1834 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001835 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001837 // Check for type conflict of methods declared in a class/protocol and
1838 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001839 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001840 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1841 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001842 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001843
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001844 // check all methods implemented in category against those declared
1845 // in its primary class.
1846 if (ObjCCategoryImplDecl *CatDecl =
1847 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1848 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001849
Chris Lattner4d391482007-12-12 07:09:47 +00001850 // Check the protocol list for unimplemented methods in the @implementation
1851 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001852 // Check and see if class methods in class interface have been
1853 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Chris Lattnercddc8882009-03-01 00:56:52 +00001855 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001856 for (ObjCInterfaceDecl::all_protocol_iterator
1857 PI = I->all_referenced_protocol_begin(),
1858 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001859 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001860 InsMap, ClsMap, I);
1861 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001862 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1863 Categories; Categories = Categories->getNextClassExtension())
1864 ImplMethodsVsClassMethods(S, IMPDecl,
1865 const_cast<ObjCCategoryDecl*>(Categories),
1866 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001867 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001868 // For extended class, unimplemented methods in its protocols will
1869 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001870 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001871 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1872 E = C->protocol_end(); PI != E; ++PI)
1873 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001874 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001875 // Report unimplemented properties in the category as well.
1876 // When reporting on missing setter/getters, do not report when
1877 // setter/getter is implemented in category's primary class
1878 // implementation.
1879 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1880 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1881 for (ObjCImplementationDecl::instmeth_iterator
1882 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1883 InsMap.insert((*I)->getSelector());
1884 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001885 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001886 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001887 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001888 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001889}
1890
Mike Stump1eb44332009-09-09 15:08:12 +00001891/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001892Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001893Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001894 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001895 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001896 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001897 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001898 for (unsigned i = 0; i != NumElts; ++i) {
1899 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001900 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001901 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001902 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001903 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001904 // Maybe we will complain about the shadowed template parameter.
1905 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1906 // Just pretend that we didn't see the previous declaration.
1907 PrevDecl = 0;
1908 }
1909
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001910 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001911 // GCC apparently allows the following idiom:
1912 //
1913 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1914 // @class XCElementToggler;
1915 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001916 // Here we have chosen to ignore the forward class declaration
1917 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001918 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001919 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001920 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001921 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001922 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001923 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001924 // to the underlying class. Just ignore the forward class with a warning
1925 // as this will force the intended behavior which is to lookup the typedef
1926 // name.
1927 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1928 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1929 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1930 continue;
1931 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001932 }
Chris Lattner4d391482007-12-12 07:09:47 +00001933 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001934
1935 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001936 ObjCInterfaceDecl *PrevIDecl
1937 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001938 ObjCInterfaceDecl *IDecl
1939 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001940 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001941 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001942
Douglas Gregor7723fec2011-12-15 20:29:51 +00001943 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001944 CheckObjCDeclScope(IDecl);
1945 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001946 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001947
1948 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001949}
1950
John McCall0f4c4c42011-06-16 01:15:19 +00001951static bool tryMatchRecordTypes(ASTContext &Context,
1952 Sema::MethodMatchStrategy strategy,
1953 const Type *left, const Type *right);
1954
John McCallf85e1932011-06-15 23:02:42 +00001955static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1956 QualType leftQT, QualType rightQT) {
1957 const Type *left =
1958 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1959 const Type *right =
1960 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1961
1962 if (left == right) return true;
1963
1964 // If we're doing a strict match, the types have to match exactly.
1965 if (strategy == Sema::MMS_strict) return false;
1966
1967 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1968
1969 // Otherwise, use this absurdly complicated algorithm to try to
1970 // validate the basic, low-level compatibility of the two types.
1971
1972 // As a minimum, require the sizes and alignments to match.
1973 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1974 return false;
1975
1976 // Consider all the kinds of non-dependent canonical types:
1977 // - functions and arrays aren't possible as return and parameter types
1978
1979 // - vector types of equal size can be arbitrarily mixed
1980 if (isa<VectorType>(left)) return isa<VectorType>(right);
1981 if (isa<VectorType>(right)) return false;
1982
1983 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001984 // - structs, unions, and Objective-C objects must match more-or-less
1985 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001986 // - everything else should be a scalar
1987 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001988 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001989
John McCall1d9b3b22011-09-09 05:25:32 +00001990 // Make scalars agree in kind, except count bools as chars, and group
1991 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001992 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1993 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1994 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1995 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001996 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1997 leftSK = Type::STK_ObjCObjectPointer;
1998 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1999 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00002000
2001 // Note that data member pointers and function member pointers don't
2002 // intermix because of the size differences.
2003
2004 return (leftSK == rightSK);
2005}
Chris Lattner4d391482007-12-12 07:09:47 +00002006
John McCall0f4c4c42011-06-16 01:15:19 +00002007static bool tryMatchRecordTypes(ASTContext &Context,
2008 Sema::MethodMatchStrategy strategy,
2009 const Type *lt, const Type *rt) {
2010 assert(lt && rt && lt != rt);
2011
2012 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2013 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2014 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2015
2016 // Require union-hood to match.
2017 if (left->isUnion() != right->isUnion()) return false;
2018
2019 // Require an exact match if either is non-POD.
2020 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2021 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2022 return false;
2023
2024 // Require size and alignment to match.
2025 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2026
2027 // Require fields to match.
2028 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2029 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2030 for (; li != le && ri != re; ++li, ++ri) {
2031 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2032 return false;
2033 }
2034 return (li == le && ri == re);
2035}
2036
Chris Lattner4d391482007-12-12 07:09:47 +00002037/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2038/// returns true, or false, accordingly.
2039/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00002040bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2041 const ObjCMethodDecl *right,
2042 MethodMatchStrategy strategy) {
2043 if (!matchTypes(Context, strategy,
2044 left->getResultType(), right->getResultType()))
2045 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002046
David Blaikie4e4d0842012-03-11 07:00:24 +00002047 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002048 (left->hasAttr<NSReturnsRetainedAttr>()
2049 != right->hasAttr<NSReturnsRetainedAttr>() ||
2050 left->hasAttr<NSConsumesSelfAttr>()
2051 != right->hasAttr<NSConsumesSelfAttr>()))
2052 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002053
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002054 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002055 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2056 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002057
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002058 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002059 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002060 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002061
2062 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2063 return false;
2064
David Blaikie4e4d0842012-03-11 07:00:24 +00002065 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002066 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2067 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002068 }
2069 return true;
2070}
2071
Douglas Gregorff310c72012-05-01 23:37:00 +00002072void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002073 // If the list is empty, make it a singleton list.
2074 if (List->Method == 0) {
2075 List->Method = Method;
2076 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002077 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002078 }
2079
2080 // We've seen a method with this name, see if we have already seen this type
2081 // signature.
2082 ObjCMethodList *Previous = List;
2083 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002084 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002085 continue;
2086
2087 ObjCMethodDecl *PrevObjCMethod = List->Method;
2088
2089 // Propagate the 'defined' bit.
2090 if (Method->isDefined())
2091 PrevObjCMethod->setDefined(true);
2092
2093 // If a method is deprecated, push it in the global pool.
2094 // This is used for better diagnostics.
2095 if (Method->isDeprecated()) {
2096 if (!PrevObjCMethod->isDeprecated())
2097 List->Method = Method;
2098 }
2099 // If new method is unavailable, push it into global pool
2100 // unless previous one is deprecated.
2101 if (Method->isUnavailable()) {
2102 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2103 List->Method = Method;
2104 }
2105
Douglas Gregorff310c72012-05-01 23:37:00 +00002106 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002107 }
2108
2109 // We have a new signature for an existing method - add it.
2110 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002111 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002112 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2113}
2114
Sebastian Redldb9d2142010-08-02 23:18:59 +00002115/// \brief Read the contents of the method pool for a given selector from
2116/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002117void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002118 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002119 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002120}
2121
Douglas Gregorff310c72012-05-01 23:37:00 +00002122void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002123 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002124 // Ignore methods of invalid containers.
2125 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002126 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002127
Douglas Gregor0d266d62012-01-25 00:59:09 +00002128 if (ExternalSource)
2129 ReadMethodPool(Method->getSelector());
2130
Sebastian Redldb9d2142010-08-02 23:18:59 +00002131 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002132 if (Pos == MethodPool.end())
2133 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2134 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002135
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002136 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002137
Sebastian Redldb9d2142010-08-02 23:18:59 +00002138 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002139 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002140}
2141
John McCallf85e1932011-06-15 23:02:42 +00002142/// Determines if this is an "acceptable" loose mismatch in the global
2143/// method pool. This exists mostly as a hack to get around certain
2144/// global mismatches which we can't afford to make warnings / errors.
2145/// Really, what we want is a way to take a method out of the global
2146/// method pool.
2147static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2148 ObjCMethodDecl *other) {
2149 if (!chosen->isInstanceMethod())
2150 return false;
2151
2152 Selector sel = chosen->getSelector();
2153 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2154 return false;
2155
2156 // Don't complain about mismatches for -length if the method we
2157 // chose has an integral result type.
2158 return (chosen->getResultType()->isIntegerType());
2159}
2160
Sebastian Redldb9d2142010-08-02 23:18:59 +00002161ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002162 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002163 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002164 if (ExternalSource)
2165 ReadMethodPool(Sel);
2166
Sebastian Redldb9d2142010-08-02 23:18:59 +00002167 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002168 if (Pos == MethodPool.end())
2169 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002170
Sebastian Redldb9d2142010-08-02 23:18:59 +00002171 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002172
Sebastian Redldb9d2142010-08-02 23:18:59 +00002173 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002174 bool issueDiagnostic = false, issueError = false;
2175
2176 // We support a warning which complains about *any* difference in
2177 // method signature.
2178 bool strictSelectorMatch =
2179 (receiverIdOrClass && warn &&
2180 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2181 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002182 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002183 if (strictSelectorMatch)
2184 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002185 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2186 MMS_strict)) {
2187 issueDiagnostic = true;
2188 break;
2189 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002190 }
2191
John McCallf85e1932011-06-15 23:02:42 +00002192 // If we didn't see any strict differences, we won't see any loose
2193 // differences. In ARC, however, we also need to check for loose
2194 // mismatches, because most of them are errors.
2195 if (!strictSelectorMatch ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002196 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002197 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002198 // This checks if the methods differ in type mismatch.
2199 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2200 MMS_loose) &&
2201 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2202 issueDiagnostic = true;
David Blaikie4e4d0842012-03-11 07:00:24 +00002203 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002204 issueError = true;
2205 break;
2206 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002207 }
2208
John McCallf85e1932011-06-15 23:02:42 +00002209 if (issueDiagnostic) {
2210 if (issueError)
2211 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2212 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002213 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2214 else
2215 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002216
2217 Diag(MethList.Method->getLocStart(),
2218 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002219 << MethList.Method->getSourceRange();
2220 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2221 Diag(Next->Method->getLocStart(), diag::note_also_found)
2222 << Next->Method->getSourceRange();
2223 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002224 }
2225 return MethList.Method;
2226}
2227
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002228ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002229 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2230 if (Pos == MethodPool.end())
2231 return 0;
2232
2233 GlobalMethods &Methods = Pos->second;
2234
2235 if (Methods.first.Method && Methods.first.Method->isDefined())
2236 return Methods.first.Method;
2237 if (Methods.second.Method && Methods.second.Method->isDefined())
2238 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002239 return 0;
2240}
2241
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002242/// DiagnoseDuplicateIvars -
2243/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002244/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002245/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002246/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002247void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2248 ObjCInterfaceDecl *SID) {
2249 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2250 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002251 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002252 if (Ivar->isInvalidDecl())
2253 continue;
2254 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2255 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2256 if (prevIvar) {
2257 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2258 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2259 Ivar->setInvalidDecl();
2260 }
2261 }
2262 }
2263}
2264
Erik Verbruggend64251f2011-12-06 09:25:23 +00002265Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2266 switch (CurContext->getDeclKind()) {
2267 case Decl::ObjCInterface:
2268 return Sema::OCK_Interface;
2269 case Decl::ObjCProtocol:
2270 return Sema::OCK_Protocol;
2271 case Decl::ObjCCategory:
2272 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2273 return Sema::OCK_ClassExtension;
2274 else
2275 return Sema::OCK_Category;
2276 case Decl::ObjCImplementation:
2277 return Sema::OCK_Implementation;
2278 case Decl::ObjCCategoryImpl:
2279 return Sema::OCK_CategoryImplementation;
2280
2281 default:
2282 return Sema::OCK_None;
2283 }
2284}
2285
Steve Naroffa56f6162007-12-18 01:30:32 +00002286// Note: For class/category implemenations, allMethods/allProperties is
2287// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002288Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2289 Decl **allMethods, unsigned allNum,
2290 Decl **allProperties, unsigned pNum,
2291 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002292
Erik Verbruggend64251f2011-12-06 09:25:23 +00002293 if (getObjCContainerKind() == Sema::OCK_None)
2294 return 0;
2295
2296 assert(AtEnd.isValid() && "Invalid location for '@end'");
2297
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002298 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2299 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002300
Mike Stump1eb44332009-09-09 15:08:12 +00002301 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002302 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2303 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002304 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002305
Steve Naroff0701bbb2009-01-08 17:28:14 +00002306 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2307 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2308 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2309
Chris Lattner4d391482007-12-12 07:09:47 +00002310 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002311 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002312 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002313
2314 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002315 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002316 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002317 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002318 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002319 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002320 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002321 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002322 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002323 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002324 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002325 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002326 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002327 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002328 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002329 if (!Context.getSourceManager().isInSystemHeader(
2330 Method->getLocation()))
2331 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2332 << Method->getDeclName();
2333 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2334 }
Chris Lattner4d391482007-12-12 07:09:47 +00002335 InsMap[Method->getSelector()] = Method;
2336 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002337 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002338 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002339 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002340 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002341 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002342 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002343 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002344 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002345 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002346 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002347 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002348 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002349 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002350 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002351 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002352 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002353 if (!Context.getSourceManager().isInSystemHeader(
2354 Method->getLocation()))
2355 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2356 << Method->getDeclName();
2357 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2358 }
Chris Lattner4d391482007-12-12 07:09:47 +00002359 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002360 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002361 }
2362 }
2363 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002364 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002365 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002366 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002367 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002368 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002369 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002370 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002371 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002372 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002373
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002374 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002375 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002376 if (C->IsClassExtension()) {
2377 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2378 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002379 }
Chris Lattner4d391482007-12-12 07:09:47 +00002380 }
Steve Naroff09c47192009-01-09 15:36:25 +00002381 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002382 if (CDecl->getIdentifier())
2383 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2384 // user-defined setter/getter. It also synthesizes setter/getter methods
2385 // and adds them to the DeclContext and global method pools.
2386 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2387 E = CDecl->prop_end();
2388 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002389 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002390 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002391 }
2392 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002393 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002394 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002395 // Any property declared in a class extension might have user
2396 // declared setter or getter in current class extension or one
2397 // of the other class extensions. Mark them as synthesized as
2398 // property will be synthesized when property with same name is
2399 // seen in the @implementation.
2400 for (const ObjCCategoryDecl *ClsExtDecl =
2401 IDecl->getFirstClassExtension();
2402 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2403 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2404 E = ClsExtDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002405 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002406 // Skip over properties declared @dynamic
2407 if (const ObjCPropertyImplDecl *PIDecl
2408 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2409 if (PIDecl->getPropertyImplementation()
2410 == ObjCPropertyImplDecl::Dynamic)
2411 continue;
2412
2413 for (const ObjCCategoryDecl *CExtDecl =
2414 IDecl->getFirstClassExtension();
2415 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2416 if (ObjCMethodDecl *GetterMethod =
2417 CExtDecl->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002418 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002419 if (!Property->isReadOnly())
2420 if (ObjCMethodDecl *SetterMethod =
2421 CExtDecl->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002422 SetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002423 }
2424 }
2425 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002426 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002427 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002428 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002429
Patrick Beardb2f68202012-04-06 18:12:22 +00002430 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2431 if (IDecl->getSuperClass() == NULL) {
2432 // This class has no superclass, so check that it has been marked with
2433 // __attribute((objc_root_class)).
2434 if (!HasRootClassAttr) {
2435 SourceLocation DeclLoc(IDecl->getLocation());
2436 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2437 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2438 << IDecl->getIdentifier();
2439 // See if NSObject is in the current scope, and if it is, suggest
2440 // adding " : NSObject " to the class declaration.
2441 NamedDecl *IF = LookupSingleName(TUScope,
2442 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2443 DeclLoc, LookupOrdinaryName);
2444 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2445 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2446 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2447 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2448 } else {
2449 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2450 }
2451 }
2452 } else if (HasRootClassAttr) {
2453 // Complain that only root classes may have this attribute.
2454 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2455 }
2456
John McCall260611a2012-06-20 06:18:46 +00002457 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002458 while (IDecl->getSuperClass()) {
2459 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2460 IDecl = IDecl->getSuperClass();
2461 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002462 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002463 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002464 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002465 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002466 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002467 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002468
Chris Lattner4d391482007-12-12 07:09:47 +00002469 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002470 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002471 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002472 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002473 Categories; Categories = Categories->getNextClassCategory()) {
2474 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002475 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002476 break;
2477 }
2478 }
2479 }
2480 }
Chris Lattner682bf922009-03-29 16:50:03 +00002481 if (isInterfaceDeclKind) {
2482 // Reject invalid vardecls.
2483 for (unsigned i = 0; i != tuvNum; i++) {
2484 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2485 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2486 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002487 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002488 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002489 }
Chris Lattner682bf922009-03-29 16:50:03 +00002490 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002491 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002492 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002493
2494 for (unsigned i = 0; i != tuvNum; i++) {
2495 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002496 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2497 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002498 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2499 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002500
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002501 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002502 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002503}
2504
2505
2506/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2507/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002508static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002509CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002510 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002511}
2512
Ted Kremenek422bae72010-04-18 04:59:38 +00002513static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002514unsigned countAlignAttr(const AttrVec &A) {
2515 unsigned count=0;
2516 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2517 if ((*i)->getKind() == attr::Aligned)
2518 ++count;
2519 return count;
2520}
2521
2522static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002523bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2524 const AttrVec &A) {
2525 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002526 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002527 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002528 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002529
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002530 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002531 // But implementation has attributes. This is invalid.
2532 // Except when implementation has 'Align' attribute which is
2533 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002534 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002535 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002536
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002537 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002538
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002539 unsigned countAlignOnImpl = countAlignAttr(A);
2540 if (!countAlignOnImpl && (A.size() != D.size()))
2541 return true;
2542 else if (countAlignOnImpl) {
2543 unsigned countAlignOnDecl = countAlignAttr(D);
2544 if (countAlignOnDecl && (A.size() != D.size()))
2545 return true;
2546 else if (!countAlignOnDecl &&
2547 ((A.size()-countAlignOnImpl) != D.size()))
2548 return true;
2549 }
2550
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002551 // attributes on method declaration and definition must match exactly.
2552 // Note that we have at most a couple of attributes on methods, so this
2553 // n*n search is good enough.
2554 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002555 if ((*i)->getKind() == attr::Aligned)
2556 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002557 bool match = false;
2558 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2559 if ((*i)->getKind() == (*i1)->getKind()) {
2560 match = true;
2561 break;
2562 }
2563 }
2564 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002565 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002566 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002567
Sean Huntcf807c42010-08-18 23:23:40 +00002568 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002569}
2570
Douglas Gregor926df6c2011-06-11 01:09:30 +00002571/// \brief Check whether the declared result type of the given Objective-C
2572/// method declaration is compatible with the method's class.
2573///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002574static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002575CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2576 ObjCInterfaceDecl *CurrentClass) {
2577 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002578
2579 // If an Objective-C method inherits its related result type, then its
2580 // declared result type must be compatible with its own class type. The
2581 // declared result type is compatible if:
2582 if (const ObjCObjectPointerType *ResultObjectType
2583 = ResultType->getAs<ObjCObjectPointerType>()) {
2584 // - it is id or qualified id, or
2585 if (ResultObjectType->isObjCIdType() ||
2586 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002587 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002588
2589 if (CurrentClass) {
2590 if (ObjCInterfaceDecl *ResultClass
2591 = ResultObjectType->getInterfaceDecl()) {
2592 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002593 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002594 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002595
2596 // - it is a superclass of the method's class type
2597 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002598 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002599 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002600 } else {
2601 // Any Objective-C pointer type might be acceptable for a protocol
2602 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002603 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002604 }
2605 }
2606
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002607 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002608}
2609
John McCall6c2c2502011-07-22 02:45:48 +00002610namespace {
2611/// A helper class for searching for methods which a particular method
2612/// overrides.
2613class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002614public:
John McCall6c2c2502011-07-22 02:45:48 +00002615 Sema &S;
2616 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002617 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002618 bool Recursive;
2619
2620public:
2621 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2622 Selector selector = method->getSelector();
2623
2624 // Bypass this search if we've never seen an instance/class method
2625 // with this selector before.
2626 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2627 if (it == S.MethodPool.end()) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00002628 if (!S.getExternalSource()) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002629 S.ReadMethodPool(selector);
2630
2631 it = S.MethodPool.find(selector);
2632 if (it == S.MethodPool.end())
2633 return;
John McCall6c2c2502011-07-22 02:45:48 +00002634 }
2635 ObjCMethodList &list =
2636 method->isInstanceMethod() ? it->second.first : it->second.second;
2637 if (!list.Method) return;
2638
2639 ObjCContainerDecl *container
2640 = cast<ObjCContainerDecl>(method->getDeclContext());
2641
2642 // Prevent the search from reaching this container again. This is
2643 // important with categories, which override methods from the
2644 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002645 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2646 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002647 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2648 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002649 } else {
2650 searchFromContainer(container);
2651 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002652 }
John McCall6c2c2502011-07-22 02:45:48 +00002653
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002654 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002655 iterator begin() const { return Overridden.begin(); }
2656 iterator end() const { return Overridden.end(); }
2657
2658private:
2659 void searchFromContainer(ObjCContainerDecl *container) {
2660 if (container->isInvalidDecl()) return;
2661
2662 switch (container->getDeclKind()) {
2663#define OBJCCONTAINER(type, base) \
2664 case Decl::type: \
2665 searchFrom(cast<type##Decl>(container)); \
2666 break;
2667#define ABSTRACT_DECL(expansion)
2668#define DECL(type, base) \
2669 case Decl::type:
2670#include "clang/AST/DeclNodes.inc"
2671 llvm_unreachable("not an ObjC container!");
2672 }
2673 }
2674
2675 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002676 if (!protocol->hasDefinition())
2677 return;
2678
John McCall6c2c2502011-07-22 02:45:48 +00002679 // A method in a protocol declaration overrides declarations from
2680 // referenced ("parent") protocols.
2681 search(protocol->getReferencedProtocols());
2682 }
2683
2684 void searchFrom(ObjCCategoryDecl *category) {
2685 // A method in a category declaration overrides declarations from
2686 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002687 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002688 search(category->getReferencedProtocols());
2689 }
2690
2691 void searchFrom(ObjCCategoryImplDecl *impl) {
2692 // A method in a category definition that has a category
2693 // declaration overrides declarations from the category
2694 // declaration.
2695 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2696 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002697 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2698 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002699
2700 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002701 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2702 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002703 }
2704 }
2705
2706 void searchFrom(ObjCInterfaceDecl *iface) {
2707 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002708 if (!iface->hasDefinition())
2709 return;
2710
John McCall6c2c2502011-07-22 02:45:48 +00002711 // - categories,
2712 for (ObjCCategoryDecl *category = iface->getCategoryList();
2713 category; category = category->getNextClassCategory())
2714 search(category);
2715
2716 // - the super class, and
2717 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2718 search(super);
2719
2720 // - any referenced protocols.
2721 search(iface->getReferencedProtocols());
2722 }
2723
2724 void searchFrom(ObjCImplementationDecl *impl) {
2725 // A method in a class implementation overrides declarations from
2726 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002727 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2728 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002729 }
2730
2731
2732 void search(const ObjCProtocolList &protocols) {
2733 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2734 i != e; ++i)
2735 search(*i);
2736 }
2737
2738 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002739 // Check for a method in this container which matches this selector.
2740 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2741 Method->isInstanceMethod());
2742
2743 // If we find one, record it and bail out.
2744 if (meth) {
2745 Overridden.insert(meth);
2746 return;
2747 }
2748
2749 // Otherwise, search for methods that a hypothetical method here
2750 // would have overridden.
2751
2752 // Note that we're now in a recursive case.
2753 Recursive = true;
2754
2755 searchFromContainer(container);
2756 }
2757};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002758}
2759
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002760void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2761 ObjCInterfaceDecl *CurrentClass,
2762 ResultTypeCompatibilityKind RTC) {
2763 // Search for overridden methods and merge information down from them.
2764 OverrideSearch overrides(*this, ObjCMethod);
2765 // Keep track if the method overrides any method in the class's base classes,
2766 // its protocols, or its categories' protocols; we will keep that info
2767 // in the ObjCMethodDecl.
2768 // For this info, a method in an implementation is not considered as
2769 // overriding the same method in the interface or its categories.
2770 bool hasOverriddenMethodsInBaseOrProtocol = false;
2771 for (OverrideSearch::iterator
2772 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2773 ObjCMethodDecl *overridden = *i;
2774
2775 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2776 CurrentClass != overridden->getClassInterface() ||
2777 overridden->isOverriding())
2778 hasOverriddenMethodsInBaseOrProtocol = true;
2779
2780 // Propagate down the 'related result type' bit from overridden methods.
2781 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2782 ObjCMethod->SetRelatedResultType();
2783
2784 // Then merge the declarations.
2785 mergeObjCMethodDecls(ObjCMethod, overridden);
2786
2787 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2788 continue; // Conflicting properties are detected elsewhere.
2789
2790 // Check for overriding methods
2791 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2792 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2793 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2794 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2795
2796 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00002797 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
2798 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002799 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2800 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002801 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2802 PrevE = overridden->param_end();
2803 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002804 assert(PrevI != overridden->param_end() && "Param mismatch");
2805 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2806 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2807 // If type of argument of method in this class does not match its
2808 // respective argument type in the super class method, issue warning;
2809 if (!Context.typesAreCompatible(T1, T2)) {
2810 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2811 << T1 << T2;
2812 Diag(overridden->getLocation(), diag::note_previous_declaration);
2813 break;
2814 }
2815 }
2816 }
2817 }
2818
2819 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2820}
2821
John McCalld226f652010-08-21 09:40:31 +00002822Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002823 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002824 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002825 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002826 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002827 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002828 Selector Sel,
2829 // optional arguments. The number of types/arguments is obtained
2830 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002831 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002832 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002833 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002834 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002835 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002836 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002837 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002838 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002839 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002840 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2841 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002842 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002843
Douglas Gregore97179c2011-09-08 01:46:34 +00002844 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002845 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002846 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002847 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002848
Steve Naroffccef3712009-02-20 22:59:16 +00002849 // Methods cannot return interface types. All ObjC objects are
2850 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002851 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002852 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2853 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002854 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002855 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002856
2857 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002858 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002859 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002860 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002861 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002862 }
Mike Stump1eb44332009-09-09 15:08:12 +00002863
2864 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002865 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002866 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002867 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002868 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002869 MethodType == tok::minus, isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +00002870 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002871 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002872 MethodDeclKind == tok::objc_optional
2873 ? ObjCMethodDecl::Optional
2874 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002875 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002876
Chris Lattner5f9e2722011-07-23 10:55:15 +00002877 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002878
Chris Lattner7db638d2009-04-11 19:42:43 +00002879 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002880 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002881 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002882
Chris Lattnere294d3f2009-04-11 18:57:04 +00002883 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002884 ArgType = Context.getObjCIdType();
2885 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002886 } else {
John McCall58e46772009-10-23 21:48:59 +00002887 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002888 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002889 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002890 }
Mike Stump1eb44332009-09-09 15:08:12 +00002891
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002892 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2893 LookupOrdinaryName, ForRedeclaration);
2894 LookupName(R, S);
2895 if (R.isSingleResult()) {
2896 NamedDecl *PrevDecl = R.getFoundDecl();
2897 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002898 Diag(ArgInfo[i].NameLoc,
2899 (MethodDefinition ? diag::warn_method_param_redefinition
2900 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002901 << ArgInfo[i].Name;
2902 Diag(PrevDecl->getLocation(),
2903 diag::note_previous_declaration);
2904 }
2905 }
2906
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002907 SourceLocation StartLoc = DI
2908 ? DI->getTypeLoc().getBeginLoc()
2909 : ArgInfo[i].NameLoc;
2910
John McCall81ef3e62011-04-23 02:46:06 +00002911 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2912 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2913 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002914
John McCall70798862011-05-02 00:30:12 +00002915 Param->setObjCMethodScopeInfo(i);
2916
Chris Lattner0ed844b2008-04-04 06:12:32 +00002917 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002918 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002919
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002920 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002921 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002922
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002923 if (Param->hasAttr<BlocksAttr>()) {
2924 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2925 Param->setInvalidDecl();
2926 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002927 S->AddDecl(Param);
2928 IdResolver.AddDecl(Param);
2929
Chris Lattner0ed844b2008-04-04 06:12:32 +00002930 Params.push_back(Param);
2931 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002932
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002933 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002934 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002935 QualType ArgType = Param->getType();
2936 if (ArgType.isNull())
2937 ArgType = Context.getObjCIdType();
2938 else
2939 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002940 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002941 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002942 Diag(Param->getLocation(),
2943 diag::err_object_cannot_be_passed_returned_by_value)
2944 << 1 << ArgType;
2945 Param->setInvalidDecl();
2946 }
2947 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002948
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002949 Params.push_back(Param);
2950 }
2951
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002952 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002953 ObjCMethod->setObjCDeclQualifier(
2954 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002955
2956 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002957 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002958
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002959 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002960 const ObjCMethodDecl *PrevMethod = 0;
2961 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002962 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002963 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2964 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002965 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002966 PrevMethod = ImpDecl->getClassMethod(Sel);
2967 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002968 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002969
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002970 ObjCMethodDecl *IMD = 0;
2971 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2972 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2973 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002974 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002975 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002976 SourceLocation MethodLoc = IMD->getLocation();
2977 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2978 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002979 Diag(MethodLoc, diag::note_method_declared_at)
2980 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002981 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002982 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002983 } else {
2984 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002985 }
John McCall6c2c2502011-07-22 02:45:48 +00002986
Chris Lattner4d391482007-12-12 07:09:47 +00002987 if (PrevMethod) {
2988 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002989 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002990 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002991 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002992 }
John McCall54abf7d2009-11-04 02:18:39 +00002993
Douglas Gregor926df6c2011-06-11 01:09:30 +00002994 // If this Objective-C method does not have a related result type, but we
2995 // are allowed to infer related result types, try to do so based on the
2996 // method family.
2997 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2998 if (!CurrentClass) {
2999 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3000 CurrentClass = Cat->getClassInterface();
3001 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3002 CurrentClass = Impl->getClassInterface();
3003 else if (ObjCCategoryImplDecl *CatImpl
3004 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3005 CurrentClass = CatImpl->getClassInterface();
3006 }
John McCall6c2c2502011-07-22 02:45:48 +00003007
Douglas Gregore97179c2011-09-08 01:46:34 +00003008 ResultTypeCompatibilityKind RTC
3009 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00003010
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003011 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00003012
John McCallf85e1932011-06-15 23:02:42 +00003013 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00003014 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00003015 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
3016
Douglas Gregore97179c2011-09-08 01:46:34 +00003017 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003018 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00003019 !ObjCMethod->hasRelatedResultType() &&
3020 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00003021 bool InferRelatedResultType = false;
3022 switch (ObjCMethod->getMethodFamily()) {
3023 case OMF_None:
3024 case OMF_copy:
3025 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00003026 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003027 case OMF_mutableCopy:
3028 case OMF_release:
3029 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00003030 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003031 break;
3032
3033 case OMF_alloc:
3034 case OMF_new:
3035 InferRelatedResultType = ObjCMethod->isClassMethod();
3036 break;
3037
3038 case OMF_init:
3039 case OMF_autorelease:
3040 case OMF_retain:
3041 case OMF_self:
3042 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3043 break;
3044 }
3045
John McCall6c2c2502011-07-22 02:45:48 +00003046 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003047 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003048 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003049
3050 ActOnDocumentableDecl(ObjCMethod);
3051
John McCalld226f652010-08-21 09:40:31 +00003052 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003053}
3054
Chris Lattnercc98eac2008-12-17 07:13:27 +00003055bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003056 // Following is also an error. But it is caused by a missing @end
3057 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003058 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003059 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003060
3061 // If we switched context to translation unit while we are still lexically in
3062 // an objc container, it means the parser missed emitting an error.
3063 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3064 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003065
Anders Carlsson15281452008-11-04 16:57:32 +00003066 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3067 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003068
Anders Carlsson15281452008-11-04 16:57:32 +00003069 return true;
3070}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003071
James Dennett1dfbd922012-06-14 21:40:34 +00003072/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003073/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003074void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003075 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003076 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003077 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003078 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003079 if (!Class) {
3080 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3081 return;
3082 }
John McCall260611a2012-06-20 06:18:46 +00003083 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003084 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3085 return;
3086 }
Mike Stump1eb44332009-09-09 15:08:12 +00003087
Chris Lattnercc98eac2008-12-17 07:13:27 +00003088 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003089 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003090 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003091 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003092 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003093 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003094 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003095 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3096 /*FIXME: StartL=*/ID->getLocation(),
3097 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003098 ID->getIdentifier(), ID->getType(),
3099 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003100 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003101 }
Mike Stump1eb44332009-09-09 15:08:12 +00003102
Chris Lattnercc98eac2008-12-17 07:13:27 +00003103 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003104 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003105 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003106 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003107 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003108 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003109 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003110 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003111 }
3112}
3113
Douglas Gregor160b5632010-04-26 17:32:49 +00003114/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003115VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3116 SourceLocation StartLoc,
3117 SourceLocation IdLoc,
3118 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003119 bool Invalid) {
3120 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3121 // duration shall not be qualified by an address-space qualifier."
3122 // Since all parameters have automatic store duration, they can not have
3123 // an address space.
3124 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003125 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003126 Invalid = true;
3127 }
3128
3129 // An @catch parameter must be an unqualified object pointer type;
3130 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3131 if (Invalid) {
3132 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003133 } else if (T->isDependentType()) {
3134 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003135 } else if (!T->isObjCObjectPointerType()) {
3136 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003137 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003138 } else if (T->isObjCQualifiedIdType()) {
3139 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003140 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003141 }
3142
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003143 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3144 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003145 New->setExceptionVariable(true);
3146
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003147 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003148 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003149 Invalid = true;
3150
Douglas Gregor160b5632010-04-26 17:32:49 +00003151 if (Invalid)
3152 New->setInvalidDecl();
3153 return New;
3154}
3155
John McCalld226f652010-08-21 09:40:31 +00003156Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003157 const DeclSpec &DS = D.getDeclSpec();
3158
3159 // We allow the "register" storage class on exception variables because
3160 // GCC did, but we drop it completely. Any other storage class is an error.
3161 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3162 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3163 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3164 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3165 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3166 << DS.getStorageClassSpec();
3167 }
3168 if (D.getDeclSpec().isThreadSpecified())
3169 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3170 D.getMutableDeclSpec().ClearStorageClassSpecs();
3171
3172 DiagnoseFunctionSpecifiers(D);
3173
3174 // Check that there are no default arguments inside the type of this
3175 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003176 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003177 CheckExtraCXXDefaultArguments(D);
3178
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003179 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003180 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003181
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003182 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3183 D.getSourceRange().getBegin(),
3184 D.getIdentifierLoc(),
3185 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003186 D.isInvalidType());
3187
3188 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3189 if (D.getCXXScopeSpec().isSet()) {
3190 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3191 << D.getCXXScopeSpec().getRange();
3192 New->setInvalidDecl();
3193 }
3194
3195 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003196 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003197 if (D.getIdentifier())
3198 IdResolver.AddDecl(New);
3199
3200 ProcessDeclAttributes(S, New, D);
3201
3202 if (New->hasAttr<BlocksAttr>())
3203 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003204 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003205}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003206
3207/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003208/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003209void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003210 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003211 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3212 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003213 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003214 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003215 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003216 }
3217}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003218
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003219void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003220 // Load referenced selectors from the external source.
3221 if (ExternalSource) {
3222 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3223 ExternalSource->ReadReferencedSelectors(Sels);
3224 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3225 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3226 }
3227
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003228 // Warning will be issued only when selector table is
3229 // generated (which means there is at lease one implementation
3230 // in the TU). This is to match gcc's behavior.
3231 if (ReferencedSelectors.empty() ||
3232 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003233 return;
3234 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3235 ReferencedSelectors.begin(),
3236 E = ReferencedSelectors.end(); S != E; ++S) {
3237 Selector Sel = (*S).first;
3238 if (!LookupImplementedMethodInGlobalPool(Sel))
3239 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3240 }
3241 return;
3242}