blob: 54cf1c26260ab1960b54437a5557fdbfc2ea29d9 [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 Gregor5e2a1ff2012-01-01 19:29:29 +0000746 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000747 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000748 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000749 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000750 }
751}
752
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000753/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000754/// a class method in its extension.
755///
Mike Stump1eb44332009-09-09 15:08:12 +0000756void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000757 ObjCInterfaceDecl *ID) {
758 if (!ID)
759 return; // Possibly due to previous error
760
761 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000762 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
763 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000764 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000765 MethodMap[MD->getSelector()] = MD;
766 }
767
768 if (MethodMap.empty())
769 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000770 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
771 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000772 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000773 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
774 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
775 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
776 << Method->getDeclName();
777 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
778 }
779 }
780}
781
James Dennett1dfbd922012-06-14 21:40:34 +0000782/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000783Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000784Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000785 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000786 unsigned NumElts,
787 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000788 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000789 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000790 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000791 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
792 ForRedeclaration);
793 ObjCProtocolDecl *PDecl
794 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
795 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000796 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000797
798 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000799 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000800
Douglas Gregor3937f872012-01-01 20:33:24 +0000801 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000802 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000803
804 if (PrevDecl)
805 mergeDeclAttributes(PDecl, PrevDecl);
806
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000807 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000808 }
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000810 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000811}
812
John McCalld226f652010-08-21 09:40:31 +0000813Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000814ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
815 IdentifierInfo *ClassName, SourceLocation ClassLoc,
816 IdentifierInfo *CategoryName,
817 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000818 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000819 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000820 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000821 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000822 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000823 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000824
825 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000826
827 if (!IDecl
828 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000829 diag::err_category_forward_interface,
830 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000831 // Create an invalid ObjCCategoryDecl to serve as context for
832 // the enclosing method declarations. We mark the decl invalid
833 // to make it clear that this isn't a valid AST.
834 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000835 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000836 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000837 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000838
839 if (!IDecl)
840 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000841 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000842 }
843
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000844 if (!CategoryName && IDecl->getImplementation()) {
845 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
846 Diag(IDecl->getImplementation()->getLocation(),
847 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000848 }
849
Fariborz Jahanian25760612010-02-15 21:55:26 +0000850 if (CategoryName) {
851 /// Check for duplicate interface declaration for this category
Douglas Gregord3297242013-01-16 23:00:23 +0000852 if (ObjCCategoryDecl *Previous
853 = IDecl->FindCategoryDeclaration(CategoryName)) {
854 // Class extensions can be declared multiple times, categories cannot.
855 Diag(CategoryLoc, diag::warn_dup_category_def)
856 << ClassName << CategoryName;
857 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner70f19542009-02-16 21:26:43 +0000858 }
859 }
Chris Lattner70f19542009-02-16 21:26:43 +0000860
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000861 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
862 ClassLoc, CategoryLoc, CategoryName, IDecl);
863 // FIXME: PushOnScopeChains?
864 CurContext->addDecl(CDecl);
865
Chris Lattner4d391482007-12-12 07:09:47 +0000866 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000867 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000868 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000869 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000870 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000871 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000872 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Anders Carlsson15281452008-11-04 16:57:32 +0000875 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000876 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000877}
878
879/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000880/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000881/// object.
John McCalld226f652010-08-21 09:40:31 +0000882Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000883 SourceLocation AtCatImplLoc,
884 IdentifierInfo *ClassName, SourceLocation ClassLoc,
885 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000886 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000887 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000888 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000889 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
890 if (!CatIDecl) {
891 // Category @implementation with no corresponding @interface.
892 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000893 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
894 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000895 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000896 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000897 }
898 }
899
Mike Stump1eb44332009-09-09 15:08:12 +0000900 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000901 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000902 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000903 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000904 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000905 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000906 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000907 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
908 diag::err_undef_interface)) {
909 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000910 }
Chris Lattner4d391482007-12-12 07:09:47 +0000911
Douglas Gregord0434102009-01-09 00:49:46 +0000912 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000913 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000914
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000915 // If the interface is deprecated/unavailable, warn/error about it.
916 if (IDecl)
917 DiagnoseUseOfDecl(IDecl, ClassLoc);
918
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000919 /// Check that CatName, category name, is not used in another implementation.
920 if (CatIDecl) {
921 if (CatIDecl->getImplementation()) {
922 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
923 << CatName;
924 Diag(CatIDecl->getImplementation()->getLocation(),
925 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000926 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000927 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000928 // Warn on implementating category of deprecated class under
929 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000930 DiagnoseObjCImplementedDeprecations(*this,
931 dyn_cast<NamedDecl>(IDecl),
932 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000933 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000934 }
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Anders Carlsson15281452008-11-04 16:57:32 +0000936 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000937 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000938}
939
John McCalld226f652010-08-21 09:40:31 +0000940Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000941 SourceLocation AtClassImplLoc,
942 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000943 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000944 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000945 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000946 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000947 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000948 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
949 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000950 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000951 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000952 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000953 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000954 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
955 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000956 } else {
957 // We did not find anything with the name ClassName; try to correct for
958 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000959 ObjCInterfaceValidatorCCC Validator;
960 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000961 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000962 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000963 // Suggest the (potentially) correct interface name. However, put the
964 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000965 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000966 // provide a code-modification hint or use the typo name for recovery,
967 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000968 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000969 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000970 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000971 << ClassName << CorrectedName;
972 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
973 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000974 IDecl = 0;
975 } else {
976 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
977 }
Chris Lattner4d391482007-12-12 07:09:47 +0000978 }
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Chris Lattner4d391482007-12-12 07:09:47 +0000980 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000981 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000982 if (SuperClassname) {
983 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000984 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
985 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000987 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
988 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000989 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000990 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000991 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000992 if (SDecl && !SDecl->hasDefinition())
993 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000994 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000995 Diag(SuperClassLoc, diag::err_undef_superclass)
996 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000997 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000998 // This implementation and its interface do not have the same
999 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +00001000 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +00001001 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001002 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001003 }
1004 }
1005 }
Mike Stump1eb44332009-09-09 15:08:12 +00001006
Chris Lattner4d391482007-12-12 07:09:47 +00001007 if (!IDecl) {
1008 // Legacy case of @implementation with no corresponding @interface.
1009 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +00001010
Mike Stump390b4cc2009-05-16 07:39:55 +00001011 // FIXME: Do we support attributes on the @implementation? If so we should
1012 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001013 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001014 ClassName, /*PrevDecl=*/0, ClassLoc,
1015 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001016 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001017 if (SDecl) {
1018 IDecl->setSuperClass(SDecl);
1019 IDecl->setSuperClassLoc(SuperClassLoc);
1020 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1021 } else {
1022 IDecl->setEndOfDefinitionLoc(ClassLoc);
1023 }
1024
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001025 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001026 } else {
1027 // Mark the interface as being completed, even if it was just as
1028 // @class ....;
1029 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001030 if (!IDecl->hasDefinition())
1031 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
1034 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001035 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1036 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Anders Carlsson15281452008-11-04 16:57:32 +00001038 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001039 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Chris Lattner4d391482007-12-12 07:09:47 +00001041 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001042 if (IDecl->getImplementation()) {
1043 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001044 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001045 Diag(IDecl->getImplementation()->getLocation(),
1046 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001047 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001048 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001049 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001050 // Warn on implementating deprecated class under
1051 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001052 DiagnoseObjCImplementedDeprecations(*this,
1053 dyn_cast<NamedDecl>(IDecl),
1054 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001055 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001056 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001057}
1058
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001059Sema::DeclGroupPtrTy
1060Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1061 SmallVector<Decl *, 64> DeclsInGroup;
1062 DeclsInGroup.reserve(Decls.size() + 1);
1063
1064 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1065 Decl *Dcl = Decls[i];
1066 if (!Dcl)
1067 continue;
1068 if (Dcl->getDeclContext()->isFileContext())
1069 Dcl->setTopLevelDeclInObjCContainer();
1070 DeclsInGroup.push_back(Dcl);
1071 }
1072
1073 DeclsInGroup.push_back(ObjCImpDecl);
1074
1075 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1076}
1077
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001078void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1079 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001080 SourceLocation RBrace) {
1081 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001082 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001083 if (!IDecl)
1084 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001085 /// Check case of non-existing \@interface decl.
1086 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001087 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001088 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001089 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001090 // Add ivar's to class's DeclContext.
1091 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001092 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001093 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001094 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001095 }
1096
Chris Lattner4d391482007-12-12 07:09:47 +00001097 return;
1098 }
1099 // If implementation has empty ivar list, just return.
1100 if (numIvars == 0)
1101 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001102
Chris Lattner4d391482007-12-12 07:09:47 +00001103 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001104 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001105 if (ImpDecl->getSuperClass())
1106 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1107 for (unsigned i = 0; i < numIvars; i++) {
1108 ObjCIvarDecl* ImplIvar = ivars[i];
1109 if (const ObjCIvarDecl *ClsIvar =
1110 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1111 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1112 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1113 continue;
1114 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001115 // Instance ivar to Implementation's DeclContext.
1116 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001117 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001118 ImpDecl->addDecl(ImplIvar);
1119 }
1120 return;
1121 }
Chris Lattner4d391482007-12-12 07:09:47 +00001122 // Check interface's Ivar list against those in the implementation.
1123 // names and types must match.
1124 //
Chris Lattner4d391482007-12-12 07:09:47 +00001125 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001126 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001127 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1128 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001129 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001130 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001131 assert (ImplIvar && "missing implementation ivar");
1132 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001133
Steve Naroffca331292009-03-03 14:49:36 +00001134 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001135 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001136 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001137 << ImplIvar->getIdentifier()
1138 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001139 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001140 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1141 ImplIvar->getBitWidthValue(Context) !=
1142 ClsIvar->getBitWidthValue(Context)) {
1143 Diag(ImplIvar->getBitWidth()->getLocStart(),
1144 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1145 Diag(ClsIvar->getBitWidth()->getLocStart(),
1146 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001147 }
Steve Naroffca331292009-03-03 14:49:36 +00001148 // Make sure the names are identical.
1149 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001150 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001151 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001152 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001153 }
1154 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001155 }
Mike Stump1eb44332009-09-09 15:08:12 +00001156
Chris Lattner609e4c72007-12-12 18:11:49 +00001157 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001158 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001159 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001160 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001161}
1162
Steve Naroff3c2eb662008-02-10 21:38:56 +00001163void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001164 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001165 // No point warning no definition of method which is 'unavailable'.
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001166 switch (method->getAvailability()) {
1167 case AR_Available:
1168 case AR_Deprecated:
1169 break;
1170
1171 // Don't warn about unavailable or not-yet-introduced methods.
1172 case AR_NotYetIntroduced:
1173 case AR_Unavailable:
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001174 return;
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001175 }
1176
Steve Naroff3c2eb662008-02-10 21:38:56 +00001177 if (!IncompleteImpl) {
1178 Diag(ImpLoc, diag::warn_incomplete_impl);
1179 IncompleteImpl = true;
1180 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001181 if (DiagID == diag::warn_unimplemented_protocol_method)
1182 Diag(ImpLoc, DiagID) << method->getDeclName();
1183 else
1184 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001185}
1186
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001187/// Determines if type B can be substituted for type A. Returns true if we can
1188/// guarantee that anything that the user will do to an object of type A can
1189/// also be done to an object of type B. This is trivially true if the two
1190/// types are the same, or if B is a subclass of A. It becomes more complex
1191/// in cases where protocols are involved.
1192///
1193/// Object types in Objective-C describe the minimum requirements for an
1194/// object, rather than providing a complete description of a type. For
1195/// example, if A is a subclass of B, then B* may refer to an instance of A.
1196/// The principle of substitutability means that we may use an instance of A
1197/// anywhere that we may use an instance of B - it will implement all of the
1198/// ivars of B and all of the methods of B.
1199///
1200/// This substitutability is important when type checking methods, because
1201/// the implementation may have stricter type definitions than the interface.
1202/// The interface specifies minimum requirements, but the implementation may
1203/// have more accurate ones. For example, a method may privately accept
1204/// instances of B, but only publish that it accepts instances of A. Any
1205/// object passed to it will be type checked against B, and so will implicitly
1206/// by a valid A*. Similarly, a method may return a subclass of the class that
1207/// it is declared as returning.
1208///
1209/// This is most important when considering subclassing. A method in a
1210/// subclass must accept any object as an argument that its superclass's
1211/// implementation accepts. It may, however, accept a more general type
1212/// without breaking substitutability (i.e. you can still use the subclass
1213/// anywhere that you can use the superclass, but not vice versa). The
1214/// converse requirement applies to return types: the return type for a
1215/// subclass method must be a valid object of the kind that the superclass
1216/// advertises, but it may be specified more accurately. This avoids the need
1217/// for explicit down-casting by callers.
1218///
1219/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001220static bool isObjCTypeSubstitutable(ASTContext &Context,
1221 const ObjCObjectPointerType *A,
1222 const ObjCObjectPointerType *B,
1223 bool rejectId) {
1224 // Reject a protocol-unqualified id.
1225 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001226
1227 // If B is a qualified id, then A must also be a qualified id and it must
1228 // implement all of the protocols in B. It may not be a qualified class.
1229 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1230 // stricter definition so it is not substitutable for id<A>.
1231 if (B->isObjCQualifiedIdType()) {
1232 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001233 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1234 QualType(B,0),
1235 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001236 }
1237
1238 /*
1239 // id is a special type that bypasses type checking completely. We want a
1240 // warning when it is used in one place but not another.
1241 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1242
1243
1244 // If B is a qualified id, then A must also be a qualified id (which it isn't
1245 // if we've got this far)
1246 if (B->isObjCQualifiedIdType()) return false;
1247 */
1248
1249 // Now we know that A and B are (potentially-qualified) class types. The
1250 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001251 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001252}
1253
John McCall10302c02010-10-28 02:34:38 +00001254static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1255 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1256}
1257
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001258static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001259 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001260 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001261 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001262 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001263 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001264 if (IsProtocolMethodDecl &&
1265 (MethodDecl->getObjCDeclQualifier() !=
1266 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001267 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001268 S.Diag(MethodImpl->getLocation(),
1269 (IsOverridingMode ?
1270 diag::warn_conflicting_overriding_ret_type_modifiers
1271 : diag::warn_conflicting_ret_type_modifiers))
1272 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001273 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1274 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1275 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1276 }
1277 else
1278 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001279 }
1280
John McCall10302c02010-10-28 02:34:38 +00001281 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001282 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001283 return true;
1284 if (!Warn)
1285 return false;
John McCall10302c02010-10-28 02:34:38 +00001286
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001287 unsigned DiagID =
1288 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1289 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001290
1291 // Mismatches between ObjC pointers go into a different warning
1292 // category, and sometimes they're even completely whitelisted.
1293 if (const ObjCObjectPointerType *ImplPtrTy =
1294 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1295 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001296 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001297 // Allow non-matching return types as long as they don't violate
1298 // the principle of substitutability. Specifically, we permit
1299 // return types that are subclasses of the declared return type,
1300 // or that are more-qualified versions of the declared type.
1301 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001302 return false;
John McCall10302c02010-10-28 02:34:38 +00001303
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001304 DiagID =
1305 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1306 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001307 }
1308 }
1309
1310 S.Diag(MethodImpl->getLocation(), DiagID)
1311 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001312 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001313 << MethodImpl->getResultType()
1314 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001315 S.Diag(MethodDecl->getLocation(),
1316 IsOverridingMode ? diag::note_previous_declaration
1317 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001318 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001319 return false;
John McCall10302c02010-10-28 02:34:38 +00001320}
1321
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001322static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001323 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001324 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001325 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001326 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001327 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001328 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001329 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001330 if (IsProtocolMethodDecl &&
1331 (ImplVar->getObjCDeclQualifier() !=
1332 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001333 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001334 if (IsOverridingMode)
1335 S.Diag(ImplVar->getLocation(),
1336 diag::warn_conflicting_overriding_param_modifiers)
1337 << getTypeRange(ImplVar->getTypeSourceInfo())
1338 << MethodImpl->getDeclName();
1339 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001340 diag::warn_conflicting_param_modifiers)
1341 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001342 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001343 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1344 << getTypeRange(IfaceVar->getTypeSourceInfo());
1345 }
1346 else
1347 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001348 }
1349
John McCall10302c02010-10-28 02:34:38 +00001350 QualType ImplTy = ImplVar->getType();
1351 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001352
John McCall10302c02010-10-28 02:34:38 +00001353 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001354 return true;
1355
1356 if (!Warn)
1357 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001358 unsigned DiagID =
1359 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1360 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001361
1362 // Mismatches between ObjC pointers go into a different warning
1363 // category, and sometimes they're even completely whitelisted.
1364 if (const ObjCObjectPointerType *ImplPtrTy =
1365 ImplTy->getAs<ObjCObjectPointerType>()) {
1366 if (const ObjCObjectPointerType *IfacePtrTy =
1367 IfaceTy->getAs<ObjCObjectPointerType>()) {
1368 // Allow non-matching argument types as long as they don't
1369 // violate the principle of substitutability. Specifically, the
1370 // implementation must accept any objects that the superclass
1371 // accepts, however it may also accept others.
1372 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001373 return false;
John McCall10302c02010-10-28 02:34:38 +00001374
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001375 DiagID =
1376 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1377 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001378 }
1379 }
1380
1381 S.Diag(ImplVar->getLocation(), DiagID)
1382 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001383 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1384 S.Diag(IfaceVar->getLocation(),
1385 (IsOverridingMode ? diag::note_previous_declaration
1386 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001387 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001388 return false;
John McCall10302c02010-10-28 02:34:38 +00001389}
John McCallf85e1932011-06-15 23:02:42 +00001390
1391/// In ARC, check whether the conventional meanings of the two methods
1392/// match. If they don't, it's a hard error.
1393static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1394 ObjCMethodDecl *decl) {
1395 ObjCMethodFamily implFamily = impl->getMethodFamily();
1396 ObjCMethodFamily declFamily = decl->getMethodFamily();
1397 if (implFamily == declFamily) return false;
1398
1399 // Since conventions are sorted by selector, the only possibility is
1400 // that the types differ enough to cause one selector or the other
1401 // to fall out of the family.
1402 assert(implFamily == OMF_None || declFamily == OMF_None);
1403
1404 // No further diagnostics required on invalid declarations.
1405 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1406
1407 const ObjCMethodDecl *unmatched = impl;
1408 ObjCMethodFamily family = declFamily;
1409 unsigned errorID = diag::err_arc_lost_method_convention;
1410 unsigned noteID = diag::note_arc_lost_method_convention;
1411 if (declFamily == OMF_None) {
1412 unmatched = decl;
1413 family = implFamily;
1414 errorID = diag::err_arc_gained_method_convention;
1415 noteID = diag::note_arc_gained_method_convention;
1416 }
1417
1418 // Indexes into a %select clause in the diagnostic.
1419 enum FamilySelector {
1420 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1421 };
1422 FamilySelector familySelector = FamilySelector();
1423
1424 switch (family) {
1425 case OMF_None: llvm_unreachable("logic error, no method convention");
1426 case OMF_retain:
1427 case OMF_release:
1428 case OMF_autorelease:
1429 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001430 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001431 case OMF_retainCount:
1432 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001433 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001434 // Mismatches for these methods don't change ownership
1435 // conventions, so we don't care.
1436 return false;
1437
1438 case OMF_init: familySelector = F_init; break;
1439 case OMF_alloc: familySelector = F_alloc; break;
1440 case OMF_copy: familySelector = F_copy; break;
1441 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1442 case OMF_new: familySelector = F_new; break;
1443 }
1444
1445 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1446 ReasonSelector reasonSelector;
1447
1448 // The only reason these methods don't fall within their families is
1449 // due to unusual result types.
1450 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1451 reasonSelector = R_UnrelatedReturn;
1452 } else {
1453 reasonSelector = R_NonObjectReturn;
1454 }
1455
1456 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1457 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1458
1459 return true;
1460}
John McCall10302c02010-10-28 02:34:38 +00001461
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001462void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001463 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001464 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001465 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001466 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1467 return;
1468
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001469 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001470 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001471 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Chris Lattner3aff9192009-04-11 19:58:42 +00001473 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001474 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1475 EF = MethodDecl->param_end();
1476 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001477 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001478 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001479 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001480
Fariborz Jahanian21121902011-08-08 18:03:17 +00001481 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001482 Diag(ImpMethodDecl->getLocation(),
1483 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001484 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001485 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001486}
1487
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001488void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1489 ObjCMethodDecl *Overridden,
1490 bool IsProtocolMethodDecl) {
1491
1492 CheckMethodOverrideReturn(*this, Method, Overridden,
1493 IsProtocolMethodDecl, true,
1494 true);
1495
1496 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001497 IF = Overridden->param_begin(), EM = Method->param_end(),
1498 EF = Overridden->param_end();
1499 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001500 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1501 IsProtocolMethodDecl, true, true);
1502 }
1503
1504 if (Method->isVariadic() != Overridden->isVariadic()) {
1505 Diag(Method->getLocation(),
1506 diag::warn_conflicting_overriding_variadic);
1507 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1508 }
1509}
1510
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001511/// WarnExactTypedMethods - This routine issues a warning if method
1512/// implementation declaration matches exactly that of its declaration.
1513void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1514 ObjCMethodDecl *MethodDecl,
1515 bool IsProtocolMethodDecl) {
1516 // don't issue warning when protocol method is optional because primary
1517 // class is not required to implement it and it is safe for protocol
1518 // to implement it.
1519 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1520 return;
1521 // don't issue warning when primary class's method is
1522 // depecated/unavailable.
1523 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1524 MethodDecl->hasAttr<DeprecatedAttr>())
1525 return;
1526
1527 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1528 IsProtocolMethodDecl, false, false);
1529 if (match)
1530 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001531 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1532 EF = MethodDecl->param_end();
1533 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001534 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1535 *IM, *IF,
1536 IsProtocolMethodDecl, false, false);
1537 if (!match)
1538 break;
1539 }
1540 if (match)
1541 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001542 if (match)
1543 match = !(MethodDecl->isClassMethod() &&
1544 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001545
1546 if (match) {
1547 Diag(ImpMethodDecl->getLocation(),
1548 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001549 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1550 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001551 }
1552}
1553
Mike Stump390b4cc2009-05-16 07:39:55 +00001554/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1555/// improve the efficiency of selector lookups and type checking by associating
1556/// with each protocol / interface / category the flattened instance tables. If
1557/// we used an immutable set to keep the table then it wouldn't add significant
1558/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001559
Steve Naroffefe7f362008-02-08 22:06:17 +00001560/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001561/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001562void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1563 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001564 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001565 const SelectorSet &InsMap,
1566 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001567 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001568 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1569 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1570 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001571 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1572
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001573 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001574 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001575 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001576 // check to see if class implements forwardInvocation method and objects
1577 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001578 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001579 // Under such conditions, which means that every method possible is
1580 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001581 // found" warnings.
1582 // FIXME: Use a general GetUnarySelector method for this.
1583 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1584 Selector fISelector = Context.Selectors.getSelector(1, &II);
1585 if (InsMap.count(fISelector))
1586 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1587 // need be implemented in the implementation.
1588 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1589 }
Mike Stump1eb44332009-09-09 15:08:12 +00001590
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001591 // If this is a forward protocol declaration, get its definition.
1592 if (!PDecl->isThisDeclarationADefinition() &&
1593 PDecl->getDefinition())
1594 PDecl = PDecl->getDefinition();
1595
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001596 // If a method lookup fails locally we still need to look and see if
1597 // the method was implemented by a base class or an inherited
1598 // protocol. This lookup is slow, but occurs rarely in correct code
1599 // and otherwise would terminate in a warning.
1600
Chris Lattner4d391482007-12-12 07:09:47 +00001601 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001602 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001603 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001604 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001605 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001606 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001607 !method->isPropertyAccessor() &&
1608 !InsMap.count(method->getSelector()) &&
1609 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001610 // If a method is not implemented in the category implementation but
1611 // has been declared in its primary class, superclass,
1612 // or in one of their protocols, no need to issue the warning.
1613 // This is because method will be implemented in the primary class
1614 // or one of its super class implementation.
1615
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001616 // Ugly, but necessary. Method declared in protcol might have
1617 // have been synthesized due to a property declared in the class which
1618 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001619 if (ObjCMethodDecl *MethodInClass =
1620 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001621 true /*shallowCategoryLookup*/))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001622 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001623 continue;
1624 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1625 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1626 != DiagnosticsEngine::Ignored) {
1627 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001628 Diag(method->getLocation(), diag::note_method_declared_at)
1629 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001630 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1631 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001632 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001633 }
1634 }
Chris Lattner4d391482007-12-12 07:09:47 +00001635 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001636 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001637 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001638 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001639 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001640 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1641 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001642 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001643 // See above comment for instance method lookups.
1644 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001645 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001646 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001647 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001648 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1649 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001650 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001651 Diag(method->getLocation(), diag::note_method_declared_at)
1652 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001653 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1654 PDecl->getDeclName();
1655 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001656 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001657 }
Chris Lattner780f3292008-07-21 21:32:27 +00001658 // Check on this protocols's referenced protocols, recursively.
1659 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1660 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001661 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001662}
1663
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001664/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001665/// or protocol against those declared in their implementations.
1666///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001667void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1668 const SelectorSet &ClsMap,
1669 SelectorSet &InsMapSeen,
1670 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001671 ObjCImplDecl* IMPDecl,
1672 ObjCContainerDecl* CDecl,
1673 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001674 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001675 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001676 // Check and see if instance methods in class interface have been
1677 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001678 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1679 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001680 if (InsMapSeen.count((*I)->getSelector()))
1681 continue;
1682 InsMapSeen.insert((*I)->getSelector());
Jordan Rose1e4691b2012-10-10 16:42:25 +00001683 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001684 !InsMap.count((*I)->getSelector())) {
1685 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001686 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1687 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001688 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001689 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001690 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001691 IMPDecl->getInstanceMethod((*I)->getSelector());
1692 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1693 "Expected to find the method through lookup as well");
1694 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001695 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001696 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001697 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001698 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1699 isa<ObjCProtocolDecl>(CDecl));
Jordan Rose1e4691b2012-10-10 16:42:25 +00001700 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001701 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001702 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001703 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001704 }
1705 }
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001707 // Check and see if class methods in class interface have been
1708 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001709 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001710 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001711 if (ClsMapSeen.count((*I)->getSelector()))
1712 continue;
1713 ClsMapSeen.insert((*I)->getSelector());
1714 if (!ClsMap.count((*I)->getSelector())) {
1715 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001716 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1717 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001718 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001719 ObjCMethodDecl *ImpMethodDecl =
1720 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001721 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1722 "Expected to find the method through lookup as well");
1723 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001724 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001725 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1726 isa<ObjCProtocolDecl>(CDecl));
1727 else
1728 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001729 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001730 }
1731 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001732
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001733 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001734 // when checking that methods in implementation match their declaration,
1735 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1736 // extension; as well as those in categories.
Douglas Gregord3297242013-01-16 23:00:23 +00001737 if (!WarnCategoryMethodImpl) {
1738 for (ObjCInterfaceDecl::visible_categories_iterator
1739 Cat = I->visible_categories_begin(),
1740 CatEnd = I->visible_categories_end();
1741 Cat != CatEnd; ++Cat) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001742 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001743 IMPDecl, *Cat, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001744 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001745 }
1746 } else {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001747 // Also methods in class extensions need be looked at next.
Douglas Gregord3297242013-01-16 23:00:23 +00001748 for (ObjCInterfaceDecl::visible_extensions_iterator
1749 Ext = I->visible_extensions_begin(),
1750 ExtEnd = I->visible_extensions_end();
1751 Ext != ExtEnd; ++Ext) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001752 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001753 IMPDecl, *Ext, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001754 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001755 }
1756 }
1757
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001758 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001759 for (ObjCInterfaceDecl::all_protocol_iterator
1760 PI = I->all_referenced_protocol_begin(),
1761 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001762 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1763 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001764 (*PI), IncompleteImpl, false,
1765 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001766
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001767 // FIXME. For now, we are not checking for extact match of methods
1768 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001769 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001770 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001771 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001772 I->getSuperClass(), IncompleteImpl, false);
1773 }
1774}
1775
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001776/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1777/// category matches with those implemented in its primary class and
1778/// warns each time an exact match is found.
1779void Sema::CheckCategoryVsClassMethodMatches(
1780 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001781 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001782
1783 for (ObjCImplementationDecl::instmeth_iterator
1784 I = CatIMPDecl->instmeth_begin(),
1785 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1786 InsMap.insert((*I)->getSelector());
1787
1788 for (ObjCImplementationDecl::classmeth_iterator
1789 I = CatIMPDecl->classmeth_begin(),
1790 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1791 ClsMap.insert((*I)->getSelector());
1792 if (InsMap.empty() && ClsMap.empty())
1793 return;
1794
1795 // Get category's primary class.
1796 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1797 if (!CatDecl)
1798 return;
1799 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1800 if (!IDecl)
1801 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001802 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001803 bool IncompleteImpl = false;
1804 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1805 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001806 IncompleteImpl, false,
1807 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001808}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001809
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001810void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001811 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001812 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001813 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001814 // Check and see if instance methods in class interface have been
1815 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001816 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001817 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001818 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001820 // Check and see if properties declared in the interface have either 1)
1821 // an implementation or 2) there is a @synthesize/@dynamic implementation
1822 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001823 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001824 if (!(LangOpts.ObjCDefaultSynthProperties &&
1825 LangOpts.ObjCRuntime.isNonFragile()) ||
1826 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001827 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001828
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001829 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001830 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001831 I = IMPDecl->classmeth_begin(),
1832 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001833 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001834
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001835 // Check for type conflict of methods declared in a class/protocol and
1836 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001837 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001838 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1839 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001840 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001841
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001842 // check all methods implemented in category against those declared
1843 // in its primary class.
1844 if (ObjCCategoryImplDecl *CatDecl =
1845 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1846 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001847
Chris Lattner4d391482007-12-12 07:09:47 +00001848 // Check the protocol list for unimplemented methods in the @implementation
1849 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001850 // Check and see if class methods in class interface have been
1851 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001852
Chris Lattnercddc8882009-03-01 00:56:52 +00001853 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001854 for (ObjCInterfaceDecl::all_protocol_iterator
1855 PI = I->all_referenced_protocol_begin(),
1856 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001857 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001858 InsMap, ClsMap, I);
1859 // Check class extensions (unnamed categories)
Douglas Gregord3297242013-01-16 23:00:23 +00001860 for (ObjCInterfaceDecl::visible_extensions_iterator
1861 Ext = I->visible_extensions_begin(),
1862 ExtEnd = I->visible_extensions_end();
1863 Ext != ExtEnd; ++Ext) {
1864 ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
1865 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001866 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001867 // For extended class, unimplemented methods in its protocols will
1868 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001869 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001870 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1871 E = C->protocol_end(); PI != E; ++PI)
1872 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001873 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001874 // Report unimplemented properties in the category as well.
1875 // When reporting on missing setter/getters, do not report when
1876 // setter/getter is implemented in category's primary class
1877 // implementation.
1878 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1879 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1880 for (ObjCImplementationDecl::instmeth_iterator
1881 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1882 InsMap.insert((*I)->getSelector());
1883 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001884 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001885 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001886 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001887 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001888}
1889
Mike Stump1eb44332009-09-09 15:08:12 +00001890/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001891Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001892Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001893 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001894 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001895 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001896 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001897 for (unsigned i = 0; i != NumElts; ++i) {
1898 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001899 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001900 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001901 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001902 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001903 // Maybe we will complain about the shadowed template parameter.
1904 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1905 // Just pretend that we didn't see the previous declaration.
1906 PrevDecl = 0;
1907 }
1908
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001909 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001910 // GCC apparently allows the following idiom:
1911 //
1912 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1913 // @class XCElementToggler;
1914 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001915 // Here we have chosen to ignore the forward class declaration
1916 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001917 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001918 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001919 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001920 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001921 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001922 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001923 // to the underlying class. Just ignore the forward class with a warning
1924 // as this will force the intended behavior which is to lookup the typedef
1925 // name.
1926 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1927 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1928 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1929 continue;
1930 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001931 }
Chris Lattner4d391482007-12-12 07:09:47 +00001932 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001933
1934 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001935 ObjCInterfaceDecl *PrevIDecl
1936 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001937 ObjCInterfaceDecl *IDecl
1938 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001939 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001940 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001941
Douglas Gregor7723fec2011-12-15 20:29:51 +00001942 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001943 CheckObjCDeclScope(IDecl);
1944 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001945 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001946
1947 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001948}
1949
John McCall0f4c4c42011-06-16 01:15:19 +00001950static bool tryMatchRecordTypes(ASTContext &Context,
1951 Sema::MethodMatchStrategy strategy,
1952 const Type *left, const Type *right);
1953
John McCallf85e1932011-06-15 23:02:42 +00001954static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1955 QualType leftQT, QualType rightQT) {
1956 const Type *left =
1957 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1958 const Type *right =
1959 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1960
1961 if (left == right) return true;
1962
1963 // If we're doing a strict match, the types have to match exactly.
1964 if (strategy == Sema::MMS_strict) return false;
1965
1966 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1967
1968 // Otherwise, use this absurdly complicated algorithm to try to
1969 // validate the basic, low-level compatibility of the two types.
1970
1971 // As a minimum, require the sizes and alignments to match.
1972 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1973 return false;
1974
1975 // Consider all the kinds of non-dependent canonical types:
1976 // - functions and arrays aren't possible as return and parameter types
1977
1978 // - vector types of equal size can be arbitrarily mixed
1979 if (isa<VectorType>(left)) return isa<VectorType>(right);
1980 if (isa<VectorType>(right)) return false;
1981
1982 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001983 // - structs, unions, and Objective-C objects must match more-or-less
1984 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001985 // - everything else should be a scalar
1986 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001987 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001988
John McCall1d9b3b22011-09-09 05:25:32 +00001989 // Make scalars agree in kind, except count bools as chars, and group
1990 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001991 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1992 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1993 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1994 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001995 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1996 leftSK = Type::STK_ObjCObjectPointer;
1997 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1998 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001999
2000 // Note that data member pointers and function member pointers don't
2001 // intermix because of the size differences.
2002
2003 return (leftSK == rightSK);
2004}
Chris Lattner4d391482007-12-12 07:09:47 +00002005
John McCall0f4c4c42011-06-16 01:15:19 +00002006static bool tryMatchRecordTypes(ASTContext &Context,
2007 Sema::MethodMatchStrategy strategy,
2008 const Type *lt, const Type *rt) {
2009 assert(lt && rt && lt != rt);
2010
2011 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2012 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2013 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2014
2015 // Require union-hood to match.
2016 if (left->isUnion() != right->isUnion()) return false;
2017
2018 // Require an exact match if either is non-POD.
2019 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2020 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2021 return false;
2022
2023 // Require size and alignment to match.
2024 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2025
2026 // Require fields to match.
2027 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2028 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2029 for (; li != le && ri != re; ++li, ++ri) {
2030 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2031 return false;
2032 }
2033 return (li == le && ri == re);
2034}
2035
Chris Lattner4d391482007-12-12 07:09:47 +00002036/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2037/// returns true, or false, accordingly.
2038/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00002039bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2040 const ObjCMethodDecl *right,
2041 MethodMatchStrategy strategy) {
2042 if (!matchTypes(Context, strategy,
2043 left->getResultType(), right->getResultType()))
2044 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002045
David Blaikie4e4d0842012-03-11 07:00:24 +00002046 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002047 (left->hasAttr<NSReturnsRetainedAttr>()
2048 != right->hasAttr<NSReturnsRetainedAttr>() ||
2049 left->hasAttr<NSConsumesSelfAttr>()
2050 != right->hasAttr<NSConsumesSelfAttr>()))
2051 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002052
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002053 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002054 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2055 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002056
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002057 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002058 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002059 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002060
2061 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2062 return false;
2063
David Blaikie4e4d0842012-03-11 07:00:24 +00002064 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002065 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2066 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002067 }
2068 return true;
2069}
2070
Douglas Gregorff310c72012-05-01 23:37:00 +00002071void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002072 // If the list is empty, make it a singleton list.
2073 if (List->Method == 0) {
2074 List->Method = Method;
2075 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002076 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002077 }
2078
2079 // We've seen a method with this name, see if we have already seen this type
2080 // signature.
2081 ObjCMethodList *Previous = List;
2082 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002083 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002084 continue;
2085
2086 ObjCMethodDecl *PrevObjCMethod = List->Method;
2087
2088 // Propagate the 'defined' bit.
2089 if (Method->isDefined())
2090 PrevObjCMethod->setDefined(true);
2091
2092 // If a method is deprecated, push it in the global pool.
2093 // This is used for better diagnostics.
2094 if (Method->isDeprecated()) {
2095 if (!PrevObjCMethod->isDeprecated())
2096 List->Method = Method;
2097 }
2098 // If new method is unavailable, push it into global pool
2099 // unless previous one is deprecated.
2100 if (Method->isUnavailable()) {
2101 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2102 List->Method = Method;
2103 }
2104
Douglas Gregorff310c72012-05-01 23:37:00 +00002105 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002106 }
2107
2108 // We have a new signature for an existing method - add it.
2109 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002110 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002111 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2112}
2113
Sebastian Redldb9d2142010-08-02 23:18:59 +00002114/// \brief Read the contents of the method pool for a given selector from
2115/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002116void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002117 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002118 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002119}
2120
Douglas Gregorff310c72012-05-01 23:37:00 +00002121void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002122 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002123 // Ignore methods of invalid containers.
2124 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002125 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002126
Douglas Gregor0d266d62012-01-25 00:59:09 +00002127 if (ExternalSource)
2128 ReadMethodPool(Method->getSelector());
2129
Sebastian Redldb9d2142010-08-02 23:18:59 +00002130 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002131 if (Pos == MethodPool.end())
2132 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2133 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002134
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002135 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002136
Sebastian Redldb9d2142010-08-02 23:18:59 +00002137 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002138 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002139}
2140
John McCallf85e1932011-06-15 23:02:42 +00002141/// Determines if this is an "acceptable" loose mismatch in the global
2142/// method pool. This exists mostly as a hack to get around certain
2143/// global mismatches which we can't afford to make warnings / errors.
2144/// Really, what we want is a way to take a method out of the global
2145/// method pool.
2146static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2147 ObjCMethodDecl *other) {
2148 if (!chosen->isInstanceMethod())
2149 return false;
2150
2151 Selector sel = chosen->getSelector();
2152 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2153 return false;
2154
2155 // Don't complain about mismatches for -length if the method we
2156 // chose has an integral result type.
2157 return (chosen->getResultType()->isIntegerType());
2158}
2159
Sebastian Redldb9d2142010-08-02 23:18:59 +00002160ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002161 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002162 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002163 if (ExternalSource)
2164 ReadMethodPool(Sel);
2165
Sebastian Redldb9d2142010-08-02 23:18:59 +00002166 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002167 if (Pos == MethodPool.end())
2168 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002169
Douglas Gregorf0e00042013-01-16 18:47:38 +00002170 // Gather the non-hidden methods.
Sebastian Redldb9d2142010-08-02 23:18:59 +00002171 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorf0e00042013-01-16 18:47:38 +00002172 llvm::SmallVector<ObjCMethodDecl *, 4> Methods;
2173 for (ObjCMethodList *M = &MethList; M; M = M->Next) {
2174 if (M->Method && !M->Method->isHidden()) {
2175 // If we're not supposed to warn about mismatches, we're done.
2176 if (!warn)
2177 return M->Method;
Mike Stump1eb44332009-09-09 15:08:12 +00002178
Douglas Gregorf0e00042013-01-16 18:47:38 +00002179 Methods.push_back(M->Method);
Sebastian Redldb9d2142010-08-02 23:18:59 +00002180 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002181 }
Douglas Gregorf0e00042013-01-16 18:47:38 +00002182
2183 // If there aren't any visible methods, we're done.
2184 // FIXME: Recover if there are any known-but-hidden methods?
2185 if (Methods.empty())
2186 return 0;
2187
2188 if (Methods.size() == 1)
2189 return Methods[0];
2190
2191 // We found multiple methods, so we may have to complain.
2192 bool issueDiagnostic = false, issueError = false;
2193
2194 // We support a warning which complains about *any* difference in
2195 // method signature.
2196 bool strictSelectorMatch =
2197 (receiverIdOrClass && warn &&
2198 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2199 R.getBegin())
2200 != DiagnosticsEngine::Ignored));
2201 if (strictSelectorMatch) {
2202 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2203 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2204 issueDiagnostic = true;
2205 break;
2206 }
2207 }
2208 }
2209
2210 // If we didn't see any strict differences, we won't see any loose
2211 // differences. In ARC, however, we also need to check for loose
2212 // mismatches, because most of them are errors.
2213 if (!strictSelectorMatch ||
2214 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2215 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2216 // This checks if the methods differ in type mismatch.
2217 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2218 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2219 issueDiagnostic = true;
2220 if (getLangOpts().ObjCAutoRefCount)
2221 issueError = true;
2222 break;
2223 }
2224 }
2225
2226 if (issueDiagnostic) {
2227 if (issueError)
2228 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2229 else if (strictSelectorMatch)
2230 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2231 else
2232 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2233
2234 Diag(Methods[0]->getLocStart(),
2235 issueError ? diag::note_possibility : diag::note_using)
2236 << Methods[0]->getSourceRange();
2237 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2238 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2239 << Methods[I]->getSourceRange();
2240 }
2241 }
2242 return Methods[0];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002243}
2244
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002245ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002246 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2247 if (Pos == MethodPool.end())
2248 return 0;
2249
2250 GlobalMethods &Methods = Pos->second;
2251
2252 if (Methods.first.Method && Methods.first.Method->isDefined())
2253 return Methods.first.Method;
2254 if (Methods.second.Method && Methods.second.Method->isDefined())
2255 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002256 return 0;
2257}
2258
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002259/// DiagnoseDuplicateIvars -
2260/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002261/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002262/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002263/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002264void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2265 ObjCInterfaceDecl *SID) {
2266 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2267 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002268 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002269 if (Ivar->isInvalidDecl())
2270 continue;
2271 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2272 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2273 if (prevIvar) {
2274 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2275 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2276 Ivar->setInvalidDecl();
2277 }
2278 }
2279 }
2280}
2281
Erik Verbruggend64251f2011-12-06 09:25:23 +00002282Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2283 switch (CurContext->getDeclKind()) {
2284 case Decl::ObjCInterface:
2285 return Sema::OCK_Interface;
2286 case Decl::ObjCProtocol:
2287 return Sema::OCK_Protocol;
2288 case Decl::ObjCCategory:
2289 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2290 return Sema::OCK_ClassExtension;
2291 else
2292 return Sema::OCK_Category;
2293 case Decl::ObjCImplementation:
2294 return Sema::OCK_Implementation;
2295 case Decl::ObjCCategoryImpl:
2296 return Sema::OCK_CategoryImplementation;
2297
2298 default:
2299 return Sema::OCK_None;
2300 }
2301}
2302
Steve Naroffa56f6162007-12-18 01:30:32 +00002303// Note: For class/category implemenations, allMethods/allProperties is
2304// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002305Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2306 Decl **allMethods, unsigned allNum,
2307 Decl **allProperties, unsigned pNum,
2308 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002309
Erik Verbruggend64251f2011-12-06 09:25:23 +00002310 if (getObjCContainerKind() == Sema::OCK_None)
2311 return 0;
2312
2313 assert(AtEnd.isValid() && "Invalid location for '@end'");
2314
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002315 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2316 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002317
Mike Stump1eb44332009-09-09 15:08:12 +00002318 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002319 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2320 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002321 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002322
Steve Naroff0701bbb2009-01-08 17:28:14 +00002323 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2324 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2325 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2326
Chris Lattner4d391482007-12-12 07:09:47 +00002327 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002328 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002329 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002330
2331 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002332 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002333 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002334 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002335 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002336 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002337 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002338 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002339 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002340 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002341 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002342 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002343 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002344 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002345 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002346 if (!Context.getSourceManager().isInSystemHeader(
2347 Method->getLocation()))
2348 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2349 << Method->getDeclName();
2350 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2351 }
Chris Lattner4d391482007-12-12 07:09:47 +00002352 InsMap[Method->getSelector()] = Method;
2353 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002354 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002355 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002356 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002357 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002358 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002359 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002360 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002361 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002362 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002363 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002364 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002365 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002366 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002367 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002368 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002369 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002370 if (!Context.getSourceManager().isInSystemHeader(
2371 Method->getLocation()))
2372 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2373 << Method->getDeclName();
2374 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2375 }
Chris Lattner4d391482007-12-12 07:09:47 +00002376 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002377 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002378 }
2379 }
2380 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002381 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002382 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002383 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002384 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002385 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002386 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002387 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002388 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002389 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002390
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002391 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002392 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002393 if (C->IsClassExtension()) {
2394 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2395 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002396 }
Chris Lattner4d391482007-12-12 07:09:47 +00002397 }
Steve Naroff09c47192009-01-09 15:36:25 +00002398 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002399 if (CDecl->getIdentifier())
2400 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2401 // user-defined setter/getter. It also synthesizes setter/getter methods
2402 // and adds them to the DeclContext and global method pools.
2403 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2404 E = CDecl->prop_end();
2405 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002406 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002407 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002408 }
2409 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002410 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002411 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002412 // Any property declared in a class extension might have user
2413 // declared setter or getter in current class extension or one
2414 // of the other class extensions. Mark them as synthesized as
2415 // property will be synthesized when property with same name is
2416 // seen in the @implementation.
Douglas Gregord3297242013-01-16 23:00:23 +00002417 for (ObjCInterfaceDecl::visible_extensions_iterator
2418 Ext = IDecl->visible_extensions_begin(),
2419 ExtEnd = IDecl->visible_extensions_end();
2420 Ext != ExtEnd; ++Ext) {
2421 for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
2422 E = Ext->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002423 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002424 // Skip over properties declared @dynamic
2425 if (const ObjCPropertyImplDecl *PIDecl
2426 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2427 if (PIDecl->getPropertyImplementation()
2428 == ObjCPropertyImplDecl::Dynamic)
2429 continue;
Douglas Gregord3297242013-01-16 23:00:23 +00002430
2431 for (ObjCInterfaceDecl::visible_extensions_iterator
2432 Ext = IDecl->visible_extensions_begin(),
2433 ExtEnd = IDecl->visible_extensions_end();
2434 Ext != ExtEnd; ++Ext) {
2435 if (ObjCMethodDecl *GetterMethod
2436 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002437 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002438 if (!Property->isReadOnly())
Douglas Gregord3297242013-01-16 23:00:23 +00002439 if (ObjCMethodDecl *SetterMethod
2440 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002441 SetterMethod->setPropertyAccessor(true);
Douglas Gregord3297242013-01-16 23:00:23 +00002442 }
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002443 }
2444 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002445 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002446 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002447 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002448
Patrick Beardb2f68202012-04-06 18:12:22 +00002449 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2450 if (IDecl->getSuperClass() == NULL) {
2451 // This class has no superclass, so check that it has been marked with
2452 // __attribute((objc_root_class)).
2453 if (!HasRootClassAttr) {
2454 SourceLocation DeclLoc(IDecl->getLocation());
2455 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2456 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2457 << IDecl->getIdentifier();
2458 // See if NSObject is in the current scope, and if it is, suggest
2459 // adding " : NSObject " to the class declaration.
2460 NamedDecl *IF = LookupSingleName(TUScope,
2461 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2462 DeclLoc, LookupOrdinaryName);
2463 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2464 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2465 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2466 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2467 } else {
2468 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2469 }
2470 }
2471 } else if (HasRootClassAttr) {
2472 // Complain that only root classes may have this attribute.
2473 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2474 }
2475
John McCall260611a2012-06-20 06:18:46 +00002476 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002477 while (IDecl->getSuperClass()) {
2478 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2479 IDecl = IDecl->getSuperClass();
2480 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002481 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002482 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002483 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002484 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002485 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002486 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002487
Chris Lattner4d391482007-12-12 07:09:47 +00002488 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002489 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002490 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregord3297242013-01-16 23:00:23 +00002491 if (ObjCCategoryDecl *Cat
2492 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2493 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattner4d391482007-12-12 07:09:47 +00002494 }
2495 }
2496 }
Chris Lattner682bf922009-03-29 16:50:03 +00002497 if (isInterfaceDeclKind) {
2498 // Reject invalid vardecls.
2499 for (unsigned i = 0; i != tuvNum; i++) {
2500 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2501 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2502 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002503 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002504 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002505 }
Chris Lattner682bf922009-03-29 16:50:03 +00002506 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002507 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002508 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002509
2510 for (unsigned i = 0; i != tuvNum; i++) {
2511 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002512 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2513 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002514 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2515 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002516
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002517 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002518 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002519}
2520
2521
2522/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2523/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002524static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002525CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002526 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002527}
2528
Ted Kremenek422bae72010-04-18 04:59:38 +00002529static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002530unsigned countAlignAttr(const AttrVec &A) {
2531 unsigned count=0;
2532 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2533 if ((*i)->getKind() == attr::Aligned)
2534 ++count;
2535 return count;
2536}
2537
2538static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002539bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2540 const AttrVec &A) {
2541 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002542 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002543 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002544 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002545
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002546 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002547 // But implementation has attributes. This is invalid.
2548 // Except when implementation has 'Align' attribute which is
2549 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002550 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002551 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002552
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002553 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002554
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002555 unsigned countAlignOnImpl = countAlignAttr(A);
2556 if (!countAlignOnImpl && (A.size() != D.size()))
2557 return true;
2558 else if (countAlignOnImpl) {
2559 unsigned countAlignOnDecl = countAlignAttr(D);
2560 if (countAlignOnDecl && (A.size() != D.size()))
2561 return true;
2562 else if (!countAlignOnDecl &&
2563 ((A.size()-countAlignOnImpl) != D.size()))
2564 return true;
2565 }
2566
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002567 // attributes on method declaration and definition must match exactly.
2568 // Note that we have at most a couple of attributes on methods, so this
2569 // n*n search is good enough.
2570 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002571 if ((*i)->getKind() == attr::Aligned)
2572 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002573 bool match = false;
2574 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2575 if ((*i)->getKind() == (*i1)->getKind()) {
2576 match = true;
2577 break;
2578 }
2579 }
2580 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002581 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002582 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002583
Sean Huntcf807c42010-08-18 23:23:40 +00002584 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002585}
2586
Douglas Gregor926df6c2011-06-11 01:09:30 +00002587/// \brief Check whether the declared result type of the given Objective-C
2588/// method declaration is compatible with the method's class.
2589///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002590static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002591CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2592 ObjCInterfaceDecl *CurrentClass) {
2593 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002594
2595 // If an Objective-C method inherits its related result type, then its
2596 // declared result type must be compatible with its own class type. The
2597 // declared result type is compatible if:
2598 if (const ObjCObjectPointerType *ResultObjectType
2599 = ResultType->getAs<ObjCObjectPointerType>()) {
2600 // - it is id or qualified id, or
2601 if (ResultObjectType->isObjCIdType() ||
2602 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002603 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002604
2605 if (CurrentClass) {
2606 if (ObjCInterfaceDecl *ResultClass
2607 = ResultObjectType->getInterfaceDecl()) {
2608 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002609 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002610 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002611
2612 // - it is a superclass of the method's class type
2613 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002614 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002615 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002616 } else {
2617 // Any Objective-C pointer type might be acceptable for a protocol
2618 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002619 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002620 }
2621 }
2622
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002623 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002624}
2625
John McCall6c2c2502011-07-22 02:45:48 +00002626namespace {
2627/// A helper class for searching for methods which a particular method
2628/// overrides.
2629class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002630public:
John McCall6c2c2502011-07-22 02:45:48 +00002631 Sema &S;
2632 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002633 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002634 bool Recursive;
2635
2636public:
2637 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2638 Selector selector = method->getSelector();
2639
2640 // Bypass this search if we've never seen an instance/class method
2641 // with this selector before.
2642 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2643 if (it == S.MethodPool.end()) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00002644 if (!S.getExternalSource()) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002645 S.ReadMethodPool(selector);
2646
2647 it = S.MethodPool.find(selector);
2648 if (it == S.MethodPool.end())
2649 return;
John McCall6c2c2502011-07-22 02:45:48 +00002650 }
2651 ObjCMethodList &list =
2652 method->isInstanceMethod() ? it->second.first : it->second.second;
2653 if (!list.Method) return;
2654
2655 ObjCContainerDecl *container
2656 = cast<ObjCContainerDecl>(method->getDeclContext());
2657
2658 // Prevent the search from reaching this container again. This is
2659 // important with categories, which override methods from the
2660 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002661 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2662 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002663 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2664 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002665 } else {
2666 searchFromContainer(container);
2667 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002668 }
John McCall6c2c2502011-07-22 02:45:48 +00002669
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002670 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002671 iterator begin() const { return Overridden.begin(); }
2672 iterator end() const { return Overridden.end(); }
2673
2674private:
2675 void searchFromContainer(ObjCContainerDecl *container) {
2676 if (container->isInvalidDecl()) return;
2677
2678 switch (container->getDeclKind()) {
2679#define OBJCCONTAINER(type, base) \
2680 case Decl::type: \
2681 searchFrom(cast<type##Decl>(container)); \
2682 break;
2683#define ABSTRACT_DECL(expansion)
2684#define DECL(type, base) \
2685 case Decl::type:
2686#include "clang/AST/DeclNodes.inc"
2687 llvm_unreachable("not an ObjC container!");
2688 }
2689 }
2690
2691 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002692 if (!protocol->hasDefinition())
2693 return;
2694
John McCall6c2c2502011-07-22 02:45:48 +00002695 // A method in a protocol declaration overrides declarations from
2696 // referenced ("parent") protocols.
2697 search(protocol->getReferencedProtocols());
2698 }
2699
2700 void searchFrom(ObjCCategoryDecl *category) {
2701 // A method in a category declaration overrides declarations from
2702 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002703 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002704 search(category->getReferencedProtocols());
2705 }
2706
2707 void searchFrom(ObjCCategoryImplDecl *impl) {
2708 // A method in a category definition that has a category
2709 // declaration overrides declarations from the category
2710 // declaration.
2711 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2712 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002713 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2714 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002715
2716 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002717 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2718 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002719 }
2720 }
2721
2722 void searchFrom(ObjCInterfaceDecl *iface) {
2723 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002724 if (!iface->hasDefinition())
2725 return;
2726
John McCall6c2c2502011-07-22 02:45:48 +00002727 // - categories,
Douglas Gregord3297242013-01-16 23:00:23 +00002728 for (ObjCInterfaceDecl::visible_categories_iterator
2729 cat = iface->visible_categories_begin(),
2730 catEnd = iface->visible_categories_end();
2731 cat != catEnd; ++cat) {
2732 search(*cat);
2733 }
John McCall6c2c2502011-07-22 02:45:48 +00002734
2735 // - the super class, and
2736 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2737 search(super);
2738
2739 // - any referenced protocols.
2740 search(iface->getReferencedProtocols());
2741 }
2742
2743 void searchFrom(ObjCImplementationDecl *impl) {
2744 // A method in a class implementation overrides declarations from
2745 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002746 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2747 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002748 }
2749
2750
2751 void search(const ObjCProtocolList &protocols) {
2752 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2753 i != e; ++i)
2754 search(*i);
2755 }
2756
2757 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002758 // Check for a method in this container which matches this selector.
2759 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2760 Method->isInstanceMethod());
2761
2762 // If we find one, record it and bail out.
2763 if (meth) {
2764 Overridden.insert(meth);
2765 return;
2766 }
2767
2768 // Otherwise, search for methods that a hypothetical method here
2769 // would have overridden.
2770
2771 // Note that we're now in a recursive case.
2772 Recursive = true;
2773
2774 searchFromContainer(container);
2775 }
2776};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002777}
2778
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002779void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2780 ObjCInterfaceDecl *CurrentClass,
2781 ResultTypeCompatibilityKind RTC) {
2782 // Search for overridden methods and merge information down from them.
2783 OverrideSearch overrides(*this, ObjCMethod);
2784 // Keep track if the method overrides any method in the class's base classes,
2785 // its protocols, or its categories' protocols; we will keep that info
2786 // in the ObjCMethodDecl.
2787 // For this info, a method in an implementation is not considered as
2788 // overriding the same method in the interface or its categories.
2789 bool hasOverriddenMethodsInBaseOrProtocol = false;
2790 for (OverrideSearch::iterator
2791 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2792 ObjCMethodDecl *overridden = *i;
2793
2794 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2795 CurrentClass != overridden->getClassInterface() ||
2796 overridden->isOverriding())
2797 hasOverriddenMethodsInBaseOrProtocol = true;
2798
2799 // Propagate down the 'related result type' bit from overridden methods.
2800 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2801 ObjCMethod->SetRelatedResultType();
2802
2803 // Then merge the declarations.
2804 mergeObjCMethodDecls(ObjCMethod, overridden);
2805
2806 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2807 continue; // Conflicting properties are detected elsewhere.
2808
2809 // Check for overriding methods
2810 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2811 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2812 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2813 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2814
2815 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00002816 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
2817 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002818 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2819 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002820 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2821 PrevE = overridden->param_end();
2822 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002823 assert(PrevI != overridden->param_end() && "Param mismatch");
2824 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2825 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2826 // If type of argument of method in this class does not match its
2827 // respective argument type in the super class method, issue warning;
2828 if (!Context.typesAreCompatible(T1, T2)) {
2829 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2830 << T1 << T2;
2831 Diag(overridden->getLocation(), diag::note_previous_declaration);
2832 break;
2833 }
2834 }
2835 }
2836 }
2837
2838 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2839}
2840
John McCalld226f652010-08-21 09:40:31 +00002841Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002842 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002843 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002844 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002845 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002846 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002847 Selector Sel,
2848 // optional arguments. The number of types/arguments is obtained
2849 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002850 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002851 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002852 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002853 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002854 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002855 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002856 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002857 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002858 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002859 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2860 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002861 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002862
Douglas Gregore97179c2011-09-08 01:46:34 +00002863 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002864 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002865 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002866 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002867
Steve Naroffccef3712009-02-20 22:59:16 +00002868 // Methods cannot return interface types. All ObjC objects are
2869 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002870 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002871 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2872 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002873 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002874 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002875
2876 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002877 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002878 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002879 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002880 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002881 }
Mike Stump1eb44332009-09-09 15:08:12 +00002882
2883 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002884 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002885 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002886 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002887 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002888 MethodType == tok::minus, isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +00002889 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002890 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002891 MethodDeclKind == tok::objc_optional
2892 ? ObjCMethodDecl::Optional
2893 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002894 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002895
Chris Lattner5f9e2722011-07-23 10:55:15 +00002896 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002897
Chris Lattner7db638d2009-04-11 19:42:43 +00002898 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002899 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002900 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002901
Chris Lattnere294d3f2009-04-11 18:57:04 +00002902 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002903 ArgType = Context.getObjCIdType();
2904 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002905 } else {
John McCall58e46772009-10-23 21:48:59 +00002906 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002907 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002908 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002909 }
Mike Stump1eb44332009-09-09 15:08:12 +00002910
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002911 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2912 LookupOrdinaryName, ForRedeclaration);
2913 LookupName(R, S);
2914 if (R.isSingleResult()) {
2915 NamedDecl *PrevDecl = R.getFoundDecl();
2916 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002917 Diag(ArgInfo[i].NameLoc,
2918 (MethodDefinition ? diag::warn_method_param_redefinition
2919 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002920 << ArgInfo[i].Name;
2921 Diag(PrevDecl->getLocation(),
2922 diag::note_previous_declaration);
2923 }
2924 }
2925
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002926 SourceLocation StartLoc = DI
2927 ? DI->getTypeLoc().getBeginLoc()
2928 : ArgInfo[i].NameLoc;
2929
John McCall81ef3e62011-04-23 02:46:06 +00002930 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2931 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2932 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002933
John McCall70798862011-05-02 00:30:12 +00002934 Param->setObjCMethodScopeInfo(i);
2935
Chris Lattner0ed844b2008-04-04 06:12:32 +00002936 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002937 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002938
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002939 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002940 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002941
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002942 if (Param->hasAttr<BlocksAttr>()) {
2943 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2944 Param->setInvalidDecl();
2945 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002946 S->AddDecl(Param);
2947 IdResolver.AddDecl(Param);
2948
Chris Lattner0ed844b2008-04-04 06:12:32 +00002949 Params.push_back(Param);
2950 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002951
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002952 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002953 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002954 QualType ArgType = Param->getType();
2955 if (ArgType.isNull())
2956 ArgType = Context.getObjCIdType();
2957 else
2958 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002959 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002960 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002961 Diag(Param->getLocation(),
2962 diag::err_object_cannot_be_passed_returned_by_value)
2963 << 1 << ArgType;
2964 Param->setInvalidDecl();
2965 }
2966 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002967
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002968 Params.push_back(Param);
2969 }
2970
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002971 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002972 ObjCMethod->setObjCDeclQualifier(
2973 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002974
2975 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002976 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002977
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002978 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002979 const ObjCMethodDecl *PrevMethod = 0;
2980 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002981 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002982 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2983 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002984 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002985 PrevMethod = ImpDecl->getClassMethod(Sel);
2986 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002987 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002988
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002989 ObjCMethodDecl *IMD = 0;
2990 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2991 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2992 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002993 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002994 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002995 SourceLocation MethodLoc = IMD->getLocation();
2996 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2997 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002998 Diag(MethodLoc, diag::note_method_declared_at)
2999 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00003000 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00003001 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003002 } else {
3003 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003004 }
John McCall6c2c2502011-07-22 02:45:48 +00003005
Chris Lattner4d391482007-12-12 07:09:47 +00003006 if (PrevMethod) {
3007 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00003008 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00003009 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00003010 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00003011 }
John McCall54abf7d2009-11-04 02:18:39 +00003012
Douglas Gregor926df6c2011-06-11 01:09:30 +00003013 // If this Objective-C method does not have a related result type, but we
3014 // are allowed to infer related result types, try to do so based on the
3015 // method family.
3016 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3017 if (!CurrentClass) {
3018 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3019 CurrentClass = Cat->getClassInterface();
3020 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3021 CurrentClass = Impl->getClassInterface();
3022 else if (ObjCCategoryImplDecl *CatImpl
3023 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3024 CurrentClass = CatImpl->getClassInterface();
3025 }
John McCall6c2c2502011-07-22 02:45:48 +00003026
Douglas Gregore97179c2011-09-08 01:46:34 +00003027 ResultTypeCompatibilityKind RTC
3028 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00003029
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003030 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00003031
John McCallf85e1932011-06-15 23:02:42 +00003032 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00003033 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00003034 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
3035
Douglas Gregore97179c2011-09-08 01:46:34 +00003036 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003037 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00003038 !ObjCMethod->hasRelatedResultType() &&
3039 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00003040 bool InferRelatedResultType = false;
3041 switch (ObjCMethod->getMethodFamily()) {
3042 case OMF_None:
3043 case OMF_copy:
3044 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00003045 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003046 case OMF_mutableCopy:
3047 case OMF_release:
3048 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00003049 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003050 break;
3051
3052 case OMF_alloc:
3053 case OMF_new:
3054 InferRelatedResultType = ObjCMethod->isClassMethod();
3055 break;
3056
3057 case OMF_init:
3058 case OMF_autorelease:
3059 case OMF_retain:
3060 case OMF_self:
3061 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3062 break;
3063 }
3064
John McCall6c2c2502011-07-22 02:45:48 +00003065 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003066 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003067 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003068
3069 ActOnDocumentableDecl(ObjCMethod);
3070
John McCalld226f652010-08-21 09:40:31 +00003071 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003072}
3073
Chris Lattnercc98eac2008-12-17 07:13:27 +00003074bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003075 // Following is also an error. But it is caused by a missing @end
3076 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003077 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003078 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003079
3080 // If we switched context to translation unit while we are still lexically in
3081 // an objc container, it means the parser missed emitting an error.
3082 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3083 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003084
Anders Carlsson15281452008-11-04 16:57:32 +00003085 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3086 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003087
Anders Carlsson15281452008-11-04 16:57:32 +00003088 return true;
3089}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003090
James Dennett1dfbd922012-06-14 21:40:34 +00003091/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003092/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003093void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003094 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003095 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003096 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003097 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003098 if (!Class) {
3099 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3100 return;
3101 }
John McCall260611a2012-06-20 06:18:46 +00003102 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003103 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3104 return;
3105 }
Mike Stump1eb44332009-09-09 15:08:12 +00003106
Chris Lattnercc98eac2008-12-17 07:13:27 +00003107 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003108 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003109 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003110 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003111 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003112 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003113 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003114 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3115 /*FIXME: StartL=*/ID->getLocation(),
3116 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003117 ID->getIdentifier(), ID->getType(),
3118 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003119 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003120 }
Mike Stump1eb44332009-09-09 15:08:12 +00003121
Chris Lattnercc98eac2008-12-17 07:13:27 +00003122 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003123 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003124 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003125 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003126 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003127 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003128 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003129 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003130 }
3131}
3132
Douglas Gregor160b5632010-04-26 17:32:49 +00003133/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003134VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3135 SourceLocation StartLoc,
3136 SourceLocation IdLoc,
3137 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003138 bool Invalid) {
3139 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3140 // duration shall not be qualified by an address-space qualifier."
3141 // Since all parameters have automatic store duration, they can not have
3142 // an address space.
3143 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003144 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003145 Invalid = true;
3146 }
3147
3148 // An @catch parameter must be an unqualified object pointer type;
3149 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3150 if (Invalid) {
3151 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003152 } else if (T->isDependentType()) {
3153 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003154 } else if (!T->isObjCObjectPointerType()) {
3155 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003156 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003157 } else if (T->isObjCQualifiedIdType()) {
3158 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003159 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003160 }
3161
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003162 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3163 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003164 New->setExceptionVariable(true);
3165
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003166 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003167 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003168 Invalid = true;
3169
Douglas Gregor160b5632010-04-26 17:32:49 +00003170 if (Invalid)
3171 New->setInvalidDecl();
3172 return New;
3173}
3174
John McCalld226f652010-08-21 09:40:31 +00003175Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003176 const DeclSpec &DS = D.getDeclSpec();
3177
3178 // We allow the "register" storage class on exception variables because
3179 // GCC did, but we drop it completely. Any other storage class is an error.
3180 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3181 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3182 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3183 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3184 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3185 << DS.getStorageClassSpec();
3186 }
3187 if (D.getDeclSpec().isThreadSpecified())
3188 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3189 D.getMutableDeclSpec().ClearStorageClassSpecs();
3190
3191 DiagnoseFunctionSpecifiers(D);
3192
3193 // Check that there are no default arguments inside the type of this
3194 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003195 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003196 CheckExtraCXXDefaultArguments(D);
3197
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003198 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003199 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003200
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003201 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3202 D.getSourceRange().getBegin(),
3203 D.getIdentifierLoc(),
3204 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003205 D.isInvalidType());
3206
3207 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3208 if (D.getCXXScopeSpec().isSet()) {
3209 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3210 << D.getCXXScopeSpec().getRange();
3211 New->setInvalidDecl();
3212 }
3213
3214 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003215 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003216 if (D.getIdentifier())
3217 IdResolver.AddDecl(New);
3218
3219 ProcessDeclAttributes(S, New, D);
3220
3221 if (New->hasAttr<BlocksAttr>())
3222 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003223 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003224}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003225
3226/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003227/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003228void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003229 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003230 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3231 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003232 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003233 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003234 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003235 }
3236}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003237
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003238void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003239 // Load referenced selectors from the external source.
3240 if (ExternalSource) {
3241 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3242 ExternalSource->ReadReferencedSelectors(Sels);
3243 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3244 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3245 }
3246
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003247 // Warning will be issued only when selector table is
3248 // generated (which means there is at lease one implementation
3249 // in the TU). This is to match gcc's behavior.
3250 if (ReferencedSelectors.empty() ||
3251 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003252 return;
3253 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3254 ReferencedSelectors.begin(),
3255 E = ReferencedSelectors.end(); S != E; ++S) {
3256 Selector Sel = (*S).first;
3257 if (!LookupImplementedMethodInGlobalPool(Sel))
3258 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3259 }
3260 return;
3261}