blob: 43b097d1f2b14b4dc5fbe0dabbd5a1966bbfa9b5 [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 Gregorf4d918f2013-01-15 22:43:08 +0000112 const ObjCMethodDecl *Overridden) {
Douglas Gregor926df6c2011-06-11 01:09:30 +0000113 if (Overridden->hasRelatedResultType() &&
114 !NewMethod->hasRelatedResultType()) {
115 // This can only happen when the method follows a naming convention that
116 // implies a related result type, and the original (overridden) method has
117 // a suitable return type, but the new (overriding) method does not have
118 // a suitable return type.
119 QualType ResultType = NewMethod->getResultType();
120 SourceRange ResultTypeRange;
121 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000122 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000123 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
124
125 // Figure out which class this method is part of, if any.
126 ObjCInterfaceDecl *CurrentClass
127 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
128 if (!CurrentClass) {
129 DeclContext *DC = NewMethod->getDeclContext();
130 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
131 CurrentClass = Cat->getClassInterface();
132 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
133 CurrentClass = Impl->getClassInterface();
134 else if (ObjCCategoryImplDecl *CatImpl
135 = dyn_cast<ObjCCategoryImplDecl>(DC))
136 CurrentClass = CatImpl->getClassInterface();
137 }
138
139 if (CurrentClass) {
140 Diag(NewMethod->getLocation(),
141 diag::warn_related_result_type_compatibility_class)
142 << Context.getObjCInterfaceType(CurrentClass)
143 << ResultType
144 << ResultTypeRange;
145 } else {
146 Diag(NewMethod->getLocation(),
147 diag::warn_related_result_type_compatibility_protocol)
148 << ResultType
149 << ResultTypeRange;
150 }
151
Douglas Gregore97179c2011-09-08 01:46:34 +0000152 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
153 Diag(Overridden->getLocation(),
154 diag::note_related_result_type_overridden_family)
155 << Family;
156 else
157 Diag(Overridden->getLocation(),
158 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000159 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000160 if (getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000161 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
162 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
163 Diag(NewMethod->getLocation(),
164 diag::err_nsreturns_retained_attribute_mismatch) << 1;
165 Diag(Overridden->getLocation(), diag::note_previous_decl)
166 << "method";
167 }
168 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
169 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
170 Diag(NewMethod->getLocation(),
171 diag::err_nsreturns_retained_attribute_mismatch) << 0;
172 Diag(Overridden->getLocation(), diag::note_previous_decl)
173 << "method";
174 }
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000175 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
176 oe = Overridden->param_end();
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000177 for (ObjCMethodDecl::param_iterator
178 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000179 ni != ne && oi != oe; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000180 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000181 ParmVarDecl *newDecl = (*ni);
182 if (newDecl->hasAttr<NSConsumedAttr>() !=
183 oldDecl->hasAttr<NSConsumedAttr>()) {
184 Diag(newDecl->getLocation(),
185 diag::err_nsconsumed_attribute_mismatch);
186 Diag(oldDecl->getLocation(), diag::note_previous_decl)
187 << "parameter";
188 }
189 }
190 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000191}
192
John McCallf85e1932011-06-15 23:02:42 +0000193/// \brief Check a method declaration for compatibility with the Objective-C
194/// ARC conventions.
195static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
196 ObjCMethodFamily family = method->getMethodFamily();
197 switch (family) {
198 case OMF_None:
Nico Weber80cb6e62011-08-28 22:35:17 +0000199 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000200 case OMF_retain:
201 case OMF_release:
202 case OMF_autorelease:
203 case OMF_retainCount:
204 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000205 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000206 return false;
207
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000208 case OMF_dealloc:
209 if (!S.Context.hasSameType(method->getResultType(), S.Context.VoidTy)) {
210 SourceRange ResultTypeRange;
211 if (const TypeSourceInfo *ResultTypeInfo
212 = method->getResultTypeSourceInfo())
213 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
214 if (ResultTypeRange.isInvalid())
215 S.Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
216 << method->getResultType()
217 << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
218 else
219 S.Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
220 << method->getResultType()
221 << FixItHint::CreateReplacement(ResultTypeRange, "void");
222 return true;
223 }
224 return false;
225
John McCallf85e1932011-06-15 23:02:42 +0000226 case OMF_init:
227 // If the method doesn't obey the init rules, don't bother annotating it.
228 if (S.checkInitMethod(method, QualType()))
229 return true;
230
231 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
232 S.Context));
233
234 // Don't add a second copy of this attribute, but otherwise don't
235 // let it be suppressed.
236 if (method->hasAttr<NSReturnsRetainedAttr>())
237 return false;
238 break;
239
240 case OMF_alloc:
241 case OMF_copy:
242 case OMF_mutableCopy:
243 case OMF_new:
244 if (method->hasAttr<NSReturnsRetainedAttr>() ||
245 method->hasAttr<NSReturnsNotRetainedAttr>() ||
246 method->hasAttr<NSReturnsAutoreleasedAttr>())
247 return false;
248 break;
249 }
250
251 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
252 S.Context));
253 return false;
254}
255
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000256static void DiagnoseObjCImplementedDeprecations(Sema &S,
257 NamedDecl *ND,
258 SourceLocation ImplLoc,
259 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000260 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000261 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000262 if (select == 0)
Ted Kremenek3306ec12012-02-27 22:55:11 +0000263 S.Diag(ND->getLocation(), diag::note_method_declared_at)
264 << ND->getDeclName();
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000265 else
266 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
267 }
268}
269
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000270/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
271/// pool.
272void Sema::AddAnyMethodToGlobalPool(Decl *D) {
273 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
274
275 // If we don't have a valid method decl, simply return.
276 if (!MDecl)
277 return;
278 if (MDecl->isInstanceMethod())
279 AddInstanceMethodToGlobalPool(MDecl, true);
280 else
281 AddFactoryMethodToGlobalPool(MDecl, true);
282}
283
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000284/// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer
285/// has explicit ownership attribute; false otherwise.
286static bool
287HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) {
288 QualType T = Param->getType();
289
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000290 if (const PointerType *PT = T->getAs<PointerType>()) {
291 T = PT->getPointeeType();
292 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
293 T = RT->getPointeeType();
294 } else {
295 return true;
296 }
297
298 // If we have a lifetime qualifier, but it's local, we must have
299 // inferred it. So, it is implicit.
300 return !T.getLocalQualifiers().hasObjCLifetime();
301}
302
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +0000303/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
304/// and user declared, in the method definition's AST.
305void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
306 assert((getCurMethodDecl() == 0) && "Methodparsing confused");
John McCalld226f652010-08-21 09:40:31 +0000307 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000308
Steve Naroff394f3f42008-07-25 17:57:26 +0000309 // If we don't have a valid method decl, simply return.
310 if (!MDecl)
311 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000312
Chris Lattner4d391482007-12-12 07:09:47 +0000313 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000314 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000315 PushFunctionScope();
316
Chris Lattner4d391482007-12-12 07:09:47 +0000317 // Create Decl objects for each parameter, entrring them in the scope for
318 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000319
320 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000321 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000322
Daniel Dunbar451318c2008-08-26 06:07:48 +0000323 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
324 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000325
Chris Lattner8123a952008-04-10 02:22:51 +0000326 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000327 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000328 E = MDecl->param_end(); PI != E; ++PI) {
329 ParmVarDecl *Param = (*PI);
330 if (!Param->isInvalidDecl() &&
331 RequireCompleteType(Param->getLocation(), Param->getType(),
332 diag::err_typecheck_decl_incomplete_type))
333 Param->setInvalidDecl();
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000334 if (!Param->isInvalidDecl() &&
335 getLangOpts().ObjCAutoRefCount &&
336 !HasExplicitOwnershipAttr(*this, Param))
337 Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
338 Param->getType();
Fariborz Jahanian918546c2012-08-30 23:56:02 +0000339
Chris Lattner89951a82009-02-20 18:43:26 +0000340 if ((*PI)->getIdentifier())
341 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000342 }
John McCallf85e1932011-06-15 23:02:42 +0000343
344 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000345 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000346 switch (MDecl->getMethodFamily()) {
347 case OMF_retain:
348 case OMF_retainCount:
349 case OMF_release:
350 case OMF_autorelease:
351 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
352 << MDecl->getSelector();
353 break;
354
355 case OMF_None:
356 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000357 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000358 case OMF_alloc:
359 case OMF_init:
360 case OMF_mutableCopy:
361 case OMF_copy:
362 case OMF_new:
363 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000364 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000365 break;
366 }
367 }
368
Nico Weber9a1ecf02011-08-22 17:25:57 +0000369 // Warn on deprecated methods under -Wdeprecated-implementations,
370 // and prepare for warning on missing super calls.
371 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian84101132012-09-07 23:46:23 +0000372 ObjCMethodDecl *IMD =
373 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
374
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000375 if (IMD) {
376 ObjCImplDecl *ImplDeclOfMethodDef =
377 dyn_cast<ObjCImplDecl>(MDecl->getDeclContext());
378 ObjCContainerDecl *ContDeclOfMethodDecl =
379 dyn_cast<ObjCContainerDecl>(IMD->getDeclContext());
380 ObjCImplDecl *ImplDeclOfMethodDecl = 0;
381 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
382 ImplDeclOfMethodDecl = OID->getImplementation();
383 else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl))
384 ImplDeclOfMethodDecl = CD->getImplementation();
385 // No need to issue deprecated warning if deprecated mehod in class/category
386 // is being implemented in its own implementation (no overriding is involved).
387 if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef)
388 DiagnoseObjCImplementedDeprecations(*this,
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000389 dyn_cast<NamedDecl>(IMD),
390 MDecl->getLocation(), 0);
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000391 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000392
Nico Weber80cb6e62011-08-28 22:35:17 +0000393 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000394 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
395 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
396 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000397 if (IC->getSuperClass()) {
Jordan Rose535a5d02012-10-19 16:05:26 +0000398 ObjCMethodFamily Family = MDecl->getMethodFamily();
399 if (Family == OMF_dealloc) {
400 if (!(getLangOpts().ObjCAutoRefCount ||
401 getLangOpts().getGC() == LangOptions::GCOnly))
402 getCurFunction()->ObjCShouldCallSuper = true;
403
404 } else if (Family == OMF_finalize) {
405 if (Context.getLangOpts().getGC() != LangOptions::NonGC)
406 getCurFunction()->ObjCShouldCallSuper = true;
407
408 } else {
409 const ObjCMethodDecl *SuperMethod =
410 IC->getSuperClass()->lookupMethod(MDecl->getSelector(),
411 MDecl->isInstanceMethod());
412 getCurFunction()->ObjCShouldCallSuper =
413 (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000414 }
Nico Weber80cb6e62011-08-28 22:35:17 +0000415 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000416 }
Chris Lattner4d391482007-12-12 07:09:47 +0000417}
418
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000419namespace {
420
421// Callback to only accept typo corrections that are Objective-C classes.
422// If an ObjCInterfaceDecl* is given to the constructor, then the validation
423// function will reject corrections to that class.
424class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
425 public:
426 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
427 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
428 : CurrentIDecl(IDecl) {}
429
430 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
431 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
432 return ID && !declaresSameEntity(ID, CurrentIDecl);
433 }
434
435 private:
436 ObjCInterfaceDecl *CurrentIDecl;
437};
438
439}
440
John McCalld226f652010-08-21 09:40:31 +0000441Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000442ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
443 IdentifierInfo *ClassName, SourceLocation ClassLoc,
444 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000445 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000446 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000447 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000448 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattner4d391482007-12-12 07:09:47 +0000450 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000451 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000452 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000453
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000454 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000455 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000456 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000457 }
Mike Stump1eb44332009-09-09 15:08:12 +0000458
Douglas Gregor7723fec2011-12-15 20:29:51 +0000459 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000460 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000461 ObjCInterfaceDecl *IDecl
462 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000463 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000464
Douglas Gregor7723fec2011-12-15 20:29:51 +0000465 if (PrevIDecl) {
466 // Class already seen. Was it a definition?
467 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
468 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
469 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000470 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000471 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000472 }
Chris Lattner4d391482007-12-12 07:09:47 +0000473 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000474
475 if (AttrList)
476 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
477 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Douglas Gregor7723fec2011-12-15 20:29:51 +0000479 // Start the definition of this class. If we're in a redefinition case, there
480 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000481 if (!IDecl->hasDefinition())
482 IDecl->startDefinition();
483
Chris Lattner4d391482007-12-12 07:09:47 +0000484 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000485 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000486 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
487 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000488
489 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000490 // Try to correct for a typo in the superclass name without correcting
491 // to the class we're defining.
492 ObjCInterfaceValidatorCCC Validator(IDecl);
493 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000494 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000495 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000496 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
497 Diag(SuperLoc, diag::err_undef_superclass_suggest)
498 << SuperName << ClassName << PrevDecl->getDeclName();
499 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
500 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000501 }
502 }
503
Douglas Gregor60ef3082011-12-15 00:29:59 +0000504 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000505 Diag(SuperLoc, diag::err_recursive_superclass)
506 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000507 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000508 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000509 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000510 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000511
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000512 // Diagnose classes that inherit from deprecated classes.
513 if (SuperClassDecl)
514 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000515
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000516 if (PrevDecl && SuperClassDecl == 0) {
517 // The previous declaration was not a class decl. Check if we have a
518 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000519 if (const TypedefNameDecl *TDecl =
520 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000521 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000522 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000523 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
524 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000525 }
526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000528 // This handles the following case:
529 //
530 // typedef int SuperClass;
531 // @interface MyClass : SuperClass {} @end
532 //
533 if (!SuperClassDecl) {
534 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
535 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000536 }
537 }
Mike Stump1eb44332009-09-09 15:08:12 +0000538
Richard Smith162e1c12011-04-15 14:24:37 +0000539 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000540 if (!SuperClassDecl)
541 Diag(SuperLoc, diag::err_undef_superclass)
542 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000543 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000544 Context.getObjCInterfaceType(SuperClassDecl),
545 diag::err_forward_superclass,
546 SuperClassDecl->getDeclName(),
547 ClassName,
548 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000549 SuperClassDecl = 0;
550 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000551 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000552 IDecl->setSuperClass(SuperClassDecl);
553 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000554 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000555 }
Chris Lattner4d391482007-12-12 07:09:47 +0000556 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000557 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000558 }
Mike Stump1eb44332009-09-09 15:08:12 +0000559
Sebastian Redl0b17c612010-08-13 00:28:03 +0000560 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000561 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000562 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000563 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000564 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000565 }
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Anders Carlsson15281452008-11-04 16:57:32 +0000567 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000568 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000569}
570
Richard Smithde01b7a2012-08-08 23:32:13 +0000571/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000572/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000573Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
574 IdentifierInfo *AliasName,
575 SourceLocation AliasLocation,
576 IdentifierInfo *ClassName,
577 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000578 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000579 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000580 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000581 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000582 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000583 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000584 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000585 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000586 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000587 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000588 }
589 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000590 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000591 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000592 if (const TypedefNameDecl *TDecl =
593 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000594 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000595 if (T->isObjCObjectType()) {
596 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000597 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000598 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000599 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000600 }
601 }
602 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000603 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
604 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000605 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000606 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000607 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000608 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000609 }
Mike Stump1eb44332009-09-09 15:08:12 +0000610
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000611 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000612 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000613 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Anders Carlsson15281452008-11-04 16:57:32 +0000615 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000616 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000617
John McCalld226f652010-08-21 09:40:31 +0000618 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000619}
620
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000621bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000622 IdentifierInfo *PName,
623 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000624 const ObjCList<ObjCProtocolDecl> &PList) {
625
626 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000627 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
628 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000629 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
630 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000631 if (PDecl->getIdentifier() == PName) {
632 Diag(Ploc, diag::err_protocol_has_circular_dependency);
633 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000634 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000635 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000636
637 if (!PDecl->hasDefinition())
638 continue;
639
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000640 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
641 PDecl->getLocation(), PDecl->getReferencedProtocols()))
642 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000643 }
644 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000645 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000646}
647
John McCalld226f652010-08-21 09:40:31 +0000648Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000649Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
650 IdentifierInfo *ProtocolName,
651 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000652 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000653 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000654 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000655 SourceLocation EndProtoLoc,
656 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000657 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000658 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000659 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000660 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
661 ForRedeclaration);
662 ObjCProtocolDecl *PDecl = 0;
663 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
664 // If we already have a definition, complain.
665 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
666 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Douglas Gregor27c6da22012-01-01 20:30:41 +0000668 // Create a new protocol that is completely distinct from previous
669 // declarations, and do not make this protocol available for name lookup.
670 // That way, we'll end up completely ignoring the duplicate.
671 // FIXME: Can we turn this into an error?
672 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
673 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000674 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000675 PDecl->startDefinition();
676 } else {
677 if (PrevDecl) {
678 // Check for circular dependencies among protocol declarations. This can
679 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000680 ObjCList<ObjCProtocolDecl> PList;
681 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
682 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000683 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000684 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000685
686 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000687 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000688 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000689 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000690
Douglas Gregor6e378de2009-04-23 23:18:26 +0000691 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000692 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000693 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000694
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000695 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000696 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000697
698 // Merge attributes from previous declarations.
699 if (PrevDecl)
700 mergeDeclAttributes(PDecl, PrevDecl);
701
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000702 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000703 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000704 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000705 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000706 }
Mike Stump1eb44332009-09-09 15:08:12 +0000707
708 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000709 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000710}
711
712/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000713/// issues an error if they are not declared. It returns list of
714/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000715void
Chris Lattnere13b9592008-07-26 04:03:38 +0000716Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000717 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000718 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000719 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000720 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000721 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
722 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000723 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000724 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000725 TypoCorrection Corrected = CorrectTypo(
726 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000727 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000728 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000729 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000730 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000731 Diag(PDecl->getLocation(), diag::note_previous_decl)
732 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000733 }
734 }
735
736 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000737 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000738 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000739 continue;
740 }
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000742 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000743
744 // If this is a forward declaration and we are supposed to warn in this
745 // case, do it.
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000746 // FIXME: Recover nicely in the hidden case.
747 if (WarnOnDeclarations &&
748 (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000749 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000750 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000751 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000752 }
753}
754
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000755/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000756/// a class method in its extension.
757///
Mike Stump1eb44332009-09-09 15:08:12 +0000758void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000759 ObjCInterfaceDecl *ID) {
760 if (!ID)
761 return; // Possibly due to previous error
762
763 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000764 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
765 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000766 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000767 MethodMap[MD->getSelector()] = MD;
768 }
769
770 if (MethodMap.empty())
771 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000772 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
773 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000774 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000775 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
776 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
777 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
778 << Method->getDeclName();
779 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
780 }
781 }
782}
783
James Dennett1dfbd922012-06-14 21:40:34 +0000784/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000785Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000786Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000787 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000788 unsigned NumElts,
789 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000790 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000791 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000792 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000793 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
794 ForRedeclaration);
795 ObjCProtocolDecl *PDecl
796 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
797 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000798 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000799
800 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000801 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000802
Douglas Gregor3937f872012-01-01 20:33:24 +0000803 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000804 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000805
806 if (PrevDecl)
807 mergeDeclAttributes(PDecl, PrevDecl);
808
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000809 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000810 }
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000812 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000813}
814
John McCalld226f652010-08-21 09:40:31 +0000815Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000816ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
817 IdentifierInfo *ClassName, SourceLocation ClassLoc,
818 IdentifierInfo *CategoryName,
819 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000820 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000821 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000822 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000823 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000824 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000825 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000826
827 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000828
829 if (!IDecl
830 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000831 diag::err_category_forward_interface,
832 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000833 // Create an invalid ObjCCategoryDecl to serve as context for
834 // the enclosing method declarations. We mark the decl invalid
835 // to make it clear that this isn't a valid AST.
836 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000837 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000838 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000839 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000840
841 if (!IDecl)
842 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000843 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000844 }
845
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000846 if (!CategoryName && IDecl->getImplementation()) {
847 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
848 Diag(IDecl->getImplementation()->getLocation(),
849 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000850 }
851
Fariborz Jahanian25760612010-02-15 21:55:26 +0000852 if (CategoryName) {
853 /// Check for duplicate interface declaration for this category
Douglas Gregord3297242013-01-16 23:00:23 +0000854 if (ObjCCategoryDecl *Previous
855 = IDecl->FindCategoryDeclaration(CategoryName)) {
856 // Class extensions can be declared multiple times, categories cannot.
857 Diag(CategoryLoc, diag::warn_dup_category_def)
858 << ClassName << CategoryName;
859 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner70f19542009-02-16 21:26:43 +0000860 }
861 }
Chris Lattner70f19542009-02-16 21:26:43 +0000862
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000863 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
864 ClassLoc, CategoryLoc, CategoryName, IDecl);
865 // FIXME: PushOnScopeChains?
866 CurContext->addDecl(CDecl);
867
Chris Lattner4d391482007-12-12 07:09:47 +0000868 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000869 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000870 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000871 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000872 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000873 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000874 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000875 }
Mike Stump1eb44332009-09-09 15:08:12 +0000876
Anders Carlsson15281452008-11-04 16:57:32 +0000877 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000878 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000879}
880
881/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000882/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000883/// object.
John McCalld226f652010-08-21 09:40:31 +0000884Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000885 SourceLocation AtCatImplLoc,
886 IdentifierInfo *ClassName, SourceLocation ClassLoc,
887 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000888 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000889 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000890 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000891 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
892 if (!CatIDecl) {
893 // Category @implementation with no corresponding @interface.
894 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000895 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
896 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000897 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000898 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000899 }
900 }
901
Mike Stump1eb44332009-09-09 15:08:12 +0000902 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000903 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000904 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000905 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000906 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000907 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000908 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000909 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
910 diag::err_undef_interface)) {
911 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000912 }
Chris Lattner4d391482007-12-12 07:09:47 +0000913
Douglas Gregord0434102009-01-09 00:49:46 +0000914 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000915 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000916
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000917 // If the interface is deprecated/unavailable, warn/error about it.
918 if (IDecl)
919 DiagnoseUseOfDecl(IDecl, ClassLoc);
920
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000921 /// Check that CatName, category name, is not used in another implementation.
922 if (CatIDecl) {
923 if (CatIDecl->getImplementation()) {
924 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
925 << CatName;
926 Diag(CatIDecl->getImplementation()->getLocation(),
927 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000928 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000929 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000930 // Warn on implementating category of deprecated class under
931 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000932 DiagnoseObjCImplementedDeprecations(*this,
933 dyn_cast<NamedDecl>(IDecl),
934 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000935 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000936 }
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Anders Carlsson15281452008-11-04 16:57:32 +0000938 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000939 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000940}
941
John McCalld226f652010-08-21 09:40:31 +0000942Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000943 SourceLocation AtClassImplLoc,
944 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000945 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000946 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000947 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000948 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000949 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000950 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
951 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000952 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000953 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000954 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000955 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000956 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
957 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000958 } else {
959 // We did not find anything with the name ClassName; try to correct for
960 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000961 ObjCInterfaceValidatorCCC Validator;
962 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000963 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000964 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000965 // Suggest the (potentially) correct interface name. However, put the
966 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000967 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000968 // provide a code-modification hint or use the typo name for recovery,
969 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000970 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000971 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000972 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000973 << ClassName << CorrectedName;
974 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
975 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000976 IDecl = 0;
977 } else {
978 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
979 }
Chris Lattner4d391482007-12-12 07:09:47 +0000980 }
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Chris Lattner4d391482007-12-12 07:09:47 +0000982 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000983 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000984 if (SuperClassname) {
985 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000986 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
987 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000988 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000989 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
990 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000991 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000992 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000993 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000994 if (SDecl && !SDecl->hasDefinition())
995 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000996 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000997 Diag(SuperClassLoc, diag::err_undef_superclass)
998 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000999 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001000 // This implementation and its interface do not have the same
1001 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +00001002 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +00001003 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001004 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001005 }
1006 }
1007 }
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Chris Lattner4d391482007-12-12 07:09:47 +00001009 if (!IDecl) {
1010 // Legacy case of @implementation with no corresponding @interface.
1011 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +00001012
Mike Stump390b4cc2009-05-16 07:39:55 +00001013 // FIXME: Do we support attributes on the @implementation? If so we should
1014 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001015 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001016 ClassName, /*PrevDecl=*/0, ClassLoc,
1017 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001018 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001019 if (SDecl) {
1020 IDecl->setSuperClass(SDecl);
1021 IDecl->setSuperClassLoc(SuperClassLoc);
1022 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1023 } else {
1024 IDecl->setEndOfDefinitionLoc(ClassLoc);
1025 }
1026
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001027 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001028 } else {
1029 // Mark the interface as being completed, even if it was just as
1030 // @class ....;
1031 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001032 if (!IDecl->hasDefinition())
1033 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001034 }
Mike Stump1eb44332009-09-09 15:08:12 +00001035
1036 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001037 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1038 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Anders Carlsson15281452008-11-04 16:57:32 +00001040 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001041 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Chris Lattner4d391482007-12-12 07:09:47 +00001043 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001044 if (IDecl->getImplementation()) {
1045 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001046 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001047 Diag(IDecl->getImplementation()->getLocation(),
1048 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001049 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001050 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001051 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001052 // Warn on implementating deprecated class under
1053 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001054 DiagnoseObjCImplementedDeprecations(*this,
1055 dyn_cast<NamedDecl>(IDecl),
1056 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001057 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001058 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001059}
1060
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001061Sema::DeclGroupPtrTy
1062Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1063 SmallVector<Decl *, 64> DeclsInGroup;
1064 DeclsInGroup.reserve(Decls.size() + 1);
1065
1066 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1067 Decl *Dcl = Decls[i];
1068 if (!Dcl)
1069 continue;
1070 if (Dcl->getDeclContext()->isFileContext())
1071 Dcl->setTopLevelDeclInObjCContainer();
1072 DeclsInGroup.push_back(Dcl);
1073 }
1074
1075 DeclsInGroup.push_back(ObjCImpDecl);
1076
1077 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1078}
1079
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001080void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1081 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001082 SourceLocation RBrace) {
1083 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001084 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001085 if (!IDecl)
1086 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001087 /// Check case of non-existing \@interface decl.
1088 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001089 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001090 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001091 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001092 // Add ivar's to class's DeclContext.
1093 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001094 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001095 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001096 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001097 }
1098
Chris Lattner4d391482007-12-12 07:09:47 +00001099 return;
1100 }
1101 // If implementation has empty ivar list, just return.
1102 if (numIvars == 0)
1103 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001104
Chris Lattner4d391482007-12-12 07:09:47 +00001105 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001106 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001107 if (ImpDecl->getSuperClass())
1108 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1109 for (unsigned i = 0; i < numIvars; i++) {
1110 ObjCIvarDecl* ImplIvar = ivars[i];
1111 if (const ObjCIvarDecl *ClsIvar =
1112 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1113 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1114 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1115 continue;
1116 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001117 // Instance ivar to Implementation's DeclContext.
1118 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001119 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001120 ImpDecl->addDecl(ImplIvar);
1121 }
1122 return;
1123 }
Chris Lattner4d391482007-12-12 07:09:47 +00001124 // Check interface's Ivar list against those in the implementation.
1125 // names and types must match.
1126 //
Chris Lattner4d391482007-12-12 07:09:47 +00001127 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001128 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001129 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1130 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001131 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001132 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001133 assert (ImplIvar && "missing implementation ivar");
1134 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Steve Naroffca331292009-03-03 14:49:36 +00001136 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001137 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001138 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001139 << ImplIvar->getIdentifier()
1140 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001141 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001142 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1143 ImplIvar->getBitWidthValue(Context) !=
1144 ClsIvar->getBitWidthValue(Context)) {
1145 Diag(ImplIvar->getBitWidth()->getLocStart(),
1146 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1147 Diag(ClsIvar->getBitWidth()->getLocStart(),
1148 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001149 }
Steve Naroffca331292009-03-03 14:49:36 +00001150 // Make sure the names are identical.
1151 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001152 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001153 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001154 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001155 }
1156 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001157 }
Mike Stump1eb44332009-09-09 15:08:12 +00001158
Chris Lattner609e4c72007-12-12 18:11:49 +00001159 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001160 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001161 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001162 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001163}
1164
Steve Naroff3c2eb662008-02-10 21:38:56 +00001165void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001166 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001167 // No point warning no definition of method which is 'unavailable'.
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001168 switch (method->getAvailability()) {
1169 case AR_Available:
1170 case AR_Deprecated:
1171 break;
1172
1173 // Don't warn about unavailable or not-yet-introduced methods.
1174 case AR_NotYetIntroduced:
1175 case AR_Unavailable:
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001176 return;
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001177 }
1178
Steve Naroff3c2eb662008-02-10 21:38:56 +00001179 if (!IncompleteImpl) {
1180 Diag(ImpLoc, diag::warn_incomplete_impl);
1181 IncompleteImpl = true;
1182 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001183 if (DiagID == diag::warn_unimplemented_protocol_method)
1184 Diag(ImpLoc, DiagID) << method->getDeclName();
1185 else
1186 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001187}
1188
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001189/// Determines if type B can be substituted for type A. Returns true if we can
1190/// guarantee that anything that the user will do to an object of type A can
1191/// also be done to an object of type B. This is trivially true if the two
1192/// types are the same, or if B is a subclass of A. It becomes more complex
1193/// in cases where protocols are involved.
1194///
1195/// Object types in Objective-C describe the minimum requirements for an
1196/// object, rather than providing a complete description of a type. For
1197/// example, if A is a subclass of B, then B* may refer to an instance of A.
1198/// The principle of substitutability means that we may use an instance of A
1199/// anywhere that we may use an instance of B - it will implement all of the
1200/// ivars of B and all of the methods of B.
1201///
1202/// This substitutability is important when type checking methods, because
1203/// the implementation may have stricter type definitions than the interface.
1204/// The interface specifies minimum requirements, but the implementation may
1205/// have more accurate ones. For example, a method may privately accept
1206/// instances of B, but only publish that it accepts instances of A. Any
1207/// object passed to it will be type checked against B, and so will implicitly
1208/// by a valid A*. Similarly, a method may return a subclass of the class that
1209/// it is declared as returning.
1210///
1211/// This is most important when considering subclassing. A method in a
1212/// subclass must accept any object as an argument that its superclass's
1213/// implementation accepts. It may, however, accept a more general type
1214/// without breaking substitutability (i.e. you can still use the subclass
1215/// anywhere that you can use the superclass, but not vice versa). The
1216/// converse requirement applies to return types: the return type for a
1217/// subclass method must be a valid object of the kind that the superclass
1218/// advertises, but it may be specified more accurately. This avoids the need
1219/// for explicit down-casting by callers.
1220///
1221/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001222static bool isObjCTypeSubstitutable(ASTContext &Context,
1223 const ObjCObjectPointerType *A,
1224 const ObjCObjectPointerType *B,
1225 bool rejectId) {
1226 // Reject a protocol-unqualified id.
1227 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001228
1229 // If B is a qualified id, then A must also be a qualified id and it must
1230 // implement all of the protocols in B. It may not be a qualified class.
1231 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1232 // stricter definition so it is not substitutable for id<A>.
1233 if (B->isObjCQualifiedIdType()) {
1234 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001235 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1236 QualType(B,0),
1237 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001238 }
1239
1240 /*
1241 // id is a special type that bypasses type checking completely. We want a
1242 // warning when it is used in one place but not another.
1243 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1244
1245
1246 // If B is a qualified id, then A must also be a qualified id (which it isn't
1247 // if we've got this far)
1248 if (B->isObjCQualifiedIdType()) return false;
1249 */
1250
1251 // Now we know that A and B are (potentially-qualified) class types. The
1252 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001253 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001254}
1255
John McCall10302c02010-10-28 02:34:38 +00001256static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1257 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1258}
1259
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001260static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001261 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001262 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001263 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001264 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001265 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001266 if (IsProtocolMethodDecl &&
1267 (MethodDecl->getObjCDeclQualifier() !=
1268 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001269 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001270 S.Diag(MethodImpl->getLocation(),
1271 (IsOverridingMode ?
1272 diag::warn_conflicting_overriding_ret_type_modifiers
1273 : diag::warn_conflicting_ret_type_modifiers))
1274 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001275 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1276 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1277 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1278 }
1279 else
1280 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001281 }
1282
John McCall10302c02010-10-28 02:34:38 +00001283 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001284 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001285 return true;
1286 if (!Warn)
1287 return false;
John McCall10302c02010-10-28 02:34:38 +00001288
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001289 unsigned DiagID =
1290 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1291 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001292
1293 // Mismatches between ObjC pointers go into a different warning
1294 // category, and sometimes they're even completely whitelisted.
1295 if (const ObjCObjectPointerType *ImplPtrTy =
1296 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1297 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001298 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001299 // Allow non-matching return types as long as they don't violate
1300 // the principle of substitutability. Specifically, we permit
1301 // return types that are subclasses of the declared return type,
1302 // or that are more-qualified versions of the declared type.
1303 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001304 return false;
John McCall10302c02010-10-28 02:34:38 +00001305
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001306 DiagID =
1307 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1308 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001309 }
1310 }
1311
1312 S.Diag(MethodImpl->getLocation(), DiagID)
1313 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001314 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001315 << MethodImpl->getResultType()
1316 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001317 S.Diag(MethodDecl->getLocation(),
1318 IsOverridingMode ? diag::note_previous_declaration
1319 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001320 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001321 return false;
John McCall10302c02010-10-28 02:34:38 +00001322}
1323
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001324static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001325 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001326 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001327 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001328 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001329 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001330 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001331 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001332 if (IsProtocolMethodDecl &&
1333 (ImplVar->getObjCDeclQualifier() !=
1334 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001335 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001336 if (IsOverridingMode)
1337 S.Diag(ImplVar->getLocation(),
1338 diag::warn_conflicting_overriding_param_modifiers)
1339 << getTypeRange(ImplVar->getTypeSourceInfo())
1340 << MethodImpl->getDeclName();
1341 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001342 diag::warn_conflicting_param_modifiers)
1343 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001344 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001345 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1346 << getTypeRange(IfaceVar->getTypeSourceInfo());
1347 }
1348 else
1349 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001350 }
1351
John McCall10302c02010-10-28 02:34:38 +00001352 QualType ImplTy = ImplVar->getType();
1353 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001354
John McCall10302c02010-10-28 02:34:38 +00001355 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001356 return true;
1357
1358 if (!Warn)
1359 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001360 unsigned DiagID =
1361 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1362 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001363
1364 // Mismatches between ObjC pointers go into a different warning
1365 // category, and sometimes they're even completely whitelisted.
1366 if (const ObjCObjectPointerType *ImplPtrTy =
1367 ImplTy->getAs<ObjCObjectPointerType>()) {
1368 if (const ObjCObjectPointerType *IfacePtrTy =
1369 IfaceTy->getAs<ObjCObjectPointerType>()) {
1370 // Allow non-matching argument types as long as they don't
1371 // violate the principle of substitutability. Specifically, the
1372 // implementation must accept any objects that the superclass
1373 // accepts, however it may also accept others.
1374 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001375 return false;
John McCall10302c02010-10-28 02:34:38 +00001376
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001377 DiagID =
1378 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1379 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001380 }
1381 }
1382
1383 S.Diag(ImplVar->getLocation(), DiagID)
1384 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001385 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1386 S.Diag(IfaceVar->getLocation(),
1387 (IsOverridingMode ? diag::note_previous_declaration
1388 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001389 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001390 return false;
John McCall10302c02010-10-28 02:34:38 +00001391}
John McCallf85e1932011-06-15 23:02:42 +00001392
1393/// In ARC, check whether the conventional meanings of the two methods
1394/// match. If they don't, it's a hard error.
1395static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1396 ObjCMethodDecl *decl) {
1397 ObjCMethodFamily implFamily = impl->getMethodFamily();
1398 ObjCMethodFamily declFamily = decl->getMethodFamily();
1399 if (implFamily == declFamily) return false;
1400
1401 // Since conventions are sorted by selector, the only possibility is
1402 // that the types differ enough to cause one selector or the other
1403 // to fall out of the family.
1404 assert(implFamily == OMF_None || declFamily == OMF_None);
1405
1406 // No further diagnostics required on invalid declarations.
1407 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1408
1409 const ObjCMethodDecl *unmatched = impl;
1410 ObjCMethodFamily family = declFamily;
1411 unsigned errorID = diag::err_arc_lost_method_convention;
1412 unsigned noteID = diag::note_arc_lost_method_convention;
1413 if (declFamily == OMF_None) {
1414 unmatched = decl;
1415 family = implFamily;
1416 errorID = diag::err_arc_gained_method_convention;
1417 noteID = diag::note_arc_gained_method_convention;
1418 }
1419
1420 // Indexes into a %select clause in the diagnostic.
1421 enum FamilySelector {
1422 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1423 };
1424 FamilySelector familySelector = FamilySelector();
1425
1426 switch (family) {
1427 case OMF_None: llvm_unreachable("logic error, no method convention");
1428 case OMF_retain:
1429 case OMF_release:
1430 case OMF_autorelease:
1431 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001432 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001433 case OMF_retainCount:
1434 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001435 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001436 // Mismatches for these methods don't change ownership
1437 // conventions, so we don't care.
1438 return false;
1439
1440 case OMF_init: familySelector = F_init; break;
1441 case OMF_alloc: familySelector = F_alloc; break;
1442 case OMF_copy: familySelector = F_copy; break;
1443 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1444 case OMF_new: familySelector = F_new; break;
1445 }
1446
1447 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1448 ReasonSelector reasonSelector;
1449
1450 // The only reason these methods don't fall within their families is
1451 // due to unusual result types.
1452 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1453 reasonSelector = R_UnrelatedReturn;
1454 } else {
1455 reasonSelector = R_NonObjectReturn;
1456 }
1457
1458 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1459 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1460
1461 return true;
1462}
John McCall10302c02010-10-28 02:34:38 +00001463
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001464void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001465 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001466 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001467 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001468 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1469 return;
1470
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001471 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001472 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001473 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Chris Lattner3aff9192009-04-11 19:58:42 +00001475 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001476 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1477 EF = MethodDecl->param_end();
1478 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001479 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001480 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001481 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001482
Fariborz Jahanian21121902011-08-08 18:03:17 +00001483 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001484 Diag(ImpMethodDecl->getLocation(),
1485 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001486 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001487 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001488}
1489
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001490void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1491 ObjCMethodDecl *Overridden,
1492 bool IsProtocolMethodDecl) {
1493
1494 CheckMethodOverrideReturn(*this, Method, Overridden,
1495 IsProtocolMethodDecl, true,
1496 true);
1497
1498 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001499 IF = Overridden->param_begin(), EM = Method->param_end(),
1500 EF = Overridden->param_end();
1501 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001502 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1503 IsProtocolMethodDecl, true, true);
1504 }
1505
1506 if (Method->isVariadic() != Overridden->isVariadic()) {
1507 Diag(Method->getLocation(),
1508 diag::warn_conflicting_overriding_variadic);
1509 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1510 }
1511}
1512
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001513/// WarnExactTypedMethods - This routine issues a warning if method
1514/// implementation declaration matches exactly that of its declaration.
1515void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1516 ObjCMethodDecl *MethodDecl,
1517 bool IsProtocolMethodDecl) {
1518 // don't issue warning when protocol method is optional because primary
1519 // class is not required to implement it and it is safe for protocol
1520 // to implement it.
1521 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1522 return;
1523 // don't issue warning when primary class's method is
1524 // depecated/unavailable.
1525 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1526 MethodDecl->hasAttr<DeprecatedAttr>())
1527 return;
1528
1529 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1530 IsProtocolMethodDecl, false, false);
1531 if (match)
1532 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001533 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1534 EF = MethodDecl->param_end();
1535 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001536 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1537 *IM, *IF,
1538 IsProtocolMethodDecl, false, false);
1539 if (!match)
1540 break;
1541 }
1542 if (match)
1543 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001544 if (match)
1545 match = !(MethodDecl->isClassMethod() &&
1546 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001547
1548 if (match) {
1549 Diag(ImpMethodDecl->getLocation(),
1550 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001551 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1552 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001553 }
1554}
1555
Mike Stump390b4cc2009-05-16 07:39:55 +00001556/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1557/// improve the efficiency of selector lookups and type checking by associating
1558/// with each protocol / interface / category the flattened instance tables. If
1559/// we used an immutable set to keep the table then it wouldn't add significant
1560/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001561
Steve Naroffefe7f362008-02-08 22:06:17 +00001562/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001563/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001564void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1565 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001566 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001567 const SelectorSet &InsMap,
1568 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001569 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001570 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1571 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1572 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001573 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1574
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001575 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001576 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001577 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001578 // check to see if class implements forwardInvocation method and objects
1579 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001580 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001581 // Under such conditions, which means that every method possible is
1582 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001583 // found" warnings.
1584 // FIXME: Use a general GetUnarySelector method for this.
1585 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1586 Selector fISelector = Context.Selectors.getSelector(1, &II);
1587 if (InsMap.count(fISelector))
1588 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1589 // need be implemented in the implementation.
1590 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1591 }
Mike Stump1eb44332009-09-09 15:08:12 +00001592
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001593 // If this is a forward protocol declaration, get its definition.
1594 if (!PDecl->isThisDeclarationADefinition() &&
1595 PDecl->getDefinition())
1596 PDecl = PDecl->getDefinition();
1597
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001598 // If a method lookup fails locally we still need to look and see if
1599 // the method was implemented by a base class or an inherited
1600 // protocol. This lookup is slow, but occurs rarely in correct code
1601 // and otherwise would terminate in a warning.
1602
Chris Lattner4d391482007-12-12 07:09:47 +00001603 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001604 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001605 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001606 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001607 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001608 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001609 !method->isPropertyAccessor() &&
1610 !InsMap.count(method->getSelector()) &&
1611 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001612 // If a method is not implemented in the category implementation but
1613 // has been declared in its primary class, superclass,
1614 // or in one of their protocols, no need to issue the warning.
1615 // This is because method will be implemented in the primary class
1616 // or one of its super class implementation.
1617
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001618 // Ugly, but necessary. Method declared in protcol might have
1619 // have been synthesized due to a property declared in the class which
1620 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001621 if (ObjCMethodDecl *MethodInClass =
1622 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001623 true /*shallowCategoryLookup*/))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001624 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001625 continue;
1626 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1627 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1628 != DiagnosticsEngine::Ignored) {
1629 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001630 Diag(method->getLocation(), diag::note_method_declared_at)
1631 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001632 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1633 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001634 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001635 }
1636 }
Chris Lattner4d391482007-12-12 07:09:47 +00001637 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001638 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001639 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001640 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001641 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001642 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1643 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001644 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001645 // See above comment for instance method lookups.
1646 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001647 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001648 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001649 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001650 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1651 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001652 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001653 Diag(method->getLocation(), diag::note_method_declared_at)
1654 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001655 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1656 PDecl->getDeclName();
1657 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001658 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001659 }
Chris Lattner780f3292008-07-21 21:32:27 +00001660 // Check on this protocols's referenced protocols, recursively.
1661 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1662 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001663 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001664}
1665
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001666/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001667/// or protocol against those declared in their implementations.
1668///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001669void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1670 const SelectorSet &ClsMap,
1671 SelectorSet &InsMapSeen,
1672 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001673 ObjCImplDecl* IMPDecl,
1674 ObjCContainerDecl* CDecl,
1675 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001676 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001677 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001678 // Check and see if instance methods in class interface have been
1679 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001680 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1681 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001682 if (InsMapSeen.count((*I)->getSelector()))
1683 continue;
1684 InsMapSeen.insert((*I)->getSelector());
Jordan Rose1e4691b2012-10-10 16:42:25 +00001685 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001686 !InsMap.count((*I)->getSelector())) {
1687 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001688 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1689 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001690 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001691 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001692 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001693 IMPDecl->getInstanceMethod((*I)->getSelector());
1694 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1695 "Expected to find the method through lookup as well");
1696 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001697 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001698 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001699 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001700 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1701 isa<ObjCProtocolDecl>(CDecl));
Jordan Rose1e4691b2012-10-10 16:42:25 +00001702 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001703 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001704 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001705 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001706 }
1707 }
Mike Stump1eb44332009-09-09 15:08:12 +00001708
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001709 // Check and see if class methods in class interface have been
1710 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001711 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001712 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001713 if (ClsMapSeen.count((*I)->getSelector()))
1714 continue;
1715 ClsMapSeen.insert((*I)->getSelector());
1716 if (!ClsMap.count((*I)->getSelector())) {
1717 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001718 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1719 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001720 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001721 ObjCMethodDecl *ImpMethodDecl =
1722 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001723 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1724 "Expected to find the method through lookup as well");
1725 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001726 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001727 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1728 isa<ObjCProtocolDecl>(CDecl));
1729 else
1730 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001731 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001732 }
1733 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001734
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001735 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001736 // when checking that methods in implementation match their declaration,
1737 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1738 // extension; as well as those in categories.
Douglas Gregord3297242013-01-16 23:00:23 +00001739 if (!WarnCategoryMethodImpl) {
1740 for (ObjCInterfaceDecl::visible_categories_iterator
1741 Cat = I->visible_categories_begin(),
1742 CatEnd = I->visible_categories_end();
1743 Cat != CatEnd; ++Cat) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001744 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001745 IMPDecl, *Cat, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001746 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001747 }
1748 } else {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001749 // Also methods in class extensions need be looked at next.
Douglas Gregord3297242013-01-16 23:00:23 +00001750 for (ObjCInterfaceDecl::visible_extensions_iterator
1751 Ext = I->visible_extensions_begin(),
1752 ExtEnd = I->visible_extensions_end();
1753 Ext != ExtEnd; ++Ext) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001754 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001755 IMPDecl, *Ext, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001756 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001757 }
1758 }
1759
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)
Douglas Gregord3297242013-01-16 23:00:23 +00001862 for (ObjCInterfaceDecl::visible_extensions_iterator
1863 Ext = I->visible_extensions_begin(),
1864 ExtEnd = I->visible_extensions_end();
1865 Ext != ExtEnd; ++Ext) {
1866 ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
1867 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001868 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001869 // For extended class, unimplemented methods in its protocols will
1870 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001871 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001872 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1873 E = C->protocol_end(); PI != E; ++PI)
1874 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001875 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001876 // Report unimplemented properties in the category as well.
1877 // When reporting on missing setter/getters, do not report when
1878 // setter/getter is implemented in category's primary class
1879 // implementation.
1880 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1881 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1882 for (ObjCImplementationDecl::instmeth_iterator
1883 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1884 InsMap.insert((*I)->getSelector());
1885 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001886 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001887 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001888 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001889 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001890}
1891
Mike Stump1eb44332009-09-09 15:08:12 +00001892/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001893Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001894Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001895 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001896 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001897 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001898 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001899 for (unsigned i = 0; i != NumElts; ++i) {
1900 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001901 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001902 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001903 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001904 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001905 // Maybe we will complain about the shadowed template parameter.
1906 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1907 // Just pretend that we didn't see the previous declaration.
1908 PrevDecl = 0;
1909 }
1910
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001911 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001912 // GCC apparently allows the following idiom:
1913 //
1914 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1915 // @class XCElementToggler;
1916 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001917 // Here we have chosen to ignore the forward class declaration
1918 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001919 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001920 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001921 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001922 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001923 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001924 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001925 // to the underlying class. Just ignore the forward class with a warning
1926 // as this will force the intended behavior which is to lookup the typedef
1927 // name.
1928 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1929 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1930 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1931 continue;
1932 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001933 }
Chris Lattner4d391482007-12-12 07:09:47 +00001934 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001935
1936 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001937 ObjCInterfaceDecl *PrevIDecl
1938 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001939 ObjCInterfaceDecl *IDecl
1940 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001941 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001942 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001943
Douglas Gregor7723fec2011-12-15 20:29:51 +00001944 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001945 CheckObjCDeclScope(IDecl);
1946 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001947 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001948
1949 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001950}
1951
John McCall0f4c4c42011-06-16 01:15:19 +00001952static bool tryMatchRecordTypes(ASTContext &Context,
1953 Sema::MethodMatchStrategy strategy,
1954 const Type *left, const Type *right);
1955
John McCallf85e1932011-06-15 23:02:42 +00001956static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1957 QualType leftQT, QualType rightQT) {
1958 const Type *left =
1959 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1960 const Type *right =
1961 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1962
1963 if (left == right) return true;
1964
1965 // If we're doing a strict match, the types have to match exactly.
1966 if (strategy == Sema::MMS_strict) return false;
1967
1968 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1969
1970 // Otherwise, use this absurdly complicated algorithm to try to
1971 // validate the basic, low-level compatibility of the two types.
1972
1973 // As a minimum, require the sizes and alignments to match.
1974 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1975 return false;
1976
1977 // Consider all the kinds of non-dependent canonical types:
1978 // - functions and arrays aren't possible as return and parameter types
1979
1980 // - vector types of equal size can be arbitrarily mixed
1981 if (isa<VectorType>(left)) return isa<VectorType>(right);
1982 if (isa<VectorType>(right)) return false;
1983
1984 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001985 // - structs, unions, and Objective-C objects must match more-or-less
1986 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001987 // - everything else should be a scalar
1988 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001989 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001990
John McCall1d9b3b22011-09-09 05:25:32 +00001991 // Make scalars agree in kind, except count bools as chars, and group
1992 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001993 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1994 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1995 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1996 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001997 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1998 leftSK = Type::STK_ObjCObjectPointer;
1999 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2000 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00002001
2002 // Note that data member pointers and function member pointers don't
2003 // intermix because of the size differences.
2004
2005 return (leftSK == rightSK);
2006}
Chris Lattner4d391482007-12-12 07:09:47 +00002007
John McCall0f4c4c42011-06-16 01:15:19 +00002008static bool tryMatchRecordTypes(ASTContext &Context,
2009 Sema::MethodMatchStrategy strategy,
2010 const Type *lt, const Type *rt) {
2011 assert(lt && rt && lt != rt);
2012
2013 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2014 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2015 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2016
2017 // Require union-hood to match.
2018 if (left->isUnion() != right->isUnion()) return false;
2019
2020 // Require an exact match if either is non-POD.
2021 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2022 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2023 return false;
2024
2025 // Require size and alignment to match.
2026 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2027
2028 // Require fields to match.
2029 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2030 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2031 for (; li != le && ri != re; ++li, ++ri) {
2032 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2033 return false;
2034 }
2035 return (li == le && ri == re);
2036}
2037
Chris Lattner4d391482007-12-12 07:09:47 +00002038/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2039/// returns true, or false, accordingly.
2040/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00002041bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2042 const ObjCMethodDecl *right,
2043 MethodMatchStrategy strategy) {
2044 if (!matchTypes(Context, strategy,
2045 left->getResultType(), right->getResultType()))
2046 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002047
Douglas Gregor7666b032013-02-07 19:13:24 +00002048 // If either is hidden, it is not considered to match.
2049 if (left->isHidden() || right->isHidden())
2050 return false;
2051
David Blaikie4e4d0842012-03-11 07:00:24 +00002052 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002053 (left->hasAttr<NSReturnsRetainedAttr>()
2054 != right->hasAttr<NSReturnsRetainedAttr>() ||
2055 left->hasAttr<NSConsumesSelfAttr>()
2056 != right->hasAttr<NSConsumesSelfAttr>()))
2057 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002058
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002059 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002060 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2061 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002062
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002063 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002064 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002065 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002066
2067 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2068 return false;
2069
David Blaikie4e4d0842012-03-11 07:00:24 +00002070 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002071 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2072 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002073 }
2074 return true;
2075}
2076
Douglas Gregorff310c72012-05-01 23:37:00 +00002077void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002078 // If the list is empty, make it a singleton list.
2079 if (List->Method == 0) {
2080 List->Method = Method;
2081 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002082 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002083 }
2084
2085 // We've seen a method with this name, see if we have already seen this type
2086 // signature.
2087 ObjCMethodList *Previous = List;
2088 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002089 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002090 continue;
2091
2092 ObjCMethodDecl *PrevObjCMethod = List->Method;
2093
2094 // Propagate the 'defined' bit.
2095 if (Method->isDefined())
2096 PrevObjCMethod->setDefined(true);
2097
2098 // If a method is deprecated, push it in the global pool.
2099 // This is used for better diagnostics.
2100 if (Method->isDeprecated()) {
2101 if (!PrevObjCMethod->isDeprecated())
2102 List->Method = Method;
2103 }
2104 // If new method is unavailable, push it into global pool
2105 // unless previous one is deprecated.
2106 if (Method->isUnavailable()) {
2107 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2108 List->Method = Method;
2109 }
2110
Douglas Gregorff310c72012-05-01 23:37:00 +00002111 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002112 }
2113
2114 // We have a new signature for an existing method - add it.
2115 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002116 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002117 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2118}
2119
Sebastian Redldb9d2142010-08-02 23:18:59 +00002120/// \brief Read the contents of the method pool for a given selector from
2121/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002122void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002123 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002124 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002125}
2126
Douglas Gregorff310c72012-05-01 23:37:00 +00002127void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002128 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002129 // Ignore methods of invalid containers.
2130 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002131 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002132
Douglas Gregor0d266d62012-01-25 00:59:09 +00002133 if (ExternalSource)
2134 ReadMethodPool(Method->getSelector());
2135
Sebastian Redldb9d2142010-08-02 23:18:59 +00002136 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002137 if (Pos == MethodPool.end())
2138 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2139 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002140
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002141 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002142
Sebastian Redldb9d2142010-08-02 23:18:59 +00002143 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002144 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002145}
2146
John McCallf85e1932011-06-15 23:02:42 +00002147/// Determines if this is an "acceptable" loose mismatch in the global
2148/// method pool. This exists mostly as a hack to get around certain
2149/// global mismatches which we can't afford to make warnings / errors.
2150/// Really, what we want is a way to take a method out of the global
2151/// method pool.
2152static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2153 ObjCMethodDecl *other) {
2154 if (!chosen->isInstanceMethod())
2155 return false;
2156
2157 Selector sel = chosen->getSelector();
2158 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2159 return false;
2160
2161 // Don't complain about mismatches for -length if the method we
2162 // chose has an integral result type.
2163 return (chosen->getResultType()->isIntegerType());
2164}
2165
Sebastian Redldb9d2142010-08-02 23:18:59 +00002166ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002167 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002168 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002169 if (ExternalSource)
2170 ReadMethodPool(Sel);
2171
Sebastian Redldb9d2142010-08-02 23:18:59 +00002172 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002173 if (Pos == MethodPool.end())
2174 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002175
Douglas Gregorf0e00042013-01-16 18:47:38 +00002176 // Gather the non-hidden methods.
Sebastian Redldb9d2142010-08-02 23:18:59 +00002177 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorf0e00042013-01-16 18:47:38 +00002178 llvm::SmallVector<ObjCMethodDecl *, 4> Methods;
2179 for (ObjCMethodList *M = &MethList; M; M = M->Next) {
2180 if (M->Method && !M->Method->isHidden()) {
2181 // If we're not supposed to warn about mismatches, we're done.
2182 if (!warn)
2183 return M->Method;
Mike Stump1eb44332009-09-09 15:08:12 +00002184
Douglas Gregorf0e00042013-01-16 18:47:38 +00002185 Methods.push_back(M->Method);
Sebastian Redldb9d2142010-08-02 23:18:59 +00002186 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002187 }
Douglas Gregorf0e00042013-01-16 18:47:38 +00002188
2189 // If there aren't any visible methods, we're done.
2190 // FIXME: Recover if there are any known-but-hidden methods?
2191 if (Methods.empty())
2192 return 0;
2193
2194 if (Methods.size() == 1)
2195 return Methods[0];
2196
2197 // We found multiple methods, so we may have to complain.
2198 bool issueDiagnostic = false, issueError = false;
2199
2200 // We support a warning which complains about *any* difference in
2201 // method signature.
2202 bool strictSelectorMatch =
2203 (receiverIdOrClass && warn &&
2204 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2205 R.getBegin())
2206 != DiagnosticsEngine::Ignored));
2207 if (strictSelectorMatch) {
2208 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2209 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2210 issueDiagnostic = true;
2211 break;
2212 }
2213 }
2214 }
2215
2216 // If we didn't see any strict differences, we won't see any loose
2217 // differences. In ARC, however, we also need to check for loose
2218 // mismatches, because most of them are errors.
2219 if (!strictSelectorMatch ||
2220 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2221 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2222 // This checks if the methods differ in type mismatch.
2223 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2224 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2225 issueDiagnostic = true;
2226 if (getLangOpts().ObjCAutoRefCount)
2227 issueError = true;
2228 break;
2229 }
2230 }
2231
2232 if (issueDiagnostic) {
2233 if (issueError)
2234 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2235 else if (strictSelectorMatch)
2236 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2237 else
2238 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2239
2240 Diag(Methods[0]->getLocStart(),
2241 issueError ? diag::note_possibility : diag::note_using)
2242 << Methods[0]->getSourceRange();
2243 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2244 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2245 << Methods[I]->getSourceRange();
2246 }
2247 }
2248 return Methods[0];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002249}
2250
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002251ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002252 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2253 if (Pos == MethodPool.end())
2254 return 0;
2255
2256 GlobalMethods &Methods = Pos->second;
2257
2258 if (Methods.first.Method && Methods.first.Method->isDefined())
2259 return Methods.first.Method;
2260 if (Methods.second.Method && Methods.second.Method->isDefined())
2261 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002262 return 0;
2263}
2264
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002265/// DiagnoseDuplicateIvars -
2266/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002267/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002268/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002269/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002270void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2271 ObjCInterfaceDecl *SID) {
2272 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2273 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002274 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002275 if (Ivar->isInvalidDecl())
2276 continue;
2277 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2278 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2279 if (prevIvar) {
2280 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2281 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2282 Ivar->setInvalidDecl();
2283 }
2284 }
2285 }
2286}
2287
Erik Verbruggend64251f2011-12-06 09:25:23 +00002288Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2289 switch (CurContext->getDeclKind()) {
2290 case Decl::ObjCInterface:
2291 return Sema::OCK_Interface;
2292 case Decl::ObjCProtocol:
2293 return Sema::OCK_Protocol;
2294 case Decl::ObjCCategory:
2295 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2296 return Sema::OCK_ClassExtension;
2297 else
2298 return Sema::OCK_Category;
2299 case Decl::ObjCImplementation:
2300 return Sema::OCK_Implementation;
2301 case Decl::ObjCCategoryImpl:
2302 return Sema::OCK_CategoryImplementation;
2303
2304 default:
2305 return Sema::OCK_None;
2306 }
2307}
2308
Steve Naroffa56f6162007-12-18 01:30:32 +00002309// Note: For class/category implemenations, allMethods/allProperties is
2310// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002311Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2312 Decl **allMethods, unsigned allNum,
2313 Decl **allProperties, unsigned pNum,
2314 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002315
Erik Verbruggend64251f2011-12-06 09:25:23 +00002316 if (getObjCContainerKind() == Sema::OCK_None)
2317 return 0;
2318
2319 assert(AtEnd.isValid() && "Invalid location for '@end'");
2320
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002321 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2322 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002323
Mike Stump1eb44332009-09-09 15:08:12 +00002324 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002325 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2326 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002327 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002328
Steve Naroff0701bbb2009-01-08 17:28:14 +00002329 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2330 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2331 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2332
Chris Lattner4d391482007-12-12 07:09:47 +00002333 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002334 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002335 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002336
2337 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002338 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002339 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002340 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002341 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002342 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002343 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002344 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002345 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002346 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002347 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002348 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002349 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002350 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002351 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002352 if (!Context.getSourceManager().isInSystemHeader(
2353 Method->getLocation()))
2354 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2355 << Method->getDeclName();
2356 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2357 }
Chris Lattner4d391482007-12-12 07:09:47 +00002358 InsMap[Method->getSelector()] = Method;
2359 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002360 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002361 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002362 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002363 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002364 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002365 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002366 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002367 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002368 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002369 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002370 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002371 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002372 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002373 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002374 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002375 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002376 if (!Context.getSourceManager().isInSystemHeader(
2377 Method->getLocation()))
2378 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2379 << Method->getDeclName();
2380 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2381 }
Chris Lattner4d391482007-12-12 07:09:47 +00002382 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002383 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002384 }
2385 }
2386 }
Douglas Gregorb892d702013-01-21 19:42:21 +00002387 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2388 // Nothing to do here.
Steve Naroff09c47192009-01-09 15:36:25 +00002389 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002390 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002391 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002392 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002393
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002394 if (C->IsClassExtension()) {
2395 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2396 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002397 }
Chris Lattner4d391482007-12-12 07:09:47 +00002398 }
Steve Naroff09c47192009-01-09 15:36:25 +00002399 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002400 if (CDecl->getIdentifier())
2401 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2402 // user-defined setter/getter. It also synthesizes setter/getter methods
2403 // and adds them to the DeclContext and global method pools.
2404 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2405 E = CDecl->prop_end();
2406 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002407 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002408 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002409 }
2410 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002411 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002412 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002413 // Any property declared in a class extension might have user
2414 // declared setter or getter in current class extension or one
2415 // of the other class extensions. Mark them as synthesized as
2416 // property will be synthesized when property with same name is
2417 // seen in the @implementation.
Douglas Gregord3297242013-01-16 23:00:23 +00002418 for (ObjCInterfaceDecl::visible_extensions_iterator
2419 Ext = IDecl->visible_extensions_begin(),
2420 ExtEnd = IDecl->visible_extensions_end();
2421 Ext != ExtEnd; ++Ext) {
2422 for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
2423 E = Ext->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002424 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002425 // Skip over properties declared @dynamic
2426 if (const ObjCPropertyImplDecl *PIDecl
2427 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2428 if (PIDecl->getPropertyImplementation()
2429 == ObjCPropertyImplDecl::Dynamic)
2430 continue;
Douglas Gregord3297242013-01-16 23:00:23 +00002431
2432 for (ObjCInterfaceDecl::visible_extensions_iterator
2433 Ext = IDecl->visible_extensions_begin(),
2434 ExtEnd = IDecl->visible_extensions_end();
2435 Ext != ExtEnd; ++Ext) {
2436 if (ObjCMethodDecl *GetterMethod
2437 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002438 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002439 if (!Property->isReadOnly())
Douglas Gregord3297242013-01-16 23:00:23 +00002440 if (ObjCMethodDecl *SetterMethod
2441 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002442 SetterMethod->setPropertyAccessor(true);
Douglas Gregord3297242013-01-16 23:00:23 +00002443 }
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002444 }
2445 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002446 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002447 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002448 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002449
Patrick Beardb2f68202012-04-06 18:12:22 +00002450 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2451 if (IDecl->getSuperClass() == NULL) {
2452 // This class has no superclass, so check that it has been marked with
2453 // __attribute((objc_root_class)).
2454 if (!HasRootClassAttr) {
2455 SourceLocation DeclLoc(IDecl->getLocation());
2456 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2457 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2458 << IDecl->getIdentifier();
2459 // See if NSObject is in the current scope, and if it is, suggest
2460 // adding " : NSObject " to the class declaration.
2461 NamedDecl *IF = LookupSingleName(TUScope,
2462 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2463 DeclLoc, LookupOrdinaryName);
2464 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2465 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2466 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2467 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2468 } else {
2469 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2470 }
2471 }
2472 } else if (HasRootClassAttr) {
2473 // Complain that only root classes may have this attribute.
2474 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2475 }
2476
John McCall260611a2012-06-20 06:18:46 +00002477 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002478 while (IDecl->getSuperClass()) {
2479 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2480 IDecl = IDecl->getSuperClass();
2481 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002482 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002483 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002484 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002485 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002486 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002487 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002488
Chris Lattner4d391482007-12-12 07:09:47 +00002489 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002490 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002491 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregord3297242013-01-16 23:00:23 +00002492 if (ObjCCategoryDecl *Cat
2493 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2494 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattner4d391482007-12-12 07:09:47 +00002495 }
2496 }
2497 }
Chris Lattner682bf922009-03-29 16:50:03 +00002498 if (isInterfaceDeclKind) {
2499 // Reject invalid vardecls.
2500 for (unsigned i = 0; i != tuvNum; i++) {
2501 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2502 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2503 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002504 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002505 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002506 }
Chris Lattner682bf922009-03-29 16:50:03 +00002507 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002508 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002509 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002510
2511 for (unsigned i = 0; i != tuvNum; i++) {
2512 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002513 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2514 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002515 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2516 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002517
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002518 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002519 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002520}
2521
2522
2523/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2524/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002525static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002526CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002527 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002528}
2529
Ted Kremenek422bae72010-04-18 04:59:38 +00002530static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002531unsigned countAlignAttr(const AttrVec &A) {
2532 unsigned count=0;
2533 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2534 if ((*i)->getKind() == attr::Aligned)
2535 ++count;
2536 return count;
2537}
2538
2539static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002540bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2541 const AttrVec &A) {
2542 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002543 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002544 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002545 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002546
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002547 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002548 // But implementation has attributes. This is invalid.
2549 // Except when implementation has 'Align' attribute which is
2550 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002551 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002552 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002553
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002554 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002555
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002556 unsigned countAlignOnImpl = countAlignAttr(A);
2557 if (!countAlignOnImpl && (A.size() != D.size()))
2558 return true;
2559 else if (countAlignOnImpl) {
2560 unsigned countAlignOnDecl = countAlignAttr(D);
2561 if (countAlignOnDecl && (A.size() != D.size()))
2562 return true;
2563 else if (!countAlignOnDecl &&
2564 ((A.size()-countAlignOnImpl) != D.size()))
2565 return true;
2566 }
2567
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002568 // attributes on method declaration and definition must match exactly.
2569 // Note that we have at most a couple of attributes on methods, so this
2570 // n*n search is good enough.
2571 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002572 if ((*i)->getKind() == attr::Aligned)
2573 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002574 bool match = false;
2575 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2576 if ((*i)->getKind() == (*i1)->getKind()) {
2577 match = true;
2578 break;
2579 }
2580 }
2581 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002582 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002583 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002584
Sean Huntcf807c42010-08-18 23:23:40 +00002585 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002586}
2587
Douglas Gregor926df6c2011-06-11 01:09:30 +00002588/// \brief Check whether the declared result type of the given Objective-C
2589/// method declaration is compatible with the method's class.
2590///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002591static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002592CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2593 ObjCInterfaceDecl *CurrentClass) {
2594 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002595
2596 // If an Objective-C method inherits its related result type, then its
2597 // declared result type must be compatible with its own class type. The
2598 // declared result type is compatible if:
2599 if (const ObjCObjectPointerType *ResultObjectType
2600 = ResultType->getAs<ObjCObjectPointerType>()) {
2601 // - it is id or qualified id, or
2602 if (ResultObjectType->isObjCIdType() ||
2603 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002604 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002605
2606 if (CurrentClass) {
2607 if (ObjCInterfaceDecl *ResultClass
2608 = ResultObjectType->getInterfaceDecl()) {
2609 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002610 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002611 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002612
2613 // - it is a superclass of the method's class type
2614 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002615 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002616 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002617 } else {
2618 // Any Objective-C pointer type might be acceptable for a protocol
2619 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002620 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002621 }
2622 }
2623
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002624 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002625}
2626
John McCall6c2c2502011-07-22 02:45:48 +00002627namespace {
2628/// A helper class for searching for methods which a particular method
2629/// overrides.
2630class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002631public:
John McCall6c2c2502011-07-22 02:45:48 +00002632 Sema &S;
2633 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002634 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002635 bool Recursive;
2636
2637public:
2638 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2639 Selector selector = method->getSelector();
2640
2641 // Bypass this search if we've never seen an instance/class method
2642 // with this selector before.
2643 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2644 if (it == S.MethodPool.end()) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00002645 if (!S.getExternalSource()) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002646 S.ReadMethodPool(selector);
2647
2648 it = S.MethodPool.find(selector);
2649 if (it == S.MethodPool.end())
2650 return;
John McCall6c2c2502011-07-22 02:45:48 +00002651 }
2652 ObjCMethodList &list =
2653 method->isInstanceMethod() ? it->second.first : it->second.second;
2654 if (!list.Method) return;
2655
2656 ObjCContainerDecl *container
2657 = cast<ObjCContainerDecl>(method->getDeclContext());
2658
2659 // Prevent the search from reaching this container again. This is
2660 // important with categories, which override methods from the
2661 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002662 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2663 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002664 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2665 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002666 } else {
2667 searchFromContainer(container);
2668 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002669 }
John McCall6c2c2502011-07-22 02:45:48 +00002670
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002671 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002672 iterator begin() const { return Overridden.begin(); }
2673 iterator end() const { return Overridden.end(); }
2674
2675private:
2676 void searchFromContainer(ObjCContainerDecl *container) {
2677 if (container->isInvalidDecl()) return;
2678
2679 switch (container->getDeclKind()) {
2680#define OBJCCONTAINER(type, base) \
2681 case Decl::type: \
2682 searchFrom(cast<type##Decl>(container)); \
2683 break;
2684#define ABSTRACT_DECL(expansion)
2685#define DECL(type, base) \
2686 case Decl::type:
2687#include "clang/AST/DeclNodes.inc"
2688 llvm_unreachable("not an ObjC container!");
2689 }
2690 }
2691
2692 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002693 if (!protocol->hasDefinition())
2694 return;
2695
John McCall6c2c2502011-07-22 02:45:48 +00002696 // A method in a protocol declaration overrides declarations from
2697 // referenced ("parent") protocols.
2698 search(protocol->getReferencedProtocols());
2699 }
2700
2701 void searchFrom(ObjCCategoryDecl *category) {
2702 // A method in a category declaration overrides declarations from
2703 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002704 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002705 search(category->getReferencedProtocols());
2706 }
2707
2708 void searchFrom(ObjCCategoryImplDecl *impl) {
2709 // A method in a category definition that has a category
2710 // declaration overrides declarations from the category
2711 // declaration.
2712 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2713 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002714 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2715 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002716
2717 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002718 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2719 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002720 }
2721 }
2722
2723 void searchFrom(ObjCInterfaceDecl *iface) {
2724 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002725 if (!iface->hasDefinition())
2726 return;
2727
John McCall6c2c2502011-07-22 02:45:48 +00002728 // - categories,
Douglas Gregord3297242013-01-16 23:00:23 +00002729 for (ObjCInterfaceDecl::visible_categories_iterator
2730 cat = iface->visible_categories_begin(),
2731 catEnd = iface->visible_categories_end();
2732 cat != catEnd; ++cat) {
2733 search(*cat);
2734 }
John McCall6c2c2502011-07-22 02:45:48 +00002735
2736 // - the super class, and
2737 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2738 search(super);
2739
2740 // - any referenced protocols.
2741 search(iface->getReferencedProtocols());
2742 }
2743
2744 void searchFrom(ObjCImplementationDecl *impl) {
2745 // A method in a class implementation overrides declarations from
2746 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002747 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2748 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002749 }
2750
2751
2752 void search(const ObjCProtocolList &protocols) {
2753 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2754 i != e; ++i)
2755 search(*i);
2756 }
2757
2758 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002759 // Check for a method in this container which matches this selector.
2760 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2761 Method->isInstanceMethod());
2762
2763 // If we find one, record it and bail out.
2764 if (meth) {
2765 Overridden.insert(meth);
2766 return;
2767 }
2768
2769 // Otherwise, search for methods that a hypothetical method here
2770 // would have overridden.
2771
2772 // Note that we're now in a recursive case.
2773 Recursive = true;
2774
2775 searchFromContainer(container);
2776 }
2777};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002778}
2779
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002780void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2781 ObjCInterfaceDecl *CurrentClass,
2782 ResultTypeCompatibilityKind RTC) {
2783 // Search for overridden methods and merge information down from them.
2784 OverrideSearch overrides(*this, ObjCMethod);
2785 // Keep track if the method overrides any method in the class's base classes,
2786 // its protocols, or its categories' protocols; we will keep that info
2787 // in the ObjCMethodDecl.
2788 // For this info, a method in an implementation is not considered as
2789 // overriding the same method in the interface or its categories.
2790 bool hasOverriddenMethodsInBaseOrProtocol = false;
2791 for (OverrideSearch::iterator
2792 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2793 ObjCMethodDecl *overridden = *i;
2794
2795 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2796 CurrentClass != overridden->getClassInterface() ||
2797 overridden->isOverriding())
2798 hasOverriddenMethodsInBaseOrProtocol = true;
2799
2800 // Propagate down the 'related result type' bit from overridden methods.
2801 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2802 ObjCMethod->SetRelatedResultType();
2803
2804 // Then merge the declarations.
2805 mergeObjCMethodDecls(ObjCMethod, overridden);
2806
2807 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2808 continue; // Conflicting properties are detected elsewhere.
2809
2810 // Check for overriding methods
2811 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2812 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2813 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2814 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2815
2816 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00002817 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
2818 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002819 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2820 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002821 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2822 PrevE = overridden->param_end();
2823 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002824 assert(PrevI != overridden->param_end() && "Param mismatch");
2825 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2826 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2827 // If type of argument of method in this class does not match its
2828 // respective argument type in the super class method, issue warning;
2829 if (!Context.typesAreCompatible(T1, T2)) {
2830 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2831 << T1 << T2;
2832 Diag(overridden->getLocation(), diag::note_previous_declaration);
2833 break;
2834 }
2835 }
2836 }
2837 }
2838
2839 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2840}
2841
John McCalld226f652010-08-21 09:40:31 +00002842Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002843 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002844 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002845 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002846 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002847 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002848 Selector Sel,
2849 // optional arguments. The number of types/arguments is obtained
2850 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002851 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002852 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002853 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002854 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002855 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002856 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002857 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002858 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002859 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002860 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2861 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002862 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002863
Douglas Gregore97179c2011-09-08 01:46:34 +00002864 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002865 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002866 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002867 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002868
Steve Naroffccef3712009-02-20 22:59:16 +00002869 // Methods cannot return interface types. All ObjC objects are
2870 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002871 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002872 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2873 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002874 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002875 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002876
2877 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002878 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002879 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002880 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002881 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002882 }
Mike Stump1eb44332009-09-09 15:08:12 +00002883
2884 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002885 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002886 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002887 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002888 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002889 MethodType == tok::minus, isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +00002890 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002891 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002892 MethodDeclKind == tok::objc_optional
2893 ? ObjCMethodDecl::Optional
2894 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002895 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002896
Chris Lattner5f9e2722011-07-23 10:55:15 +00002897 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002898
Chris Lattner7db638d2009-04-11 19:42:43 +00002899 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002900 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002901 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002902
Chris Lattnere294d3f2009-04-11 18:57:04 +00002903 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002904 ArgType = Context.getObjCIdType();
2905 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002906 } else {
John McCall58e46772009-10-23 21:48:59 +00002907 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002908 }
Mike Stump1eb44332009-09-09 15:08:12 +00002909
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002910 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2911 LookupOrdinaryName, ForRedeclaration);
2912 LookupName(R, S);
2913 if (R.isSingleResult()) {
2914 NamedDecl *PrevDecl = R.getFoundDecl();
2915 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002916 Diag(ArgInfo[i].NameLoc,
2917 (MethodDefinition ? diag::warn_method_param_redefinition
2918 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002919 << ArgInfo[i].Name;
2920 Diag(PrevDecl->getLocation(),
2921 diag::note_previous_declaration);
2922 }
2923 }
2924
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002925 SourceLocation StartLoc = DI
2926 ? DI->getTypeLoc().getBeginLoc()
2927 : ArgInfo[i].NameLoc;
2928
John McCall81ef3e62011-04-23 02:46:06 +00002929 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2930 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2931 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002932
John McCall70798862011-05-02 00:30:12 +00002933 Param->setObjCMethodScopeInfo(i);
2934
Chris Lattner0ed844b2008-04-04 06:12:32 +00002935 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002936 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002937
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002938 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002939 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002940
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002941 if (Param->hasAttr<BlocksAttr>()) {
2942 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2943 Param->setInvalidDecl();
2944 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002945 S->AddDecl(Param);
2946 IdResolver.AddDecl(Param);
2947
Chris Lattner0ed844b2008-04-04 06:12:32 +00002948 Params.push_back(Param);
2949 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002950
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002951 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002952 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002953 QualType ArgType = Param->getType();
2954 if (ArgType.isNull())
2955 ArgType = Context.getObjCIdType();
2956 else
2957 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002958 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002959 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002960 Diag(Param->getLocation(),
2961 diag::err_object_cannot_be_passed_returned_by_value)
2962 << 1 << ArgType;
2963 Param->setInvalidDecl();
2964 }
2965 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002966
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002967 Params.push_back(Param);
2968 }
2969
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002970 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002971 ObjCMethod->setObjCDeclQualifier(
2972 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002973
2974 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002975 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002976
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002977 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002978 const ObjCMethodDecl *PrevMethod = 0;
2979 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002980 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002981 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2982 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002983 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002984 PrevMethod = ImpDecl->getClassMethod(Sel);
2985 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002986 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002987
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002988 ObjCMethodDecl *IMD = 0;
2989 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2990 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2991 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002992 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002993 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002994 SourceLocation MethodLoc = IMD->getLocation();
2995 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2996 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002997 Diag(MethodLoc, diag::note_method_declared_at)
2998 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002999 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00003000 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003001 } else {
3002 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003003 }
John McCall6c2c2502011-07-22 02:45:48 +00003004
Chris Lattner4d391482007-12-12 07:09:47 +00003005 if (PrevMethod) {
3006 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00003007 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00003008 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00003009 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00003010 }
John McCall54abf7d2009-11-04 02:18:39 +00003011
Douglas Gregor926df6c2011-06-11 01:09:30 +00003012 // If this Objective-C method does not have a related result type, but we
3013 // are allowed to infer related result types, try to do so based on the
3014 // method family.
3015 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3016 if (!CurrentClass) {
3017 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3018 CurrentClass = Cat->getClassInterface();
3019 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3020 CurrentClass = Impl->getClassInterface();
3021 else if (ObjCCategoryImplDecl *CatImpl
3022 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3023 CurrentClass = CatImpl->getClassInterface();
3024 }
John McCall6c2c2502011-07-22 02:45:48 +00003025
Douglas Gregore97179c2011-09-08 01:46:34 +00003026 ResultTypeCompatibilityKind RTC
3027 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00003028
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003029 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00003030
John McCallf85e1932011-06-15 23:02:42 +00003031 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00003032 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00003033 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
3034
Douglas Gregore97179c2011-09-08 01:46:34 +00003035 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003036 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00003037 !ObjCMethod->hasRelatedResultType() &&
3038 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00003039 bool InferRelatedResultType = false;
3040 switch (ObjCMethod->getMethodFamily()) {
3041 case OMF_None:
3042 case OMF_copy:
3043 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00003044 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003045 case OMF_mutableCopy:
3046 case OMF_release:
3047 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00003048 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003049 break;
3050
3051 case OMF_alloc:
3052 case OMF_new:
3053 InferRelatedResultType = ObjCMethod->isClassMethod();
3054 break;
3055
3056 case OMF_init:
3057 case OMF_autorelease:
3058 case OMF_retain:
3059 case OMF_self:
3060 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3061 break;
3062 }
3063
John McCall6c2c2502011-07-22 02:45:48 +00003064 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003065 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003066 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003067
3068 ActOnDocumentableDecl(ObjCMethod);
3069
John McCalld226f652010-08-21 09:40:31 +00003070 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003071}
3072
Chris Lattnercc98eac2008-12-17 07:13:27 +00003073bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003074 // Following is also an error. But it is caused by a missing @end
3075 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003076 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003077 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003078
3079 // If we switched context to translation unit while we are still lexically in
3080 // an objc container, it means the parser missed emitting an error.
3081 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3082 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003083
Anders Carlsson15281452008-11-04 16:57:32 +00003084 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3085 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003086
Anders Carlsson15281452008-11-04 16:57:32 +00003087 return true;
3088}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003089
James Dennett1dfbd922012-06-14 21:40:34 +00003090/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003091/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003092void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003093 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003094 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003095 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003096 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003097 if (!Class) {
3098 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3099 return;
3100 }
John McCall260611a2012-06-20 06:18:46 +00003101 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003102 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3103 return;
3104 }
Mike Stump1eb44332009-09-09 15:08:12 +00003105
Chris Lattnercc98eac2008-12-17 07:13:27 +00003106 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003107 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003108 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003109 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003110 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003111 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003112 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003113 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3114 /*FIXME: StartL=*/ID->getLocation(),
3115 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003116 ID->getIdentifier(), ID->getType(),
3117 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003118 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003119 }
Mike Stump1eb44332009-09-09 15:08:12 +00003120
Chris Lattnercc98eac2008-12-17 07:13:27 +00003121 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003122 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003123 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003124 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003125 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003126 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003127 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003128 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003129 }
3130}
3131
Douglas Gregor160b5632010-04-26 17:32:49 +00003132/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003133VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3134 SourceLocation StartLoc,
3135 SourceLocation IdLoc,
3136 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003137 bool Invalid) {
3138 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3139 // duration shall not be qualified by an address-space qualifier."
3140 // Since all parameters have automatic store duration, they can not have
3141 // an address space.
3142 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003143 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003144 Invalid = true;
3145 }
3146
3147 // An @catch parameter must be an unqualified object pointer type;
3148 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3149 if (Invalid) {
3150 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003151 } else if (T->isDependentType()) {
3152 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003153 } else if (!T->isObjCObjectPointerType()) {
3154 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003155 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003156 } else if (T->isObjCQualifiedIdType()) {
3157 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003158 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003159 }
3160
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003161 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3162 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003163 New->setExceptionVariable(true);
3164
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003165 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003166 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003167 Invalid = true;
3168
Douglas Gregor160b5632010-04-26 17:32:49 +00003169 if (Invalid)
3170 New->setInvalidDecl();
3171 return New;
3172}
3173
John McCalld226f652010-08-21 09:40:31 +00003174Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003175 const DeclSpec &DS = D.getDeclSpec();
3176
3177 // We allow the "register" storage class on exception variables because
3178 // GCC did, but we drop it completely. Any other storage class is an error.
3179 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3180 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3181 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3182 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3183 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3184 << DS.getStorageClassSpec();
3185 }
3186 if (D.getDeclSpec().isThreadSpecified())
3187 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3188 D.getMutableDeclSpec().ClearStorageClassSpecs();
3189
3190 DiagnoseFunctionSpecifiers(D);
3191
3192 // Check that there are no default arguments inside the type of this
3193 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003194 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003195 CheckExtraCXXDefaultArguments(D);
3196
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003197 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003198 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003199
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003200 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3201 D.getSourceRange().getBegin(),
3202 D.getIdentifierLoc(),
3203 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003204 D.isInvalidType());
3205
3206 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3207 if (D.getCXXScopeSpec().isSet()) {
3208 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3209 << D.getCXXScopeSpec().getRange();
3210 New->setInvalidDecl();
3211 }
3212
3213 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003214 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003215 if (D.getIdentifier())
3216 IdResolver.AddDecl(New);
3217
3218 ProcessDeclAttributes(S, New, D);
3219
3220 if (New->hasAttr<BlocksAttr>())
3221 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003222 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003223}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003224
3225/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003226/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003227void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003228 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003229 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3230 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003231 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003232 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003233 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003234 }
3235}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003236
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003237void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003238 // Load referenced selectors from the external source.
3239 if (ExternalSource) {
3240 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3241 ExternalSource->ReadReferencedSelectors(Sels);
3242 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3243 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3244 }
3245
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003246 // Warning will be issued only when selector table is
3247 // generated (which means there is at lease one implementation
3248 // in the TU). This is to match gcc's behavior.
3249 if (ReferencedSelectors.empty() ||
3250 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003251 return;
3252 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3253 ReferencedSelectors.begin(),
3254 E = ReferencedSelectors.end(); S != E; ++S) {
3255 Selector Sel = (*S).first;
3256 if (!LookupImplementedMethodInGlobalPool(Sel))
3257 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3258 }
3259 return;
3260}