blob: a942d4979d737ead02c360b1c5da218adff965e8 [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 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000176 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin();
177 for (ObjCMethodDecl::param_iterator
178 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000179 ni != ne; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000180 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000181 ParmVarDecl *newDecl = (*ni);
182 if (newDecl->hasAttr<NSConsumedAttr>() !=
183 oldDecl->hasAttr<NSConsumedAttr>()) {
184 Diag(newDecl->getLocation(),
185 diag::err_nsconsumed_attribute_mismatch);
186 Diag(oldDecl->getLocation(), diag::note_previous_decl)
187 << "parameter";
188 }
189 }
190 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000191}
192
John McCallf85e1932011-06-15 23:02:42 +0000193/// \brief Check a method declaration for compatibility with the Objective-C
194/// ARC conventions.
195static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
196 ObjCMethodFamily family = method->getMethodFamily();
197 switch (family) {
198 case OMF_None:
199 case OMF_dealloc:
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
209 case OMF_init:
210 // If the method doesn't obey the init rules, don't bother annotating it.
211 if (S.checkInitMethod(method, QualType()))
212 return true;
213
214 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
215 S.Context));
216
217 // Don't add a second copy of this attribute, but otherwise don't
218 // let it be suppressed.
219 if (method->hasAttr<NSReturnsRetainedAttr>())
220 return false;
221 break;
222
223 case OMF_alloc:
224 case OMF_copy:
225 case OMF_mutableCopy:
226 case OMF_new:
227 if (method->hasAttr<NSReturnsRetainedAttr>() ||
228 method->hasAttr<NSReturnsNotRetainedAttr>() ||
229 method->hasAttr<NSReturnsAutoreleasedAttr>())
230 return false;
231 break;
232 }
233
234 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
235 S.Context));
236 return false;
237}
238
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000239static void DiagnoseObjCImplementedDeprecations(Sema &S,
240 NamedDecl *ND,
241 SourceLocation ImplLoc,
242 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000243 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000244 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000245 if (select == 0)
Ted Kremenek3306ec12012-02-27 22:55:11 +0000246 S.Diag(ND->getLocation(), diag::note_method_declared_at)
247 << ND->getDeclName();
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000248 else
249 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
250 }
251}
252
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000253/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
254/// pool.
255void Sema::AddAnyMethodToGlobalPool(Decl *D) {
256 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
257
258 // If we don't have a valid method decl, simply return.
259 if (!MDecl)
260 return;
261 if (MDecl->isInstanceMethod())
262 AddInstanceMethodToGlobalPool(MDecl, true);
263 else
264 AddFactoryMethodToGlobalPool(MDecl, true);
265}
266
Steve Naroffebf64432009-02-28 16:59:13 +0000267/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000268/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000269void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000270 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000271 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000272
Steve Naroff394f3f42008-07-25 17:57:26 +0000273 // If we don't have a valid method decl, simply return.
274 if (!MDecl)
275 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000276
Chris Lattner4d391482007-12-12 07:09:47 +0000277 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000278 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000279 PushFunctionScope();
280
Chris Lattner4d391482007-12-12 07:09:47 +0000281 // Create Decl objects for each parameter, entrring them in the scope for
282 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000283
284 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000285 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000286
Daniel Dunbar451318c2008-08-26 06:07:48 +0000287 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
288 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000289
Chris Lattner8123a952008-04-10 02:22:51 +0000290 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000291 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000292 E = MDecl->param_end(); PI != E; ++PI) {
293 ParmVarDecl *Param = (*PI);
294 if (!Param->isInvalidDecl() &&
295 RequireCompleteType(Param->getLocation(), Param->getType(),
296 diag::err_typecheck_decl_incomplete_type))
297 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000298 if ((*PI)->getIdentifier())
299 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000300 }
John McCallf85e1932011-06-15 23:02:42 +0000301
302 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000303 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000304 switch (MDecl->getMethodFamily()) {
305 case OMF_retain:
306 case OMF_retainCount:
307 case OMF_release:
308 case OMF_autorelease:
309 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
310 << MDecl->getSelector();
311 break;
312
313 case OMF_None:
314 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000315 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000316 case OMF_alloc:
317 case OMF_init:
318 case OMF_mutableCopy:
319 case OMF_copy:
320 case OMF_new:
321 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000322 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000323 break;
324 }
325 }
326
Nico Weber9a1ecf02011-08-22 17:25:57 +0000327 // Warn on deprecated methods under -Wdeprecated-implementations,
328 // and prepare for warning on missing super calls.
329 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000330 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000331 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000332 DiagnoseObjCImplementedDeprecations(*this,
333 dyn_cast<NamedDecl>(IMD),
334 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000335
Nico Weber80cb6e62011-08-28 22:35:17 +0000336 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000337 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
338 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
339 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000340 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000341 ObjCShouldCallSuperDealloc =
David Blaikie4e4d0842012-03-11 07:00:24 +0000342 !(Context.getLangOpts().ObjCAutoRefCount ||
343 Context.getLangOpts().getGC() == LangOptions::GCOnly) &&
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000344 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000345 ObjCShouldCallSuperFinalize =
David Blaikie4e4d0842012-03-11 07:00:24 +0000346 Context.getLangOpts().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000347 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000348 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000349 }
Chris Lattner4d391482007-12-12 07:09:47 +0000350}
351
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000352namespace {
353
354// Callback to only accept typo corrections that are Objective-C classes.
355// If an ObjCInterfaceDecl* is given to the constructor, then the validation
356// function will reject corrections to that class.
357class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
358 public:
359 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
360 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
361 : CurrentIDecl(IDecl) {}
362
363 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
364 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
365 return ID && !declaresSameEntity(ID, CurrentIDecl);
366 }
367
368 private:
369 ObjCInterfaceDecl *CurrentIDecl;
370};
371
372}
373
John McCalld226f652010-08-21 09:40:31 +0000374Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000375ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
376 IdentifierInfo *ClassName, SourceLocation ClassLoc,
377 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000378 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000379 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000380 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000381 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000382
Chris Lattner4d391482007-12-12 07:09:47 +0000383 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000384 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000385 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000386
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000387 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000388 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000389 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000390 }
Mike Stump1eb44332009-09-09 15:08:12 +0000391
Douglas Gregor7723fec2011-12-15 20:29:51 +0000392 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000393 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000394 ObjCInterfaceDecl *IDecl
395 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000396 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000397
Douglas Gregor7723fec2011-12-15 20:29:51 +0000398 if (PrevIDecl) {
399 // Class already seen. Was it a definition?
400 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
401 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
402 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000403 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000404 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000405 }
Chris Lattner4d391482007-12-12 07:09:47 +0000406 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000407
408 if (AttrList)
409 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
410 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Douglas Gregor7723fec2011-12-15 20:29:51 +0000412 // Start the definition of this class. If we're in a redefinition case, there
413 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000414 if (!IDecl->hasDefinition())
415 IDecl->startDefinition();
416
Chris Lattner4d391482007-12-12 07:09:47 +0000417 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000418 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000419 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
420 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000421
422 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000423 // Try to correct for a typo in the superclass name without correcting
424 // to the class we're defining.
425 ObjCInterfaceValidatorCCC Validator(IDecl);
426 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000427 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000428 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000429 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
430 Diag(SuperLoc, diag::err_undef_superclass_suggest)
431 << SuperName << ClassName << PrevDecl->getDeclName();
432 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
433 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000434 }
435 }
436
Douglas Gregor60ef3082011-12-15 00:29:59 +0000437 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000438 Diag(SuperLoc, diag::err_recursive_superclass)
439 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000440 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000441 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000442 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000443 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000444
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000445 // Diagnose classes that inherit from deprecated classes.
446 if (SuperClassDecl)
447 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000448
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000449 if (PrevDecl && SuperClassDecl == 0) {
450 // The previous declaration was not a class decl. Check if we have a
451 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000452 if (const TypedefNameDecl *TDecl =
453 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000454 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000455 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000456 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
457 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000458 }
459 }
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000461 // This handles the following case:
462 //
463 // typedef int SuperClass;
464 // @interface MyClass : SuperClass {} @end
465 //
466 if (!SuperClassDecl) {
467 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
468 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000469 }
470 }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Richard Smith162e1c12011-04-15 14:24:37 +0000472 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000473 if (!SuperClassDecl)
474 Diag(SuperLoc, diag::err_undef_superclass)
475 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000476 else if (RequireCompleteType(SuperLoc,
477 Context.getObjCInterfaceType(SuperClassDecl),
478 PDiag(diag::err_forward_superclass)
479 << SuperClassDecl->getDeclName()
480 << ClassName
481 << SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000482 SuperClassDecl = 0;
483 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000484 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000485 IDecl->setSuperClass(SuperClassDecl);
486 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000487 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000488 }
Chris Lattner4d391482007-12-12 07:09:47 +0000489 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000490 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000491 }
Mike Stump1eb44332009-09-09 15:08:12 +0000492
Sebastian Redl0b17c612010-08-13 00:28:03 +0000493 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000494 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000495 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000496 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000497 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000498 }
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Anders Carlsson15281452008-11-04 16:57:32 +0000500 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000501 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000502}
503
504/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000505/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000506Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
507 IdentifierInfo *AliasName,
508 SourceLocation AliasLocation,
509 IdentifierInfo *ClassName,
510 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000511 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000512 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000513 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000514 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000515 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000516 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000517 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000518 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000519 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000520 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000521 }
522 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000523 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000524 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000525 if (const TypedefNameDecl *TDecl =
526 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000527 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000528 if (T->isObjCObjectType()) {
529 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000530 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000531 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000532 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000533 }
534 }
535 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000536 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
537 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000538 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000539 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000540 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000541 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000542 }
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000544 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000545 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000546 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Anders Carlsson15281452008-11-04 16:57:32 +0000548 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000549 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000550
John McCalld226f652010-08-21 09:40:31 +0000551 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000552}
553
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000554bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000555 IdentifierInfo *PName,
556 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000557 const ObjCList<ObjCProtocolDecl> &PList) {
558
559 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000560 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
561 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000562 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
563 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000564 if (PDecl->getIdentifier() == PName) {
565 Diag(Ploc, diag::err_protocol_has_circular_dependency);
566 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000567 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000568 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000569
570 if (!PDecl->hasDefinition())
571 continue;
572
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000573 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
574 PDecl->getLocation(), PDecl->getReferencedProtocols()))
575 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000576 }
577 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000578 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000579}
580
John McCalld226f652010-08-21 09:40:31 +0000581Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000582Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
583 IdentifierInfo *ProtocolName,
584 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000585 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000586 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000587 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000588 SourceLocation EndProtoLoc,
589 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000590 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000591 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000592 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000593 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
594 ForRedeclaration);
595 ObjCProtocolDecl *PDecl = 0;
596 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
597 // If we already have a definition, complain.
598 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
599 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Douglas Gregor27c6da22012-01-01 20:30:41 +0000601 // Create a new protocol that is completely distinct from previous
602 // declarations, and do not make this protocol available for name lookup.
603 // That way, we'll end up completely ignoring the duplicate.
604 // FIXME: Can we turn this into an error?
605 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
606 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000607 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000608 PDecl->startDefinition();
609 } else {
610 if (PrevDecl) {
611 // Check for circular dependencies among protocol declarations. This can
612 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000613 ObjCList<ObjCProtocolDecl> PList;
614 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
615 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000616 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000617 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000618
619 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000620 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000621 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000622 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000623
Douglas Gregor6e378de2009-04-23 23:18:26 +0000624 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000625 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000626 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000627
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000628 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000629 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000630
631 // Merge attributes from previous declarations.
632 if (PrevDecl)
633 mergeDeclAttributes(PDecl, PrevDecl);
634
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000635 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000636 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000637 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
638 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000639 }
Mike Stump1eb44332009-09-09 15:08:12 +0000640
641 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000642 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000643}
644
645/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000646/// issues an error if they are not declared. It returns list of
647/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000648void
Chris Lattnere13b9592008-07-26 04:03:38 +0000649Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000650 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000651 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000652 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000653 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000654 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
655 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000656 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000657 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000658 TypoCorrection Corrected = CorrectTypo(
659 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000660 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000661 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000662 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000663 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000664 Diag(PDecl->getLocation(), diag::note_previous_decl)
665 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000666 }
667 }
668
669 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000670 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000671 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000672 continue;
673 }
Mike Stump1eb44332009-09-09 15:08:12 +0000674
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000675 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000676
677 // If this is a forward declaration and we are supposed to warn in this
678 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000679 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000680 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000681 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000682 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000683 }
684}
685
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000686/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000687/// a class method in its extension.
688///
Mike Stump1eb44332009-09-09 15:08:12 +0000689void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000690 ObjCInterfaceDecl *ID) {
691 if (!ID)
692 return; // Possibly due to previous error
693
694 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000695 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
696 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000697 ObjCMethodDecl *MD = *i;
698 MethodMap[MD->getSelector()] = MD;
699 }
700
701 if (MethodMap.empty())
702 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000703 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
704 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000705 ObjCMethodDecl *Method = *i;
706 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
707 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
708 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
709 << Method->getDeclName();
710 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
711 }
712 }
713}
714
Chris Lattner58fe03b2009-04-12 08:43:13 +0000715/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000716Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000717Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000718 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000719 unsigned NumElts,
720 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000721 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000722 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000723 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000724 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
725 ForRedeclaration);
726 ObjCProtocolDecl *PDecl
727 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
728 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000729 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000730
731 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000732 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000733
Douglas Gregor3937f872012-01-01 20:33:24 +0000734 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000735 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000736
737 if (PrevDecl)
738 mergeDeclAttributes(PDecl, PrevDecl);
739
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000740 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000741 }
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000743 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000744}
745
John McCalld226f652010-08-21 09:40:31 +0000746Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000747ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
748 IdentifierInfo *ClassName, SourceLocation ClassLoc,
749 IdentifierInfo *CategoryName,
750 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000751 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000752 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000753 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000754 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000755 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000756 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000757
758 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000759
760 if (!IDecl
761 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
762 PDiag(diag::err_category_forward_interface)
763 << (CategoryName == 0))) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000764 // Create an invalid ObjCCategoryDecl to serve as context for
765 // the enclosing method declarations. We mark the decl invalid
766 // to make it clear that this isn't a valid AST.
767 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000768 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000769 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000770 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000771
772 if (!IDecl)
773 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000774 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000775 }
776
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000777 if (!CategoryName && IDecl->getImplementation()) {
778 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
779 Diag(IDecl->getImplementation()->getLocation(),
780 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000781 }
782
Fariborz Jahanian25760612010-02-15 21:55:26 +0000783 if (CategoryName) {
784 /// Check for duplicate interface declaration for this category
785 ObjCCategoryDecl *CDeclChain;
786 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
787 CDeclChain = CDeclChain->getNextClassCategory()) {
788 if (CDeclChain->getIdentifier() == CategoryName) {
789 // Class extensions can be declared multiple times.
790 Diag(CategoryLoc, diag::warn_dup_category_def)
791 << ClassName << CategoryName;
792 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
793 break;
794 }
Chris Lattner70f19542009-02-16 21:26:43 +0000795 }
796 }
Chris Lattner70f19542009-02-16 21:26:43 +0000797
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000798 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
799 ClassLoc, CategoryLoc, CategoryName, IDecl);
800 // FIXME: PushOnScopeChains?
801 CurContext->addDecl(CDecl);
802
Chris Lattner4d391482007-12-12 07:09:47 +0000803 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000804 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000805 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000806 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000807 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000808 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000809 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000810 }
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Anders Carlsson15281452008-11-04 16:57:32 +0000812 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000813 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000814}
815
816/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000817/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000818/// object.
John McCalld226f652010-08-21 09:40:31 +0000819Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000820 SourceLocation AtCatImplLoc,
821 IdentifierInfo *ClassName, SourceLocation ClassLoc,
822 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000823 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000824 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000825 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000826 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
827 if (!CatIDecl) {
828 // Category @implementation with no corresponding @interface.
829 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000830 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
831 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000832 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000833 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000834 }
835 }
836
Mike Stump1eb44332009-09-09 15:08:12 +0000837 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000838 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000839 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000840 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000841 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000842 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000843 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000844 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
845 diag::err_undef_interface)) {
846 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000847 }
Chris Lattner4d391482007-12-12 07:09:47 +0000848
Douglas Gregord0434102009-01-09 00:49:46 +0000849 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000850 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000851
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000852 // If the interface is deprecated/unavailable, warn/error about it.
853 if (IDecl)
854 DiagnoseUseOfDecl(IDecl, ClassLoc);
855
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000856 /// Check that CatName, category name, is not used in another implementation.
857 if (CatIDecl) {
858 if (CatIDecl->getImplementation()) {
859 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
860 << CatName;
861 Diag(CatIDecl->getImplementation()->getLocation(),
862 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000863 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000864 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000865 // Warn on implementating category of deprecated class under
866 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000867 DiagnoseObjCImplementedDeprecations(*this,
868 dyn_cast<NamedDecl>(IDecl),
869 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000870 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000871 }
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Anders Carlsson15281452008-11-04 16:57:32 +0000873 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000874 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000875}
876
John McCalld226f652010-08-21 09:40:31 +0000877Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000878 SourceLocation AtClassImplLoc,
879 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000880 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000881 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000882 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000883 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000884 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000885 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
886 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000887 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000888 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000889 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000890 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000891 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
892 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000893 } else {
894 // We did not find anything with the name ClassName; try to correct for
895 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000896 ObjCInterfaceValidatorCCC Validator;
897 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000898 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000899 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000900 // Suggest the (potentially) correct interface name. However, put the
901 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000902 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000903 // provide a code-modification hint or use the typo name for recovery,
904 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000905 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000906 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000907 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000908 << ClassName << CorrectedName;
909 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
910 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000911 IDecl = 0;
912 } else {
913 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
914 }
Chris Lattner4d391482007-12-12 07:09:47 +0000915 }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Chris Lattner4d391482007-12-12 07:09:47 +0000917 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000918 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000919 if (SuperClassname) {
920 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000921 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
922 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000923 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000924 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
925 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000926 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000927 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000928 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000929 if (SDecl && !SDecl->hasDefinition())
930 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000931 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000932 Diag(SuperClassLoc, diag::err_undef_superclass)
933 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000934 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000935 // This implementation and its interface do not have the same
936 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000937 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000938 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000939 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000940 }
941 }
942 }
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Chris Lattner4d391482007-12-12 07:09:47 +0000944 if (!IDecl) {
945 // Legacy case of @implementation with no corresponding @interface.
946 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000947
Mike Stump390b4cc2009-05-16 07:39:55 +0000948 // FIXME: Do we support attributes on the @implementation? If so we should
949 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000950 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000951 ClassName, /*PrevDecl=*/0, ClassLoc,
952 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000953 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000954 if (SDecl) {
955 IDecl->setSuperClass(SDecl);
956 IDecl->setSuperClassLoc(SuperClassLoc);
957 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
958 } else {
959 IDecl->setEndOfDefinitionLoc(ClassLoc);
960 }
961
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000962 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000963 } else {
964 // Mark the interface as being completed, even if it was just as
965 // @class ....;
966 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000967 if (!IDecl->hasDefinition())
968 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000969 }
Mike Stump1eb44332009-09-09 15:08:12 +0000970
971 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000972 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
973 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Anders Carlsson15281452008-11-04 16:57:32 +0000975 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000976 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Chris Lattner4d391482007-12-12 07:09:47 +0000978 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000979 if (IDecl->getImplementation()) {
980 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000981 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000982 Diag(IDecl->getImplementation()->getLocation(),
983 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000984 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000985 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000986 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000987 // Warn on implementating deprecated class under
988 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000989 DiagnoseObjCImplementedDeprecations(*this,
990 dyn_cast<NamedDecl>(IDecl),
991 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000992 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000993 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000994}
995
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +0000996Sema::DeclGroupPtrTy
997Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
998 SmallVector<Decl *, 64> DeclsInGroup;
999 DeclsInGroup.reserve(Decls.size() + 1);
1000
1001 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1002 Decl *Dcl = Decls[i];
1003 if (!Dcl)
1004 continue;
1005 if (Dcl->getDeclContext()->isFileContext())
1006 Dcl->setTopLevelDeclInObjCContainer();
1007 DeclsInGroup.push_back(Dcl);
1008 }
1009
1010 DeclsInGroup.push_back(ObjCImpDecl);
1011
1012 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1013}
1014
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001015void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1016 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001017 SourceLocation RBrace) {
1018 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001019 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001020 if (!IDecl)
1021 return;
1022 /// Check case of non-existing @interface decl.
1023 /// (legacy objective-c @implementation decl without an @interface decl).
1024 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001025 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001026 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001027 // Add ivar's to class's DeclContext.
1028 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001029 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001030 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001031 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001032 }
1033
Chris Lattner4d391482007-12-12 07:09:47 +00001034 return;
1035 }
1036 // If implementation has empty ivar list, just return.
1037 if (numIvars == 0)
1038 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Chris Lattner4d391482007-12-12 07:09:47 +00001040 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001041 if (LangOpts.ObjCNonFragileABI2) {
1042 if (ImpDecl->getSuperClass())
1043 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1044 for (unsigned i = 0; i < numIvars; i++) {
1045 ObjCIvarDecl* ImplIvar = ivars[i];
1046 if (const ObjCIvarDecl *ClsIvar =
1047 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1048 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1049 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1050 continue;
1051 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001052 // Instance ivar to Implementation's DeclContext.
1053 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001054 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001055 ImpDecl->addDecl(ImplIvar);
1056 }
1057 return;
1058 }
Chris Lattner4d391482007-12-12 07:09:47 +00001059 // Check interface's Ivar list against those in the implementation.
1060 // names and types must match.
1061 //
Chris Lattner4d391482007-12-12 07:09:47 +00001062 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001063 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001064 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1065 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001066 ObjCIvarDecl* ImplIvar = ivars[j++];
1067 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001068 assert (ImplIvar && "missing implementation ivar");
1069 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Steve Naroffca331292009-03-03 14:49:36 +00001071 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001072 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001073 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001074 << ImplIvar->getIdentifier()
1075 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001076 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001077 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1078 ImplIvar->getBitWidthValue(Context) !=
1079 ClsIvar->getBitWidthValue(Context)) {
1080 Diag(ImplIvar->getBitWidth()->getLocStart(),
1081 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1082 Diag(ClsIvar->getBitWidth()->getLocStart(),
1083 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001084 }
Steve Naroffca331292009-03-03 14:49:36 +00001085 // Make sure the names are identical.
1086 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001087 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001088 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001089 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001090 }
1091 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001092 }
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Chris Lattner609e4c72007-12-12 18:11:49 +00001094 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001095 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001096 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001097 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001098}
1099
Steve Naroff3c2eb662008-02-10 21:38:56 +00001100void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001101 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001102 // No point warning no definition of method which is 'unavailable'.
1103 if (method->hasAttr<UnavailableAttr>())
1104 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001105 if (!IncompleteImpl) {
1106 Diag(ImpLoc, diag::warn_incomplete_impl);
1107 IncompleteImpl = true;
1108 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001109 if (DiagID == diag::warn_unimplemented_protocol_method)
1110 Diag(ImpLoc, DiagID) << method->getDeclName();
1111 else
1112 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001113}
1114
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001115/// Determines if type B can be substituted for type A. Returns true if we can
1116/// guarantee that anything that the user will do to an object of type A can
1117/// also be done to an object of type B. This is trivially true if the two
1118/// types are the same, or if B is a subclass of A. It becomes more complex
1119/// in cases where protocols are involved.
1120///
1121/// Object types in Objective-C describe the minimum requirements for an
1122/// object, rather than providing a complete description of a type. For
1123/// example, if A is a subclass of B, then B* may refer to an instance of A.
1124/// The principle of substitutability means that we may use an instance of A
1125/// anywhere that we may use an instance of B - it will implement all of the
1126/// ivars of B and all of the methods of B.
1127///
1128/// This substitutability is important when type checking methods, because
1129/// the implementation may have stricter type definitions than the interface.
1130/// The interface specifies minimum requirements, but the implementation may
1131/// have more accurate ones. For example, a method may privately accept
1132/// instances of B, but only publish that it accepts instances of A. Any
1133/// object passed to it will be type checked against B, and so will implicitly
1134/// by a valid A*. Similarly, a method may return a subclass of the class that
1135/// it is declared as returning.
1136///
1137/// This is most important when considering subclassing. A method in a
1138/// subclass must accept any object as an argument that its superclass's
1139/// implementation accepts. It may, however, accept a more general type
1140/// without breaking substitutability (i.e. you can still use the subclass
1141/// anywhere that you can use the superclass, but not vice versa). The
1142/// converse requirement applies to return types: the return type for a
1143/// subclass method must be a valid object of the kind that the superclass
1144/// advertises, but it may be specified more accurately. This avoids the need
1145/// for explicit down-casting by callers.
1146///
1147/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001148static bool isObjCTypeSubstitutable(ASTContext &Context,
1149 const ObjCObjectPointerType *A,
1150 const ObjCObjectPointerType *B,
1151 bool rejectId) {
1152 // Reject a protocol-unqualified id.
1153 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001154
1155 // If B is a qualified id, then A must also be a qualified id and it must
1156 // implement all of the protocols in B. It may not be a qualified class.
1157 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1158 // stricter definition so it is not substitutable for id<A>.
1159 if (B->isObjCQualifiedIdType()) {
1160 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001161 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1162 QualType(B,0),
1163 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001164 }
1165
1166 /*
1167 // id is a special type that bypasses type checking completely. We want a
1168 // warning when it is used in one place but not another.
1169 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1170
1171
1172 // If B is a qualified id, then A must also be a qualified id (which it isn't
1173 // if we've got this far)
1174 if (B->isObjCQualifiedIdType()) return false;
1175 */
1176
1177 // Now we know that A and B are (potentially-qualified) class types. The
1178 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001179 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001180}
1181
John McCall10302c02010-10-28 02:34:38 +00001182static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1183 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1184}
1185
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001186static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001187 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001188 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001189 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001190 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001191 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001192 if (IsProtocolMethodDecl &&
1193 (MethodDecl->getObjCDeclQualifier() !=
1194 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001195 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001196 S.Diag(MethodImpl->getLocation(),
1197 (IsOverridingMode ?
1198 diag::warn_conflicting_overriding_ret_type_modifiers
1199 : diag::warn_conflicting_ret_type_modifiers))
1200 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001201 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1202 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1203 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1204 }
1205 else
1206 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001207 }
1208
John McCall10302c02010-10-28 02:34:38 +00001209 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001210 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001211 return true;
1212 if (!Warn)
1213 return false;
John McCall10302c02010-10-28 02:34:38 +00001214
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001215 unsigned DiagID =
1216 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1217 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001218
1219 // Mismatches between ObjC pointers go into a different warning
1220 // category, and sometimes they're even completely whitelisted.
1221 if (const ObjCObjectPointerType *ImplPtrTy =
1222 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1223 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001224 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001225 // Allow non-matching return types as long as they don't violate
1226 // the principle of substitutability. Specifically, we permit
1227 // return types that are subclasses of the declared return type,
1228 // or that are more-qualified versions of the declared type.
1229 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001230 return false;
John McCall10302c02010-10-28 02:34:38 +00001231
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001232 DiagID =
1233 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1234 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001235 }
1236 }
1237
1238 S.Diag(MethodImpl->getLocation(), DiagID)
1239 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001240 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001241 << MethodImpl->getResultType()
1242 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001243 S.Diag(MethodDecl->getLocation(),
1244 IsOverridingMode ? diag::note_previous_declaration
1245 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001246 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001247 return false;
John McCall10302c02010-10-28 02:34:38 +00001248}
1249
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001250static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001251 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001252 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001253 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001254 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001255 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001256 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001257 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001258 if (IsProtocolMethodDecl &&
1259 (ImplVar->getObjCDeclQualifier() !=
1260 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001261 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001262 if (IsOverridingMode)
1263 S.Diag(ImplVar->getLocation(),
1264 diag::warn_conflicting_overriding_param_modifiers)
1265 << getTypeRange(ImplVar->getTypeSourceInfo())
1266 << MethodImpl->getDeclName();
1267 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001268 diag::warn_conflicting_param_modifiers)
1269 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001270 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001271 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1272 << getTypeRange(IfaceVar->getTypeSourceInfo());
1273 }
1274 else
1275 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001276 }
1277
John McCall10302c02010-10-28 02:34:38 +00001278 QualType ImplTy = ImplVar->getType();
1279 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001280
John McCall10302c02010-10-28 02:34:38 +00001281 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001282 return true;
1283
1284 if (!Warn)
1285 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001286 unsigned DiagID =
1287 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1288 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001289
1290 // Mismatches between ObjC pointers go into a different warning
1291 // category, and sometimes they're even completely whitelisted.
1292 if (const ObjCObjectPointerType *ImplPtrTy =
1293 ImplTy->getAs<ObjCObjectPointerType>()) {
1294 if (const ObjCObjectPointerType *IfacePtrTy =
1295 IfaceTy->getAs<ObjCObjectPointerType>()) {
1296 // Allow non-matching argument types as long as they don't
1297 // violate the principle of substitutability. Specifically, the
1298 // implementation must accept any objects that the superclass
1299 // accepts, however it may also accept others.
1300 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001301 return false;
John McCall10302c02010-10-28 02:34:38 +00001302
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001303 DiagID =
1304 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1305 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001306 }
1307 }
1308
1309 S.Diag(ImplVar->getLocation(), DiagID)
1310 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001311 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1312 S.Diag(IfaceVar->getLocation(),
1313 (IsOverridingMode ? diag::note_previous_declaration
1314 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001315 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001316 return false;
John McCall10302c02010-10-28 02:34:38 +00001317}
John McCallf85e1932011-06-15 23:02:42 +00001318
1319/// In ARC, check whether the conventional meanings of the two methods
1320/// match. If they don't, it's a hard error.
1321static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1322 ObjCMethodDecl *decl) {
1323 ObjCMethodFamily implFamily = impl->getMethodFamily();
1324 ObjCMethodFamily declFamily = decl->getMethodFamily();
1325 if (implFamily == declFamily) return false;
1326
1327 // Since conventions are sorted by selector, the only possibility is
1328 // that the types differ enough to cause one selector or the other
1329 // to fall out of the family.
1330 assert(implFamily == OMF_None || declFamily == OMF_None);
1331
1332 // No further diagnostics required on invalid declarations.
1333 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1334
1335 const ObjCMethodDecl *unmatched = impl;
1336 ObjCMethodFamily family = declFamily;
1337 unsigned errorID = diag::err_arc_lost_method_convention;
1338 unsigned noteID = diag::note_arc_lost_method_convention;
1339 if (declFamily == OMF_None) {
1340 unmatched = decl;
1341 family = implFamily;
1342 errorID = diag::err_arc_gained_method_convention;
1343 noteID = diag::note_arc_gained_method_convention;
1344 }
1345
1346 // Indexes into a %select clause in the diagnostic.
1347 enum FamilySelector {
1348 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1349 };
1350 FamilySelector familySelector = FamilySelector();
1351
1352 switch (family) {
1353 case OMF_None: llvm_unreachable("logic error, no method convention");
1354 case OMF_retain:
1355 case OMF_release:
1356 case OMF_autorelease:
1357 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001358 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001359 case OMF_retainCount:
1360 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001361 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001362 // Mismatches for these methods don't change ownership
1363 // conventions, so we don't care.
1364 return false;
1365
1366 case OMF_init: familySelector = F_init; break;
1367 case OMF_alloc: familySelector = F_alloc; break;
1368 case OMF_copy: familySelector = F_copy; break;
1369 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1370 case OMF_new: familySelector = F_new; break;
1371 }
1372
1373 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1374 ReasonSelector reasonSelector;
1375
1376 // The only reason these methods don't fall within their families is
1377 // due to unusual result types.
1378 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1379 reasonSelector = R_UnrelatedReturn;
1380 } else {
1381 reasonSelector = R_NonObjectReturn;
1382 }
1383
1384 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1385 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1386
1387 return true;
1388}
John McCall10302c02010-10-28 02:34:38 +00001389
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001390void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001391 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001392 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001393 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001394 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1395 return;
1396
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001397 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001398 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001399 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Chris Lattner3aff9192009-04-11 19:58:42 +00001401 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001402 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001403 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001404 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001405 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001406 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001407
Fariborz Jahanian21121902011-08-08 18:03:17 +00001408 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001409 Diag(ImpMethodDecl->getLocation(),
1410 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001411 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001412 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001413}
1414
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001415void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1416 ObjCMethodDecl *Overridden,
1417 bool IsProtocolMethodDecl) {
1418
1419 CheckMethodOverrideReturn(*this, Method, Overridden,
1420 IsProtocolMethodDecl, true,
1421 true);
1422
1423 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
1424 IF = Overridden->param_begin(), EM = Method->param_end();
1425 IM != EM; ++IM, ++IF) {
1426 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1427 IsProtocolMethodDecl, true, true);
1428 }
1429
1430 if (Method->isVariadic() != Overridden->isVariadic()) {
1431 Diag(Method->getLocation(),
1432 diag::warn_conflicting_overriding_variadic);
1433 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1434 }
1435}
1436
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001437/// WarnExactTypedMethods - This routine issues a warning if method
1438/// implementation declaration matches exactly that of its declaration.
1439void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1440 ObjCMethodDecl *MethodDecl,
1441 bool IsProtocolMethodDecl) {
1442 // don't issue warning when protocol method is optional because primary
1443 // class is not required to implement it and it is safe for protocol
1444 // to implement it.
1445 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1446 return;
1447 // don't issue warning when primary class's method is
1448 // depecated/unavailable.
1449 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1450 MethodDecl->hasAttr<DeprecatedAttr>())
1451 return;
1452
1453 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1454 IsProtocolMethodDecl, false, false);
1455 if (match)
1456 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1457 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1458 IM != EM; ++IM, ++IF) {
1459 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1460 *IM, *IF,
1461 IsProtocolMethodDecl, false, false);
1462 if (!match)
1463 break;
1464 }
1465 if (match)
1466 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001467 if (match)
1468 match = !(MethodDecl->isClassMethod() &&
1469 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001470
1471 if (match) {
1472 Diag(ImpMethodDecl->getLocation(),
1473 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001474 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1475 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001476 }
1477}
1478
Mike Stump390b4cc2009-05-16 07:39:55 +00001479/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1480/// improve the efficiency of selector lookups and type checking by associating
1481/// with each protocol / interface / category the flattened instance tables. If
1482/// we used an immutable set to keep the table then it wouldn't add significant
1483/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001484
Steve Naroffefe7f362008-02-08 22:06:17 +00001485/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001486/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001487void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1488 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001489 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001490 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001491 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001492 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001493 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1494 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1495 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001496 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1497
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001498 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001499 ObjCInterfaceDecl *NSIDecl = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001500 if (getLangOpts().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001501 // check to see if class implements forwardInvocation method and objects
1502 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001503 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001504 // Under such conditions, which means that every method possible is
1505 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001506 // found" warnings.
1507 // FIXME: Use a general GetUnarySelector method for this.
1508 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1509 Selector fISelector = Context.Selectors.getSelector(1, &II);
1510 if (InsMap.count(fISelector))
1511 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1512 // need be implemented in the implementation.
1513 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1514 }
Mike Stump1eb44332009-09-09 15:08:12 +00001515
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001516 // If a method lookup fails locally we still need to look and see if
1517 // the method was implemented by a base class or an inherited
1518 // protocol. This lookup is slow, but occurs rarely in correct code
1519 // and otherwise would terminate in a warning.
1520
Chris Lattner4d391482007-12-12 07:09:47 +00001521 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001522 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001523 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001524 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001525 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001526 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001527 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001528 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001529 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001530 // If a method is not implemented in the category implementation but
1531 // has been declared in its primary class, superclass,
1532 // or in one of their protocols, no need to issue the warning.
1533 // This is because method will be implemented in the primary class
1534 // or one of its super class implementation.
1535
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001536 // Ugly, but necessary. Method declared in protcol might have
1537 // have been synthesized due to a property declared in the class which
1538 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001539 if (ObjCMethodDecl *MethodInClass =
1540 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001541 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001542 if (C || MethodInClass->isSynthesized())
1543 continue;
1544 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1545 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1546 != DiagnosticsEngine::Ignored) {
1547 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001548 Diag(method->getLocation(), diag::note_method_declared_at)
1549 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001550 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1551 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001552 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001553 }
1554 }
Chris Lattner4d391482007-12-12 07:09:47 +00001555 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001556 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001557 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001558 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001559 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001560 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1561 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001562 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001563 // See above comment for instance method lookups.
1564 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001565 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001566 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001567 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001568 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1569 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001570 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001571 Diag(method->getLocation(), diag::note_method_declared_at)
1572 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001573 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1574 PDecl->getDeclName();
1575 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001576 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001577 }
Chris Lattner780f3292008-07-21 21:32:27 +00001578 // Check on this protocols's referenced protocols, recursively.
1579 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1580 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001581 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001582}
1583
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001584/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001585/// or protocol against those declared in their implementations.
1586///
1587void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1588 const llvm::DenseSet<Selector> &ClsMap,
1589 llvm::DenseSet<Selector> &InsMapSeen,
1590 llvm::DenseSet<Selector> &ClsMapSeen,
1591 ObjCImplDecl* IMPDecl,
1592 ObjCContainerDecl* CDecl,
1593 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001594 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001595 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001596 // Check and see if instance methods in class interface have been
1597 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001598 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1599 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001600 if (InsMapSeen.count((*I)->getSelector()))
1601 continue;
1602 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001603 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001604 !InsMap.count((*I)->getSelector())) {
1605 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001606 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1607 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001608 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001609 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001610 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001611 IMPDecl->getInstanceMethod((*I)->getSelector());
1612 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1613 "Expected to find the method through lookup as well");
1614 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001615 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001616 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001617 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001618 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1619 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001620 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001621 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001622 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001623 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001624 }
1625 }
Mike Stump1eb44332009-09-09 15:08:12 +00001626
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001627 // Check and see if class methods in class interface have been
1628 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001629 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001630 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001631 if (ClsMapSeen.count((*I)->getSelector()))
1632 continue;
1633 ClsMapSeen.insert((*I)->getSelector());
1634 if (!ClsMap.count((*I)->getSelector())) {
1635 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001636 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1637 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001638 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001639 ObjCMethodDecl *ImpMethodDecl =
1640 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001641 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1642 "Expected to find the method through lookup as well");
1643 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001644 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001645 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1646 isa<ObjCProtocolDecl>(CDecl));
1647 else
1648 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001649 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001650 }
1651 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001652
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001653 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001654 // Also methods in class extensions need be looked at next.
1655 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1656 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1657 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1658 IMPDecl,
1659 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001660 IncompleteImpl, false,
1661 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001662
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001663 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001664 for (ObjCInterfaceDecl::all_protocol_iterator
1665 PI = I->all_referenced_protocol_begin(),
1666 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001667 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1668 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001669 (*PI), IncompleteImpl, false,
1670 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001671
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001672 // FIXME. For now, we are not checking for extact match of methods
1673 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001674 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001675 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001676 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001677 I->getSuperClass(), IncompleteImpl, false);
1678 }
1679}
1680
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001681/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1682/// category matches with those implemented in its primary class and
1683/// warns each time an exact match is found.
1684void Sema::CheckCategoryVsClassMethodMatches(
1685 ObjCCategoryImplDecl *CatIMPDecl) {
1686 llvm::DenseSet<Selector> InsMap, ClsMap;
1687
1688 for (ObjCImplementationDecl::instmeth_iterator
1689 I = CatIMPDecl->instmeth_begin(),
1690 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1691 InsMap.insert((*I)->getSelector());
1692
1693 for (ObjCImplementationDecl::classmeth_iterator
1694 I = CatIMPDecl->classmeth_begin(),
1695 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1696 ClsMap.insert((*I)->getSelector());
1697 if (InsMap.empty() && ClsMap.empty())
1698 return;
1699
1700 // Get category's primary class.
1701 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1702 if (!CatDecl)
1703 return;
1704 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1705 if (!IDecl)
1706 return;
1707 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1708 bool IncompleteImpl = false;
1709 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1710 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001711 IncompleteImpl, false,
1712 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001713}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001714
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001715void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001716 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001717 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001718 llvm::DenseSet<Selector> InsMap;
1719 // Check and see if instance methods in class interface have been
1720 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001721 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001722 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001723 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001724
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001725 // Check and see if properties declared in the interface have either 1)
1726 // an implementation or 2) there is a @synthesize/@dynamic implementation
1727 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001728 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1729 if (!(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2) ||
Ted Kremenek71207fc2012-01-05 22:47:47 +00001730 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001731 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001732
Chris Lattner4d391482007-12-12 07:09:47 +00001733 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001734 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001735 I = IMPDecl->classmeth_begin(),
1736 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001737 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001738
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001739 // Check for type conflict of methods declared in a class/protocol and
1740 // its implementation; if any.
1741 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001742 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1743 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001744 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001745
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001746 // check all methods implemented in category against those declared
1747 // in its primary class.
1748 if (ObjCCategoryImplDecl *CatDecl =
1749 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1750 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Chris Lattner4d391482007-12-12 07:09:47 +00001752 // Check the protocol list for unimplemented methods in the @implementation
1753 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001754 // Check and see if class methods in class interface have been
1755 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Chris Lattnercddc8882009-03-01 00:56:52 +00001757 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001758 for (ObjCInterfaceDecl::all_protocol_iterator
1759 PI = I->all_referenced_protocol_begin(),
1760 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001761 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001762 InsMap, ClsMap, I);
1763 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001764 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1765 Categories; Categories = Categories->getNextClassExtension())
1766 ImplMethodsVsClassMethods(S, IMPDecl,
1767 const_cast<ObjCCategoryDecl*>(Categories),
1768 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001769 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001770 // For extended class, unimplemented methods in its protocols will
1771 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001772 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001773 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1774 E = C->protocol_end(); PI != E; ++PI)
1775 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001776 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001777 // Report unimplemented properties in the category as well.
1778 // When reporting on missing setter/getters, do not report when
1779 // setter/getter is implemented in category's primary class
1780 // implementation.
1781 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1782 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1783 for (ObjCImplementationDecl::instmeth_iterator
1784 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1785 InsMap.insert((*I)->getSelector());
1786 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001787 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001788 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001789 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001790 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001791}
1792
Mike Stump1eb44332009-09-09 15:08:12 +00001793/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001794Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001795Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001796 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001797 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001798 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001799 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001800 for (unsigned i = 0; i != NumElts; ++i) {
1801 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001802 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001803 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001804 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001805 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001806 // Maybe we will complain about the shadowed template parameter.
1807 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1808 // Just pretend that we didn't see the previous declaration.
1809 PrevDecl = 0;
1810 }
1811
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001812 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001813 // GCC apparently allows the following idiom:
1814 //
1815 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1816 // @class XCElementToggler;
1817 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001818 // Here we have chosen to ignore the forward class declaration
1819 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001820 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001821 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001822 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001823 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001824 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001825 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001826 // to the underlying class. Just ignore the forward class with a warning
1827 // as this will force the intended behavior which is to lookup the typedef
1828 // name.
1829 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1830 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1831 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1832 continue;
1833 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001834 }
Chris Lattner4d391482007-12-12 07:09:47 +00001835 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001836
1837 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001838 ObjCInterfaceDecl *PrevIDecl
1839 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001840 ObjCInterfaceDecl *IDecl
1841 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001842 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001843 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001844
Douglas Gregor7723fec2011-12-15 20:29:51 +00001845 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001846 CheckObjCDeclScope(IDecl);
1847 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001848 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001849
1850 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001851}
1852
John McCall0f4c4c42011-06-16 01:15:19 +00001853static bool tryMatchRecordTypes(ASTContext &Context,
1854 Sema::MethodMatchStrategy strategy,
1855 const Type *left, const Type *right);
1856
John McCallf85e1932011-06-15 23:02:42 +00001857static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1858 QualType leftQT, QualType rightQT) {
1859 const Type *left =
1860 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1861 const Type *right =
1862 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1863
1864 if (left == right) return true;
1865
1866 // If we're doing a strict match, the types have to match exactly.
1867 if (strategy == Sema::MMS_strict) return false;
1868
1869 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1870
1871 // Otherwise, use this absurdly complicated algorithm to try to
1872 // validate the basic, low-level compatibility of the two types.
1873
1874 // As a minimum, require the sizes and alignments to match.
1875 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1876 return false;
1877
1878 // Consider all the kinds of non-dependent canonical types:
1879 // - functions and arrays aren't possible as return and parameter types
1880
1881 // - vector types of equal size can be arbitrarily mixed
1882 if (isa<VectorType>(left)) return isa<VectorType>(right);
1883 if (isa<VectorType>(right)) return false;
1884
1885 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001886 // - structs, unions, and Objective-C objects must match more-or-less
1887 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001888 // - everything else should be a scalar
1889 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001890 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001891
John McCall1d9b3b22011-09-09 05:25:32 +00001892 // Make scalars agree in kind, except count bools as chars, and group
1893 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001894 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1895 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1896 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1897 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001898 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1899 leftSK = Type::STK_ObjCObjectPointer;
1900 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1901 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001902
1903 // Note that data member pointers and function member pointers don't
1904 // intermix because of the size differences.
1905
1906 return (leftSK == rightSK);
1907}
Chris Lattner4d391482007-12-12 07:09:47 +00001908
John McCall0f4c4c42011-06-16 01:15:19 +00001909static bool tryMatchRecordTypes(ASTContext &Context,
1910 Sema::MethodMatchStrategy strategy,
1911 const Type *lt, const Type *rt) {
1912 assert(lt && rt && lt != rt);
1913
1914 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1915 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1916 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1917
1918 // Require union-hood to match.
1919 if (left->isUnion() != right->isUnion()) return false;
1920
1921 // Require an exact match if either is non-POD.
1922 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1923 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1924 return false;
1925
1926 // Require size and alignment to match.
1927 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1928
1929 // Require fields to match.
1930 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1931 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1932 for (; li != le && ri != re; ++li, ++ri) {
1933 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1934 return false;
1935 }
1936 return (li == le && ri == re);
1937}
1938
Chris Lattner4d391482007-12-12 07:09:47 +00001939/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1940/// returns true, or false, accordingly.
1941/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001942bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1943 const ObjCMethodDecl *right,
1944 MethodMatchStrategy strategy) {
1945 if (!matchTypes(Context, strategy,
1946 left->getResultType(), right->getResultType()))
1947 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001948
David Blaikie4e4d0842012-03-11 07:00:24 +00001949 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001950 (left->hasAttr<NSReturnsRetainedAttr>()
1951 != right->hasAttr<NSReturnsRetainedAttr>() ||
1952 left->hasAttr<NSConsumesSelfAttr>()
1953 != right->hasAttr<NSConsumesSelfAttr>()))
1954 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001955
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001956 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001957 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001958
John McCallf85e1932011-06-15 23:02:42 +00001959 for (; li != le; ++li, ++ri) {
1960 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001961 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001962
1963 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1964 return false;
1965
David Blaikie4e4d0842012-03-11 07:00:24 +00001966 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001967 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1968 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001969 }
1970 return true;
1971}
1972
Douglas Gregor5ac4b692012-01-25 00:49:42 +00001973void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00001974 // If the list is empty, make it a singleton list.
1975 if (List->Method == 0) {
1976 List->Method = Method;
1977 List->Next = 0;
1978 return;
1979 }
1980
1981 // We've seen a method with this name, see if we have already seen this type
1982 // signature.
1983 ObjCMethodList *Previous = List;
1984 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00001985 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00001986 continue;
1987
1988 ObjCMethodDecl *PrevObjCMethod = List->Method;
1989
1990 // Propagate the 'defined' bit.
1991 if (Method->isDefined())
1992 PrevObjCMethod->setDefined(true);
1993
1994 // If a method is deprecated, push it in the global pool.
1995 // This is used for better diagnostics.
1996 if (Method->isDeprecated()) {
1997 if (!PrevObjCMethod->isDeprecated())
1998 List->Method = Method;
1999 }
2000 // If new method is unavailable, push it into global pool
2001 // unless previous one is deprecated.
2002 if (Method->isUnavailable()) {
2003 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2004 List->Method = Method;
2005 }
2006
2007 return;
2008 }
2009
2010 // We have a new signature for an existing method - add it.
2011 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002012 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002013 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2014}
2015
Sebastian Redldb9d2142010-08-02 23:18:59 +00002016/// \brief Read the contents of the method pool for a given selector from
2017/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002018void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002019 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002020 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002021}
2022
Sebastian Redldb9d2142010-08-02 23:18:59 +00002023void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
2024 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002025 // Ignore methods of invalid containers.
2026 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
2027 return;
2028
Douglas Gregor0d266d62012-01-25 00:59:09 +00002029 if (ExternalSource)
2030 ReadMethodPool(Method->getSelector());
2031
Sebastian Redldb9d2142010-08-02 23:18:59 +00002032 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002033 if (Pos == MethodPool.end())
2034 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2035 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002036
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002037 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002038
Sebastian Redldb9d2142010-08-02 23:18:59 +00002039 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002040 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002041}
2042
John McCallf85e1932011-06-15 23:02:42 +00002043/// Determines if this is an "acceptable" loose mismatch in the global
2044/// method pool. This exists mostly as a hack to get around certain
2045/// global mismatches which we can't afford to make warnings / errors.
2046/// Really, what we want is a way to take a method out of the global
2047/// method pool.
2048static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2049 ObjCMethodDecl *other) {
2050 if (!chosen->isInstanceMethod())
2051 return false;
2052
2053 Selector sel = chosen->getSelector();
2054 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2055 return false;
2056
2057 // Don't complain about mismatches for -length if the method we
2058 // chose has an integral result type.
2059 return (chosen->getResultType()->isIntegerType());
2060}
2061
Sebastian Redldb9d2142010-08-02 23:18:59 +00002062ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002063 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002064 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002065 if (ExternalSource)
2066 ReadMethodPool(Sel);
2067
Sebastian Redldb9d2142010-08-02 23:18:59 +00002068 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002069 if (Pos == MethodPool.end())
2070 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002071
Sebastian Redldb9d2142010-08-02 23:18:59 +00002072 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Sebastian Redldb9d2142010-08-02 23:18:59 +00002074 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002075 bool issueDiagnostic = false, issueError = false;
2076
2077 // We support a warning which complains about *any* difference in
2078 // method signature.
2079 bool strictSelectorMatch =
2080 (receiverIdOrClass && warn &&
2081 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2082 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002083 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002084 if (strictSelectorMatch)
2085 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002086 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2087 MMS_strict)) {
2088 issueDiagnostic = true;
2089 break;
2090 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002091 }
2092
John McCallf85e1932011-06-15 23:02:42 +00002093 // If we didn't see any strict differences, we won't see any loose
2094 // differences. In ARC, however, we also need to check for loose
2095 // mismatches, because most of them are errors.
2096 if (!strictSelectorMatch ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002097 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002098 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002099 // This checks if the methods differ in type mismatch.
2100 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2101 MMS_loose) &&
2102 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2103 issueDiagnostic = true;
David Blaikie4e4d0842012-03-11 07:00:24 +00002104 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002105 issueError = true;
2106 break;
2107 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002108 }
2109
John McCallf85e1932011-06-15 23:02:42 +00002110 if (issueDiagnostic) {
2111 if (issueError)
2112 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2113 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002114 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2115 else
2116 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002117
2118 Diag(MethList.Method->getLocStart(),
2119 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002120 << MethList.Method->getSourceRange();
2121 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2122 Diag(Next->Method->getLocStart(), diag::note_also_found)
2123 << Next->Method->getSourceRange();
2124 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002125 }
2126 return MethList.Method;
2127}
2128
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002129ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002130 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2131 if (Pos == MethodPool.end())
2132 return 0;
2133
2134 GlobalMethods &Methods = Pos->second;
2135
2136 if (Methods.first.Method && Methods.first.Method->isDefined())
2137 return Methods.first.Method;
2138 if (Methods.second.Method && Methods.second.Method->isDefined())
2139 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002140 return 0;
2141}
2142
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002143/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2144/// identical selector names in current and its super classes and issues
2145/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002146void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2147 ObjCMethodDecl *Method,
2148 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002149 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2150 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002151
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002152 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002153 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002154 SD->lookupMethod(Method->getSelector(), IsInstance);
2155 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002156 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002157 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002158 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002159 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2160 E = Method->param_end();
2161 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2162 for (; ParamI != E; ++ParamI, ++PrevI) {
2163 // Number of parameters are the same and is guaranteed by selector match.
2164 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2165 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2166 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002167 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002168 // respective argument type in the super class method, issue warning;
2169 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002170 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002171 << T1 << T2;
2172 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2173 return;
2174 }
2175 }
2176 ID = SD;
2177 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002178}
2179
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002180/// DiagnoseDuplicateIvars -
2181/// Check for duplicate ivars in the entire class at the start of
2182/// @implementation. This becomes necesssary because class extension can
2183/// add ivars to a class in random order which will not be known until
2184/// class's @implementation is seen.
2185void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2186 ObjCInterfaceDecl *SID) {
2187 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2188 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2189 ObjCIvarDecl* Ivar = (*IVI);
2190 if (Ivar->isInvalidDecl())
2191 continue;
2192 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2193 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2194 if (prevIvar) {
2195 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2196 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2197 Ivar->setInvalidDecl();
2198 }
2199 }
2200 }
2201}
2202
Erik Verbruggend64251f2011-12-06 09:25:23 +00002203Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2204 switch (CurContext->getDeclKind()) {
2205 case Decl::ObjCInterface:
2206 return Sema::OCK_Interface;
2207 case Decl::ObjCProtocol:
2208 return Sema::OCK_Protocol;
2209 case Decl::ObjCCategory:
2210 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2211 return Sema::OCK_ClassExtension;
2212 else
2213 return Sema::OCK_Category;
2214 case Decl::ObjCImplementation:
2215 return Sema::OCK_Implementation;
2216 case Decl::ObjCCategoryImpl:
2217 return Sema::OCK_CategoryImplementation;
2218
2219 default:
2220 return Sema::OCK_None;
2221 }
2222}
2223
Steve Naroffa56f6162007-12-18 01:30:32 +00002224// Note: For class/category implemenations, allMethods/allProperties is
2225// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002226Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2227 Decl **allMethods, unsigned allNum,
2228 Decl **allProperties, unsigned pNum,
2229 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002230
Erik Verbruggend64251f2011-12-06 09:25:23 +00002231 if (getObjCContainerKind() == Sema::OCK_None)
2232 return 0;
2233
2234 assert(AtEnd.isValid() && "Invalid location for '@end'");
2235
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002236 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2237 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002238
Mike Stump1eb44332009-09-09 15:08:12 +00002239 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002240 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2241 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002242 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002243
Steve Naroff0701bbb2009-01-08 17:28:14 +00002244 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2245 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2246 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2247
Chris Lattner4d391482007-12-12 07:09:47 +00002248 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002249 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002250 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002251
2252 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002253 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002254 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002255 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002256 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002257 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002258 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002259 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002260 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002261 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002262 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002263 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002264 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002265 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002266 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002267 if (!Context.getSourceManager().isInSystemHeader(
2268 Method->getLocation()))
2269 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2270 << Method->getDeclName();
2271 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2272 }
Chris Lattner4d391482007-12-12 07:09:47 +00002273 InsMap[Method->getSelector()] = Method;
2274 /// The following allows us to typecheck messages to "id".
2275 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002276 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002277 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002278 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002279 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002280 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002281 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002282 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002283 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002284 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002285 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002286 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002287 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002288 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002289 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002290 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002291 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002292 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002293 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002294 if (!Context.getSourceManager().isInSystemHeader(
2295 Method->getLocation()))
2296 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2297 << Method->getDeclName();
2298 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2299 }
Chris Lattner4d391482007-12-12 07:09:47 +00002300 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002301 /// The following allows us to typecheck messages to "Class".
2302 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002303 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002304 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002305 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002306 }
2307 }
2308 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002309 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002310 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002311 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002312 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002313 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002314 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002315 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002316 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002317 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002318
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002319 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002320 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002321 if (C->IsClassExtension()) {
2322 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2323 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002324 }
Chris Lattner4d391482007-12-12 07:09:47 +00002325 }
Steve Naroff09c47192009-01-09 15:36:25 +00002326 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002327 if (CDecl->getIdentifier())
2328 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2329 // user-defined setter/getter. It also synthesizes setter/getter methods
2330 // and adds them to the DeclContext and global method pools.
2331 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2332 E = CDecl->prop_end();
2333 I != E; ++I)
2334 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002335 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002336 }
2337 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002338 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002339 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002340 // Any property declared in a class extension might have user
2341 // declared setter or getter in current class extension or one
2342 // of the other class extensions. Mark them as synthesized as
2343 // property will be synthesized when property with same name is
2344 // seen in the @implementation.
2345 for (const ObjCCategoryDecl *ClsExtDecl =
2346 IDecl->getFirstClassExtension();
2347 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2348 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2349 E = ClsExtDecl->prop_end(); I != E; ++I) {
2350 ObjCPropertyDecl *Property = (*I);
2351 // Skip over properties declared @dynamic
2352 if (const ObjCPropertyImplDecl *PIDecl
2353 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2354 if (PIDecl->getPropertyImplementation()
2355 == ObjCPropertyImplDecl::Dynamic)
2356 continue;
2357
2358 for (const ObjCCategoryDecl *CExtDecl =
2359 IDecl->getFirstClassExtension();
2360 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2361 if (ObjCMethodDecl *GetterMethod =
2362 CExtDecl->getInstanceMethod(Property->getGetterName()))
2363 GetterMethod->setSynthesized(true);
2364 if (!Property->isReadOnly())
2365 if (ObjCMethodDecl *SetterMethod =
2366 CExtDecl->getInstanceMethod(Property->getSetterName()))
2367 SetterMethod->setSynthesized(true);
2368 }
2369 }
2370 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002371 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002372 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002373 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002374
Patrick Beardb2f68202012-04-06 18:12:22 +00002375 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2376 if (IDecl->getSuperClass() == NULL) {
2377 // This class has no superclass, so check that it has been marked with
2378 // __attribute((objc_root_class)).
2379 if (!HasRootClassAttr) {
2380 SourceLocation DeclLoc(IDecl->getLocation());
2381 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2382 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2383 << IDecl->getIdentifier();
2384 // See if NSObject is in the current scope, and if it is, suggest
2385 // adding " : NSObject " to the class declaration.
2386 NamedDecl *IF = LookupSingleName(TUScope,
2387 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2388 DeclLoc, LookupOrdinaryName);
2389 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2390 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2391 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2392 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2393 } else {
2394 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2395 }
2396 }
2397 } else if (HasRootClassAttr) {
2398 // Complain that only root classes may have this attribute.
2399 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2400 }
2401
2402 if (LangOpts.ObjCNonFragileABI2) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002403 while (IDecl->getSuperClass()) {
2404 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2405 IDecl = IDecl->getSuperClass();
2406 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002407 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002408 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002409 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002410 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002411 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002412 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002413
Chris Lattner4d391482007-12-12 07:09:47 +00002414 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002415 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002416 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002417 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002418 Categories; Categories = Categories->getNextClassCategory()) {
2419 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002420 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002421 break;
2422 }
2423 }
2424 }
2425 }
Chris Lattner682bf922009-03-29 16:50:03 +00002426 if (isInterfaceDeclKind) {
2427 // Reject invalid vardecls.
2428 for (unsigned i = 0; i != tuvNum; i++) {
2429 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2430 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2431 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002432 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002433 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002434 }
Chris Lattner682bf922009-03-29 16:50:03 +00002435 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002436 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002437 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002438
2439 for (unsigned i = 0; i != tuvNum; i++) {
2440 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002441 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2442 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002443 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2444 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002445
2446 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002447}
2448
2449
2450/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2451/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002452static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002453CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002454 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002455}
2456
Ted Kremenek422bae72010-04-18 04:59:38 +00002457static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002458bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2459 const AttrVec &A) {
2460 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002461 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002462 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002463 return false;
2464
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002465 // method declared in interface has no attribute.
2466 // But implementation has attributes. This is invalid
2467 if (!IMD->hasAttrs())
2468 return true;
2469
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002470 const AttrVec &D = IMD->getAttrs();
2471 if (D.size() != A.size())
2472 return true;
2473
2474 // attributes on method declaration and definition must match exactly.
2475 // Note that we have at most a couple of attributes on methods, so this
2476 // n*n search is good enough.
2477 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2478 bool match = false;
2479 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2480 if ((*i)->getKind() == (*i1)->getKind()) {
2481 match = true;
2482 break;
2483 }
2484 }
2485 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002486 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002487 }
Sean Huntcf807c42010-08-18 23:23:40 +00002488 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002489}
2490
Douglas Gregore97179c2011-09-08 01:46:34 +00002491namespace {
2492 /// \brief Describes the compatibility of a result type with its method.
2493 enum ResultTypeCompatibilityKind {
2494 RTC_Compatible,
2495 RTC_Incompatible,
2496 RTC_Unknown
2497 };
2498}
2499
Douglas Gregor926df6c2011-06-11 01:09:30 +00002500/// \brief Check whether the declared result type of the given Objective-C
2501/// method declaration is compatible with the method's class.
2502///
Douglas Gregore97179c2011-09-08 01:46:34 +00002503static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002504CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2505 ObjCInterfaceDecl *CurrentClass) {
2506 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002507
2508 // If an Objective-C method inherits its related result type, then its
2509 // declared result type must be compatible with its own class type. The
2510 // declared result type is compatible if:
2511 if (const ObjCObjectPointerType *ResultObjectType
2512 = ResultType->getAs<ObjCObjectPointerType>()) {
2513 // - it is id or qualified id, or
2514 if (ResultObjectType->isObjCIdType() ||
2515 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002516 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002517
2518 if (CurrentClass) {
2519 if (ObjCInterfaceDecl *ResultClass
2520 = ResultObjectType->getInterfaceDecl()) {
2521 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002522 if (declaresSameEntity(CurrentClass, ResultClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002523 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002524
2525 // - it is a superclass of the method's class type
2526 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002527 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002528 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002529 } else {
2530 // Any Objective-C pointer type might be acceptable for a protocol
2531 // method; we just don't know.
2532 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002533 }
2534 }
2535
Douglas Gregore97179c2011-09-08 01:46:34 +00002536 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002537}
2538
John McCall6c2c2502011-07-22 02:45:48 +00002539namespace {
2540/// A helper class for searching for methods which a particular method
2541/// overrides.
2542class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002543public:
John McCall6c2c2502011-07-22 02:45:48 +00002544 Sema &S;
2545 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002546 llvm::SmallPtrSet<ObjCContainerDecl*, 128> Searched;
2547 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002548 bool Recursive;
2549
2550public:
2551 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2552 Selector selector = method->getSelector();
2553
2554 // Bypass this search if we've never seen an instance/class method
2555 // with this selector before.
2556 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2557 if (it == S.MethodPool.end()) {
2558 if (!S.ExternalSource) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002559 S.ReadMethodPool(selector);
2560
2561 it = S.MethodPool.find(selector);
2562 if (it == S.MethodPool.end())
2563 return;
John McCall6c2c2502011-07-22 02:45:48 +00002564 }
2565 ObjCMethodList &list =
2566 method->isInstanceMethod() ? it->second.first : it->second.second;
2567 if (!list.Method) return;
2568
2569 ObjCContainerDecl *container
2570 = cast<ObjCContainerDecl>(method->getDeclContext());
2571
2572 // Prevent the search from reaching this container again. This is
2573 // important with categories, which override methods from the
2574 // interface and each other.
2575 Searched.insert(container);
2576 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002577 }
John McCall6c2c2502011-07-22 02:45:48 +00002578
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002579 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002580 iterator begin() const { return Overridden.begin(); }
2581 iterator end() const { return Overridden.end(); }
2582
2583private:
2584 void searchFromContainer(ObjCContainerDecl *container) {
2585 if (container->isInvalidDecl()) return;
2586
2587 switch (container->getDeclKind()) {
2588#define OBJCCONTAINER(type, base) \
2589 case Decl::type: \
2590 searchFrom(cast<type##Decl>(container)); \
2591 break;
2592#define ABSTRACT_DECL(expansion)
2593#define DECL(type, base) \
2594 case Decl::type:
2595#include "clang/AST/DeclNodes.inc"
2596 llvm_unreachable("not an ObjC container!");
2597 }
2598 }
2599
2600 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002601 if (!protocol->hasDefinition())
2602 return;
2603
John McCall6c2c2502011-07-22 02:45:48 +00002604 // A method in a protocol declaration overrides declarations from
2605 // referenced ("parent") protocols.
2606 search(protocol->getReferencedProtocols());
2607 }
2608
2609 void searchFrom(ObjCCategoryDecl *category) {
2610 // A method in a category declaration overrides declarations from
2611 // the main class and from protocols the category references.
2612 search(category->getClassInterface());
2613 search(category->getReferencedProtocols());
2614 }
2615
2616 void searchFrom(ObjCCategoryImplDecl *impl) {
2617 // A method in a category definition that has a category
2618 // declaration overrides declarations from the category
2619 // declaration.
2620 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2621 search(category);
2622
2623 // Otherwise it overrides declarations from the class.
2624 } else {
2625 search(impl->getClassInterface());
2626 }
2627 }
2628
2629 void searchFrom(ObjCInterfaceDecl *iface) {
2630 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002631 if (!iface->hasDefinition())
2632 return;
2633
John McCall6c2c2502011-07-22 02:45:48 +00002634 // - categories,
2635 for (ObjCCategoryDecl *category = iface->getCategoryList();
2636 category; category = category->getNextClassCategory())
2637 search(category);
2638
2639 // - the super class, and
2640 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2641 search(super);
2642
2643 // - any referenced protocols.
2644 search(iface->getReferencedProtocols());
2645 }
2646
2647 void searchFrom(ObjCImplementationDecl *impl) {
2648 // A method in a class implementation overrides declarations from
2649 // the class interface.
2650 search(impl->getClassInterface());
2651 }
2652
2653
2654 void search(const ObjCProtocolList &protocols) {
2655 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2656 i != e; ++i)
2657 search(*i);
2658 }
2659
2660 void search(ObjCContainerDecl *container) {
2661 // Abort if we've already searched this container.
2662 if (!Searched.insert(container)) return;
2663
2664 // Check for a method in this container which matches this selector.
2665 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2666 Method->isInstanceMethod());
2667
2668 // If we find one, record it and bail out.
2669 if (meth) {
2670 Overridden.insert(meth);
2671 return;
2672 }
2673
2674 // Otherwise, search for methods that a hypothetical method here
2675 // would have overridden.
2676
2677 // Note that we're now in a recursive case.
2678 Recursive = true;
2679
2680 searchFromContainer(container);
2681 }
2682};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002683}
2684
John McCalld226f652010-08-21 09:40:31 +00002685Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002686 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002687 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002688 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002689 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002690 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002691 Selector Sel,
2692 // optional arguments. The number of types/arguments is obtained
2693 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002694 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002695 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002696 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002697 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002698 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002699 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002700 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002701 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002702 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002703 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2704 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002705 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002706
Douglas Gregore97179c2011-09-08 01:46:34 +00002707 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002708 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002709 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002710 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002711
Steve Naroffccef3712009-02-20 22:59:16 +00002712 // Methods cannot return interface types. All ObjC objects are
2713 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002714 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002715 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2716 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002717 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002718 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002719
2720 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002721 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002722 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002723 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002724 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002725 }
Mike Stump1eb44332009-09-09 15:08:12 +00002726
2727 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002728 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002729 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002730 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002731 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002732 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002733 /*isSynthesized=*/false,
2734 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002735 MethodDeclKind == tok::objc_optional
2736 ? ObjCMethodDecl::Optional
2737 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002738 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002739
Chris Lattner5f9e2722011-07-23 10:55:15 +00002740 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002741
Chris Lattner7db638d2009-04-11 19:42:43 +00002742 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002743 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002744 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002745
Chris Lattnere294d3f2009-04-11 18:57:04 +00002746 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002747 ArgType = Context.getObjCIdType();
2748 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002749 } else {
John McCall58e46772009-10-23 21:48:59 +00002750 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002751 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002752 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002753 }
Mike Stump1eb44332009-09-09 15:08:12 +00002754
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002755 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2756 LookupOrdinaryName, ForRedeclaration);
2757 LookupName(R, S);
2758 if (R.isSingleResult()) {
2759 NamedDecl *PrevDecl = R.getFoundDecl();
2760 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002761 Diag(ArgInfo[i].NameLoc,
2762 (MethodDefinition ? diag::warn_method_param_redefinition
2763 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002764 << ArgInfo[i].Name;
2765 Diag(PrevDecl->getLocation(),
2766 diag::note_previous_declaration);
2767 }
2768 }
2769
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002770 SourceLocation StartLoc = DI
2771 ? DI->getTypeLoc().getBeginLoc()
2772 : ArgInfo[i].NameLoc;
2773
John McCall81ef3e62011-04-23 02:46:06 +00002774 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2775 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2776 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002777
John McCall70798862011-05-02 00:30:12 +00002778 Param->setObjCMethodScopeInfo(i);
2779
Chris Lattner0ed844b2008-04-04 06:12:32 +00002780 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002781 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002782
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002783 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002784 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002785
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002786 if (Param->hasAttr<BlocksAttr>()) {
2787 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2788 Param->setInvalidDecl();
2789 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002790 S->AddDecl(Param);
2791 IdResolver.AddDecl(Param);
2792
Chris Lattner0ed844b2008-04-04 06:12:32 +00002793 Params.push_back(Param);
2794 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002795
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002796 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002797 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002798 QualType ArgType = Param->getType();
2799 if (ArgType.isNull())
2800 ArgType = Context.getObjCIdType();
2801 else
2802 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002803 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002804 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002805 Diag(Param->getLocation(),
2806 diag::err_object_cannot_be_passed_returned_by_value)
2807 << 1 << ArgType;
2808 Param->setInvalidDecl();
2809 }
2810 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002811
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002812 Params.push_back(Param);
2813 }
2814
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002815 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002816 ObjCMethod->setObjCDeclQualifier(
2817 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002818
2819 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002820 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002821
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002822 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002823 const ObjCMethodDecl *PrevMethod = 0;
2824 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002825 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002826 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2827 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002828 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002829 PrevMethod = ImpDecl->getClassMethod(Sel);
2830 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002831 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002832
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002833 ObjCMethodDecl *IMD = 0;
2834 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2835 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2836 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002837 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002838 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002839 SourceLocation MethodLoc = IMD->getLocation();
2840 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2841 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002842 Diag(MethodLoc, diag::note_method_declared_at)
2843 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002844 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002845 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002846 } else {
2847 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002848 }
John McCall6c2c2502011-07-22 02:45:48 +00002849
Chris Lattner4d391482007-12-12 07:09:47 +00002850 if (PrevMethod) {
2851 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002852 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002853 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002854 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002855 }
John McCall54abf7d2009-11-04 02:18:39 +00002856
Douglas Gregor926df6c2011-06-11 01:09:30 +00002857 // If this Objective-C method does not have a related result type, but we
2858 // are allowed to infer related result types, try to do so based on the
2859 // method family.
2860 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2861 if (!CurrentClass) {
2862 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2863 CurrentClass = Cat->getClassInterface();
2864 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2865 CurrentClass = Impl->getClassInterface();
2866 else if (ObjCCategoryImplDecl *CatImpl
2867 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2868 CurrentClass = CatImpl->getClassInterface();
2869 }
John McCall6c2c2502011-07-22 02:45:48 +00002870
Douglas Gregore97179c2011-09-08 01:46:34 +00002871 ResultTypeCompatibilityKind RTC
2872 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002873
2874 // Search for overridden methods and merge information down from them.
2875 OverrideSearch overrides(*this, ObjCMethod);
2876 for (OverrideSearch::iterator
2877 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2878 ObjCMethodDecl *overridden = *i;
2879
2880 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002881 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002882 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002883
2884 // Then merge the declarations.
2885 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002886
2887 // Check for overriding methods
2888 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00002889 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2890 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2891 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
Douglas Gregor926df6c2011-06-11 01:09:30 +00002892 }
2893
John McCallf85e1932011-06-15 23:02:42 +00002894 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002895 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002896 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2897
Douglas Gregore97179c2011-09-08 01:46:34 +00002898 // Infer the related result type when possible.
2899 if (!ARCError && RTC == RTC_Compatible &&
2900 !ObjCMethod->hasRelatedResultType() &&
2901 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002902 bool InferRelatedResultType = false;
2903 switch (ObjCMethod->getMethodFamily()) {
2904 case OMF_None:
2905 case OMF_copy:
2906 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002907 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002908 case OMF_mutableCopy:
2909 case OMF_release:
2910 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002911 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002912 break;
2913
2914 case OMF_alloc:
2915 case OMF_new:
2916 InferRelatedResultType = ObjCMethod->isClassMethod();
2917 break;
2918
2919 case OMF_init:
2920 case OMF_autorelease:
2921 case OMF_retain:
2922 case OMF_self:
2923 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2924 break;
2925 }
2926
John McCall6c2c2502011-07-22 02:45:48 +00002927 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002928 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002929 }
2930
John McCalld226f652010-08-21 09:40:31 +00002931 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002932}
2933
Chris Lattnercc98eac2008-12-17 07:13:27 +00002934bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002935 // Following is also an error. But it is caused by a missing @end
2936 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00002937 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002938 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00002939
2940 // If we switched context to translation unit while we are still lexically in
2941 // an objc container, it means the parser missed emitting an error.
2942 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
2943 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002944
Anders Carlsson15281452008-11-04 16:57:32 +00002945 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2946 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002947
Anders Carlsson15281452008-11-04 16:57:32 +00002948 return true;
2949}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002950
Chris Lattnercc98eac2008-12-17 07:13:27 +00002951/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2952/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002953void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002954 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002955 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002956 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002957 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002958 if (!Class) {
2959 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2960 return;
2961 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002962 if (LangOpts.ObjCNonFragileABI) {
2963 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2964 return;
2965 }
Mike Stump1eb44332009-09-09 15:08:12 +00002966
Chris Lattnercc98eac2008-12-17 07:13:27 +00002967 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002968 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002969 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002970 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002971 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002972 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002973 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002974 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2975 /*FIXME: StartL=*/ID->getLocation(),
2976 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002977 ID->getIdentifier(), ID->getType(),
2978 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002979 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002980 }
Mike Stump1eb44332009-09-09 15:08:12 +00002981
Chris Lattnercc98eac2008-12-17 07:13:27 +00002982 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002983 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002984 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002985 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00002986 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00002987 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002988 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002989 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002990 }
2991}
2992
Douglas Gregor160b5632010-04-26 17:32:49 +00002993/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002994VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2995 SourceLocation StartLoc,
2996 SourceLocation IdLoc,
2997 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002998 bool Invalid) {
2999 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
3000 // duration shall not be qualified by an address-space qualifier."
3001 // Since all parameters have automatic store duration, they can not have
3002 // an address space.
3003 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003004 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003005 Invalid = true;
3006 }
3007
3008 // An @catch parameter must be an unqualified object pointer type;
3009 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3010 if (Invalid) {
3011 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003012 } else if (T->isDependentType()) {
3013 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003014 } else if (!T->isObjCObjectPointerType()) {
3015 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003016 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003017 } else if (T->isObjCQualifiedIdType()) {
3018 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003019 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003020 }
3021
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003022 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3023 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003024 New->setExceptionVariable(true);
3025
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003026 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003027 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003028 Invalid = true;
3029
Douglas Gregor160b5632010-04-26 17:32:49 +00003030 if (Invalid)
3031 New->setInvalidDecl();
3032 return New;
3033}
3034
John McCalld226f652010-08-21 09:40:31 +00003035Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003036 const DeclSpec &DS = D.getDeclSpec();
3037
3038 // We allow the "register" storage class on exception variables because
3039 // GCC did, but we drop it completely. Any other storage class is an error.
3040 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3041 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3042 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3043 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3044 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3045 << DS.getStorageClassSpec();
3046 }
3047 if (D.getDeclSpec().isThreadSpecified())
3048 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3049 D.getMutableDeclSpec().ClearStorageClassSpecs();
3050
3051 DiagnoseFunctionSpecifiers(D);
3052
3053 // Check that there are no default arguments inside the type of this
3054 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003055 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003056 CheckExtraCXXDefaultArguments(D);
3057
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003058 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003059 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003060
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003061 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3062 D.getSourceRange().getBegin(),
3063 D.getIdentifierLoc(),
3064 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003065 D.isInvalidType());
3066
3067 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3068 if (D.getCXXScopeSpec().isSet()) {
3069 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3070 << D.getCXXScopeSpec().getRange();
3071 New->setInvalidDecl();
3072 }
3073
3074 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003075 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003076 if (D.getIdentifier())
3077 IdResolver.AddDecl(New);
3078
3079 ProcessDeclAttributes(S, New, D);
3080
3081 if (New->hasAttr<BlocksAttr>())
3082 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003083 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003084}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003085
3086/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003087/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003088void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003089 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003090 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3091 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003092 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003093 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003094 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003095 }
3096}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003097
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003098void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003099 // Load referenced selectors from the external source.
3100 if (ExternalSource) {
3101 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3102 ExternalSource->ReadReferencedSelectors(Sels);
3103 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3104 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3105 }
3106
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003107 // Warning will be issued only when selector table is
3108 // generated (which means there is at lease one implementation
3109 // in the TU). This is to match gcc's behavior.
3110 if (ReferencedSelectors.empty() ||
3111 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003112 return;
3113 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3114 ReferencedSelectors.begin(),
3115 E = ReferencedSelectors.end(); S != E; ++S) {
3116 Selector Sel = (*S).first;
3117 if (!LookupImplementedMethodInGlobalPool(Sel))
3118 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3119 }
3120 return;
3121}