blob: 1e2e4843baec7b6751d921ef29e5b8562dd6276d [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(),
John McCall7cca8212013-03-19 07:04:25 +0000154 diag::note_related_result_type_family)
155 << /*overridden method*/ 0
Douglas Gregore97179c2011-09-08 01:46:34 +0000156 << Family;
157 else
158 Diag(Overridden->getLocation(),
159 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000160 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000161 if (getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000162 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
163 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
164 Diag(NewMethod->getLocation(),
165 diag::err_nsreturns_retained_attribute_mismatch) << 1;
166 Diag(Overridden->getLocation(), diag::note_previous_decl)
167 << "method";
168 }
169 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
170 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
171 Diag(NewMethod->getLocation(),
172 diag::err_nsreturns_retained_attribute_mismatch) << 0;
173 Diag(Overridden->getLocation(), diag::note_previous_decl)
174 << "method";
175 }
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000176 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
177 oe = Overridden->param_end();
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000178 for (ObjCMethodDecl::param_iterator
179 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000180 ni != ne && oi != oe; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000181 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000182 ParmVarDecl *newDecl = (*ni);
183 if (newDecl->hasAttr<NSConsumedAttr>() !=
184 oldDecl->hasAttr<NSConsumedAttr>()) {
185 Diag(newDecl->getLocation(),
186 diag::err_nsconsumed_attribute_mismatch);
187 Diag(oldDecl->getLocation(), diag::note_previous_decl)
188 << "parameter";
189 }
190 }
191 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000192}
193
John McCallf85e1932011-06-15 23:02:42 +0000194/// \brief Check a method declaration for compatibility with the Objective-C
195/// ARC conventions.
196static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
197 ObjCMethodFamily family = method->getMethodFamily();
198 switch (family) {
199 case OMF_None:
Nico Weber80cb6e62011-08-28 22:35:17 +0000200 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000201 case OMF_retain:
202 case OMF_release:
203 case OMF_autorelease:
204 case OMF_retainCount:
205 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000206 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000207 return false;
208
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000209 case OMF_dealloc:
210 if (!S.Context.hasSameType(method->getResultType(), S.Context.VoidTy)) {
211 SourceRange ResultTypeRange;
212 if (const TypeSourceInfo *ResultTypeInfo
213 = method->getResultTypeSourceInfo())
214 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
215 if (ResultTypeRange.isInvalid())
216 S.Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
217 << method->getResultType()
218 << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
219 else
220 S.Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
221 << method->getResultType()
222 << FixItHint::CreateReplacement(ResultTypeRange, "void");
223 return true;
224 }
225 return false;
226
John McCallf85e1932011-06-15 23:02:42 +0000227 case OMF_init:
228 // If the method doesn't obey the init rules, don't bother annotating it.
229 if (S.checkInitMethod(method, QualType()))
230 return true;
231
232 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
233 S.Context));
234
235 // Don't add a second copy of this attribute, but otherwise don't
236 // let it be suppressed.
237 if (method->hasAttr<NSReturnsRetainedAttr>())
238 return false;
239 break;
240
241 case OMF_alloc:
242 case OMF_copy:
243 case OMF_mutableCopy:
244 case OMF_new:
245 if (method->hasAttr<NSReturnsRetainedAttr>() ||
246 method->hasAttr<NSReturnsNotRetainedAttr>() ||
247 method->hasAttr<NSReturnsAutoreleasedAttr>())
248 return false;
249 break;
250 }
251
252 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
253 S.Context));
254 return false;
255}
256
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000257static void DiagnoseObjCImplementedDeprecations(Sema &S,
258 NamedDecl *ND,
259 SourceLocation ImplLoc,
260 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000261 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000262 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000263 if (select == 0)
Ted Kremenek3306ec12012-02-27 22:55:11 +0000264 S.Diag(ND->getLocation(), diag::note_method_declared_at)
265 << ND->getDeclName();
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000266 else
267 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
268 }
269}
270
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000271/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
272/// pool.
273void Sema::AddAnyMethodToGlobalPool(Decl *D) {
274 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
275
276 // If we don't have a valid method decl, simply return.
277 if (!MDecl)
278 return;
279 if (MDecl->isInstanceMethod())
280 AddInstanceMethodToGlobalPool(MDecl, true);
281 else
282 AddFactoryMethodToGlobalPool(MDecl, true);
283}
284
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000285/// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer
286/// has explicit ownership attribute; false otherwise.
287static bool
288HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) {
289 QualType T = Param->getType();
290
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000291 if (const PointerType *PT = T->getAs<PointerType>()) {
292 T = PT->getPointeeType();
293 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
294 T = RT->getPointeeType();
295 } else {
296 return true;
297 }
298
299 // If we have a lifetime qualifier, but it's local, we must have
300 // inferred it. So, it is implicit.
301 return !T.getLocalQualifiers().hasObjCLifetime();
302}
303
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +0000304/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
305/// and user declared, in the method definition's AST.
306void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
307 assert((getCurMethodDecl() == 0) && "Methodparsing confused");
John McCalld226f652010-08-21 09:40:31 +0000308 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000309
Steve Naroff394f3f42008-07-25 17:57:26 +0000310 // If we don't have a valid method decl, simply return.
311 if (!MDecl)
312 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000313
Chris Lattner4d391482007-12-12 07:09:47 +0000314 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000315 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000316 PushFunctionScope();
317
Chris Lattner4d391482007-12-12 07:09:47 +0000318 // Create Decl objects for each parameter, entrring them in the scope for
319 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000320
321 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000322 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Daniel Dunbar451318c2008-08-26 06:07:48 +0000324 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
325 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000326
Chris Lattner8123a952008-04-10 02:22:51 +0000327 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000328 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000329 E = MDecl->param_end(); PI != E; ++PI) {
330 ParmVarDecl *Param = (*PI);
331 if (!Param->isInvalidDecl() &&
332 RequireCompleteType(Param->getLocation(), Param->getType(),
333 diag::err_typecheck_decl_incomplete_type))
334 Param->setInvalidDecl();
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000335 if (!Param->isInvalidDecl() &&
336 getLangOpts().ObjCAutoRefCount &&
337 !HasExplicitOwnershipAttr(*this, Param))
338 Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
339 Param->getType();
Fariborz Jahanian918546c2012-08-30 23:56:02 +0000340
Chris Lattner89951a82009-02-20 18:43:26 +0000341 if ((*PI)->getIdentifier())
342 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000343 }
John McCallf85e1932011-06-15 23:02:42 +0000344
345 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000346 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000347 switch (MDecl->getMethodFamily()) {
348 case OMF_retain:
349 case OMF_retainCount:
350 case OMF_release:
351 case OMF_autorelease:
352 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
353 << MDecl->getSelector();
354 break;
355
356 case OMF_None:
357 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000358 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000359 case OMF_alloc:
360 case OMF_init:
361 case OMF_mutableCopy:
362 case OMF_copy:
363 case OMF_new:
364 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000365 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000366 break;
367 }
368 }
369
Nico Weber9a1ecf02011-08-22 17:25:57 +0000370 // Warn on deprecated methods under -Wdeprecated-implementations,
371 // and prepare for warning on missing super calls.
372 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian84101132012-09-07 23:46:23 +0000373 ObjCMethodDecl *IMD =
374 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
375
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000376 if (IMD) {
377 ObjCImplDecl *ImplDeclOfMethodDef =
378 dyn_cast<ObjCImplDecl>(MDecl->getDeclContext());
379 ObjCContainerDecl *ContDeclOfMethodDecl =
380 dyn_cast<ObjCContainerDecl>(IMD->getDeclContext());
381 ObjCImplDecl *ImplDeclOfMethodDecl = 0;
382 if (ObjCInterfaceDecl *OID = dyn_cast<ObjCInterfaceDecl>(ContDeclOfMethodDecl))
383 ImplDeclOfMethodDecl = OID->getImplementation();
384 else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(ContDeclOfMethodDecl))
385 ImplDeclOfMethodDecl = CD->getImplementation();
386 // No need to issue deprecated warning if deprecated mehod in class/category
387 // is being implemented in its own implementation (no overriding is involved).
388 if (!ImplDeclOfMethodDecl || ImplDeclOfMethodDecl != ImplDeclOfMethodDef)
389 DiagnoseObjCImplementedDeprecations(*this,
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000390 dyn_cast<NamedDecl>(IMD),
391 MDecl->getLocation(), 0);
Fariborz Jahanian5fa66762012-11-17 20:53:53 +0000392 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000393
Nico Weber80cb6e62011-08-28 22:35:17 +0000394 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000395 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
396 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
397 // Only do this if the current class actually has a superclass.
Jordan Rose41f3f3a2013-03-05 01:27:54 +0000398 if (const ObjCInterfaceDecl *SuperClass = IC->getSuperClass()) {
Jordan Rose535a5d02012-10-19 16:05:26 +0000399 ObjCMethodFamily Family = MDecl->getMethodFamily();
400 if (Family == OMF_dealloc) {
401 if (!(getLangOpts().ObjCAutoRefCount ||
402 getLangOpts().getGC() == LangOptions::GCOnly))
403 getCurFunction()->ObjCShouldCallSuper = true;
404
405 } else if (Family == OMF_finalize) {
406 if (Context.getLangOpts().getGC() != LangOptions::NonGC)
407 getCurFunction()->ObjCShouldCallSuper = true;
408
409 } else {
410 const ObjCMethodDecl *SuperMethod =
Jordan Rose41f3f3a2013-03-05 01:27:54 +0000411 SuperClass->lookupMethod(MDecl->getSelector(),
412 MDecl->isInstanceMethod());
Jordan Rose535a5d02012-10-19 16:05:26 +0000413 getCurFunction()->ObjCShouldCallSuper =
414 (SuperMethod && SuperMethod->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000415 }
Nico Weber80cb6e62011-08-28 22:35:17 +0000416 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000417 }
Chris Lattner4d391482007-12-12 07:09:47 +0000418}
419
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000420namespace {
421
422// Callback to only accept typo corrections that are Objective-C classes.
423// If an ObjCInterfaceDecl* is given to the constructor, then the validation
424// function will reject corrections to that class.
425class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
426 public:
427 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
428 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
429 : CurrentIDecl(IDecl) {}
430
431 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
432 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
433 return ID && !declaresSameEntity(ID, CurrentIDecl);
434 }
435
436 private:
437 ObjCInterfaceDecl *CurrentIDecl;
438};
439
440}
441
John McCalld226f652010-08-21 09:40:31 +0000442Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000443ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
444 IdentifierInfo *ClassName, SourceLocation ClassLoc,
445 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000446 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000447 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000448 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000449 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Chris Lattner4d391482007-12-12 07:09:47 +0000451 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000452 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000453 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000454
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000455 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000456 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000457 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000458 }
Mike Stump1eb44332009-09-09 15:08:12 +0000459
Douglas Gregor7723fec2011-12-15 20:29:51 +0000460 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000461 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000462 ObjCInterfaceDecl *IDecl
463 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000464 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000465
Douglas Gregor7723fec2011-12-15 20:29:51 +0000466 if (PrevIDecl) {
467 // Class already seen. Was it a definition?
468 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
469 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
470 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000471 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000472 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000473 }
Chris Lattner4d391482007-12-12 07:09:47 +0000474 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000475
476 if (AttrList)
477 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
478 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000479
Douglas Gregor7723fec2011-12-15 20:29:51 +0000480 // Start the definition of this class. If we're in a redefinition case, there
481 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000482 if (!IDecl->hasDefinition())
483 IDecl->startDefinition();
484
Chris Lattner4d391482007-12-12 07:09:47 +0000485 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000486 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000487 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
488 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000489
490 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000491 // Try to correct for a typo in the superclass name without correcting
492 // to the class we're defining.
493 ObjCInterfaceValidatorCCC Validator(IDecl);
494 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000495 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000496 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000497 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
498 Diag(SuperLoc, diag::err_undef_superclass_suggest)
499 << SuperName << ClassName << PrevDecl->getDeclName();
500 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
501 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000502 }
503 }
504
Douglas Gregor60ef3082011-12-15 00:29:59 +0000505 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000506 Diag(SuperLoc, diag::err_recursive_superclass)
507 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000508 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000509 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000510 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000511 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000512
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000513 // Diagnose classes that inherit from deprecated classes.
514 if (SuperClassDecl)
515 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000516
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000517 if (PrevDecl && SuperClassDecl == 0) {
518 // The previous declaration was not a class decl. Check if we have a
519 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000520 if (const TypedefNameDecl *TDecl =
521 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000522 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000523 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000524 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
525 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000526 }
527 }
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000529 // This handles the following case:
530 //
531 // typedef int SuperClass;
532 // @interface MyClass : SuperClass {} @end
533 //
534 if (!SuperClassDecl) {
535 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
536 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000537 }
538 }
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Richard Smith162e1c12011-04-15 14:24:37 +0000540 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000541 if (!SuperClassDecl)
542 Diag(SuperLoc, diag::err_undef_superclass)
543 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000544 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000545 Context.getObjCInterfaceType(SuperClassDecl),
546 diag::err_forward_superclass,
547 SuperClassDecl->getDeclName(),
548 ClassName,
549 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000550 SuperClassDecl = 0;
551 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000552 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000553 IDecl->setSuperClass(SuperClassDecl);
554 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000555 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000556 }
Chris Lattner4d391482007-12-12 07:09:47 +0000557 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000558 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000559 }
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Sebastian Redl0b17c612010-08-13 00:28:03 +0000561 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000562 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000563 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000564 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000565 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000566 }
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Anders Carlsson15281452008-11-04 16:57:32 +0000568 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000569 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000570}
571
Richard Smithde01b7a2012-08-08 23:32:13 +0000572/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000573/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000574Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
575 IdentifierInfo *AliasName,
576 SourceLocation AliasLocation,
577 IdentifierInfo *ClassName,
578 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000579 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000580 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000581 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000582 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000583 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000584 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000585 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000586 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000587 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000588 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000589 }
590 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000591 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000592 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000593 if (const TypedefNameDecl *TDecl =
594 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000595 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000596 if (T->isObjCObjectType()) {
597 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000598 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000599 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000600 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000601 }
602 }
603 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000604 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
605 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000606 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000607 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000608 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000609 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000610 }
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000612 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000613 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000614 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Anders Carlsson15281452008-11-04 16:57:32 +0000616 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000617 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000618
John McCalld226f652010-08-21 09:40:31 +0000619 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000620}
621
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000622bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000623 IdentifierInfo *PName,
624 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000625 const ObjCList<ObjCProtocolDecl> &PList) {
626
627 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000628 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
629 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000630 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
631 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000632 if (PDecl->getIdentifier() == PName) {
633 Diag(Ploc, diag::err_protocol_has_circular_dependency);
634 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000635 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000636 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000637
638 if (!PDecl->hasDefinition())
639 continue;
640
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000641 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
642 PDecl->getLocation(), PDecl->getReferencedProtocols()))
643 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000644 }
645 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000646 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000647}
648
John McCalld226f652010-08-21 09:40:31 +0000649Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000650Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
651 IdentifierInfo *ProtocolName,
652 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000653 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000654 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000655 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000656 SourceLocation EndProtoLoc,
657 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000658 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000659 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000660 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000661 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
662 ForRedeclaration);
663 ObjCProtocolDecl *PDecl = 0;
664 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
665 // If we already have a definition, complain.
666 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
667 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Douglas Gregor27c6da22012-01-01 20:30:41 +0000669 // Create a new protocol that is completely distinct from previous
670 // declarations, and do not make this protocol available for name lookup.
671 // That way, we'll end up completely ignoring the duplicate.
672 // FIXME: Can we turn this into an error?
673 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
674 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000675 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000676 PDecl->startDefinition();
677 } else {
678 if (PrevDecl) {
679 // Check for circular dependencies among protocol declarations. This can
680 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000681 ObjCList<ObjCProtocolDecl> PList;
682 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
683 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000684 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000685 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000686
687 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000688 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000689 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000690 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000691
Douglas Gregor6e378de2009-04-23 23:18:26 +0000692 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000693 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000694 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000695
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000696 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000697 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000698
699 // Merge attributes from previous declarations.
700 if (PrevDecl)
701 mergeDeclAttributes(PDecl, PrevDecl);
702
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000703 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000704 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000705 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000706 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000707 }
Mike Stump1eb44332009-09-09 15:08:12 +0000708
709 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000710 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000711}
712
713/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000714/// issues an error if they are not declared. It returns list of
715/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000716void
Chris Lattnere13b9592008-07-26 04:03:38 +0000717Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000718 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000719 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000720 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000721 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000722 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
723 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000724 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000725 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000726 TypoCorrection Corrected = CorrectTypo(
727 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000728 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000729 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000730 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000731 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000732 Diag(PDecl->getLocation(), diag::note_previous_decl)
733 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000734 }
735 }
736
737 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000738 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000739 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000740 continue;
741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000743 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000744
745 // If this is a forward declaration and we are supposed to warn in this
746 // case, do it.
Douglas Gregor0f9b9f32013-01-17 00:38:46 +0000747 // FIXME: Recover nicely in the hidden case.
748 if (WarnOnDeclarations &&
749 (!PDecl->hasDefinition() || PDecl->getDefinition()->isHidden()))
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000750 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000751 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000752 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000753 }
754}
755
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000756/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000757/// a class method in its extension.
758///
Mike Stump1eb44332009-09-09 15:08:12 +0000759void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000760 ObjCInterfaceDecl *ID) {
761 if (!ID)
762 return; // Possibly due to previous error
763
764 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000765 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
766 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000767 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000768 MethodMap[MD->getSelector()] = MD;
769 }
770
771 if (MethodMap.empty())
772 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000773 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
774 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000775 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000776 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
777 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
778 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
779 << Method->getDeclName();
780 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
781 }
782 }
783}
784
James Dennett1dfbd922012-06-14 21:40:34 +0000785/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000786Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000787Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000788 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000789 unsigned NumElts,
790 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000791 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000792 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000793 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000794 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
795 ForRedeclaration);
796 ObjCProtocolDecl *PDecl
797 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
798 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000799 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000800
801 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000802 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000803
Douglas Gregor3937f872012-01-01 20:33:24 +0000804 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000805 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000806
807 if (PrevDecl)
808 mergeDeclAttributes(PDecl, PrevDecl);
809
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000810 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000811 }
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000813 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000814}
815
John McCalld226f652010-08-21 09:40:31 +0000816Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000817ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
818 IdentifierInfo *ClassName, SourceLocation ClassLoc,
819 IdentifierInfo *CategoryName,
820 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000821 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000822 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000823 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000824 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000825 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000826 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000827
828 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000829
830 if (!IDecl
831 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000832 diag::err_category_forward_interface,
833 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000834 // Create an invalid ObjCCategoryDecl to serve as context for
835 // the enclosing method declarations. We mark the decl invalid
836 // to make it clear that this isn't a valid AST.
837 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000838 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000839 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000840 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000841
842 if (!IDecl)
843 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000844 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000845 }
846
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000847 if (!CategoryName && IDecl->getImplementation()) {
848 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
849 Diag(IDecl->getImplementation()->getLocation(),
850 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000851 }
852
Fariborz Jahanian25760612010-02-15 21:55:26 +0000853 if (CategoryName) {
854 /// Check for duplicate interface declaration for this category
Douglas Gregord3297242013-01-16 23:00:23 +0000855 if (ObjCCategoryDecl *Previous
856 = IDecl->FindCategoryDeclaration(CategoryName)) {
857 // Class extensions can be declared multiple times, categories cannot.
858 Diag(CategoryLoc, diag::warn_dup_category_def)
859 << ClassName << CategoryName;
860 Diag(Previous->getLocation(), diag::note_previous_definition);
Chris Lattner70f19542009-02-16 21:26:43 +0000861 }
862 }
Chris Lattner70f19542009-02-16 21:26:43 +0000863
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000864 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
865 ClassLoc, CategoryLoc, CategoryName, IDecl);
866 // FIXME: PushOnScopeChains?
867 CurContext->addDecl(CDecl);
868
Chris Lattner4d391482007-12-12 07:09:47 +0000869 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000870 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000871 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000872 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000873 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000874 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000875 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Anders Carlsson15281452008-11-04 16:57:32 +0000878 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000879 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000880}
881
882/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000883/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000884/// object.
John McCalld226f652010-08-21 09:40:31 +0000885Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000886 SourceLocation AtCatImplLoc,
887 IdentifierInfo *ClassName, SourceLocation ClassLoc,
888 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000889 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000890 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000891 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000892 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
893 if (!CatIDecl) {
894 // Category @implementation with no corresponding @interface.
895 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000896 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
897 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000898 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000899 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000900 }
901 }
902
Mike Stump1eb44332009-09-09 15:08:12 +0000903 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000904 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000905 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000906 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000907 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000908 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000909 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000910 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
911 diag::err_undef_interface)) {
912 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000913 }
Chris Lattner4d391482007-12-12 07:09:47 +0000914
Douglas Gregord0434102009-01-09 00:49:46 +0000915 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000916 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000917
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000918 // If the interface is deprecated/unavailable, warn/error about it.
919 if (IDecl)
920 DiagnoseUseOfDecl(IDecl, ClassLoc);
921
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000922 /// Check that CatName, category name, is not used in another implementation.
923 if (CatIDecl) {
924 if (CatIDecl->getImplementation()) {
925 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
926 << CatName;
927 Diag(CatIDecl->getImplementation()->getLocation(),
928 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000929 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000930 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000931 // Warn on implementating category of deprecated class under
932 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000933 DiagnoseObjCImplementedDeprecations(*this,
934 dyn_cast<NamedDecl>(IDecl),
935 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000936 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000937 }
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Anders Carlsson15281452008-11-04 16:57:32 +0000939 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000940 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000941}
942
John McCalld226f652010-08-21 09:40:31 +0000943Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000944 SourceLocation AtClassImplLoc,
945 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000946 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000947 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000948 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000949 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000950 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000951 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
952 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000953 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000954 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000955 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000956 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000957 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
958 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000959 } else {
960 // We did not find anything with the name ClassName; try to correct for
961 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000962 ObjCInterfaceValidatorCCC Validator;
963 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000964 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000965 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000966 // Suggest the (potentially) correct interface name. However, put the
967 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000968 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000969 // provide a code-modification hint or use the typo name for recovery,
970 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000971 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000972 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000973 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000974 << ClassName << CorrectedName;
975 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
976 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000977 IDecl = 0;
978 } else {
979 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
980 }
Chris Lattner4d391482007-12-12 07:09:47 +0000981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Chris Lattner4d391482007-12-12 07:09:47 +0000983 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000984 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000985 if (SuperClassname) {
986 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000987 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
988 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000989 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000990 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
991 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000992 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000993 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000994 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000995 if (SDecl && !SDecl->hasDefinition())
996 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000997 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000998 Diag(SuperClassLoc, diag::err_undef_superclass)
999 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +00001000 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00001001 // This implementation and its interface do not have the same
1002 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +00001003 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +00001004 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001005 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001006 }
1007 }
1008 }
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Chris Lattner4d391482007-12-12 07:09:47 +00001010 if (!IDecl) {
1011 // Legacy case of @implementation with no corresponding @interface.
1012 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +00001013
Mike Stump390b4cc2009-05-16 07:39:55 +00001014 // FIXME: Do we support attributes on the @implementation? If so we should
1015 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001016 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001017 ClassName, /*PrevDecl=*/0, ClassLoc,
1018 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001019 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001020 if (SDecl) {
1021 IDecl->setSuperClass(SDecl);
1022 IDecl->setSuperClassLoc(SuperClassLoc);
1023 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1024 } else {
1025 IDecl->setEndOfDefinitionLoc(ClassLoc);
1026 }
1027
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001028 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001029 } else {
1030 // Mark the interface as being completed, even if it was just as
1031 // @class ....;
1032 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001033 if (!IDecl->hasDefinition())
1034 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001035 }
Mike Stump1eb44332009-09-09 15:08:12 +00001036
1037 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001038 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1039 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Anders Carlsson15281452008-11-04 16:57:32 +00001041 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001042 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001043
Chris Lattner4d391482007-12-12 07:09:47 +00001044 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001045 if (IDecl->getImplementation()) {
1046 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001047 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001048 Diag(IDecl->getImplementation()->getLocation(),
1049 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001050 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001051 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001052 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001053 // Warn on implementating deprecated class under
1054 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001055 DiagnoseObjCImplementedDeprecations(*this,
1056 dyn_cast<NamedDecl>(IDecl),
1057 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001058 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001059 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001060}
1061
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001062Sema::DeclGroupPtrTy
1063Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1064 SmallVector<Decl *, 64> DeclsInGroup;
1065 DeclsInGroup.reserve(Decls.size() + 1);
1066
1067 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1068 Decl *Dcl = Decls[i];
1069 if (!Dcl)
1070 continue;
1071 if (Dcl->getDeclContext()->isFileContext())
1072 Dcl->setTopLevelDeclInObjCContainer();
1073 DeclsInGroup.push_back(Dcl);
1074 }
1075
1076 DeclsInGroup.push_back(ObjCImpDecl);
1077
1078 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1079}
1080
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001081void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1082 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001083 SourceLocation RBrace) {
1084 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001085 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001086 if (!IDecl)
1087 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001088 /// Check case of non-existing \@interface decl.
1089 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001090 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001091 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001092 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001093 // Add ivar's to class's DeclContext.
1094 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001095 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001096 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001097 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001098 }
1099
Chris Lattner4d391482007-12-12 07:09:47 +00001100 return;
1101 }
1102 // If implementation has empty ivar list, just return.
1103 if (numIvars == 0)
1104 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Chris Lattner4d391482007-12-12 07:09:47 +00001106 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001107 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001108 if (ImpDecl->getSuperClass())
1109 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1110 for (unsigned i = 0; i < numIvars; i++) {
1111 ObjCIvarDecl* ImplIvar = ivars[i];
1112 if (const ObjCIvarDecl *ClsIvar =
1113 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1114 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1115 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1116 continue;
1117 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001118 // Instance ivar to Implementation's DeclContext.
1119 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001120 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001121 ImpDecl->addDecl(ImplIvar);
1122 }
1123 return;
1124 }
Chris Lattner4d391482007-12-12 07:09:47 +00001125 // Check interface's Ivar list against those in the implementation.
1126 // names and types must match.
1127 //
Chris Lattner4d391482007-12-12 07:09:47 +00001128 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001129 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001130 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1131 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001132 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001133 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001134 assert (ImplIvar && "missing implementation ivar");
1135 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001136
Steve Naroffca331292009-03-03 14:49:36 +00001137 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001138 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001139 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001140 << ImplIvar->getIdentifier()
1141 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001142 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001143 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1144 ImplIvar->getBitWidthValue(Context) !=
1145 ClsIvar->getBitWidthValue(Context)) {
1146 Diag(ImplIvar->getBitWidth()->getLocStart(),
1147 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1148 Diag(ClsIvar->getBitWidth()->getLocStart(),
1149 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001150 }
Steve Naroffca331292009-03-03 14:49:36 +00001151 // Make sure the names are identical.
1152 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001153 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001154 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001155 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001156 }
1157 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001158 }
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Chris Lattner609e4c72007-12-12 18:11:49 +00001160 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001161 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001162 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001163 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001164}
1165
Steve Naroff3c2eb662008-02-10 21:38:56 +00001166void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001167 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001168 // No point warning no definition of method which is 'unavailable'.
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001169 switch (method->getAvailability()) {
1170 case AR_Available:
1171 case AR_Deprecated:
1172 break;
1173
1174 // Don't warn about unavailable or not-yet-introduced methods.
1175 case AR_NotYetIntroduced:
1176 case AR_Unavailable:
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001177 return;
Douglas Gregor86f6cf62012-12-11 18:53:07 +00001178 }
1179
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001180 // FIXME: For now ignore 'IncompleteImpl'.
1181 // Previously we grouped all unimplemented methods under a single
1182 // warning, but some users strongly voiced that they would prefer
1183 // separate warnings. We will give that approach a try, as that
1184 // matches what we do with protocols.
1185
1186 Diag(ImpLoc, DiagID) << method->getDeclName();
1187
1188 // Issue a note to the original declaration.
1189 SourceLocation MethodLoc = method->getLocStart();
1190 if (MethodLoc.isValid())
1191 Diag(MethodLoc, diag::note_method_declared_at) << method;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001192}
1193
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001194/// Determines if type B can be substituted for type A. Returns true if we can
1195/// guarantee that anything that the user will do to an object of type A can
1196/// also be done to an object of type B. This is trivially true if the two
1197/// types are the same, or if B is a subclass of A. It becomes more complex
1198/// in cases where protocols are involved.
1199///
1200/// Object types in Objective-C describe the minimum requirements for an
1201/// object, rather than providing a complete description of a type. For
1202/// example, if A is a subclass of B, then B* may refer to an instance of A.
1203/// The principle of substitutability means that we may use an instance of A
1204/// anywhere that we may use an instance of B - it will implement all of the
1205/// ivars of B and all of the methods of B.
1206///
1207/// This substitutability is important when type checking methods, because
1208/// the implementation may have stricter type definitions than the interface.
1209/// The interface specifies minimum requirements, but the implementation may
1210/// have more accurate ones. For example, a method may privately accept
1211/// instances of B, but only publish that it accepts instances of A. Any
1212/// object passed to it will be type checked against B, and so will implicitly
1213/// by a valid A*. Similarly, a method may return a subclass of the class that
1214/// it is declared as returning.
1215///
1216/// This is most important when considering subclassing. A method in a
1217/// subclass must accept any object as an argument that its superclass's
1218/// implementation accepts. It may, however, accept a more general type
1219/// without breaking substitutability (i.e. you can still use the subclass
1220/// anywhere that you can use the superclass, but not vice versa). The
1221/// converse requirement applies to return types: the return type for a
1222/// subclass method must be a valid object of the kind that the superclass
1223/// advertises, but it may be specified more accurately. This avoids the need
1224/// for explicit down-casting by callers.
1225///
1226/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001227static bool isObjCTypeSubstitutable(ASTContext &Context,
1228 const ObjCObjectPointerType *A,
1229 const ObjCObjectPointerType *B,
1230 bool rejectId) {
1231 // Reject a protocol-unqualified id.
1232 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001233
1234 // If B is a qualified id, then A must also be a qualified id and it must
1235 // implement all of the protocols in B. It may not be a qualified class.
1236 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1237 // stricter definition so it is not substitutable for id<A>.
1238 if (B->isObjCQualifiedIdType()) {
1239 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001240 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1241 QualType(B,0),
1242 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001243 }
1244
1245 /*
1246 // id is a special type that bypasses type checking completely. We want a
1247 // warning when it is used in one place but not another.
1248 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1249
1250
1251 // If B is a qualified id, then A must also be a qualified id (which it isn't
1252 // if we've got this far)
1253 if (B->isObjCQualifiedIdType()) return false;
1254 */
1255
1256 // Now we know that A and B are (potentially-qualified) class types. The
1257 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001258 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001259}
1260
John McCall10302c02010-10-28 02:34:38 +00001261static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1262 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1263}
1264
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001265static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001266 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001267 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001268 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001269 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001270 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001271 if (IsProtocolMethodDecl &&
1272 (MethodDecl->getObjCDeclQualifier() !=
1273 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001274 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001275 S.Diag(MethodImpl->getLocation(),
1276 (IsOverridingMode ?
1277 diag::warn_conflicting_overriding_ret_type_modifiers
1278 : diag::warn_conflicting_ret_type_modifiers))
1279 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001280 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1281 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1282 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1283 }
1284 else
1285 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001286 }
1287
John McCall10302c02010-10-28 02:34:38 +00001288 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001289 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001290 return true;
1291 if (!Warn)
1292 return false;
John McCall10302c02010-10-28 02:34:38 +00001293
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001294 unsigned DiagID =
1295 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1296 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001297
1298 // Mismatches between ObjC pointers go into a different warning
1299 // category, and sometimes they're even completely whitelisted.
1300 if (const ObjCObjectPointerType *ImplPtrTy =
1301 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1302 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001303 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001304 // Allow non-matching return types as long as they don't violate
1305 // the principle of substitutability. Specifically, we permit
1306 // return types that are subclasses of the declared return type,
1307 // or that are more-qualified versions of the declared type.
1308 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001309 return false;
John McCall10302c02010-10-28 02:34:38 +00001310
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001311 DiagID =
1312 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1313 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001314 }
1315 }
1316
1317 S.Diag(MethodImpl->getLocation(), DiagID)
1318 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001319 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001320 << MethodImpl->getResultType()
1321 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001322 S.Diag(MethodDecl->getLocation(),
1323 IsOverridingMode ? diag::note_previous_declaration
1324 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001325 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001326 return false;
John McCall10302c02010-10-28 02:34:38 +00001327}
1328
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001329static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001330 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001331 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001332 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001333 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001334 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001335 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001336 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001337 if (IsProtocolMethodDecl &&
1338 (ImplVar->getObjCDeclQualifier() !=
1339 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001340 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001341 if (IsOverridingMode)
1342 S.Diag(ImplVar->getLocation(),
1343 diag::warn_conflicting_overriding_param_modifiers)
1344 << getTypeRange(ImplVar->getTypeSourceInfo())
1345 << MethodImpl->getDeclName();
1346 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001347 diag::warn_conflicting_param_modifiers)
1348 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001349 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001350 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1351 << getTypeRange(IfaceVar->getTypeSourceInfo());
1352 }
1353 else
1354 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001355 }
1356
John McCall10302c02010-10-28 02:34:38 +00001357 QualType ImplTy = ImplVar->getType();
1358 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001359
John McCall10302c02010-10-28 02:34:38 +00001360 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001361 return true;
1362
1363 if (!Warn)
1364 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001365 unsigned DiagID =
1366 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1367 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001368
1369 // Mismatches between ObjC pointers go into a different warning
1370 // category, and sometimes they're even completely whitelisted.
1371 if (const ObjCObjectPointerType *ImplPtrTy =
1372 ImplTy->getAs<ObjCObjectPointerType>()) {
1373 if (const ObjCObjectPointerType *IfacePtrTy =
1374 IfaceTy->getAs<ObjCObjectPointerType>()) {
1375 // Allow non-matching argument types as long as they don't
1376 // violate the principle of substitutability. Specifically, the
1377 // implementation must accept any objects that the superclass
1378 // accepts, however it may also accept others.
1379 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001380 return false;
John McCall10302c02010-10-28 02:34:38 +00001381
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001382 DiagID =
1383 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1384 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001385 }
1386 }
1387
1388 S.Diag(ImplVar->getLocation(), DiagID)
1389 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001390 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1391 S.Diag(IfaceVar->getLocation(),
1392 (IsOverridingMode ? diag::note_previous_declaration
1393 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001394 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001395 return false;
John McCall10302c02010-10-28 02:34:38 +00001396}
John McCallf85e1932011-06-15 23:02:42 +00001397
1398/// In ARC, check whether the conventional meanings of the two methods
1399/// match. If they don't, it's a hard error.
1400static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1401 ObjCMethodDecl *decl) {
1402 ObjCMethodFamily implFamily = impl->getMethodFamily();
1403 ObjCMethodFamily declFamily = decl->getMethodFamily();
1404 if (implFamily == declFamily) return false;
1405
1406 // Since conventions are sorted by selector, the only possibility is
1407 // that the types differ enough to cause one selector or the other
1408 // to fall out of the family.
1409 assert(implFamily == OMF_None || declFamily == OMF_None);
1410
1411 // No further diagnostics required on invalid declarations.
1412 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1413
1414 const ObjCMethodDecl *unmatched = impl;
1415 ObjCMethodFamily family = declFamily;
1416 unsigned errorID = diag::err_arc_lost_method_convention;
1417 unsigned noteID = diag::note_arc_lost_method_convention;
1418 if (declFamily == OMF_None) {
1419 unmatched = decl;
1420 family = implFamily;
1421 errorID = diag::err_arc_gained_method_convention;
1422 noteID = diag::note_arc_gained_method_convention;
1423 }
1424
1425 // Indexes into a %select clause in the diagnostic.
1426 enum FamilySelector {
1427 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1428 };
1429 FamilySelector familySelector = FamilySelector();
1430
1431 switch (family) {
1432 case OMF_None: llvm_unreachable("logic error, no method convention");
1433 case OMF_retain:
1434 case OMF_release:
1435 case OMF_autorelease:
1436 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001437 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001438 case OMF_retainCount:
1439 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001440 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001441 // Mismatches for these methods don't change ownership
1442 // conventions, so we don't care.
1443 return false;
1444
1445 case OMF_init: familySelector = F_init; break;
1446 case OMF_alloc: familySelector = F_alloc; break;
1447 case OMF_copy: familySelector = F_copy; break;
1448 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1449 case OMF_new: familySelector = F_new; break;
1450 }
1451
1452 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1453 ReasonSelector reasonSelector;
1454
1455 // The only reason these methods don't fall within their families is
1456 // due to unusual result types.
1457 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1458 reasonSelector = R_UnrelatedReturn;
1459 } else {
1460 reasonSelector = R_NonObjectReturn;
1461 }
1462
1463 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1464 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1465
1466 return true;
1467}
John McCall10302c02010-10-28 02:34:38 +00001468
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001469void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001470 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001471 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001472 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001473 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1474 return;
1475
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001476 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001477 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001478 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Chris Lattner3aff9192009-04-11 19:58:42 +00001480 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001481 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1482 EF = MethodDecl->param_end();
1483 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001484 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001485 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001486 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001487
Fariborz Jahanian21121902011-08-08 18:03:17 +00001488 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001489 Diag(ImpMethodDecl->getLocation(),
1490 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001491 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001492 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001493}
1494
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001495void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1496 ObjCMethodDecl *Overridden,
1497 bool IsProtocolMethodDecl) {
1498
1499 CheckMethodOverrideReturn(*this, Method, Overridden,
1500 IsProtocolMethodDecl, true,
1501 true);
1502
1503 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001504 IF = Overridden->param_begin(), EM = Method->param_end(),
1505 EF = Overridden->param_end();
1506 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001507 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1508 IsProtocolMethodDecl, true, true);
1509 }
1510
1511 if (Method->isVariadic() != Overridden->isVariadic()) {
1512 Diag(Method->getLocation(),
1513 diag::warn_conflicting_overriding_variadic);
1514 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1515 }
1516}
1517
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001518/// WarnExactTypedMethods - This routine issues a warning if method
1519/// implementation declaration matches exactly that of its declaration.
1520void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1521 ObjCMethodDecl *MethodDecl,
1522 bool IsProtocolMethodDecl) {
1523 // don't issue warning when protocol method is optional because primary
1524 // class is not required to implement it and it is safe for protocol
1525 // to implement it.
1526 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1527 return;
1528 // don't issue warning when primary class's method is
1529 // depecated/unavailable.
1530 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1531 MethodDecl->hasAttr<DeprecatedAttr>())
1532 return;
1533
1534 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1535 IsProtocolMethodDecl, false, false);
1536 if (match)
1537 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001538 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1539 EF = MethodDecl->param_end();
1540 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001541 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1542 *IM, *IF,
1543 IsProtocolMethodDecl, false, false);
1544 if (!match)
1545 break;
1546 }
1547 if (match)
1548 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001549 if (match)
1550 match = !(MethodDecl->isClassMethod() &&
1551 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001552
1553 if (match) {
1554 Diag(ImpMethodDecl->getLocation(),
1555 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001556 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1557 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001558 }
1559}
1560
Mike Stump390b4cc2009-05-16 07:39:55 +00001561/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1562/// improve the efficiency of selector lookups and type checking by associating
1563/// with each protocol / interface / category the flattened instance tables. If
1564/// we used an immutable set to keep the table then it wouldn't add significant
1565/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001566
Steve Naroffefe7f362008-02-08 22:06:17 +00001567/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001568/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001569void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1570 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001571 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001572 const SelectorSet &InsMap,
1573 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001574 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001575 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1576 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1577 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001578 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1579
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001580 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001581 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001582 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001583 // check to see if class implements forwardInvocation method and objects
1584 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001585 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001586 // Under such conditions, which means that every method possible is
1587 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001588 // found" warnings.
1589 // FIXME: Use a general GetUnarySelector method for this.
1590 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1591 Selector fISelector = Context.Selectors.getSelector(1, &II);
1592 if (InsMap.count(fISelector))
1593 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1594 // need be implemented in the implementation.
1595 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1596 }
Mike Stump1eb44332009-09-09 15:08:12 +00001597
Fariborz Jahanian32b94be2013-01-07 19:21:03 +00001598 // If this is a forward protocol declaration, get its definition.
1599 if (!PDecl->isThisDeclarationADefinition() &&
1600 PDecl->getDefinition())
1601 PDecl = PDecl->getDefinition();
1602
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001603 // If a method lookup fails locally we still need to look and see if
1604 // the method was implemented by a base class or an inherited
1605 // protocol. This lookup is slow, but occurs rarely in correct code
1606 // and otherwise would terminate in a warning.
1607
Chris Lattner4d391482007-12-12 07:09:47 +00001608 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001609 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001610 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001611 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001612 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001613 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001614 !method->isPropertyAccessor() &&
1615 !InsMap.count(method->getSelector()) &&
1616 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001617 // If a method is not implemented in the category implementation but
1618 // has been declared in its primary class, superclass,
1619 // or in one of their protocols, no need to issue the warning.
1620 // This is because method will be implemented in the primary class
1621 // or one of its super class implementation.
1622
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001623 // Ugly, but necessary. Method declared in protcol might have
1624 // have been synthesized due to a property declared in the class which
1625 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001626 if (ObjCMethodDecl *MethodInClass =
1627 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001628 true /*shallowCategoryLookup*/))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001629 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001630 continue;
1631 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1632 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1633 != DiagnosticsEngine::Ignored) {
1634 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001635 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1636 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001637 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001638 }
1639 }
Chris Lattner4d391482007-12-12 07:09:47 +00001640 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001641 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001642 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001643 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001644 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001645 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1646 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001647 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001648 // See above comment for instance method lookups.
1649 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001650 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001651 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001652 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001653 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1654 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001655 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
1656 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1657 PDecl->getDeclName();
1658 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001659 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001660 }
Chris Lattner780f3292008-07-21 21:32:27 +00001661 // Check on this protocols's referenced protocols, recursively.
1662 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1663 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001664 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001665}
1666
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001667/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001668/// or protocol against those declared in their implementations.
1669///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001670void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1671 const SelectorSet &ClsMap,
1672 SelectorSet &InsMapSeen,
1673 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001674 ObjCImplDecl* IMPDecl,
1675 ObjCContainerDecl* CDecl,
1676 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001677 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001678 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001679 // Check and see if instance methods in class interface have been
1680 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001681 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1682 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001683 if (InsMapSeen.count((*I)->getSelector()))
1684 continue;
1685 InsMapSeen.insert((*I)->getSelector());
Jordan Rose1e4691b2012-10-10 16:42:25 +00001686 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001687 !InsMap.count((*I)->getSelector())) {
1688 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001689 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001690 diag::warn_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001691 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001692 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001693 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001694 IMPDecl->getInstanceMethod((*I)->getSelector());
1695 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1696 "Expected to find the method through lookup as well");
1697 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001698 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001699 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001700 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001701 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1702 isa<ObjCProtocolDecl>(CDecl));
Jordan Rose1e4691b2012-10-10 16:42:25 +00001703 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001704 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001705 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001706 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001707 }
1708 }
Mike Stump1eb44332009-09-09 15:08:12 +00001709
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001710 // Check and see if class methods in class interface have been
1711 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001712 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001713 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001714 if (ClsMapSeen.count((*I)->getSelector()))
1715 continue;
1716 ClsMapSeen.insert((*I)->getSelector());
1717 if (!ClsMap.count((*I)->getSelector())) {
1718 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001719 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
Ted Kremenek8b43d2b2013-03-27 00:02:21 +00001720 diag::warn_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001721 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001722 ObjCMethodDecl *ImpMethodDecl =
1723 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001724 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1725 "Expected to find the method through lookup as well");
1726 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001727 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001728 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1729 isa<ObjCProtocolDecl>(CDecl));
1730 else
1731 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001732 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001733 }
1734 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001735
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001736 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001737 // when checking that methods in implementation match their declaration,
1738 // i.e. when WarnCategoryMethodImpl is false, check declarations in class
1739 // extension; as well as those in categories.
Douglas Gregord3297242013-01-16 23:00:23 +00001740 if (!WarnCategoryMethodImpl) {
1741 for (ObjCInterfaceDecl::visible_categories_iterator
1742 Cat = I->visible_categories_begin(),
1743 CatEnd = I->visible_categories_end();
1744 Cat != CatEnd; ++Cat) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001745 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001746 IMPDecl, *Cat, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001747 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001748 }
1749 } else {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001750 // Also methods in class extensions need be looked at next.
Douglas Gregord3297242013-01-16 23:00:23 +00001751 for (ObjCInterfaceDecl::visible_extensions_iterator
1752 Ext = I->visible_extensions_begin(),
1753 ExtEnd = I->visible_extensions_end();
1754 Ext != ExtEnd; ++Ext) {
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001755 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Douglas Gregord3297242013-01-16 23:00:23 +00001756 IMPDecl, *Ext, IncompleteImpl, false,
Fariborz Jahanian6a6bb282012-10-23 23:06:22 +00001757 WarnCategoryMethodImpl);
Douglas Gregord3297242013-01-16 23:00:23 +00001758 }
1759 }
1760
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001761 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001762 for (ObjCInterfaceDecl::all_protocol_iterator
1763 PI = I->all_referenced_protocol_begin(),
1764 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001765 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1766 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001767 (*PI), IncompleteImpl, false,
1768 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001769
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001770 // FIXME. For now, we are not checking for extact match of methods
1771 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001772 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001773 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001774 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001775 I->getSuperClass(), IncompleteImpl, false);
1776 }
1777}
1778
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001779/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1780/// category matches with those implemented in its primary class and
1781/// warns each time an exact match is found.
1782void Sema::CheckCategoryVsClassMethodMatches(
1783 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001784 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001785
1786 for (ObjCImplementationDecl::instmeth_iterator
1787 I = CatIMPDecl->instmeth_begin(),
1788 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1789 InsMap.insert((*I)->getSelector());
1790
1791 for (ObjCImplementationDecl::classmeth_iterator
1792 I = CatIMPDecl->classmeth_begin(),
1793 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1794 ClsMap.insert((*I)->getSelector());
1795 if (InsMap.empty() && ClsMap.empty())
1796 return;
1797
1798 // Get category's primary class.
1799 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1800 if (!CatDecl)
1801 return;
1802 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1803 if (!IDecl)
1804 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001805 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001806 bool IncompleteImpl = false;
1807 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1808 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001809 IncompleteImpl, false,
1810 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001811}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001812
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001813void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001814 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001815 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001816 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001817 // Check and see if instance methods in class interface have been
1818 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001819 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001820 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001821 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001822
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001823 // Check and see if properties declared in the interface have either 1)
1824 // an implementation or 2) there is a @synthesize/@dynamic implementation
1825 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001826 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001827 if (!(LangOpts.ObjCDefaultSynthProperties &&
1828 LangOpts.ObjCRuntime.isNonFragile()) ||
1829 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001830 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001831
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001832 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001833 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001834 I = IMPDecl->classmeth_begin(),
1835 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001836 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001838 // Check for type conflict of methods declared in a class/protocol and
1839 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001840 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001841 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1842 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001843 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001844
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001845 // check all methods implemented in category against those declared
1846 // in its primary class.
1847 if (ObjCCategoryImplDecl *CatDecl =
1848 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1849 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001850
Chris Lattner4d391482007-12-12 07:09:47 +00001851 // Check the protocol list for unimplemented methods in the @implementation
1852 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001853 // Check and see if class methods in class interface have been
1854 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001855
Chris Lattnercddc8882009-03-01 00:56:52 +00001856 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001857 for (ObjCInterfaceDecl::all_protocol_iterator
1858 PI = I->all_referenced_protocol_begin(),
1859 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001860 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001861 InsMap, ClsMap, I);
1862 // Check class extensions (unnamed categories)
Douglas Gregord3297242013-01-16 23:00:23 +00001863 for (ObjCInterfaceDecl::visible_extensions_iterator
1864 Ext = I->visible_extensions_begin(),
1865 ExtEnd = I->visible_extensions_end();
1866 Ext != ExtEnd; ++Ext) {
1867 ImplMethodsVsClassMethods(S, IMPDecl, *Ext, IncompleteImpl);
1868 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001869 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001870 // For extended class, unimplemented methods in its protocols will
1871 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001872 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001873 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1874 E = C->protocol_end(); PI != E; ++PI)
1875 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001876 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001877 // Report unimplemented properties in the category as well.
1878 // When reporting on missing setter/getters, do not report when
1879 // setter/getter is implemented in category's primary class
1880 // implementation.
1881 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1882 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1883 for (ObjCImplementationDecl::instmeth_iterator
1884 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1885 InsMap.insert((*I)->getSelector());
1886 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001887 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001888 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001889 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001890 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001891}
1892
Mike Stump1eb44332009-09-09 15:08:12 +00001893/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001894Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001895Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001896 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001897 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001898 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001899 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001900 for (unsigned i = 0; i != NumElts; ++i) {
1901 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001902 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001903 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001904 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001905 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001906 // Maybe we will complain about the shadowed template parameter.
1907 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1908 // Just pretend that we didn't see the previous declaration.
1909 PrevDecl = 0;
1910 }
1911
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001912 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001913 // GCC apparently allows the following idiom:
1914 //
1915 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1916 // @class XCElementToggler;
1917 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001918 // Here we have chosen to ignore the forward class declaration
1919 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001920 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001921 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001922 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001923 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001924 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001925 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001926 // to the underlying class. Just ignore the forward class with a warning
1927 // as this will force the intended behavior which is to lookup the typedef
1928 // name.
1929 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1930 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1931 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1932 continue;
1933 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001934 }
Chris Lattner4d391482007-12-12 07:09:47 +00001935 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001936
1937 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001938 ObjCInterfaceDecl *PrevIDecl
1939 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001940 ObjCInterfaceDecl *IDecl
1941 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001942 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001943 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001944
Douglas Gregor7723fec2011-12-15 20:29:51 +00001945 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001946 CheckObjCDeclScope(IDecl);
1947 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001948 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001949
1950 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001951}
1952
John McCall0f4c4c42011-06-16 01:15:19 +00001953static bool tryMatchRecordTypes(ASTContext &Context,
1954 Sema::MethodMatchStrategy strategy,
1955 const Type *left, const Type *right);
1956
John McCallf85e1932011-06-15 23:02:42 +00001957static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1958 QualType leftQT, QualType rightQT) {
1959 const Type *left =
1960 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1961 const Type *right =
1962 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1963
1964 if (left == right) return true;
1965
1966 // If we're doing a strict match, the types have to match exactly.
1967 if (strategy == Sema::MMS_strict) return false;
1968
1969 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1970
1971 // Otherwise, use this absurdly complicated algorithm to try to
1972 // validate the basic, low-level compatibility of the two types.
1973
1974 // As a minimum, require the sizes and alignments to match.
1975 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1976 return false;
1977
1978 // Consider all the kinds of non-dependent canonical types:
1979 // - functions and arrays aren't possible as return and parameter types
1980
1981 // - vector types of equal size can be arbitrarily mixed
1982 if (isa<VectorType>(left)) return isa<VectorType>(right);
1983 if (isa<VectorType>(right)) return false;
1984
1985 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001986 // - structs, unions, and Objective-C objects must match more-or-less
1987 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001988 // - everything else should be a scalar
1989 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001990 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001991
John McCall1d9b3b22011-09-09 05:25:32 +00001992 // Make scalars agree in kind, except count bools as chars, and group
1993 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001994 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1995 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1996 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1997 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001998 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1999 leftSK = Type::STK_ObjCObjectPointer;
2000 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
2001 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00002002
2003 // Note that data member pointers and function member pointers don't
2004 // intermix because of the size differences.
2005
2006 return (leftSK == rightSK);
2007}
Chris Lattner4d391482007-12-12 07:09:47 +00002008
John McCall0f4c4c42011-06-16 01:15:19 +00002009static bool tryMatchRecordTypes(ASTContext &Context,
2010 Sema::MethodMatchStrategy strategy,
2011 const Type *lt, const Type *rt) {
2012 assert(lt && rt && lt != rt);
2013
2014 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
2015 RecordDecl *left = cast<RecordType>(lt)->getDecl();
2016 RecordDecl *right = cast<RecordType>(rt)->getDecl();
2017
2018 // Require union-hood to match.
2019 if (left->isUnion() != right->isUnion()) return false;
2020
2021 // Require an exact match if either is non-POD.
2022 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
2023 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
2024 return false;
2025
2026 // Require size and alignment to match.
2027 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
2028
2029 // Require fields to match.
2030 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
2031 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
2032 for (; li != le && ri != re; ++li, ++ri) {
2033 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
2034 return false;
2035 }
2036 return (li == le && ri == re);
2037}
2038
Chris Lattner4d391482007-12-12 07:09:47 +00002039/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
2040/// returns true, or false, accordingly.
2041/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00002042bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
2043 const ObjCMethodDecl *right,
2044 MethodMatchStrategy strategy) {
2045 if (!matchTypes(Context, strategy,
2046 left->getResultType(), right->getResultType()))
2047 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002048
Douglas Gregor7666b032013-02-07 19:13:24 +00002049 // If either is hidden, it is not considered to match.
2050 if (left->isHidden() || right->isHidden())
2051 return false;
2052
David Blaikie4e4d0842012-03-11 07:00:24 +00002053 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002054 (left->hasAttr<NSReturnsRetainedAttr>()
2055 != right->hasAttr<NSReturnsRetainedAttr>() ||
2056 left->hasAttr<NSConsumesSelfAttr>()
2057 != right->hasAttr<NSConsumesSelfAttr>()))
2058 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002059
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002060 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002061 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2062 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002063
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002064 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002065 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002066 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002067
2068 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2069 return false;
2070
David Blaikie4e4d0842012-03-11 07:00:24 +00002071 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002072 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2073 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002074 }
2075 return true;
2076}
2077
Douglas Gregorff310c72012-05-01 23:37:00 +00002078void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002079 // If the list is empty, make it a singleton list.
2080 if (List->Method == 0) {
2081 List->Method = Method;
2082 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002083 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002084 }
2085
2086 // We've seen a method with this name, see if we have already seen this type
2087 // signature.
2088 ObjCMethodList *Previous = List;
2089 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002090 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002091 continue;
2092
2093 ObjCMethodDecl *PrevObjCMethod = List->Method;
2094
2095 // Propagate the 'defined' bit.
2096 if (Method->isDefined())
2097 PrevObjCMethod->setDefined(true);
2098
2099 // If a method is deprecated, push it in the global pool.
2100 // This is used for better diagnostics.
2101 if (Method->isDeprecated()) {
2102 if (!PrevObjCMethod->isDeprecated())
2103 List->Method = Method;
2104 }
2105 // If new method is unavailable, push it into global pool
2106 // unless previous one is deprecated.
2107 if (Method->isUnavailable()) {
2108 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2109 List->Method = Method;
2110 }
2111
Douglas Gregorff310c72012-05-01 23:37:00 +00002112 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002113 }
2114
2115 // We have a new signature for an existing method - add it.
2116 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002117 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002118 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2119}
2120
Sebastian Redldb9d2142010-08-02 23:18:59 +00002121/// \brief Read the contents of the method pool for a given selector from
2122/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002123void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002124 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002125 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002126}
2127
Douglas Gregorff310c72012-05-01 23:37:00 +00002128void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002129 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002130 // Ignore methods of invalid containers.
2131 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002132 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002133
Douglas Gregor0d266d62012-01-25 00:59:09 +00002134 if (ExternalSource)
2135 ReadMethodPool(Method->getSelector());
2136
Sebastian Redldb9d2142010-08-02 23:18:59 +00002137 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002138 if (Pos == MethodPool.end())
2139 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2140 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002141
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002142 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002143
Sebastian Redldb9d2142010-08-02 23:18:59 +00002144 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002145 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002146}
2147
John McCallf85e1932011-06-15 23:02:42 +00002148/// Determines if this is an "acceptable" loose mismatch in the global
2149/// method pool. This exists mostly as a hack to get around certain
2150/// global mismatches which we can't afford to make warnings / errors.
2151/// Really, what we want is a way to take a method out of the global
2152/// method pool.
2153static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2154 ObjCMethodDecl *other) {
2155 if (!chosen->isInstanceMethod())
2156 return false;
2157
2158 Selector sel = chosen->getSelector();
2159 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2160 return false;
2161
2162 // Don't complain about mismatches for -length if the method we
2163 // chose has an integral result type.
2164 return (chosen->getResultType()->isIntegerType());
2165}
2166
Sebastian Redldb9d2142010-08-02 23:18:59 +00002167ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002168 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002169 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002170 if (ExternalSource)
2171 ReadMethodPool(Sel);
2172
Sebastian Redldb9d2142010-08-02 23:18:59 +00002173 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002174 if (Pos == MethodPool.end())
2175 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002176
Douglas Gregorf0e00042013-01-16 18:47:38 +00002177 // Gather the non-hidden methods.
Sebastian Redldb9d2142010-08-02 23:18:59 +00002178 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorf0e00042013-01-16 18:47:38 +00002179 llvm::SmallVector<ObjCMethodDecl *, 4> Methods;
2180 for (ObjCMethodList *M = &MethList; M; M = M->Next) {
2181 if (M->Method && !M->Method->isHidden()) {
2182 // If we're not supposed to warn about mismatches, we're done.
2183 if (!warn)
2184 return M->Method;
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Douglas Gregorf0e00042013-01-16 18:47:38 +00002186 Methods.push_back(M->Method);
Sebastian Redldb9d2142010-08-02 23:18:59 +00002187 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002188 }
Douglas Gregorf0e00042013-01-16 18:47:38 +00002189
2190 // If there aren't any visible methods, we're done.
2191 // FIXME: Recover if there are any known-but-hidden methods?
2192 if (Methods.empty())
2193 return 0;
2194
2195 if (Methods.size() == 1)
2196 return Methods[0];
2197
2198 // We found multiple methods, so we may have to complain.
2199 bool issueDiagnostic = false, issueError = false;
2200
2201 // We support a warning which complains about *any* difference in
2202 // method signature.
2203 bool strictSelectorMatch =
2204 (receiverIdOrClass && warn &&
2205 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2206 R.getBegin())
2207 != DiagnosticsEngine::Ignored));
2208 if (strictSelectorMatch) {
2209 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2210 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_strict)) {
2211 issueDiagnostic = true;
2212 break;
2213 }
2214 }
2215 }
2216
2217 // If we didn't see any strict differences, we won't see any loose
2218 // differences. In ARC, however, we also need to check for loose
2219 // mismatches, because most of them are errors.
2220 if (!strictSelectorMatch ||
2221 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
2222 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2223 // This checks if the methods differ in type mismatch.
2224 if (!MatchTwoMethodDeclarations(Methods[0], Methods[I], MMS_loose) &&
2225 !isAcceptableMethodMismatch(Methods[0], Methods[I])) {
2226 issueDiagnostic = true;
2227 if (getLangOpts().ObjCAutoRefCount)
2228 issueError = true;
2229 break;
2230 }
2231 }
2232
2233 if (issueDiagnostic) {
2234 if (issueError)
2235 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2236 else if (strictSelectorMatch)
2237 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2238 else
2239 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
2240
2241 Diag(Methods[0]->getLocStart(),
2242 issueError ? diag::note_possibility : diag::note_using)
2243 << Methods[0]->getSourceRange();
2244 for (unsigned I = 1, N = Methods.size(); I != N; ++I) {
2245 Diag(Methods[I]->getLocStart(), diag::note_also_found)
2246 << Methods[I]->getSourceRange();
2247 }
2248 }
2249 return Methods[0];
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002250}
2251
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002252ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002253 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2254 if (Pos == MethodPool.end())
2255 return 0;
2256
2257 GlobalMethods &Methods = Pos->second;
2258
2259 if (Methods.first.Method && Methods.first.Method->isDefined())
2260 return Methods.first.Method;
2261 if (Methods.second.Method && Methods.second.Method->isDefined())
2262 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002263 return 0;
2264}
2265
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002266/// DiagnoseDuplicateIvars -
2267/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002268/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002269/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002270/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002271void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2272 ObjCInterfaceDecl *SID) {
2273 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2274 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002275 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002276 if (Ivar->isInvalidDecl())
2277 continue;
2278 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2279 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2280 if (prevIvar) {
2281 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2282 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2283 Ivar->setInvalidDecl();
2284 }
2285 }
2286 }
2287}
2288
Erik Verbruggend64251f2011-12-06 09:25:23 +00002289Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2290 switch (CurContext->getDeclKind()) {
2291 case Decl::ObjCInterface:
2292 return Sema::OCK_Interface;
2293 case Decl::ObjCProtocol:
2294 return Sema::OCK_Protocol;
2295 case Decl::ObjCCategory:
2296 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2297 return Sema::OCK_ClassExtension;
2298 else
2299 return Sema::OCK_Category;
2300 case Decl::ObjCImplementation:
2301 return Sema::OCK_Implementation;
2302 case Decl::ObjCCategoryImpl:
2303 return Sema::OCK_CategoryImplementation;
2304
2305 default:
2306 return Sema::OCK_None;
2307 }
2308}
2309
Steve Naroffa56f6162007-12-18 01:30:32 +00002310// Note: For class/category implemenations, allMethods/allProperties is
2311// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002312Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2313 Decl **allMethods, unsigned allNum,
2314 Decl **allProperties, unsigned pNum,
2315 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002316
Erik Verbruggend64251f2011-12-06 09:25:23 +00002317 if (getObjCContainerKind() == Sema::OCK_None)
2318 return 0;
2319
2320 assert(AtEnd.isValid() && "Invalid location for '@end'");
2321
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002322 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2323 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002324
Mike Stump1eb44332009-09-09 15:08:12 +00002325 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002326 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2327 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002328 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002329
Steve Naroff0701bbb2009-01-08 17:28:14 +00002330 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2331 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2332 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2333
Chris Lattner4d391482007-12-12 07:09:47 +00002334 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002335 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002336 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002337
2338 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002339 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002340 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002341 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002342 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002343 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002344 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002345 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002346 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002347 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002348 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002349 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002350 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002351 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002352 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002353 if (!Context.getSourceManager().isInSystemHeader(
2354 Method->getLocation()))
2355 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2356 << Method->getDeclName();
2357 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2358 }
Chris Lattner4d391482007-12-12 07:09:47 +00002359 InsMap[Method->getSelector()] = Method;
2360 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002361 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002362 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002363 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002364 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002365 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002366 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002367 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002368 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002369 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002370 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002371 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002372 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002373 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002374 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002375 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002376 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002377 if (!Context.getSourceManager().isInSystemHeader(
2378 Method->getLocation()))
2379 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2380 << Method->getDeclName();
2381 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2382 }
Chris Lattner4d391482007-12-12 07:09:47 +00002383 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002384 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002385 }
2386 }
2387 }
Douglas Gregorb892d702013-01-21 19:42:21 +00002388 if (isa<ObjCInterfaceDecl>(ClassDecl)) {
2389 // Nothing to do here.
Steve Naroff09c47192009-01-09 15:36:25 +00002390 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002391 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002392 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002393 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002394
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002395 if (C->IsClassExtension()) {
2396 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2397 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002398 }
Chris Lattner4d391482007-12-12 07:09:47 +00002399 }
Steve Naroff09c47192009-01-09 15:36:25 +00002400 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002401 if (CDecl->getIdentifier())
2402 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2403 // user-defined setter/getter. It also synthesizes setter/getter methods
2404 // and adds them to the DeclContext and global method pools.
2405 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2406 E = CDecl->prop_end();
2407 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002408 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002409 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002410 }
2411 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002412 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002413 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002414 // Any property declared in a class extension might have user
2415 // declared setter or getter in current class extension or one
2416 // of the other class extensions. Mark them as synthesized as
2417 // property will be synthesized when property with same name is
2418 // seen in the @implementation.
Douglas Gregord3297242013-01-16 23:00:23 +00002419 for (ObjCInterfaceDecl::visible_extensions_iterator
2420 Ext = IDecl->visible_extensions_begin(),
2421 ExtEnd = IDecl->visible_extensions_end();
2422 Ext != ExtEnd; ++Ext) {
2423 for (ObjCContainerDecl::prop_iterator I = Ext->prop_begin(),
2424 E = Ext->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002425 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002426 // Skip over properties declared @dynamic
2427 if (const ObjCPropertyImplDecl *PIDecl
2428 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2429 if (PIDecl->getPropertyImplementation()
2430 == ObjCPropertyImplDecl::Dynamic)
2431 continue;
Douglas Gregord3297242013-01-16 23:00:23 +00002432
2433 for (ObjCInterfaceDecl::visible_extensions_iterator
2434 Ext = IDecl->visible_extensions_begin(),
2435 ExtEnd = IDecl->visible_extensions_end();
2436 Ext != ExtEnd; ++Ext) {
2437 if (ObjCMethodDecl *GetterMethod
2438 = Ext->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002439 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002440 if (!Property->isReadOnly())
Douglas Gregord3297242013-01-16 23:00:23 +00002441 if (ObjCMethodDecl *SetterMethod
2442 = Ext->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002443 SetterMethod->setPropertyAccessor(true);
Douglas Gregord3297242013-01-16 23:00:23 +00002444 }
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002445 }
2446 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002447 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002448 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002449 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002450
Patrick Beardb2f68202012-04-06 18:12:22 +00002451 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2452 if (IDecl->getSuperClass() == NULL) {
2453 // This class has no superclass, so check that it has been marked with
2454 // __attribute((objc_root_class)).
2455 if (!HasRootClassAttr) {
2456 SourceLocation DeclLoc(IDecl->getLocation());
2457 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2458 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2459 << IDecl->getIdentifier();
2460 // See if NSObject is in the current scope, and if it is, suggest
2461 // adding " : NSObject " to the class declaration.
2462 NamedDecl *IF = LookupSingleName(TUScope,
2463 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2464 DeclLoc, LookupOrdinaryName);
2465 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2466 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2467 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2468 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2469 } else {
2470 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2471 }
2472 }
2473 } else if (HasRootClassAttr) {
2474 // Complain that only root classes may have this attribute.
2475 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2476 }
2477
John McCall260611a2012-06-20 06:18:46 +00002478 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002479 while (IDecl->getSuperClass()) {
2480 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2481 IDecl = IDecl->getSuperClass();
2482 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002483 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002484 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002485 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002486 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002487 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002488 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002489
Chris Lattner4d391482007-12-12 07:09:47 +00002490 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002491 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002492 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Douglas Gregord3297242013-01-16 23:00:23 +00002493 if (ObjCCategoryDecl *Cat
2494 = IDecl->FindCategoryDeclaration(CatImplClass->getIdentifier())) {
2495 ImplMethodsVsClassMethods(S, CatImplClass, Cat);
Chris Lattner4d391482007-12-12 07:09:47 +00002496 }
2497 }
2498 }
Chris Lattner682bf922009-03-29 16:50:03 +00002499 if (isInterfaceDeclKind) {
2500 // Reject invalid vardecls.
2501 for (unsigned i = 0; i != tuvNum; i++) {
2502 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2503 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2504 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002505 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002506 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002507 }
Chris Lattner682bf922009-03-29 16:50:03 +00002508 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002509 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002510 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002511
2512 for (unsigned i = 0; i != tuvNum; i++) {
2513 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002514 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2515 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002516 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2517 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002518
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002519 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002520 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002521}
2522
2523
2524/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2525/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002526static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002527CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002528 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002529}
2530
Ted Kremenek422bae72010-04-18 04:59:38 +00002531static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002532unsigned countAlignAttr(const AttrVec &A) {
2533 unsigned count=0;
2534 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2535 if ((*i)->getKind() == attr::Aligned)
2536 ++count;
2537 return count;
2538}
2539
2540static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002541bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2542 const AttrVec &A) {
2543 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002544 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002545 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002546 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002547
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002548 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002549 // But implementation has attributes. This is invalid.
2550 // Except when implementation has 'Align' attribute which is
2551 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002552 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002553 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002554
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002555 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002556
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002557 unsigned countAlignOnImpl = countAlignAttr(A);
2558 if (!countAlignOnImpl && (A.size() != D.size()))
2559 return true;
2560 else if (countAlignOnImpl) {
2561 unsigned countAlignOnDecl = countAlignAttr(D);
2562 if (countAlignOnDecl && (A.size() != D.size()))
2563 return true;
2564 else if (!countAlignOnDecl &&
2565 ((A.size()-countAlignOnImpl) != D.size()))
2566 return true;
2567 }
2568
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002569 // attributes on method declaration and definition must match exactly.
2570 // Note that we have at most a couple of attributes on methods, so this
2571 // n*n search is good enough.
2572 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002573 if ((*i)->getKind() == attr::Aligned)
2574 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002575 bool match = false;
2576 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2577 if ((*i)->getKind() == (*i1)->getKind()) {
2578 match = true;
2579 break;
2580 }
2581 }
2582 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002583 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002584 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002585
Sean Huntcf807c42010-08-18 23:23:40 +00002586 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002587}
2588
Douglas Gregor926df6c2011-06-11 01:09:30 +00002589/// \brief Check whether the declared result type of the given Objective-C
2590/// method declaration is compatible with the method's class.
2591///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002592static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002593CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2594 ObjCInterfaceDecl *CurrentClass) {
2595 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002596
2597 // If an Objective-C method inherits its related result type, then its
2598 // declared result type must be compatible with its own class type. The
2599 // declared result type is compatible if:
2600 if (const ObjCObjectPointerType *ResultObjectType
2601 = ResultType->getAs<ObjCObjectPointerType>()) {
2602 // - it is id or qualified id, or
2603 if (ResultObjectType->isObjCIdType() ||
2604 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002605 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002606
2607 if (CurrentClass) {
2608 if (ObjCInterfaceDecl *ResultClass
2609 = ResultObjectType->getInterfaceDecl()) {
2610 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002611 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002612 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002613
2614 // - it is a superclass of the method's class type
2615 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002616 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002617 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002618 } else {
2619 // Any Objective-C pointer type might be acceptable for a protocol
2620 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002621 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002622 }
2623 }
2624
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002625 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002626}
2627
John McCall6c2c2502011-07-22 02:45:48 +00002628namespace {
2629/// A helper class for searching for methods which a particular method
2630/// overrides.
2631class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002632public:
John McCall6c2c2502011-07-22 02:45:48 +00002633 Sema &S;
2634 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002635 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002636 bool Recursive;
2637
2638public:
2639 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2640 Selector selector = method->getSelector();
2641
2642 // Bypass this search if we've never seen an instance/class method
2643 // with this selector before.
2644 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2645 if (it == S.MethodPool.end()) {
Axel Naumann0ec56b72012-10-18 19:05:02 +00002646 if (!S.getExternalSource()) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002647 S.ReadMethodPool(selector);
2648
2649 it = S.MethodPool.find(selector);
2650 if (it == S.MethodPool.end())
2651 return;
John McCall6c2c2502011-07-22 02:45:48 +00002652 }
2653 ObjCMethodList &list =
2654 method->isInstanceMethod() ? it->second.first : it->second.second;
2655 if (!list.Method) return;
2656
2657 ObjCContainerDecl *container
2658 = cast<ObjCContainerDecl>(method->getDeclContext());
2659
2660 // Prevent the search from reaching this container again. This is
2661 // important with categories, which override methods from the
2662 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002663 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2664 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002665 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2666 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002667 } else {
2668 searchFromContainer(container);
2669 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002670 }
John McCall6c2c2502011-07-22 02:45:48 +00002671
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002672 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002673 iterator begin() const { return Overridden.begin(); }
2674 iterator end() const { return Overridden.end(); }
2675
2676private:
2677 void searchFromContainer(ObjCContainerDecl *container) {
2678 if (container->isInvalidDecl()) return;
2679
2680 switch (container->getDeclKind()) {
2681#define OBJCCONTAINER(type, base) \
2682 case Decl::type: \
2683 searchFrom(cast<type##Decl>(container)); \
2684 break;
2685#define ABSTRACT_DECL(expansion)
2686#define DECL(type, base) \
2687 case Decl::type:
2688#include "clang/AST/DeclNodes.inc"
2689 llvm_unreachable("not an ObjC container!");
2690 }
2691 }
2692
2693 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002694 if (!protocol->hasDefinition())
2695 return;
2696
John McCall6c2c2502011-07-22 02:45:48 +00002697 // A method in a protocol declaration overrides declarations from
2698 // referenced ("parent") protocols.
2699 search(protocol->getReferencedProtocols());
2700 }
2701
2702 void searchFrom(ObjCCategoryDecl *category) {
2703 // A method in a category declaration overrides declarations from
2704 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002705 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002706 search(category->getReferencedProtocols());
2707 }
2708
2709 void searchFrom(ObjCCategoryImplDecl *impl) {
2710 // A method in a category definition that has a category
2711 // declaration overrides declarations from the category
2712 // declaration.
2713 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2714 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002715 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2716 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002717
2718 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002719 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2720 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002721 }
2722 }
2723
2724 void searchFrom(ObjCInterfaceDecl *iface) {
2725 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002726 if (!iface->hasDefinition())
2727 return;
2728
John McCall6c2c2502011-07-22 02:45:48 +00002729 // - categories,
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002730 for (ObjCInterfaceDecl::known_categories_iterator
2731 cat = iface->known_categories_begin(),
2732 catEnd = iface->known_categories_end();
Douglas Gregord3297242013-01-16 23:00:23 +00002733 cat != catEnd; ++cat) {
2734 search(*cat);
2735 }
John McCall6c2c2502011-07-22 02:45:48 +00002736
2737 // - the super class, and
2738 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2739 search(super);
2740
2741 // - any referenced protocols.
2742 search(iface->getReferencedProtocols());
2743 }
2744
2745 void searchFrom(ObjCImplementationDecl *impl) {
2746 // A method in a class implementation overrides declarations from
2747 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002748 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2749 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002750 }
2751
2752
2753 void search(const ObjCProtocolList &protocols) {
2754 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2755 i != e; ++i)
2756 search(*i);
2757 }
2758
2759 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002760 // Check for a method in this container which matches this selector.
2761 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
Argyrios Kyrtzidis04593d02013-03-29 21:51:48 +00002762 Method->isInstanceMethod(),
2763 /*AllowHidden=*/true);
John McCall6c2c2502011-07-22 02:45:48 +00002764
2765 // If we find one, record it and bail out.
2766 if (meth) {
2767 Overridden.insert(meth);
2768 return;
2769 }
2770
2771 // Otherwise, search for methods that a hypothetical method here
2772 // would have overridden.
2773
2774 // Note that we're now in a recursive case.
2775 Recursive = true;
2776
2777 searchFromContainer(container);
2778 }
2779};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002780}
2781
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002782void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2783 ObjCInterfaceDecl *CurrentClass,
2784 ResultTypeCompatibilityKind RTC) {
2785 // Search for overridden methods and merge information down from them.
2786 OverrideSearch overrides(*this, ObjCMethod);
2787 // Keep track if the method overrides any method in the class's base classes,
2788 // its protocols, or its categories' protocols; we will keep that info
2789 // in the ObjCMethodDecl.
2790 // For this info, a method in an implementation is not considered as
2791 // overriding the same method in the interface or its categories.
2792 bool hasOverriddenMethodsInBaseOrProtocol = false;
2793 for (OverrideSearch::iterator
2794 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2795 ObjCMethodDecl *overridden = *i;
2796
2797 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2798 CurrentClass != overridden->getClassInterface() ||
2799 overridden->isOverriding())
2800 hasOverriddenMethodsInBaseOrProtocol = true;
2801
2802 // Propagate down the 'related result type' bit from overridden methods.
2803 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2804 ObjCMethod->SetRelatedResultType();
2805
2806 // Then merge the declarations.
2807 mergeObjCMethodDecls(ObjCMethod, overridden);
2808
2809 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2810 continue; // Conflicting properties are detected elsewhere.
2811
2812 // Check for overriding methods
2813 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2814 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2815 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2816 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2817
2818 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00002819 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
2820 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002821 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2822 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002823 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2824 PrevE = overridden->param_end();
2825 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002826 assert(PrevI != overridden->param_end() && "Param mismatch");
2827 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2828 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2829 // If type of argument of method in this class does not match its
2830 // respective argument type in the super class method, issue warning;
2831 if (!Context.typesAreCompatible(T1, T2)) {
2832 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2833 << T1 << T2;
2834 Diag(overridden->getLocation(), diag::note_previous_declaration);
2835 break;
2836 }
2837 }
2838 }
2839 }
2840
2841 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2842}
2843
John McCalld226f652010-08-21 09:40:31 +00002844Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002845 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002846 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002847 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002848 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002849 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002850 Selector Sel,
2851 // optional arguments. The number of types/arguments is obtained
2852 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002853 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002854 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002855 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002856 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002857 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002858 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002859 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002860 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002861 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002862 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2863 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002864 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002865
Douglas Gregore97179c2011-09-08 01:46:34 +00002866 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002867 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002868 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002869 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002870
Steve Naroffccef3712009-02-20 22:59:16 +00002871 // Methods cannot return interface types. All ObjC objects are
2872 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002873 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002874 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2875 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002876 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002877 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002878
2879 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002880 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002881 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002882 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002883 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002884 }
Mike Stump1eb44332009-09-09 15:08:12 +00002885
2886 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002887 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002888 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002889 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002890 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002891 MethodType == tok::minus, isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +00002892 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002893 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002894 MethodDeclKind == tok::objc_optional
2895 ? ObjCMethodDecl::Optional
2896 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002897 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002898
Chris Lattner5f9e2722011-07-23 10:55:15 +00002899 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002900
Chris Lattner7db638d2009-04-11 19:42:43 +00002901 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002902 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002903 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002904
Chris Lattnere294d3f2009-04-11 18:57:04 +00002905 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002906 ArgType = Context.getObjCIdType();
2907 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002908 } else {
John McCall58e46772009-10-23 21:48:59 +00002909 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002910 }
Mike Stump1eb44332009-09-09 15:08:12 +00002911
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002912 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2913 LookupOrdinaryName, ForRedeclaration);
2914 LookupName(R, S);
2915 if (R.isSingleResult()) {
2916 NamedDecl *PrevDecl = R.getFoundDecl();
2917 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002918 Diag(ArgInfo[i].NameLoc,
2919 (MethodDefinition ? diag::warn_method_param_redefinition
2920 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002921 << ArgInfo[i].Name;
2922 Diag(PrevDecl->getLocation(),
2923 diag::note_previous_declaration);
2924 }
2925 }
2926
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002927 SourceLocation StartLoc = DI
2928 ? DI->getTypeLoc().getBeginLoc()
2929 : ArgInfo[i].NameLoc;
2930
John McCall81ef3e62011-04-23 02:46:06 +00002931 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2932 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2933 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002934
John McCall70798862011-05-02 00:30:12 +00002935 Param->setObjCMethodScopeInfo(i);
2936
Chris Lattner0ed844b2008-04-04 06:12:32 +00002937 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002938 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002939
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002940 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002941 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002942
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002943 if (Param->hasAttr<BlocksAttr>()) {
2944 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2945 Param->setInvalidDecl();
2946 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002947 S->AddDecl(Param);
2948 IdResolver.AddDecl(Param);
2949
Chris Lattner0ed844b2008-04-04 06:12:32 +00002950 Params.push_back(Param);
2951 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002952
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002953 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002954 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002955 QualType ArgType = Param->getType();
2956 if (ArgType.isNull())
2957 ArgType = Context.getObjCIdType();
2958 else
2959 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002960 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002961 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002962 Diag(Param->getLocation(),
2963 diag::err_object_cannot_be_passed_returned_by_value)
2964 << 1 << ArgType;
2965 Param->setInvalidDecl();
2966 }
2967 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002968
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002969 Params.push_back(Param);
2970 }
2971
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002972 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002973 ObjCMethod->setObjCDeclQualifier(
2974 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002975
2976 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002977 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002978
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002979 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002980 const ObjCMethodDecl *PrevMethod = 0;
2981 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002982 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002983 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2984 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002985 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002986 PrevMethod = ImpDecl->getClassMethod(Sel);
2987 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002988 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002989
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002990 ObjCMethodDecl *IMD = 0;
2991 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2992 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2993 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002994 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002995 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002996 SourceLocation MethodLoc = IMD->getLocation();
2997 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2998 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002999 Diag(MethodLoc, diag::note_method_declared_at)
3000 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00003001 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00003002 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00003003 } else {
3004 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00003005 }
John McCall6c2c2502011-07-22 02:45:48 +00003006
Chris Lattner4d391482007-12-12 07:09:47 +00003007 if (PrevMethod) {
3008 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00003009 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00003010 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00003011 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00003012 }
John McCall54abf7d2009-11-04 02:18:39 +00003013
Douglas Gregor926df6c2011-06-11 01:09:30 +00003014 // If this Objective-C method does not have a related result type, but we
3015 // are allowed to infer related result types, try to do so based on the
3016 // method family.
3017 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
3018 if (!CurrentClass) {
3019 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
3020 CurrentClass = Cat->getClassInterface();
3021 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
3022 CurrentClass = Impl->getClassInterface();
3023 else if (ObjCCategoryImplDecl *CatImpl
3024 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
3025 CurrentClass = CatImpl->getClassInterface();
3026 }
John McCall6c2c2502011-07-22 02:45:48 +00003027
Douglas Gregore97179c2011-09-08 01:46:34 +00003028 ResultTypeCompatibilityKind RTC
3029 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00003030
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003031 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00003032
John McCallf85e1932011-06-15 23:02:42 +00003033 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00003034 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00003035 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
3036
Douglas Gregore97179c2011-09-08 01:46:34 +00003037 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00003038 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00003039 !ObjCMethod->hasRelatedResultType() &&
3040 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00003041 bool InferRelatedResultType = false;
3042 switch (ObjCMethod->getMethodFamily()) {
3043 case OMF_None:
3044 case OMF_copy:
3045 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00003046 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003047 case OMF_mutableCopy:
3048 case OMF_release:
3049 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00003050 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00003051 break;
3052
3053 case OMF_alloc:
3054 case OMF_new:
3055 InferRelatedResultType = ObjCMethod->isClassMethod();
3056 break;
3057
3058 case OMF_init:
3059 case OMF_autorelease:
3060 case OMF_retain:
3061 case OMF_self:
3062 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3063 break;
3064 }
3065
John McCall6c2c2502011-07-22 02:45:48 +00003066 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003067 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003068 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003069
3070 ActOnDocumentableDecl(ObjCMethod);
3071
John McCalld226f652010-08-21 09:40:31 +00003072 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003073}
3074
Chris Lattnercc98eac2008-12-17 07:13:27 +00003075bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003076 // Following is also an error. But it is caused by a missing @end
3077 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003078 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003079 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003080
3081 // If we switched context to translation unit while we are still lexically in
3082 // an objc container, it means the parser missed emitting an error.
3083 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3084 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003085
Anders Carlsson15281452008-11-04 16:57:32 +00003086 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3087 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003088
Anders Carlsson15281452008-11-04 16:57:32 +00003089 return true;
3090}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003091
James Dennett1dfbd922012-06-14 21:40:34 +00003092/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003093/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003094void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003095 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003096 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003097 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003098 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003099 if (!Class) {
3100 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3101 return;
3102 }
John McCall260611a2012-06-20 06:18:46 +00003103 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003104 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3105 return;
3106 }
Mike Stump1eb44332009-09-09 15:08:12 +00003107
Chris Lattnercc98eac2008-12-17 07:13:27 +00003108 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003109 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003110 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003111 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003112 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003113 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003114 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003115 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3116 /*FIXME: StartL=*/ID->getLocation(),
3117 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003118 ID->getIdentifier(), ID->getType(),
3119 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003120 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003121 }
Mike Stump1eb44332009-09-09 15:08:12 +00003122
Chris Lattnercc98eac2008-12-17 07:13:27 +00003123 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003124 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003125 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003126 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003127 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003128 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003129 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003130 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003131 }
3132}
3133
Douglas Gregor160b5632010-04-26 17:32:49 +00003134/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003135VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3136 SourceLocation StartLoc,
3137 SourceLocation IdLoc,
3138 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003139 bool Invalid) {
3140 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3141 // duration shall not be qualified by an address-space qualifier."
3142 // Since all parameters have automatic store duration, they can not have
3143 // an address space.
3144 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003145 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003146 Invalid = true;
3147 }
3148
3149 // An @catch parameter must be an unqualified object pointer type;
3150 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3151 if (Invalid) {
3152 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003153 } else if (T->isDependentType()) {
3154 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003155 } else if (!T->isObjCObjectPointerType()) {
3156 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003157 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003158 } else if (T->isObjCQualifiedIdType()) {
3159 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003160 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003161 }
3162
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003163 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3164 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003165 New->setExceptionVariable(true);
3166
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003167 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003168 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003169 Invalid = true;
3170
Douglas Gregor160b5632010-04-26 17:32:49 +00003171 if (Invalid)
3172 New->setInvalidDecl();
3173 return New;
3174}
3175
John McCalld226f652010-08-21 09:40:31 +00003176Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003177 const DeclSpec &DS = D.getDeclSpec();
3178
3179 // We allow the "register" storage class on exception variables because
3180 // GCC did, but we drop it completely. Any other storage class is an error.
3181 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3182 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3183 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3184 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3185 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3186 << DS.getStorageClassSpec();
3187 }
3188 if (D.getDeclSpec().isThreadSpecified())
3189 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3190 D.getMutableDeclSpec().ClearStorageClassSpecs();
3191
Richard Smithc7f81162013-03-18 22:52:47 +00003192 DiagnoseFunctionSpecifiers(D.getDeclSpec());
Douglas Gregor160b5632010-04-26 17:32:49 +00003193
3194 // Check that there are no default arguments inside the type of this
3195 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003196 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003197 CheckExtraCXXDefaultArguments(D);
3198
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003199 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003200 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003201
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003202 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3203 D.getSourceRange().getBegin(),
3204 D.getIdentifierLoc(),
3205 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003206 D.isInvalidType());
3207
3208 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3209 if (D.getCXXScopeSpec().isSet()) {
3210 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3211 << D.getCXXScopeSpec().getRange();
3212 New->setInvalidDecl();
3213 }
3214
3215 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003216 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003217 if (D.getIdentifier())
3218 IdResolver.AddDecl(New);
3219
3220 ProcessDeclAttributes(S, New, D);
3221
3222 if (New->hasAttr<BlocksAttr>())
3223 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003224 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003225}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003226
3227/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003228/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003229void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003230 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003231 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3232 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003233 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003234 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003235 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003236 }
3237}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003238
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003239void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003240 // Load referenced selectors from the external source.
3241 if (ExternalSource) {
3242 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3243 ExternalSource->ReadReferencedSelectors(Sels);
3244 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3245 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3246 }
3247
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003248 // Warning will be issued only when selector table is
3249 // generated (which means there is at lease one implementation
3250 // in the TU). This is to match gcc's behavior.
3251 if (ReferencedSelectors.empty() ||
3252 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003253 return;
3254 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3255 ReferencedSelectors.begin(),
3256 E = ReferencedSelectors.end(); S != E; ++S) {
3257 Selector Sel = (*S).first;
3258 if (!LookupImplementedMethodInGlobalPool(Sel))
3259 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3260 }
3261 return;
3262}