blob: 38c05ff780da85a5c8efb64bc16b3119e35a6a6d [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000017#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000019#include "clang/AST/ASTConsumer.h"
Steve Naroffca331292009-03-03 14:49:36 +000020#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/AST/ExprObjC.h"
Chris Lattner4d391482007-12-12 07:09:47 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +000024#include "clang/AST/ASTMutationListener.h"
John McCallf85e1932011-06-15 23:02:42 +000025#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
Patrick Beardb2f68202012-04-06 18:12:22 +000027#include "clang/Lex/Preprocessor.h"
John McCall50df6ae2010-08-25 07:03:20 +000028#include "llvm/ADT/DenseSet.h"
29
Chris Lattner4d391482007-12-12 07:09:47 +000030using namespace clang;
31
John McCallf85e1932011-06-15 23:02:42 +000032/// Check whether the given method, which must be in the 'init'
33/// family, is a valid member of that family.
34///
35/// \param receiverTypeIfCall - if null, check this as if declaring it;
36/// if non-null, check this as if making a call to it with the given
37/// receiver type
38///
39/// \return true to indicate that there was an error and appropriate
40/// actions were taken
41bool Sema::checkInitMethod(ObjCMethodDecl *method,
42 QualType receiverTypeIfCall) {
43 if (method->isInvalidDecl()) return true;
44
45 // This castAs is safe: methods that don't return an object
46 // pointer won't be inferred as inits and will reject an explicit
47 // objc_method_family(init).
48
49 // We ignore protocols here. Should we? What about Class?
50
51 const ObjCObjectType *result = method->getResultType()
52 ->castAs<ObjCObjectPointerType>()->getObjectType();
53
54 if (result->isObjCId()) {
55 return false;
56 } else if (result->isObjCClass()) {
57 // fall through: always an error
58 } else {
59 ObjCInterfaceDecl *resultClass = result->getInterface();
60 assert(resultClass && "unexpected object type!");
61
62 // It's okay for the result type to still be a forward declaration
63 // if we're checking an interface declaration.
Douglas Gregor7723fec2011-12-15 20:29:51 +000064 if (!resultClass->hasDefinition()) {
John McCallf85e1932011-06-15 23:02:42 +000065 if (receiverTypeIfCall.isNull() &&
66 !isa<ObjCImplementationDecl>(method->getDeclContext()))
67 return false;
68
69 // Otherwise, we try to compare class types.
70 } else {
71 // If this method was declared in a protocol, we can't check
72 // anything unless we have a receiver type that's an interface.
73 const ObjCInterfaceDecl *receiverClass = 0;
74 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
75 if (receiverTypeIfCall.isNull())
76 return false;
77
78 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
79 ->getInterfaceDecl();
80
81 // This can be null for calls to e.g. id<Foo>.
82 if (!receiverClass) return false;
83 } else {
84 receiverClass = method->getClassInterface();
85 assert(receiverClass && "method not associated with a class!");
86 }
87
88 // If either class is a subclass of the other, it's fine.
89 if (receiverClass->isSuperClassOf(resultClass) ||
90 resultClass->isSuperClassOf(receiverClass))
91 return false;
92 }
93 }
94
95 SourceLocation loc = method->getLocation();
96
97 // If we're in a system header, and this is not a call, just make
98 // the method unusable.
99 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
100 method->addAttr(new (Context) UnavailableAttr(loc, Context,
101 "init method returns a type unrelated to its receiver type"));
102 return true;
103 }
104
105 // Otherwise, it's an error.
106 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
107 method->setInvalidDecl();
108 return true;
109}
110
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000111void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000112 const ObjCMethodDecl *Overridden,
113 bool IsImplementation) {
114 if (Overridden->hasRelatedResultType() &&
115 !NewMethod->hasRelatedResultType()) {
116 // This can only happen when the method follows a naming convention that
117 // implies a related result type, and the original (overridden) method has
118 // a suitable return type, but the new (overriding) method does not have
119 // a suitable return type.
120 QualType ResultType = NewMethod->getResultType();
121 SourceRange ResultTypeRange;
122 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000123 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000124 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
125
126 // Figure out which class this method is part of, if any.
127 ObjCInterfaceDecl *CurrentClass
128 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
129 if (!CurrentClass) {
130 DeclContext *DC = NewMethod->getDeclContext();
131 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
132 CurrentClass = Cat->getClassInterface();
133 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
134 CurrentClass = Impl->getClassInterface();
135 else if (ObjCCategoryImplDecl *CatImpl
136 = dyn_cast<ObjCCategoryImplDecl>(DC))
137 CurrentClass = CatImpl->getClassInterface();
138 }
139
140 if (CurrentClass) {
141 Diag(NewMethod->getLocation(),
142 diag::warn_related_result_type_compatibility_class)
143 << Context.getObjCInterfaceType(CurrentClass)
144 << ResultType
145 << ResultTypeRange;
146 } else {
147 Diag(NewMethod->getLocation(),
148 diag::warn_related_result_type_compatibility_protocol)
149 << ResultType
150 << ResultTypeRange;
151 }
152
Douglas Gregore97179c2011-09-08 01:46:34 +0000153 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
154 Diag(Overridden->getLocation(),
155 diag::note_related_result_type_overridden_family)
156 << Family;
157 else
158 Diag(Overridden->getLocation(),
159 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000160 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000161 if (getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000162 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
163 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
164 Diag(NewMethod->getLocation(),
165 diag::err_nsreturns_retained_attribute_mismatch) << 1;
166 Diag(Overridden->getLocation(), diag::note_previous_decl)
167 << "method";
168 }
169 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
170 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
171 Diag(NewMethod->getLocation(),
172 diag::err_nsreturns_retained_attribute_mismatch) << 0;
173 Diag(Overridden->getLocation(), diag::note_previous_decl)
174 << "method";
175 }
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000176 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
177 oe = Overridden->param_end();
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000178 for (ObjCMethodDecl::param_iterator
179 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000180 ni != ne && oi != oe; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000181 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000182 ParmVarDecl *newDecl = (*ni);
183 if (newDecl->hasAttr<NSConsumedAttr>() !=
184 oldDecl->hasAttr<NSConsumedAttr>()) {
185 Diag(newDecl->getLocation(),
186 diag::err_nsconsumed_attribute_mismatch);
187 Diag(oldDecl->getLocation(), diag::note_previous_decl)
188 << "parameter";
189 }
190 }
191 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000192}
193
John McCallf85e1932011-06-15 23:02:42 +0000194/// \brief Check a method declaration for compatibility with the Objective-C
195/// ARC conventions.
196static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
197 ObjCMethodFamily family = method->getMethodFamily();
198 switch (family) {
199 case OMF_None:
Nico Weber80cb6e62011-08-28 22:35:17 +0000200 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000201 case OMF_retain:
202 case OMF_release:
203 case OMF_autorelease:
204 case OMF_retainCount:
205 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000206 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000207 return false;
208
Fariborz Jahanian1b0a13e2012-07-30 20:52:48 +0000209 case OMF_dealloc:
210 if (!S.Context.hasSameType(method->getResultType(), S.Context.VoidTy)) {
211 SourceRange ResultTypeRange;
212 if (const TypeSourceInfo *ResultTypeInfo
213 = method->getResultTypeSourceInfo())
214 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
215 if (ResultTypeRange.isInvalid())
216 S.Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
217 << method->getResultType()
218 << FixItHint::CreateInsertion(method->getSelectorLoc(0), "(void)");
219 else
220 S.Diag(method->getLocation(), diag::error_dealloc_bad_result_type)
221 << method->getResultType()
222 << FixItHint::CreateReplacement(ResultTypeRange, "void");
223 return true;
224 }
225 return false;
226
John McCallf85e1932011-06-15 23:02:42 +0000227 case OMF_init:
228 // If the method doesn't obey the init rules, don't bother annotating it.
229 if (S.checkInitMethod(method, QualType()))
230 return true;
231
232 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
233 S.Context));
234
235 // Don't add a second copy of this attribute, but otherwise don't
236 // let it be suppressed.
237 if (method->hasAttr<NSReturnsRetainedAttr>())
238 return false;
239 break;
240
241 case OMF_alloc:
242 case OMF_copy:
243 case OMF_mutableCopy:
244 case OMF_new:
245 if (method->hasAttr<NSReturnsRetainedAttr>() ||
246 method->hasAttr<NSReturnsNotRetainedAttr>() ||
247 method->hasAttr<NSReturnsAutoreleasedAttr>())
248 return false;
249 break;
250 }
251
252 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
253 S.Context));
254 return false;
255}
256
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000257static void DiagnoseObjCImplementedDeprecations(Sema &S,
258 NamedDecl *ND,
259 SourceLocation ImplLoc,
260 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000261 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000262 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000263 if (select == 0)
Ted Kremenek3306ec12012-02-27 22:55:11 +0000264 S.Diag(ND->getLocation(), diag::note_method_declared_at)
265 << ND->getDeclName();
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000266 else
267 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
268 }
269}
270
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000271/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
272/// pool.
273void Sema::AddAnyMethodToGlobalPool(Decl *D) {
274 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
275
276 // If we don't have a valid method decl, simply return.
277 if (!MDecl)
278 return;
279 if (MDecl->isInstanceMethod())
280 AddInstanceMethodToGlobalPool(MDecl, true);
281 else
282 AddFactoryMethodToGlobalPool(MDecl, true);
283}
284
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000285/// HasExplicitOwnershipAttr - returns true when pointer to ObjC pointer
286/// has explicit ownership attribute; false otherwise.
287static bool
288HasExplicitOwnershipAttr(Sema &S, ParmVarDecl *Param) {
289 QualType T = Param->getType();
290
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000291 if (const PointerType *PT = T->getAs<PointerType>()) {
292 T = PT->getPointeeType();
293 } else if (const ReferenceType *RT = T->getAs<ReferenceType>()) {
294 T = RT->getPointeeType();
295 } else {
296 return true;
297 }
298
299 // If we have a lifetime qualifier, but it's local, we must have
300 // inferred it. So, it is implicit.
301 return !T.getLocalQualifiers().hasObjCLifetime();
302}
303
Fariborz Jahanian8c6cb462012-08-08 23:41:08 +0000304/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
305/// and user declared, in the method definition's AST.
306void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
307 assert((getCurMethodDecl() == 0) && "Methodparsing confused");
John McCalld226f652010-08-21 09:40:31 +0000308 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +0000309
Steve Naroff394f3f42008-07-25 17:57:26 +0000310 // If we don't have a valid method decl, simply return.
311 if (!MDecl)
312 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000313
Chris Lattner4d391482007-12-12 07:09:47 +0000314 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000315 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000316 PushFunctionScope();
317
Chris Lattner4d391482007-12-12 07:09:47 +0000318 // Create Decl objects for each parameter, entrring them in the scope for
319 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000320
321 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000322 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000323
Daniel Dunbar451318c2008-08-26 06:07:48 +0000324 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
325 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000326
Chris Lattner8123a952008-04-10 02:22:51 +0000327 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000328 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000329 E = MDecl->param_end(); PI != E; ++PI) {
330 ParmVarDecl *Param = (*PI);
331 if (!Param->isInvalidDecl() &&
332 RequireCompleteType(Param->getLocation(), Param->getType(),
333 diag::err_typecheck_decl_incomplete_type))
334 Param->setInvalidDecl();
Fariborz Jahaniana1a32f72012-09-13 18:53:14 +0000335 if (!Param->isInvalidDecl() &&
336 getLangOpts().ObjCAutoRefCount &&
337 !HasExplicitOwnershipAttr(*this, Param))
338 Diag(Param->getLocation(), diag::warn_arc_strong_pointer_objc_pointer) <<
339 Param->getType();
Fariborz Jahanian918546c2012-08-30 23:56:02 +0000340
Chris Lattner89951a82009-02-20 18:43:26 +0000341 if ((*PI)->getIdentifier())
342 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000343 }
John McCallf85e1932011-06-15 23:02:42 +0000344
345 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000346 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000347 switch (MDecl->getMethodFamily()) {
348 case OMF_retain:
349 case OMF_retainCount:
350 case OMF_release:
351 case OMF_autorelease:
352 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
353 << MDecl->getSelector();
354 break;
355
356 case OMF_None:
357 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000358 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000359 case OMF_alloc:
360 case OMF_init:
361 case OMF_mutableCopy:
362 case OMF_copy:
363 case OMF_new:
364 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000365 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000366 break;
367 }
368 }
369
Nico Weber9a1ecf02011-08-22 17:25:57 +0000370 // Warn on deprecated methods under -Wdeprecated-implementations,
371 // and prepare for warning on missing super calls.
372 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian84101132012-09-07 23:46:23 +0000373 ObjCMethodDecl *IMD =
374 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod());
375
376 if (IMD)
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000377 DiagnoseObjCImplementedDeprecations(*this,
378 dyn_cast<NamedDecl>(IMD),
379 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000380
Nico Weber80cb6e62011-08-28 22:35:17 +0000381 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000382 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
383 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
384 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000385 if (IC->getSuperClass()) {
Eli Friedman95aac152012-08-01 21:02:59 +0000386 getCurFunction()->ObjCShouldCallSuperDealloc =
David Blaikie4e4d0842012-03-11 07:00:24 +0000387 !(Context.getLangOpts().ObjCAutoRefCount ||
388 Context.getLangOpts().getGC() == LangOptions::GCOnly) &&
Fariborz Jahanian84101132012-09-07 23:46:23 +0000389 MDecl->getMethodFamily() == OMF_dealloc;
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000390 if (!getCurFunction()->ObjCShouldCallSuperDealloc) {
391 IMD = IC->getSuperClass()->lookupMethod(MDecl->getSelector(),
392 MDecl->isInstanceMethod());
Fariborz Jahanian84101132012-09-07 23:46:23 +0000393 getCurFunction()->ObjCShouldCallSuperDealloc =
394 (IMD && IMD->hasAttr<ObjCRequiresSuperAttr>());
Fariborz Jahanian6f938602012-09-10 18:04:25 +0000395 }
Eli Friedman95aac152012-08-01 21:02:59 +0000396 getCurFunction()->ObjCShouldCallSuperFinalize =
David Blaikie4e4d0842012-03-11 07:00:24 +0000397 Context.getLangOpts().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000398 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000399 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000400 }
Chris Lattner4d391482007-12-12 07:09:47 +0000401}
402
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000403namespace {
404
405// Callback to only accept typo corrections that are Objective-C classes.
406// If an ObjCInterfaceDecl* is given to the constructor, then the validation
407// function will reject corrections to that class.
408class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
409 public:
410 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
411 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
412 : CurrentIDecl(IDecl) {}
413
414 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
415 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
416 return ID && !declaresSameEntity(ID, CurrentIDecl);
417 }
418
419 private:
420 ObjCInterfaceDecl *CurrentIDecl;
421};
422
423}
424
John McCalld226f652010-08-21 09:40:31 +0000425Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000426ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
427 IdentifierInfo *ClassName, SourceLocation ClassLoc,
428 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000429 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000430 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000431 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000432 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Chris Lattner4d391482007-12-12 07:09:47 +0000434 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000435 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000436 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000437
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000438 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000439 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000440 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000441 }
Mike Stump1eb44332009-09-09 15:08:12 +0000442
Douglas Gregor7723fec2011-12-15 20:29:51 +0000443 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000444 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000445 ObjCInterfaceDecl *IDecl
446 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000447 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000448
Douglas Gregor7723fec2011-12-15 20:29:51 +0000449 if (PrevIDecl) {
450 // Class already seen. Was it a definition?
451 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
452 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
453 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000454 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000455 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000456 }
Chris Lattner4d391482007-12-12 07:09:47 +0000457 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000458
459 if (AttrList)
460 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
461 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Douglas Gregor7723fec2011-12-15 20:29:51 +0000463 // Start the definition of this class. If we're in a redefinition case, there
464 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000465 if (!IDecl->hasDefinition())
466 IDecl->startDefinition();
467
Chris Lattner4d391482007-12-12 07:09:47 +0000468 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000469 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000470 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
471 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000472
473 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000474 // Try to correct for a typo in the superclass name without correcting
475 // to the class we're defining.
476 ObjCInterfaceValidatorCCC Validator(IDecl);
477 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000478 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000479 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000480 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
481 Diag(SuperLoc, diag::err_undef_superclass_suggest)
482 << SuperName << ClassName << PrevDecl->getDeclName();
483 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
484 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000485 }
486 }
487
Douglas Gregor60ef3082011-12-15 00:29:59 +0000488 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000489 Diag(SuperLoc, diag::err_recursive_superclass)
490 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000491 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000492 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000493 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000494 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000495
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000496 // Diagnose classes that inherit from deprecated classes.
497 if (SuperClassDecl)
498 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000500 if (PrevDecl && SuperClassDecl == 0) {
501 // The previous declaration was not a class decl. Check if we have a
502 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000503 if (const TypedefNameDecl *TDecl =
504 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000505 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000506 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000507 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
508 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000509 }
510 }
Mike Stump1eb44332009-09-09 15:08:12 +0000511
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000512 // This handles the following case:
513 //
514 // typedef int SuperClass;
515 // @interface MyClass : SuperClass {} @end
516 //
517 if (!SuperClassDecl) {
518 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
519 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000520 }
521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Richard Smith162e1c12011-04-15 14:24:37 +0000523 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000524 if (!SuperClassDecl)
525 Diag(SuperLoc, diag::err_undef_superclass)
526 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000527 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000528 Context.getObjCInterfaceType(SuperClassDecl),
529 diag::err_forward_superclass,
530 SuperClassDecl->getDeclName(),
531 ClassName,
532 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000533 SuperClassDecl = 0;
534 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000535 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000536 IDecl->setSuperClass(SuperClassDecl);
537 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000538 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000539 }
Chris Lattner4d391482007-12-12 07:09:47 +0000540 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000541 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000542 }
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Sebastian Redl0b17c612010-08-13 00:28:03 +0000544 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000545 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000546 IDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000547 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000548 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000549 }
Mike Stump1eb44332009-09-09 15:08:12 +0000550
Anders Carlsson15281452008-11-04 16:57:32 +0000551 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000552 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000553}
554
Richard Smithde01b7a2012-08-08 23:32:13 +0000555/// ActOnCompatibilityAlias - this action is called after complete parsing of
James Dennett1dfbd922012-06-14 21:40:34 +0000556/// a \@compatibility_alias declaration. It sets up the alias relationships.
Richard Smithde01b7a2012-08-08 23:32:13 +0000557Decl *Sema::ActOnCompatibilityAlias(SourceLocation AtLoc,
558 IdentifierInfo *AliasName,
559 SourceLocation AliasLocation,
560 IdentifierInfo *ClassName,
561 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000562 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000563 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000564 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000565 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000566 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000567 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000568 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000569 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000570 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000571 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000572 }
573 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000574 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000575 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000576 if (const TypedefNameDecl *TDecl =
577 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000578 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000579 if (T->isObjCObjectType()) {
580 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000581 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000582 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000583 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000584 }
585 }
586 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000587 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
588 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000589 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000590 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000591 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000592 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000593 }
Mike Stump1eb44332009-09-09 15:08:12 +0000594
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000595 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000596 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000597 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Anders Carlsson15281452008-11-04 16:57:32 +0000599 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000600 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000601
John McCalld226f652010-08-21 09:40:31 +0000602 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000603}
604
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000605bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000606 IdentifierInfo *PName,
607 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000608 const ObjCList<ObjCProtocolDecl> &PList) {
609
610 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000611 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
612 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000613 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
614 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000615 if (PDecl->getIdentifier() == PName) {
616 Diag(Ploc, diag::err_protocol_has_circular_dependency);
617 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000618 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000619 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000620
621 if (!PDecl->hasDefinition())
622 continue;
623
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000624 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
625 PDecl->getLocation(), PDecl->getReferencedProtocols()))
626 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000627 }
628 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000629 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000630}
631
John McCalld226f652010-08-21 09:40:31 +0000632Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000633Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
634 IdentifierInfo *ProtocolName,
635 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000636 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000637 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000638 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000639 SourceLocation EndProtoLoc,
640 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000641 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000642 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000643 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000644 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
645 ForRedeclaration);
646 ObjCProtocolDecl *PDecl = 0;
647 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
648 // If we already have a definition, complain.
649 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
650 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Douglas Gregor27c6da22012-01-01 20:30:41 +0000652 // Create a new protocol that is completely distinct from previous
653 // declarations, and do not make this protocol available for name lookup.
654 // That way, we'll end up completely ignoring the duplicate.
655 // FIXME: Can we turn this into an error?
656 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
657 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000658 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000659 PDecl->startDefinition();
660 } else {
661 if (PrevDecl) {
662 // Check for circular dependencies among protocol declarations. This can
663 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000664 ObjCList<ObjCProtocolDecl> PList;
665 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
666 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000667 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000668 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000669
670 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000671 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000672 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000673 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000674
Douglas Gregor6e378de2009-04-23 23:18:26 +0000675 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000676 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000677 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000678
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000679 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000680 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000681
682 // Merge attributes from previous declarations.
683 if (PrevDecl)
684 mergeDeclAttributes(PDecl, PrevDecl);
685
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000686 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000687 /// Check then save referenced protocols.
Roman Divacky31ba6132012-09-06 15:59:27 +0000688 PDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000689 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000690 }
Mike Stump1eb44332009-09-09 15:08:12 +0000691
692 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000693 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000694}
695
696/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000697/// issues an error if they are not declared. It returns list of
698/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000699void
Chris Lattnere13b9592008-07-26 04:03:38 +0000700Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000701 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000702 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000703 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000704 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000705 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
706 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000707 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000708 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000709 TypoCorrection Corrected = CorrectTypo(
710 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000711 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000712 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000713 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000714 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000715 Diag(PDecl->getLocation(), diag::note_previous_decl)
716 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000717 }
718 }
719
720 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000721 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000722 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000723 continue;
724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000726 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000727
728 // If this is a forward declaration and we are supposed to warn in this
729 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000730 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000731 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000732 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000733 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000734 }
735}
736
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000737/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000738/// a class method in its extension.
739///
Mike Stump1eb44332009-09-09 15:08:12 +0000740void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000741 ObjCInterfaceDecl *ID) {
742 if (!ID)
743 return; // Possibly due to previous error
744
745 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000746 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
747 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000748 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000749 MethodMap[MD->getSelector()] = MD;
750 }
751
752 if (MethodMap.empty())
753 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000754 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
755 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000756 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000757 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
758 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
759 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
760 << Method->getDeclName();
761 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
762 }
763 }
764}
765
James Dennett1dfbd922012-06-14 21:40:34 +0000766/// ActOnForwardProtocolDeclaration - Handle \@protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000767Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000768Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000769 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000770 unsigned NumElts,
771 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000772 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000773 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000774 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000775 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
776 ForRedeclaration);
777 ObjCProtocolDecl *PDecl
778 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
779 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000780 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000781
782 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000783 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000784
Douglas Gregor3937f872012-01-01 20:33:24 +0000785 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000786 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000787
788 if (PrevDecl)
789 mergeDeclAttributes(PDecl, PrevDecl);
790
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000791 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000792 }
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000794 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000795}
796
John McCalld226f652010-08-21 09:40:31 +0000797Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000798ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
799 IdentifierInfo *ClassName, SourceLocation ClassLoc,
800 IdentifierInfo *CategoryName,
801 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000802 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000803 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000804 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000805 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000806 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000807 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000808
809 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000810
811 if (!IDecl
812 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000813 diag::err_category_forward_interface,
814 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000815 // Create an invalid ObjCCategoryDecl to serve as context for
816 // the enclosing method declarations. We mark the decl invalid
817 // to make it clear that this isn't a valid AST.
818 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000819 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000820 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000821 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000822
823 if (!IDecl)
824 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000825 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000826 }
827
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000828 if (!CategoryName && IDecl->getImplementation()) {
829 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
830 Diag(IDecl->getImplementation()->getLocation(),
831 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000832 }
833
Fariborz Jahanian25760612010-02-15 21:55:26 +0000834 if (CategoryName) {
835 /// Check for duplicate interface declaration for this category
836 ObjCCategoryDecl *CDeclChain;
837 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
838 CDeclChain = CDeclChain->getNextClassCategory()) {
839 if (CDeclChain->getIdentifier() == CategoryName) {
840 // Class extensions can be declared multiple times.
841 Diag(CategoryLoc, diag::warn_dup_category_def)
842 << ClassName << CategoryName;
843 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
844 break;
845 }
Chris Lattner70f19542009-02-16 21:26:43 +0000846 }
847 }
Chris Lattner70f19542009-02-16 21:26:43 +0000848
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000849 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
850 ClassLoc, CategoryLoc, CategoryName, IDecl);
851 // FIXME: PushOnScopeChains?
852 CurContext->addDecl(CDecl);
853
Chris Lattner4d391482007-12-12 07:09:47 +0000854 if (NumProtoRefs) {
Roman Divacky31ba6132012-09-06 15:59:27 +0000855 CDecl->setProtocolList((ObjCProtocolDecl*const*)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000856 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000857 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000858 if (CDecl->IsClassExtension())
Roman Divacky31ba6132012-09-06 15:59:27 +0000859 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl*const*)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000860 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000861 }
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Anders Carlsson15281452008-11-04 16:57:32 +0000863 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000864 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000865}
866
867/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000868/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000869/// object.
John McCalld226f652010-08-21 09:40:31 +0000870Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000871 SourceLocation AtCatImplLoc,
872 IdentifierInfo *ClassName, SourceLocation ClassLoc,
873 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000874 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000875 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000876 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000877 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
878 if (!CatIDecl) {
879 // Category @implementation with no corresponding @interface.
880 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000881 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
882 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000883 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000884 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000885 }
886 }
887
Mike Stump1eb44332009-09-09 15:08:12 +0000888 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000889 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000890 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000891 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000892 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000893 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000894 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000895 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
896 diag::err_undef_interface)) {
897 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000898 }
Chris Lattner4d391482007-12-12 07:09:47 +0000899
Douglas Gregord0434102009-01-09 00:49:46 +0000900 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000901 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000902
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000903 // If the interface is deprecated/unavailable, warn/error about it.
904 if (IDecl)
905 DiagnoseUseOfDecl(IDecl, ClassLoc);
906
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000907 /// Check that CatName, category name, is not used in another implementation.
908 if (CatIDecl) {
909 if (CatIDecl->getImplementation()) {
910 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
911 << CatName;
912 Diag(CatIDecl->getImplementation()->getLocation(),
913 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000914 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000915 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000916 // Warn on implementating category of deprecated class under
917 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000918 DiagnoseObjCImplementedDeprecations(*this,
919 dyn_cast<NamedDecl>(IDecl),
920 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000921 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000922 }
Mike Stump1eb44332009-09-09 15:08:12 +0000923
Anders Carlsson15281452008-11-04 16:57:32 +0000924 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000925 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000926}
927
John McCalld226f652010-08-21 09:40:31 +0000928Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000929 SourceLocation AtClassImplLoc,
930 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000931 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000932 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000933 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000934 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000935 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000936 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
937 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000938 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000939 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000940 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000941 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000942 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
943 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000944 } else {
945 // We did not find anything with the name ClassName; try to correct for
946 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000947 ObjCInterfaceValidatorCCC Validator;
948 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000949 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000950 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000951 // Suggest the (potentially) correct interface name. However, put the
952 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000953 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000954 // provide a code-modification hint or use the typo name for recovery,
955 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000956 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000957 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000958 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000959 << ClassName << CorrectedName;
960 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
961 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000962 IDecl = 0;
963 } else {
964 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
965 }
Chris Lattner4d391482007-12-12 07:09:47 +0000966 }
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Chris Lattner4d391482007-12-12 07:09:47 +0000968 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000969 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000970 if (SuperClassname) {
971 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000972 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
973 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000974 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000975 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
976 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000977 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000978 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000979 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000980 if (SDecl && !SDecl->hasDefinition())
981 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000982 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000983 Diag(SuperClassLoc, diag::err_undef_superclass)
984 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000985 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000986 // This implementation and its interface do not have the same
987 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000988 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000989 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000990 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000991 }
992 }
993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Chris Lattner4d391482007-12-12 07:09:47 +0000995 if (!IDecl) {
996 // Legacy case of @implementation with no corresponding @interface.
997 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000998
Mike Stump390b4cc2009-05-16 07:39:55 +0000999 // FIXME: Do we support attributes on the @implementation? If so we should
1000 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +00001001 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +00001002 ClassName, /*PrevDecl=*/0, ClassLoc,
1003 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001004 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +00001005 if (SDecl) {
1006 IDecl->setSuperClass(SDecl);
1007 IDecl->setSuperClassLoc(SuperClassLoc);
1008 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
1009 } else {
1010 IDecl->setEndOfDefinitionLoc(ClassLoc);
1011 }
1012
Douglas Gregor8b9fb302009-04-24 00:16:12 +00001013 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001014 } else {
1015 // Mark the interface as being completed, even if it was just as
1016 // @class ....;
1017 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00001018 if (!IDecl->hasDefinition())
1019 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00001020 }
Mike Stump1eb44332009-09-09 15:08:12 +00001021
1022 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +00001023 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
1024 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Anders Carlsson15281452008-11-04 16:57:32 +00001026 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001027 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Chris Lattner4d391482007-12-12 07:09:47 +00001029 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001030 if (IDecl->getImplementation()) {
1031 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +00001032 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +00001033 Diag(IDecl->getImplementation()->getLocation(),
1034 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001035 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001036 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +00001037 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +00001038 // Warn on implementating deprecated class under
1039 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +00001040 DiagnoseObjCImplementedDeprecations(*this,
1041 dyn_cast<NamedDecl>(IDecl),
1042 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +00001043 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +00001044 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001045}
1046
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +00001047Sema::DeclGroupPtrTy
1048Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
1049 SmallVector<Decl *, 64> DeclsInGroup;
1050 DeclsInGroup.reserve(Decls.size() + 1);
1051
1052 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1053 Decl *Dcl = Decls[i];
1054 if (!Dcl)
1055 continue;
1056 if (Dcl->getDeclContext()->isFileContext())
1057 Dcl->setTopLevelDeclInObjCContainer();
1058 DeclsInGroup.push_back(Dcl);
1059 }
1060
1061 DeclsInGroup.push_back(ObjCImpDecl);
1062
1063 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1064}
1065
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001066void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1067 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001068 SourceLocation RBrace) {
1069 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001070 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001071 if (!IDecl)
1072 return;
James Dennett1dfbd922012-06-14 21:40:34 +00001073 /// Check case of non-existing \@interface decl.
1074 /// (legacy objective-c \@implementation decl without an \@interface decl).
Chris Lattner4d391482007-12-12 07:09:47 +00001075 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001076 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001077 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001078 // Add ivar's to class's DeclContext.
1079 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001080 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001081 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001082 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001083 }
1084
Chris Lattner4d391482007-12-12 07:09:47 +00001085 return;
1086 }
1087 // If implementation has empty ivar list, just return.
1088 if (numIvars == 0)
1089 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001090
Chris Lattner4d391482007-12-12 07:09:47 +00001091 assert(ivars && "missing @implementation ivars");
John McCall260611a2012-06-20 06:18:46 +00001092 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001093 if (ImpDecl->getSuperClass())
1094 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1095 for (unsigned i = 0; i < numIvars; i++) {
1096 ObjCIvarDecl* ImplIvar = ivars[i];
1097 if (const ObjCIvarDecl *ClsIvar =
1098 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1099 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1100 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1101 continue;
1102 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001103 // Instance ivar to Implementation's DeclContext.
1104 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001105 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001106 ImpDecl->addDecl(ImplIvar);
1107 }
1108 return;
1109 }
Chris Lattner4d391482007-12-12 07:09:47 +00001110 // Check interface's Ivar list against those in the implementation.
1111 // names and types must match.
1112 //
Chris Lattner4d391482007-12-12 07:09:47 +00001113 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001114 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001115 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1116 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001117 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001118 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001119 assert (ImplIvar && "missing implementation ivar");
1120 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001121
Steve Naroffca331292009-03-03 14:49:36 +00001122 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001123 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001124 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001125 << ImplIvar->getIdentifier()
1126 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001127 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001128 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1129 ImplIvar->getBitWidthValue(Context) !=
1130 ClsIvar->getBitWidthValue(Context)) {
1131 Diag(ImplIvar->getBitWidth()->getLocStart(),
1132 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1133 Diag(ClsIvar->getBitWidth()->getLocStart(),
1134 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001135 }
Steve Naroffca331292009-03-03 14:49:36 +00001136 // Make sure the names are identical.
1137 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001138 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001139 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001140 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001141 }
1142 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001143 }
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Chris Lattner609e4c72007-12-12 18:11:49 +00001145 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001146 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001147 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001148 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001149}
1150
Steve Naroff3c2eb662008-02-10 21:38:56 +00001151void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001152 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001153 // No point warning no definition of method which is 'unavailable'.
1154 if (method->hasAttr<UnavailableAttr>())
1155 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001156 if (!IncompleteImpl) {
1157 Diag(ImpLoc, diag::warn_incomplete_impl);
1158 IncompleteImpl = true;
1159 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001160 if (DiagID == diag::warn_unimplemented_protocol_method)
1161 Diag(ImpLoc, DiagID) << method->getDeclName();
1162 else
1163 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001164}
1165
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001166/// Determines if type B can be substituted for type A. Returns true if we can
1167/// guarantee that anything that the user will do to an object of type A can
1168/// also be done to an object of type B. This is trivially true if the two
1169/// types are the same, or if B is a subclass of A. It becomes more complex
1170/// in cases where protocols are involved.
1171///
1172/// Object types in Objective-C describe the minimum requirements for an
1173/// object, rather than providing a complete description of a type. For
1174/// example, if A is a subclass of B, then B* may refer to an instance of A.
1175/// The principle of substitutability means that we may use an instance of A
1176/// anywhere that we may use an instance of B - it will implement all of the
1177/// ivars of B and all of the methods of B.
1178///
1179/// This substitutability is important when type checking methods, because
1180/// the implementation may have stricter type definitions than the interface.
1181/// The interface specifies minimum requirements, but the implementation may
1182/// have more accurate ones. For example, a method may privately accept
1183/// instances of B, but only publish that it accepts instances of A. Any
1184/// object passed to it will be type checked against B, and so will implicitly
1185/// by a valid A*. Similarly, a method may return a subclass of the class that
1186/// it is declared as returning.
1187///
1188/// This is most important when considering subclassing. A method in a
1189/// subclass must accept any object as an argument that its superclass's
1190/// implementation accepts. It may, however, accept a more general type
1191/// without breaking substitutability (i.e. you can still use the subclass
1192/// anywhere that you can use the superclass, but not vice versa). The
1193/// converse requirement applies to return types: the return type for a
1194/// subclass method must be a valid object of the kind that the superclass
1195/// advertises, but it may be specified more accurately. This avoids the need
1196/// for explicit down-casting by callers.
1197///
1198/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001199static bool isObjCTypeSubstitutable(ASTContext &Context,
1200 const ObjCObjectPointerType *A,
1201 const ObjCObjectPointerType *B,
1202 bool rejectId) {
1203 // Reject a protocol-unqualified id.
1204 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001205
1206 // If B is a qualified id, then A must also be a qualified id and it must
1207 // implement all of the protocols in B. It may not be a qualified class.
1208 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1209 // stricter definition so it is not substitutable for id<A>.
1210 if (B->isObjCQualifiedIdType()) {
1211 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001212 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1213 QualType(B,0),
1214 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001215 }
1216
1217 /*
1218 // id is a special type that bypasses type checking completely. We want a
1219 // warning when it is used in one place but not another.
1220 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1221
1222
1223 // If B is a qualified id, then A must also be a qualified id (which it isn't
1224 // if we've got this far)
1225 if (B->isObjCQualifiedIdType()) return false;
1226 */
1227
1228 // Now we know that A and B are (potentially-qualified) class types. The
1229 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001230 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001231}
1232
John McCall10302c02010-10-28 02:34:38 +00001233static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1234 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1235}
1236
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001237static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001238 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001239 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001240 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001241 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001242 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001243 if (IsProtocolMethodDecl &&
1244 (MethodDecl->getObjCDeclQualifier() !=
1245 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001246 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001247 S.Diag(MethodImpl->getLocation(),
1248 (IsOverridingMode ?
1249 diag::warn_conflicting_overriding_ret_type_modifiers
1250 : diag::warn_conflicting_ret_type_modifiers))
1251 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001252 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1253 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1254 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1255 }
1256 else
1257 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001258 }
1259
John McCall10302c02010-10-28 02:34:38 +00001260 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001261 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001262 return true;
1263 if (!Warn)
1264 return false;
John McCall10302c02010-10-28 02:34:38 +00001265
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001266 unsigned DiagID =
1267 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1268 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001269
1270 // Mismatches between ObjC pointers go into a different warning
1271 // category, and sometimes they're even completely whitelisted.
1272 if (const ObjCObjectPointerType *ImplPtrTy =
1273 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1274 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001275 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001276 // Allow non-matching return types as long as they don't violate
1277 // the principle of substitutability. Specifically, we permit
1278 // return types that are subclasses of the declared return type,
1279 // or that are more-qualified versions of the declared type.
1280 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001281 return false;
John McCall10302c02010-10-28 02:34:38 +00001282
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001283 DiagID =
1284 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1285 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001286 }
1287 }
1288
1289 S.Diag(MethodImpl->getLocation(), DiagID)
1290 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001291 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001292 << MethodImpl->getResultType()
1293 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001294 S.Diag(MethodDecl->getLocation(),
1295 IsOverridingMode ? diag::note_previous_declaration
1296 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001297 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001298 return false;
John McCall10302c02010-10-28 02:34:38 +00001299}
1300
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001301static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001302 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001303 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001304 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001305 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001306 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001307 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001308 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001309 if (IsProtocolMethodDecl &&
1310 (ImplVar->getObjCDeclQualifier() !=
1311 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001312 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001313 if (IsOverridingMode)
1314 S.Diag(ImplVar->getLocation(),
1315 diag::warn_conflicting_overriding_param_modifiers)
1316 << getTypeRange(ImplVar->getTypeSourceInfo())
1317 << MethodImpl->getDeclName();
1318 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001319 diag::warn_conflicting_param_modifiers)
1320 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001321 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001322 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1323 << getTypeRange(IfaceVar->getTypeSourceInfo());
1324 }
1325 else
1326 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001327 }
1328
John McCall10302c02010-10-28 02:34:38 +00001329 QualType ImplTy = ImplVar->getType();
1330 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001331
John McCall10302c02010-10-28 02:34:38 +00001332 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001333 return true;
1334
1335 if (!Warn)
1336 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001337 unsigned DiagID =
1338 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1339 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001340
1341 // Mismatches between ObjC pointers go into a different warning
1342 // category, and sometimes they're even completely whitelisted.
1343 if (const ObjCObjectPointerType *ImplPtrTy =
1344 ImplTy->getAs<ObjCObjectPointerType>()) {
1345 if (const ObjCObjectPointerType *IfacePtrTy =
1346 IfaceTy->getAs<ObjCObjectPointerType>()) {
1347 // Allow non-matching argument types as long as they don't
1348 // violate the principle of substitutability. Specifically, the
1349 // implementation must accept any objects that the superclass
1350 // accepts, however it may also accept others.
1351 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001352 return false;
John McCall10302c02010-10-28 02:34:38 +00001353
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001354 DiagID =
1355 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1356 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001357 }
1358 }
1359
1360 S.Diag(ImplVar->getLocation(), DiagID)
1361 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001362 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1363 S.Diag(IfaceVar->getLocation(),
1364 (IsOverridingMode ? diag::note_previous_declaration
1365 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001366 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001367 return false;
John McCall10302c02010-10-28 02:34:38 +00001368}
John McCallf85e1932011-06-15 23:02:42 +00001369
1370/// In ARC, check whether the conventional meanings of the two methods
1371/// match. If they don't, it's a hard error.
1372static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1373 ObjCMethodDecl *decl) {
1374 ObjCMethodFamily implFamily = impl->getMethodFamily();
1375 ObjCMethodFamily declFamily = decl->getMethodFamily();
1376 if (implFamily == declFamily) return false;
1377
1378 // Since conventions are sorted by selector, the only possibility is
1379 // that the types differ enough to cause one selector or the other
1380 // to fall out of the family.
1381 assert(implFamily == OMF_None || declFamily == OMF_None);
1382
1383 // No further diagnostics required on invalid declarations.
1384 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1385
1386 const ObjCMethodDecl *unmatched = impl;
1387 ObjCMethodFamily family = declFamily;
1388 unsigned errorID = diag::err_arc_lost_method_convention;
1389 unsigned noteID = diag::note_arc_lost_method_convention;
1390 if (declFamily == OMF_None) {
1391 unmatched = decl;
1392 family = implFamily;
1393 errorID = diag::err_arc_gained_method_convention;
1394 noteID = diag::note_arc_gained_method_convention;
1395 }
1396
1397 // Indexes into a %select clause in the diagnostic.
1398 enum FamilySelector {
1399 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1400 };
1401 FamilySelector familySelector = FamilySelector();
1402
1403 switch (family) {
1404 case OMF_None: llvm_unreachable("logic error, no method convention");
1405 case OMF_retain:
1406 case OMF_release:
1407 case OMF_autorelease:
1408 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001409 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001410 case OMF_retainCount:
1411 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001412 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001413 // Mismatches for these methods don't change ownership
1414 // conventions, so we don't care.
1415 return false;
1416
1417 case OMF_init: familySelector = F_init; break;
1418 case OMF_alloc: familySelector = F_alloc; break;
1419 case OMF_copy: familySelector = F_copy; break;
1420 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1421 case OMF_new: familySelector = F_new; break;
1422 }
1423
1424 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1425 ReasonSelector reasonSelector;
1426
1427 // The only reason these methods don't fall within their families is
1428 // due to unusual result types.
1429 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1430 reasonSelector = R_UnrelatedReturn;
1431 } else {
1432 reasonSelector = R_NonObjectReturn;
1433 }
1434
1435 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1436 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1437
1438 return true;
1439}
John McCall10302c02010-10-28 02:34:38 +00001440
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001441void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001442 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001443 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001444 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001445 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1446 return;
1447
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001448 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001449 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001450 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Chris Lattner3aff9192009-04-11 19:58:42 +00001452 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001453 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1454 EF = MethodDecl->param_end();
1455 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001456 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001457 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001458 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001459
Fariborz Jahanian21121902011-08-08 18:03:17 +00001460 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001461 Diag(ImpMethodDecl->getLocation(),
1462 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001463 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001464 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001465}
1466
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001467void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1468 ObjCMethodDecl *Overridden,
1469 bool IsProtocolMethodDecl) {
1470
1471 CheckMethodOverrideReturn(*this, Method, Overridden,
1472 IsProtocolMethodDecl, true,
1473 true);
1474
1475 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001476 IF = Overridden->param_begin(), EM = Method->param_end(),
1477 EF = Overridden->param_end();
1478 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001479 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1480 IsProtocolMethodDecl, true, true);
1481 }
1482
1483 if (Method->isVariadic() != Overridden->isVariadic()) {
1484 Diag(Method->getLocation(),
1485 diag::warn_conflicting_overriding_variadic);
1486 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1487 }
1488}
1489
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001490/// WarnExactTypedMethods - This routine issues a warning if method
1491/// implementation declaration matches exactly that of its declaration.
1492void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1493 ObjCMethodDecl *MethodDecl,
1494 bool IsProtocolMethodDecl) {
1495 // don't issue warning when protocol method is optional because primary
1496 // class is not required to implement it and it is safe for protocol
1497 // to implement it.
1498 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1499 return;
1500 // don't issue warning when primary class's method is
1501 // depecated/unavailable.
1502 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1503 MethodDecl->hasAttr<DeprecatedAttr>())
1504 return;
1505
1506 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1507 IsProtocolMethodDecl, false, false);
1508 if (match)
1509 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001510 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1511 EF = MethodDecl->param_end();
1512 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001513 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1514 *IM, *IF,
1515 IsProtocolMethodDecl, false, false);
1516 if (!match)
1517 break;
1518 }
1519 if (match)
1520 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001521 if (match)
1522 match = !(MethodDecl->isClassMethod() &&
1523 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001524
1525 if (match) {
1526 Diag(ImpMethodDecl->getLocation(),
1527 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001528 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1529 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001530 }
1531}
1532
Mike Stump390b4cc2009-05-16 07:39:55 +00001533/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1534/// improve the efficiency of selector lookups and type checking by associating
1535/// with each protocol / interface / category the flattened instance tables. If
1536/// we used an immutable set to keep the table then it wouldn't add significant
1537/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001538
Steve Naroffefe7f362008-02-08 22:06:17 +00001539/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001540/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001541void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1542 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001543 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001544 const SelectorSet &InsMap,
1545 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001546 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001547 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1548 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1549 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001550 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1551
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001552 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001553 ObjCInterfaceDecl *NSIDecl = 0;
John McCall260611a2012-06-20 06:18:46 +00001554 if (getLangOpts().ObjCRuntime.isNeXTFamily()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001555 // check to see if class implements forwardInvocation method and objects
1556 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001557 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001558 // Under such conditions, which means that every method possible is
1559 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001560 // found" warnings.
1561 // FIXME: Use a general GetUnarySelector method for this.
1562 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1563 Selector fISelector = Context.Selectors.getSelector(1, &II);
1564 if (InsMap.count(fISelector))
1565 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1566 // need be implemented in the implementation.
1567 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1568 }
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001570 // If a method lookup fails locally we still need to look and see if
1571 // the method was implemented by a base class or an inherited
1572 // protocol. This lookup is slow, but occurs rarely in correct code
1573 // and otherwise would terminate in a warning.
1574
Chris Lattner4d391482007-12-12 07:09:47 +00001575 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001576 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001577 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001578 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001579 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001580 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Jordan Rose1e4691b2012-10-10 16:42:25 +00001581 !method->isPropertyAccessor() &&
1582 !InsMap.count(method->getSelector()) &&
1583 (!Super || !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001584 // If a method is not implemented in the category implementation but
1585 // has been declared in its primary class, superclass,
1586 // or in one of their protocols, no need to issue the warning.
1587 // This is because method will be implemented in the primary class
1588 // or one of its super class implementation.
1589
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001590 // Ugly, but necessary. Method declared in protcol might have
1591 // have been synthesized due to a property declared in the class which
1592 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001593 if (ObjCMethodDecl *MethodInClass =
1594 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001595 true /*shallowCategoryLookup*/))
Jordan Rose1e4691b2012-10-10 16:42:25 +00001596 if (C || MethodInClass->isPropertyAccessor())
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001597 continue;
1598 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1599 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1600 != DiagnosticsEngine::Ignored) {
1601 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001602 Diag(method->getLocation(), diag::note_method_declared_at)
1603 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001604 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1605 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001606 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001607 }
1608 }
Chris Lattner4d391482007-12-12 07:09:47 +00001609 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001610 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001611 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001612 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001613 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001614 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1615 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001616 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001617 // See above comment for instance method lookups.
1618 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001619 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001620 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001621 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001622 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1623 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001624 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001625 Diag(method->getLocation(), diag::note_method_declared_at)
1626 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001627 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1628 PDecl->getDeclName();
1629 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001630 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001631 }
Chris Lattner780f3292008-07-21 21:32:27 +00001632 // Check on this protocols's referenced protocols, recursively.
1633 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1634 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001635 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001636}
1637
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001638/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001639/// or protocol against those declared in their implementations.
1640///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001641void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1642 const SelectorSet &ClsMap,
1643 SelectorSet &InsMapSeen,
1644 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001645 ObjCImplDecl* IMPDecl,
1646 ObjCContainerDecl* CDecl,
1647 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001648 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001649 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001650 // Check and see if instance methods in class interface have been
1651 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001652 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1653 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001654 if (InsMapSeen.count((*I)->getSelector()))
1655 continue;
1656 InsMapSeen.insert((*I)->getSelector());
Jordan Rose1e4691b2012-10-10 16:42:25 +00001657 if (!(*I)->isPropertyAccessor() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001658 !InsMap.count((*I)->getSelector())) {
1659 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001660 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1661 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001662 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001663 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001664 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001665 IMPDecl->getInstanceMethod((*I)->getSelector());
1666 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1667 "Expected to find the method through lookup as well");
1668 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001669 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001670 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001671 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001672 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1673 isa<ObjCProtocolDecl>(CDecl));
Jordan Rose1e4691b2012-10-10 16:42:25 +00001674 else if (!MethodDecl->isPropertyAccessor())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001675 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001676 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001677 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001678 }
1679 }
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001681 // Check and see if class methods in class interface have been
1682 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001683 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001684 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001685 if (ClsMapSeen.count((*I)->getSelector()))
1686 continue;
1687 ClsMapSeen.insert((*I)->getSelector());
1688 if (!ClsMap.count((*I)->getSelector())) {
1689 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001690 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1691 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001692 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001693 ObjCMethodDecl *ImpMethodDecl =
1694 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001695 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1696 "Expected to find the method through lookup as well");
1697 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001698 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001699 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1700 isa<ObjCProtocolDecl>(CDecl));
1701 else
1702 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001703 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001704 }
1705 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001706
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001707 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001708 // Also methods in class extensions need be looked at next.
1709 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1710 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1711 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1712 IMPDecl,
1713 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001714 IncompleteImpl, false,
1715 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001716
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001717 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001718 for (ObjCInterfaceDecl::all_protocol_iterator
1719 PI = I->all_referenced_protocol_begin(),
1720 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001721 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1722 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001723 (*PI), IncompleteImpl, false,
1724 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001725
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001726 // FIXME. For now, we are not checking for extact match of methods
1727 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001728 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001729 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001730 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001731 I->getSuperClass(), IncompleteImpl, false);
1732 }
1733}
1734
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001735/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1736/// category matches with those implemented in its primary class and
1737/// warns each time an exact match is found.
1738void Sema::CheckCategoryVsClassMethodMatches(
1739 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001740 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001741
1742 for (ObjCImplementationDecl::instmeth_iterator
1743 I = CatIMPDecl->instmeth_begin(),
1744 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1745 InsMap.insert((*I)->getSelector());
1746
1747 for (ObjCImplementationDecl::classmeth_iterator
1748 I = CatIMPDecl->classmeth_begin(),
1749 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1750 ClsMap.insert((*I)->getSelector());
1751 if (InsMap.empty() && ClsMap.empty())
1752 return;
1753
1754 // Get category's primary class.
1755 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1756 if (!CatDecl)
1757 return;
1758 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1759 if (!IDecl)
1760 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001761 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001762 bool IncompleteImpl = false;
1763 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1764 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001765 IncompleteImpl, false,
1766 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001767}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001768
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001769void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001770 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001771 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001772 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001773 // Check and see if instance methods in class interface have been
1774 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001775 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001776 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001777 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001778
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001779 // Check and see if properties declared in the interface have either 1)
1780 // an implementation or 2) there is a @synthesize/@dynamic implementation
1781 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001782 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
John McCall260611a2012-06-20 06:18:46 +00001783 if (!(LangOpts.ObjCDefaultSynthProperties &&
1784 LangOpts.ObjCRuntime.isNonFragile()) ||
1785 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001786 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001787
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001788 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001789 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001790 I = IMPDecl->classmeth_begin(),
1791 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001792 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001794 // Check for type conflict of methods declared in a class/protocol and
1795 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001796 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001797 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1798 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001799 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001800
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001801 // check all methods implemented in category against those declared
1802 // in its primary class.
1803 if (ObjCCategoryImplDecl *CatDecl =
1804 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1805 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Chris Lattner4d391482007-12-12 07:09:47 +00001807 // Check the protocol list for unimplemented methods in the @implementation
1808 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001809 // Check and see if class methods in class interface have been
1810 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001811
Chris Lattnercddc8882009-03-01 00:56:52 +00001812 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001813 for (ObjCInterfaceDecl::all_protocol_iterator
1814 PI = I->all_referenced_protocol_begin(),
1815 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001816 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001817 InsMap, ClsMap, I);
1818 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001819 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1820 Categories; Categories = Categories->getNextClassExtension())
1821 ImplMethodsVsClassMethods(S, IMPDecl,
1822 const_cast<ObjCCategoryDecl*>(Categories),
1823 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001824 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001825 // For extended class, unimplemented methods in its protocols will
1826 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001827 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001828 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1829 E = C->protocol_end(); PI != E; ++PI)
1830 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001831 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001832 // Report unimplemented properties in the category as well.
1833 // When reporting on missing setter/getters, do not report when
1834 // setter/getter is implemented in category's primary class
1835 // implementation.
1836 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1837 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1838 for (ObjCImplementationDecl::instmeth_iterator
1839 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1840 InsMap.insert((*I)->getSelector());
1841 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001842 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001843 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001844 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001845 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001846}
1847
Mike Stump1eb44332009-09-09 15:08:12 +00001848/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001849Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001850Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001851 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001852 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001853 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001854 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001855 for (unsigned i = 0; i != NumElts; ++i) {
1856 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001857 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001858 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001859 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001860 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001861 // Maybe we will complain about the shadowed template parameter.
1862 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1863 // Just pretend that we didn't see the previous declaration.
1864 PrevDecl = 0;
1865 }
1866
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001867 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001868 // GCC apparently allows the following idiom:
1869 //
1870 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1871 // @class XCElementToggler;
1872 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001873 // Here we have chosen to ignore the forward class declaration
1874 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001875 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001876 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001877 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001878 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001879 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001880 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001881 // to the underlying class. Just ignore the forward class with a warning
1882 // as this will force the intended behavior which is to lookup the typedef
1883 // name.
1884 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1885 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1886 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1887 continue;
1888 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001889 }
Chris Lattner4d391482007-12-12 07:09:47 +00001890 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001891
1892 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001893 ObjCInterfaceDecl *PrevIDecl
1894 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001895 ObjCInterfaceDecl *IDecl
1896 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001897 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001898 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001899
Douglas Gregor7723fec2011-12-15 20:29:51 +00001900 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001901 CheckObjCDeclScope(IDecl);
1902 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001903 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001904
1905 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001906}
1907
John McCall0f4c4c42011-06-16 01:15:19 +00001908static bool tryMatchRecordTypes(ASTContext &Context,
1909 Sema::MethodMatchStrategy strategy,
1910 const Type *left, const Type *right);
1911
John McCallf85e1932011-06-15 23:02:42 +00001912static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1913 QualType leftQT, QualType rightQT) {
1914 const Type *left =
1915 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1916 const Type *right =
1917 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1918
1919 if (left == right) return true;
1920
1921 // If we're doing a strict match, the types have to match exactly.
1922 if (strategy == Sema::MMS_strict) return false;
1923
1924 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1925
1926 // Otherwise, use this absurdly complicated algorithm to try to
1927 // validate the basic, low-level compatibility of the two types.
1928
1929 // As a minimum, require the sizes and alignments to match.
1930 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1931 return false;
1932
1933 // Consider all the kinds of non-dependent canonical types:
1934 // - functions and arrays aren't possible as return and parameter types
1935
1936 // - vector types of equal size can be arbitrarily mixed
1937 if (isa<VectorType>(left)) return isa<VectorType>(right);
1938 if (isa<VectorType>(right)) return false;
1939
1940 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001941 // - structs, unions, and Objective-C objects must match more-or-less
1942 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001943 // - everything else should be a scalar
1944 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001945 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001946
John McCall1d9b3b22011-09-09 05:25:32 +00001947 // Make scalars agree in kind, except count bools as chars, and group
1948 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001949 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1950 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1951 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1952 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001953 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1954 leftSK = Type::STK_ObjCObjectPointer;
1955 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1956 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001957
1958 // Note that data member pointers and function member pointers don't
1959 // intermix because of the size differences.
1960
1961 return (leftSK == rightSK);
1962}
Chris Lattner4d391482007-12-12 07:09:47 +00001963
John McCall0f4c4c42011-06-16 01:15:19 +00001964static bool tryMatchRecordTypes(ASTContext &Context,
1965 Sema::MethodMatchStrategy strategy,
1966 const Type *lt, const Type *rt) {
1967 assert(lt && rt && lt != rt);
1968
1969 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1970 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1971 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1972
1973 // Require union-hood to match.
1974 if (left->isUnion() != right->isUnion()) return false;
1975
1976 // Require an exact match if either is non-POD.
1977 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1978 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1979 return false;
1980
1981 // Require size and alignment to match.
1982 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1983
1984 // Require fields to match.
1985 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1986 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1987 for (; li != le && ri != re; ++li, ++ri) {
1988 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1989 return false;
1990 }
1991 return (li == le && ri == re);
1992}
1993
Chris Lattner4d391482007-12-12 07:09:47 +00001994/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1995/// returns true, or false, accordingly.
1996/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001997bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1998 const ObjCMethodDecl *right,
1999 MethodMatchStrategy strategy) {
2000 if (!matchTypes(Context, strategy,
2001 left->getResultType(), right->getResultType()))
2002 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002003
David Blaikie4e4d0842012-03-11 07:00:24 +00002004 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002005 (left->hasAttr<NSReturnsRetainedAttr>()
2006 != right->hasAttr<NSReturnsRetainedAttr>() ||
2007 left->hasAttr<NSConsumesSelfAttr>()
2008 != right->hasAttr<NSConsumesSelfAttr>()))
2009 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002010
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002011 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002012 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
2013 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00002014
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002015 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00002016 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002017 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00002018
2019 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
2020 return false;
2021
David Blaikie4e4d0842012-03-11 07:00:24 +00002022 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00002023 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
2024 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00002025 }
2026 return true;
2027}
2028
Douglas Gregorff310c72012-05-01 23:37:00 +00002029void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00002030 // If the list is empty, make it a singleton list.
2031 if (List->Method == 0) {
2032 List->Method = Method;
2033 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00002034 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002035 }
2036
2037 // We've seen a method with this name, see if we have already seen this type
2038 // signature.
2039 ObjCMethodList *Previous = List;
2040 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002041 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00002042 continue;
2043
2044 ObjCMethodDecl *PrevObjCMethod = List->Method;
2045
2046 // Propagate the 'defined' bit.
2047 if (Method->isDefined())
2048 PrevObjCMethod->setDefined(true);
2049
2050 // If a method is deprecated, push it in the global pool.
2051 // This is used for better diagnostics.
2052 if (Method->isDeprecated()) {
2053 if (!PrevObjCMethod->isDeprecated())
2054 List->Method = Method;
2055 }
2056 // If new method is unavailable, push it into global pool
2057 // unless previous one is deprecated.
2058 if (Method->isUnavailable()) {
2059 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2060 List->Method = Method;
2061 }
2062
Douglas Gregorff310c72012-05-01 23:37:00 +00002063 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002064 }
2065
2066 // We have a new signature for an existing method - add it.
2067 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002068 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002069 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2070}
2071
Sebastian Redldb9d2142010-08-02 23:18:59 +00002072/// \brief Read the contents of the method pool for a given selector from
2073/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002074void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002075 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002076 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002077}
2078
Douglas Gregorff310c72012-05-01 23:37:00 +00002079void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002080 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002081 // Ignore methods of invalid containers.
2082 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002083 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002084
Douglas Gregor0d266d62012-01-25 00:59:09 +00002085 if (ExternalSource)
2086 ReadMethodPool(Method->getSelector());
2087
Sebastian Redldb9d2142010-08-02 23:18:59 +00002088 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002089 if (Pos == MethodPool.end())
2090 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2091 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002092
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002093 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002094
Sebastian Redldb9d2142010-08-02 23:18:59 +00002095 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002096 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002097}
2098
John McCallf85e1932011-06-15 23:02:42 +00002099/// Determines if this is an "acceptable" loose mismatch in the global
2100/// method pool. This exists mostly as a hack to get around certain
2101/// global mismatches which we can't afford to make warnings / errors.
2102/// Really, what we want is a way to take a method out of the global
2103/// method pool.
2104static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2105 ObjCMethodDecl *other) {
2106 if (!chosen->isInstanceMethod())
2107 return false;
2108
2109 Selector sel = chosen->getSelector();
2110 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2111 return false;
2112
2113 // Don't complain about mismatches for -length if the method we
2114 // chose has an integral result type.
2115 return (chosen->getResultType()->isIntegerType());
2116}
2117
Sebastian Redldb9d2142010-08-02 23:18:59 +00002118ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002119 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002120 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002121 if (ExternalSource)
2122 ReadMethodPool(Sel);
2123
Sebastian Redldb9d2142010-08-02 23:18:59 +00002124 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002125 if (Pos == MethodPool.end())
2126 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002127
Sebastian Redldb9d2142010-08-02 23:18:59 +00002128 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002129
Sebastian Redldb9d2142010-08-02 23:18:59 +00002130 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002131 bool issueDiagnostic = false, issueError = false;
2132
2133 // We support a warning which complains about *any* difference in
2134 // method signature.
2135 bool strictSelectorMatch =
2136 (receiverIdOrClass && warn &&
2137 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2138 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002139 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002140 if (strictSelectorMatch)
2141 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002142 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2143 MMS_strict)) {
2144 issueDiagnostic = true;
2145 break;
2146 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002147 }
2148
John McCallf85e1932011-06-15 23:02:42 +00002149 // If we didn't see any strict differences, we won't see any loose
2150 // differences. In ARC, however, we also need to check for loose
2151 // mismatches, because most of them are errors.
2152 if (!strictSelectorMatch ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002153 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002154 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002155 // This checks if the methods differ in type mismatch.
2156 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2157 MMS_loose) &&
2158 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2159 issueDiagnostic = true;
David Blaikie4e4d0842012-03-11 07:00:24 +00002160 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002161 issueError = true;
2162 break;
2163 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002164 }
2165
John McCallf85e1932011-06-15 23:02:42 +00002166 if (issueDiagnostic) {
2167 if (issueError)
2168 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2169 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002170 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2171 else
2172 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002173
2174 Diag(MethList.Method->getLocStart(),
2175 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002176 << MethList.Method->getSourceRange();
2177 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2178 Diag(Next->Method->getLocStart(), diag::note_also_found)
2179 << Next->Method->getSourceRange();
2180 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002181 }
2182 return MethList.Method;
2183}
2184
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002185ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002186 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2187 if (Pos == MethodPool.end())
2188 return 0;
2189
2190 GlobalMethods &Methods = Pos->second;
2191
2192 if (Methods.first.Method && Methods.first.Method->isDefined())
2193 return Methods.first.Method;
2194 if (Methods.second.Method && Methods.second.Method->isDefined())
2195 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002196 return 0;
2197}
2198
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002199/// DiagnoseDuplicateIvars -
2200/// Check for duplicate ivars in the entire class at the start of
James Dennett1dfbd922012-06-14 21:40:34 +00002201/// \@implementation. This becomes necesssary because class extension can
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002202/// add ivars to a class in random order which will not be known until
James Dennett1dfbd922012-06-14 21:40:34 +00002203/// class's \@implementation is seen.
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002204void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2205 ObjCInterfaceDecl *SID) {
2206 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2207 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002208 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002209 if (Ivar->isInvalidDecl())
2210 continue;
2211 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2212 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2213 if (prevIvar) {
2214 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2215 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2216 Ivar->setInvalidDecl();
2217 }
2218 }
2219 }
2220}
2221
Erik Verbruggend64251f2011-12-06 09:25:23 +00002222Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2223 switch (CurContext->getDeclKind()) {
2224 case Decl::ObjCInterface:
2225 return Sema::OCK_Interface;
2226 case Decl::ObjCProtocol:
2227 return Sema::OCK_Protocol;
2228 case Decl::ObjCCategory:
2229 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2230 return Sema::OCK_ClassExtension;
2231 else
2232 return Sema::OCK_Category;
2233 case Decl::ObjCImplementation:
2234 return Sema::OCK_Implementation;
2235 case Decl::ObjCCategoryImpl:
2236 return Sema::OCK_CategoryImplementation;
2237
2238 default:
2239 return Sema::OCK_None;
2240 }
2241}
2242
Steve Naroffa56f6162007-12-18 01:30:32 +00002243// Note: For class/category implemenations, allMethods/allProperties is
2244// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002245Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2246 Decl **allMethods, unsigned allNum,
2247 Decl **allProperties, unsigned pNum,
2248 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002249
Erik Verbruggend64251f2011-12-06 09:25:23 +00002250 if (getObjCContainerKind() == Sema::OCK_None)
2251 return 0;
2252
2253 assert(AtEnd.isValid() && "Invalid location for '@end'");
2254
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002255 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2256 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002257
Mike Stump1eb44332009-09-09 15:08:12 +00002258 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002259 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2260 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002261 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002262
Steve Naroff0701bbb2009-01-08 17:28:14 +00002263 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2264 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2265 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2266
Chris Lattner4d391482007-12-12 07:09:47 +00002267 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002268 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002269 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002270
2271 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002272 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002273 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002274 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002275 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002276 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002277 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002278 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002279 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002280 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002281 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002282 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002283 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002284 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002285 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002286 if (!Context.getSourceManager().isInSystemHeader(
2287 Method->getLocation()))
2288 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2289 << Method->getDeclName();
2290 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2291 }
Chris Lattner4d391482007-12-12 07:09:47 +00002292 InsMap[Method->getSelector()] = Method;
2293 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002294 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002295 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002296 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002297 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002298 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002299 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002300 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002301 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002302 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002303 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002304 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002305 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002306 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002307 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002308 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002309 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002310 if (!Context.getSourceManager().isInSystemHeader(
2311 Method->getLocation()))
2312 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2313 << Method->getDeclName();
2314 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2315 }
Chris Lattner4d391482007-12-12 07:09:47 +00002316 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002317 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002318 }
2319 }
2320 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002321 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002322 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002323 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002324 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002325 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002326 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002327 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002328 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002329 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002330
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002331 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002332 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002333 if (C->IsClassExtension()) {
2334 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2335 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002336 }
Chris Lattner4d391482007-12-12 07:09:47 +00002337 }
Steve Naroff09c47192009-01-09 15:36:25 +00002338 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002339 if (CDecl->getIdentifier())
2340 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2341 // user-defined setter/getter. It also synthesizes setter/getter methods
2342 // and adds them to the DeclContext and global method pools.
2343 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2344 E = CDecl->prop_end();
2345 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002346 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002347 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002348 }
2349 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002350 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002351 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002352 // Any property declared in a class extension might have user
2353 // declared setter or getter in current class extension or one
2354 // of the other class extensions. Mark them as synthesized as
2355 // property will be synthesized when property with same name is
2356 // seen in the @implementation.
2357 for (const ObjCCategoryDecl *ClsExtDecl =
2358 IDecl->getFirstClassExtension();
2359 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2360 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2361 E = ClsExtDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002362 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002363 // Skip over properties declared @dynamic
2364 if (const ObjCPropertyImplDecl *PIDecl
2365 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2366 if (PIDecl->getPropertyImplementation()
2367 == ObjCPropertyImplDecl::Dynamic)
2368 continue;
2369
2370 for (const ObjCCategoryDecl *CExtDecl =
2371 IDecl->getFirstClassExtension();
2372 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2373 if (ObjCMethodDecl *GetterMethod =
2374 CExtDecl->getInstanceMethod(Property->getGetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002375 GetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002376 if (!Property->isReadOnly())
2377 if (ObjCMethodDecl *SetterMethod =
2378 CExtDecl->getInstanceMethod(Property->getSetterName()))
Jordan Rose1e4691b2012-10-10 16:42:25 +00002379 SetterMethod->setPropertyAccessor(true);
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002380 }
2381 }
2382 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002383 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002384 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002385 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002386
Patrick Beardb2f68202012-04-06 18:12:22 +00002387 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2388 if (IDecl->getSuperClass() == NULL) {
2389 // This class has no superclass, so check that it has been marked with
2390 // __attribute((objc_root_class)).
2391 if (!HasRootClassAttr) {
2392 SourceLocation DeclLoc(IDecl->getLocation());
2393 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2394 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2395 << IDecl->getIdentifier();
2396 // See if NSObject is in the current scope, and if it is, suggest
2397 // adding " : NSObject " to the class declaration.
2398 NamedDecl *IF = LookupSingleName(TUScope,
2399 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2400 DeclLoc, LookupOrdinaryName);
2401 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2402 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2403 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2404 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2405 } else {
2406 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2407 }
2408 }
2409 } else if (HasRootClassAttr) {
2410 // Complain that only root classes may have this attribute.
2411 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2412 }
2413
John McCall260611a2012-06-20 06:18:46 +00002414 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002415 while (IDecl->getSuperClass()) {
2416 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2417 IDecl = IDecl->getSuperClass();
2418 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002419 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002420 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002421 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002422 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002423 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002424 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Chris Lattner4d391482007-12-12 07:09:47 +00002426 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002427 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002428 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002429 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002430 Categories; Categories = Categories->getNextClassCategory()) {
2431 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002432 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002433 break;
2434 }
2435 }
2436 }
2437 }
Chris Lattner682bf922009-03-29 16:50:03 +00002438 if (isInterfaceDeclKind) {
2439 // Reject invalid vardecls.
2440 for (unsigned i = 0; i != tuvNum; i++) {
2441 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2442 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2443 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002444 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002445 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002446 }
Chris Lattner682bf922009-03-29 16:50:03 +00002447 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002448 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002449 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002450
2451 for (unsigned i = 0; i != tuvNum; i++) {
2452 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002453 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2454 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002455 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2456 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002457
Dmitri Gribenkoabd56c82012-07-13 01:06:46 +00002458 ActOnDocumentableDecl(ClassDecl);
Erik Verbruggend64251f2011-12-06 09:25:23 +00002459 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002460}
2461
2462
2463/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2464/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002465static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002466CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002467 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002468}
2469
Ted Kremenek422bae72010-04-18 04:59:38 +00002470static inline
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002471unsigned countAlignAttr(const AttrVec &A) {
2472 unsigned count=0;
2473 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2474 if ((*i)->getKind() == attr::Aligned)
2475 ++count;
2476 return count;
2477}
2478
2479static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002480bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2481 const AttrVec &A) {
2482 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002483 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002484 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002485 return false;
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002486
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002487 // method declared in interface has no attribute.
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002488 // But implementation has attributes. This is invalid.
2489 // Except when implementation has 'Align' attribute which is
2490 // immaterial to method declared in interface.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002491 if (!IMD->hasAttrs())
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002492 return (A.size() > countAlignAttr(A));
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002493
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002494 const AttrVec &D = IMD->getAttrs();
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002495
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002496 unsigned countAlignOnImpl = countAlignAttr(A);
2497 if (!countAlignOnImpl && (A.size() != D.size()))
2498 return true;
2499 else if (countAlignOnImpl) {
2500 unsigned countAlignOnDecl = countAlignAttr(D);
2501 if (countAlignOnDecl && (A.size() != D.size()))
2502 return true;
2503 else if (!countAlignOnDecl &&
2504 ((A.size()-countAlignOnImpl) != D.size()))
2505 return true;
2506 }
2507
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002508 // attributes on method declaration and definition must match exactly.
2509 // Note that we have at most a couple of attributes on methods, so this
2510 // n*n search is good enough.
2511 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002512 if ((*i)->getKind() == attr::Aligned)
2513 continue;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002514 bool match = false;
2515 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2516 if ((*i)->getKind() == (*i1)->getKind()) {
2517 match = true;
2518 break;
2519 }
2520 }
2521 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002522 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002523 }
Fariborz Jahanian129a60b2012-08-24 23:50:13 +00002524
Sean Huntcf807c42010-08-18 23:23:40 +00002525 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002526}
2527
Douglas Gregor926df6c2011-06-11 01:09:30 +00002528/// \brief Check whether the declared result type of the given Objective-C
2529/// method declaration is compatible with the method's class.
2530///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002531static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002532CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2533 ObjCInterfaceDecl *CurrentClass) {
2534 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002535
2536 // If an Objective-C method inherits its related result type, then its
2537 // declared result type must be compatible with its own class type. The
2538 // declared result type is compatible if:
2539 if (const ObjCObjectPointerType *ResultObjectType
2540 = ResultType->getAs<ObjCObjectPointerType>()) {
2541 // - it is id or qualified id, or
2542 if (ResultObjectType->isObjCIdType() ||
2543 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002544 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002545
2546 if (CurrentClass) {
2547 if (ObjCInterfaceDecl *ResultClass
2548 = ResultObjectType->getInterfaceDecl()) {
2549 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002550 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002551 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002552
2553 // - it is a superclass of the method's class type
2554 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002555 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002556 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002557 } else {
2558 // Any Objective-C pointer type might be acceptable for a protocol
2559 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002560 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002561 }
2562 }
2563
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002564 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002565}
2566
John McCall6c2c2502011-07-22 02:45:48 +00002567namespace {
2568/// A helper class for searching for methods which a particular method
2569/// overrides.
2570class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002571public:
John McCall6c2c2502011-07-22 02:45:48 +00002572 Sema &S;
2573 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002574 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002575 bool Recursive;
2576
2577public:
2578 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2579 Selector selector = method->getSelector();
2580
2581 // Bypass this search if we've never seen an instance/class method
2582 // with this selector before.
2583 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2584 if (it == S.MethodPool.end()) {
2585 if (!S.ExternalSource) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002586 S.ReadMethodPool(selector);
2587
2588 it = S.MethodPool.find(selector);
2589 if (it == S.MethodPool.end())
2590 return;
John McCall6c2c2502011-07-22 02:45:48 +00002591 }
2592 ObjCMethodList &list =
2593 method->isInstanceMethod() ? it->second.first : it->second.second;
2594 if (!list.Method) return;
2595
2596 ObjCContainerDecl *container
2597 = cast<ObjCContainerDecl>(method->getDeclContext());
2598
2599 // Prevent the search from reaching this container again. This is
2600 // important with categories, which override methods from the
2601 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002602 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2603 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002604 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2605 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002606 } else {
2607 searchFromContainer(container);
2608 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002609 }
John McCall6c2c2502011-07-22 02:45:48 +00002610
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002611 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002612 iterator begin() const { return Overridden.begin(); }
2613 iterator end() const { return Overridden.end(); }
2614
2615private:
2616 void searchFromContainer(ObjCContainerDecl *container) {
2617 if (container->isInvalidDecl()) return;
2618
2619 switch (container->getDeclKind()) {
2620#define OBJCCONTAINER(type, base) \
2621 case Decl::type: \
2622 searchFrom(cast<type##Decl>(container)); \
2623 break;
2624#define ABSTRACT_DECL(expansion)
2625#define DECL(type, base) \
2626 case Decl::type:
2627#include "clang/AST/DeclNodes.inc"
2628 llvm_unreachable("not an ObjC container!");
2629 }
2630 }
2631
2632 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002633 if (!protocol->hasDefinition())
2634 return;
2635
John McCall6c2c2502011-07-22 02:45:48 +00002636 // A method in a protocol declaration overrides declarations from
2637 // referenced ("parent") protocols.
2638 search(protocol->getReferencedProtocols());
2639 }
2640
2641 void searchFrom(ObjCCategoryDecl *category) {
2642 // A method in a category declaration overrides declarations from
2643 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002644 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002645 search(category->getReferencedProtocols());
2646 }
2647
2648 void searchFrom(ObjCCategoryImplDecl *impl) {
2649 // A method in a category definition that has a category
2650 // declaration overrides declarations from the category
2651 // declaration.
2652 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2653 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002654 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2655 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002656
2657 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002658 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2659 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002660 }
2661 }
2662
2663 void searchFrom(ObjCInterfaceDecl *iface) {
2664 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002665 if (!iface->hasDefinition())
2666 return;
2667
John McCall6c2c2502011-07-22 02:45:48 +00002668 // - categories,
2669 for (ObjCCategoryDecl *category = iface->getCategoryList();
2670 category; category = category->getNextClassCategory())
2671 search(category);
2672
2673 // - the super class, and
2674 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2675 search(super);
2676
2677 // - any referenced protocols.
2678 search(iface->getReferencedProtocols());
2679 }
2680
2681 void searchFrom(ObjCImplementationDecl *impl) {
2682 // A method in a class implementation overrides declarations from
2683 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002684 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2685 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002686 }
2687
2688
2689 void search(const ObjCProtocolList &protocols) {
2690 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2691 i != e; ++i)
2692 search(*i);
2693 }
2694
2695 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002696 // Check for a method in this container which matches this selector.
2697 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2698 Method->isInstanceMethod());
2699
2700 // If we find one, record it and bail out.
2701 if (meth) {
2702 Overridden.insert(meth);
2703 return;
2704 }
2705
2706 // Otherwise, search for methods that a hypothetical method here
2707 // would have overridden.
2708
2709 // Note that we're now in a recursive case.
2710 Recursive = true;
2711
2712 searchFromContainer(container);
2713 }
2714};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002715}
2716
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002717void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2718 ObjCInterfaceDecl *CurrentClass,
2719 ResultTypeCompatibilityKind RTC) {
2720 // Search for overridden methods and merge information down from them.
2721 OverrideSearch overrides(*this, ObjCMethod);
2722 // Keep track if the method overrides any method in the class's base classes,
2723 // its protocols, or its categories' protocols; we will keep that info
2724 // in the ObjCMethodDecl.
2725 // For this info, a method in an implementation is not considered as
2726 // overriding the same method in the interface or its categories.
2727 bool hasOverriddenMethodsInBaseOrProtocol = false;
2728 for (OverrideSearch::iterator
2729 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2730 ObjCMethodDecl *overridden = *i;
2731
2732 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2733 CurrentClass != overridden->getClassInterface() ||
2734 overridden->isOverriding())
2735 hasOverriddenMethodsInBaseOrProtocol = true;
2736
2737 // Propagate down the 'related result type' bit from overridden methods.
2738 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2739 ObjCMethod->SetRelatedResultType();
2740
2741 // Then merge the declarations.
2742 mergeObjCMethodDecls(ObjCMethod, overridden);
2743
2744 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2745 continue; // Conflicting properties are detected elsewhere.
2746
2747 // Check for overriding methods
2748 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2749 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2750 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2751 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2752
2753 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
Fariborz Jahanianc4133a42012-07-05 22:26:07 +00002754 isa<ObjCInterfaceDecl>(overridden->getDeclContext()) &&
2755 !overridden->isImplicit() /* not meant for properties */) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002756 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2757 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002758 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2759 PrevE = overridden->param_end();
2760 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002761 assert(PrevI != overridden->param_end() && "Param mismatch");
2762 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2763 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2764 // If type of argument of method in this class does not match its
2765 // respective argument type in the super class method, issue warning;
2766 if (!Context.typesAreCompatible(T1, T2)) {
2767 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2768 << T1 << T2;
2769 Diag(overridden->getLocation(), diag::note_previous_declaration);
2770 break;
2771 }
2772 }
2773 }
2774 }
2775
2776 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2777}
2778
John McCalld226f652010-08-21 09:40:31 +00002779Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002780 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002781 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002782 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002783 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002784 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002785 Selector Sel,
2786 // optional arguments. The number of types/arguments is obtained
2787 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002788 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002789 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002790 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002791 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002792 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002793 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002794 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002795 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002796 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002797 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2798 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002799 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002800
Douglas Gregore97179c2011-09-08 01:46:34 +00002801 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002802 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002803 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002804 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002805
Steve Naroffccef3712009-02-20 22:59:16 +00002806 // Methods cannot return interface types. All ObjC objects are
2807 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002808 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002809 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2810 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002811 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002812 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002813
2814 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002815 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002816 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002817 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002818 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002819 }
Mike Stump1eb44332009-09-09 15:08:12 +00002820
2821 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002822 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002823 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002824 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002825 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002826 MethodType == tok::minus, isVariadic,
Jordan Rose1e4691b2012-10-10 16:42:25 +00002827 /*isPropertyAccessor=*/false,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002828 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002829 MethodDeclKind == tok::objc_optional
2830 ? ObjCMethodDecl::Optional
2831 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002832 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002833
Chris Lattner5f9e2722011-07-23 10:55:15 +00002834 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002835
Chris Lattner7db638d2009-04-11 19:42:43 +00002836 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002837 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002838 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002839
Chris Lattnere294d3f2009-04-11 18:57:04 +00002840 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002841 ArgType = Context.getObjCIdType();
2842 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002843 } else {
John McCall58e46772009-10-23 21:48:59 +00002844 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002845 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002846 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002847 }
Mike Stump1eb44332009-09-09 15:08:12 +00002848
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002849 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2850 LookupOrdinaryName, ForRedeclaration);
2851 LookupName(R, S);
2852 if (R.isSingleResult()) {
2853 NamedDecl *PrevDecl = R.getFoundDecl();
2854 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002855 Diag(ArgInfo[i].NameLoc,
2856 (MethodDefinition ? diag::warn_method_param_redefinition
2857 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002858 << ArgInfo[i].Name;
2859 Diag(PrevDecl->getLocation(),
2860 diag::note_previous_declaration);
2861 }
2862 }
2863
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002864 SourceLocation StartLoc = DI
2865 ? DI->getTypeLoc().getBeginLoc()
2866 : ArgInfo[i].NameLoc;
2867
John McCall81ef3e62011-04-23 02:46:06 +00002868 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2869 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2870 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002871
John McCall70798862011-05-02 00:30:12 +00002872 Param->setObjCMethodScopeInfo(i);
2873
Chris Lattner0ed844b2008-04-04 06:12:32 +00002874 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002875 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002876
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002877 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002878 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002879
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002880 if (Param->hasAttr<BlocksAttr>()) {
2881 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2882 Param->setInvalidDecl();
2883 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002884 S->AddDecl(Param);
2885 IdResolver.AddDecl(Param);
2886
Chris Lattner0ed844b2008-04-04 06:12:32 +00002887 Params.push_back(Param);
2888 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002889
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002890 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002891 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002892 QualType ArgType = Param->getType();
2893 if (ArgType.isNull())
2894 ArgType = Context.getObjCIdType();
2895 else
2896 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002897 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002898 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002899 Diag(Param->getLocation(),
2900 diag::err_object_cannot_be_passed_returned_by_value)
2901 << 1 << ArgType;
2902 Param->setInvalidDecl();
2903 }
2904 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002905
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002906 Params.push_back(Param);
2907 }
2908
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002909 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002910 ObjCMethod->setObjCDeclQualifier(
2911 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002912
2913 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002914 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002915
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002916 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002917 const ObjCMethodDecl *PrevMethod = 0;
2918 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002919 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002920 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2921 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002922 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002923 PrevMethod = ImpDecl->getClassMethod(Sel);
2924 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002925 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002926
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002927 ObjCMethodDecl *IMD = 0;
2928 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2929 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2930 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002931 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002932 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002933 SourceLocation MethodLoc = IMD->getLocation();
2934 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2935 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002936 Diag(MethodLoc, diag::note_method_declared_at)
2937 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002938 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002939 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002940 } else {
2941 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002942 }
John McCall6c2c2502011-07-22 02:45:48 +00002943
Chris Lattner4d391482007-12-12 07:09:47 +00002944 if (PrevMethod) {
2945 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002946 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002947 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002948 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002949 }
John McCall54abf7d2009-11-04 02:18:39 +00002950
Douglas Gregor926df6c2011-06-11 01:09:30 +00002951 // If this Objective-C method does not have a related result type, but we
2952 // are allowed to infer related result types, try to do so based on the
2953 // method family.
2954 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2955 if (!CurrentClass) {
2956 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2957 CurrentClass = Cat->getClassInterface();
2958 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2959 CurrentClass = Impl->getClassInterface();
2960 else if (ObjCCategoryImplDecl *CatImpl
2961 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2962 CurrentClass = CatImpl->getClassInterface();
2963 }
John McCall6c2c2502011-07-22 02:45:48 +00002964
Douglas Gregore97179c2011-09-08 01:46:34 +00002965 ResultTypeCompatibilityKind RTC
2966 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002967
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002968 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00002969
John McCallf85e1932011-06-15 23:02:42 +00002970 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002971 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002972 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2973
Douglas Gregore97179c2011-09-08 01:46:34 +00002974 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002975 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00002976 !ObjCMethod->hasRelatedResultType() &&
2977 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002978 bool InferRelatedResultType = false;
2979 switch (ObjCMethod->getMethodFamily()) {
2980 case OMF_None:
2981 case OMF_copy:
2982 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002983 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002984 case OMF_mutableCopy:
2985 case OMF_release:
2986 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002987 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002988 break;
2989
2990 case OMF_alloc:
2991 case OMF_new:
2992 InferRelatedResultType = ObjCMethod->isClassMethod();
2993 break;
2994
2995 case OMF_init:
2996 case OMF_autorelease:
2997 case OMF_retain:
2998 case OMF_self:
2999 InferRelatedResultType = ObjCMethod->isInstanceMethod();
3000 break;
3001 }
3002
John McCall6c2c2502011-07-22 02:45:48 +00003003 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00003004 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00003005 }
Dmitri Gribenkoa5ef44f2012-07-11 21:38:39 +00003006
3007 ActOnDocumentableDecl(ObjCMethod);
3008
John McCalld226f652010-08-21 09:40:31 +00003009 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00003010}
3011
Chris Lattnercc98eac2008-12-17 07:13:27 +00003012bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00003013 // Following is also an error. But it is caused by a missing @end
3014 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003015 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003016 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00003017
3018 // If we switched context to translation unit while we are still lexically in
3019 // an objc container, it means the parser missed emitting an error.
3020 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
3021 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00003022
Anders Carlsson15281452008-11-04 16:57:32 +00003023 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
3024 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00003025
Anders Carlsson15281452008-11-04 16:57:32 +00003026 return true;
3027}
Chris Lattnercc98eac2008-12-17 07:13:27 +00003028
James Dennett1dfbd922012-06-14 21:40:34 +00003029/// Called whenever \@defs(ClassName) is encountered in the source. Inserts the
Chris Lattnercc98eac2008-12-17 07:13:27 +00003030/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00003031void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00003032 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003033 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00003034 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00003035 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003036 if (!Class) {
3037 Diag(DeclStart, diag::err_undef_interface) << ClassName;
3038 return;
3039 }
John McCall260611a2012-06-20 06:18:46 +00003040 if (LangOpts.ObjCRuntime.isNonFragile()) {
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00003041 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
3042 return;
3043 }
Mike Stump1eb44332009-09-09 15:08:12 +00003044
Chris Lattnercc98eac2008-12-17 07:13:27 +00003045 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00003046 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003047 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003048 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003049 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00003050 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00003051 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003052 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
3053 /*FIXME: StartL=*/ID->getLocation(),
3054 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00003055 ID->getIdentifier(), ID->getType(),
3056 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00003057 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00003058 }
Mike Stump1eb44332009-09-09 15:08:12 +00003059
Chris Lattnercc98eac2008-12-17 07:13:27 +00003060 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003061 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00003062 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00003063 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00003064 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00003065 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00003066 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00003067 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00003068 }
3069}
3070
Douglas Gregor160b5632010-04-26 17:32:49 +00003071/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003072VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
3073 SourceLocation StartLoc,
3074 SourceLocation IdLoc,
3075 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00003076 bool Invalid) {
3077 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3078 // duration shall not be qualified by an address-space qualifier."
3079 // Since all parameters have automatic store duration, they can not have
3080 // an address space.
3081 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003082 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003083 Invalid = true;
3084 }
3085
3086 // An @catch parameter must be an unqualified object pointer type;
3087 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3088 if (Invalid) {
3089 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003090 } else if (T->isDependentType()) {
3091 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003092 } else if (!T->isObjCObjectPointerType()) {
3093 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003094 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003095 } else if (T->isObjCQualifiedIdType()) {
3096 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003097 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003098 }
3099
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003100 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3101 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003102 New->setExceptionVariable(true);
3103
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003104 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003105 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003106 Invalid = true;
3107
Douglas Gregor160b5632010-04-26 17:32:49 +00003108 if (Invalid)
3109 New->setInvalidDecl();
3110 return New;
3111}
3112
John McCalld226f652010-08-21 09:40:31 +00003113Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003114 const DeclSpec &DS = D.getDeclSpec();
3115
3116 // We allow the "register" storage class on exception variables because
3117 // GCC did, but we drop it completely. Any other storage class is an error.
3118 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3119 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3120 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3121 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3122 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3123 << DS.getStorageClassSpec();
3124 }
3125 if (D.getDeclSpec().isThreadSpecified())
3126 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3127 D.getMutableDeclSpec().ClearStorageClassSpecs();
3128
3129 DiagnoseFunctionSpecifiers(D);
3130
3131 // Check that there are no default arguments inside the type of this
3132 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003133 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003134 CheckExtraCXXDefaultArguments(D);
3135
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003136 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003137 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003138
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003139 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3140 D.getSourceRange().getBegin(),
3141 D.getIdentifierLoc(),
3142 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003143 D.isInvalidType());
3144
3145 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3146 if (D.getCXXScopeSpec().isSet()) {
3147 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3148 << D.getCXXScopeSpec().getRange();
3149 New->setInvalidDecl();
3150 }
3151
3152 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003153 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003154 if (D.getIdentifier())
3155 IdResolver.AddDecl(New);
3156
3157 ProcessDeclAttributes(S, New, D);
3158
3159 if (New->hasAttr<BlocksAttr>())
3160 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003161 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003162}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003163
3164/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003165/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003166void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003167 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003168 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3169 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003170 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003171 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003172 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003173 }
3174}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003175
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003176void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003177 // Load referenced selectors from the external source.
3178 if (ExternalSource) {
3179 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3180 ExternalSource->ReadReferencedSelectors(Sels);
3181 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3182 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3183 }
3184
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003185 // Warning will be issued only when selector table is
3186 // generated (which means there is at lease one implementation
3187 // in the TU). This is to match gcc's behavior.
3188 if (ReferencedSelectors.empty() ||
3189 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003190 return;
3191 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3192 ReferencedSelectors.begin(),
3193 E = ReferencedSelectors.end(); S != E; ++S) {
3194 Selector Sel = (*S).first;
3195 if (!LookupImplementedMethodInGlobalPool(Sel))
3196 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3197 }
3198 return;
3199}