blob: 33047b2e5ebd051fca5e3da2f638b4f978719c45 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000017#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000019#include "clang/AST/ASTConsumer.h"
Steve Naroffca331292009-03-03 14:49:36 +000020#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/AST/ExprObjC.h"
Chris Lattner4d391482007-12-12 07:09:47 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +000024#include "clang/AST/ASTMutationListener.h"
John McCallf85e1932011-06-15 23:02:42 +000025#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
Patrick Beardb2f68202012-04-06 18:12:22 +000027#include "clang/Lex/Preprocessor.h"
John McCall50df6ae2010-08-25 07:03:20 +000028#include "llvm/ADT/DenseSet.h"
29
Chris Lattner4d391482007-12-12 07:09:47 +000030using namespace clang;
31
John McCallf85e1932011-06-15 23:02:42 +000032/// Check whether the given method, which must be in the 'init'
33/// family, is a valid member of that family.
34///
35/// \param receiverTypeIfCall - if null, check this as if declaring it;
36/// if non-null, check this as if making a call to it with the given
37/// receiver type
38///
39/// \return true to indicate that there was an error and appropriate
40/// actions were taken
41bool Sema::checkInitMethod(ObjCMethodDecl *method,
42 QualType receiverTypeIfCall) {
43 if (method->isInvalidDecl()) return true;
44
45 // This castAs is safe: methods that don't return an object
46 // pointer won't be inferred as inits and will reject an explicit
47 // objc_method_family(init).
48
49 // We ignore protocols here. Should we? What about Class?
50
51 const ObjCObjectType *result = method->getResultType()
52 ->castAs<ObjCObjectPointerType>()->getObjectType();
53
54 if (result->isObjCId()) {
55 return false;
56 } else if (result->isObjCClass()) {
57 // fall through: always an error
58 } else {
59 ObjCInterfaceDecl *resultClass = result->getInterface();
60 assert(resultClass && "unexpected object type!");
61
62 // It's okay for the result type to still be a forward declaration
63 // if we're checking an interface declaration.
Douglas Gregor7723fec2011-12-15 20:29:51 +000064 if (!resultClass->hasDefinition()) {
John McCallf85e1932011-06-15 23:02:42 +000065 if (receiverTypeIfCall.isNull() &&
66 !isa<ObjCImplementationDecl>(method->getDeclContext()))
67 return false;
68
69 // Otherwise, we try to compare class types.
70 } else {
71 // If this method was declared in a protocol, we can't check
72 // anything unless we have a receiver type that's an interface.
73 const ObjCInterfaceDecl *receiverClass = 0;
74 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
75 if (receiverTypeIfCall.isNull())
76 return false;
77
78 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
79 ->getInterfaceDecl();
80
81 // This can be null for calls to e.g. id<Foo>.
82 if (!receiverClass) return false;
83 } else {
84 receiverClass = method->getClassInterface();
85 assert(receiverClass && "method not associated with a class!");
86 }
87
88 // If either class is a subclass of the other, it's fine.
89 if (receiverClass->isSuperClassOf(resultClass) ||
90 resultClass->isSuperClassOf(receiverClass))
91 return false;
92 }
93 }
94
95 SourceLocation loc = method->getLocation();
96
97 // If we're in a system header, and this is not a call, just make
98 // the method unusable.
99 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
100 method->addAttr(new (Context) UnavailableAttr(loc, Context,
101 "init method returns a type unrelated to its receiver type"));
102 return true;
103 }
104
105 // Otherwise, it's an error.
106 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
107 method->setInvalidDecl();
108 return true;
109}
110
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000111void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000112 const ObjCMethodDecl *Overridden,
113 bool IsImplementation) {
114 if (Overridden->hasRelatedResultType() &&
115 !NewMethod->hasRelatedResultType()) {
116 // This can only happen when the method follows a naming convention that
117 // implies a related result type, and the original (overridden) method has
118 // a suitable return type, but the new (overriding) method does not have
119 // a suitable return type.
120 QualType ResultType = NewMethod->getResultType();
121 SourceRange ResultTypeRange;
122 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000123 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000124 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
125
126 // Figure out which class this method is part of, if any.
127 ObjCInterfaceDecl *CurrentClass
128 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
129 if (!CurrentClass) {
130 DeclContext *DC = NewMethod->getDeclContext();
131 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
132 CurrentClass = Cat->getClassInterface();
133 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
134 CurrentClass = Impl->getClassInterface();
135 else if (ObjCCategoryImplDecl *CatImpl
136 = dyn_cast<ObjCCategoryImplDecl>(DC))
137 CurrentClass = CatImpl->getClassInterface();
138 }
139
140 if (CurrentClass) {
141 Diag(NewMethod->getLocation(),
142 diag::warn_related_result_type_compatibility_class)
143 << Context.getObjCInterfaceType(CurrentClass)
144 << ResultType
145 << ResultTypeRange;
146 } else {
147 Diag(NewMethod->getLocation(),
148 diag::warn_related_result_type_compatibility_protocol)
149 << ResultType
150 << ResultTypeRange;
151 }
152
Douglas Gregore97179c2011-09-08 01:46:34 +0000153 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
154 Diag(Overridden->getLocation(),
155 diag::note_related_result_type_overridden_family)
156 << Family;
157 else
158 Diag(Overridden->getLocation(),
159 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000160 }
David Blaikie4e4d0842012-03-11 07:00:24 +0000161 if (getLangOpts().ObjCAutoRefCount) {
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000162 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
163 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
164 Diag(NewMethod->getLocation(),
165 diag::err_nsreturns_retained_attribute_mismatch) << 1;
166 Diag(Overridden->getLocation(), diag::note_previous_decl)
167 << "method";
168 }
169 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
170 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
171 Diag(NewMethod->getLocation(),
172 diag::err_nsreturns_retained_attribute_mismatch) << 0;
173 Diag(Overridden->getLocation(), diag::note_previous_decl)
174 << "method";
175 }
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000176 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin(),
177 oe = Overridden->param_end();
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000178 for (ObjCMethodDecl::param_iterator
179 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +0000180 ni != ne && oi != oe; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000181 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000182 ParmVarDecl *newDecl = (*ni);
183 if (newDecl->hasAttr<NSConsumedAttr>() !=
184 oldDecl->hasAttr<NSConsumedAttr>()) {
185 Diag(newDecl->getLocation(),
186 diag::err_nsconsumed_attribute_mismatch);
187 Diag(oldDecl->getLocation(), diag::note_previous_decl)
188 << "parameter";
189 }
190 }
191 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000192}
193
John McCallf85e1932011-06-15 23:02:42 +0000194/// \brief Check a method declaration for compatibility with the Objective-C
195/// ARC conventions.
196static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
197 ObjCMethodFamily family = method->getMethodFamily();
198 switch (family) {
199 case OMF_None:
200 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000201 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000202 case OMF_retain:
203 case OMF_release:
204 case OMF_autorelease:
205 case OMF_retainCount:
206 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000207 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000208 return false;
209
210 case OMF_init:
211 // If the method doesn't obey the init rules, don't bother annotating it.
212 if (S.checkInitMethod(method, QualType()))
213 return true;
214
215 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
216 S.Context));
217
218 // Don't add a second copy of this attribute, but otherwise don't
219 // let it be suppressed.
220 if (method->hasAttr<NSReturnsRetainedAttr>())
221 return false;
222 break;
223
224 case OMF_alloc:
225 case OMF_copy:
226 case OMF_mutableCopy:
227 case OMF_new:
228 if (method->hasAttr<NSReturnsRetainedAttr>() ||
229 method->hasAttr<NSReturnsNotRetainedAttr>() ||
230 method->hasAttr<NSReturnsAutoreleasedAttr>())
231 return false;
232 break;
233 }
234
235 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
236 S.Context));
237 return false;
238}
239
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000240static void DiagnoseObjCImplementedDeprecations(Sema &S,
241 NamedDecl *ND,
242 SourceLocation ImplLoc,
243 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000244 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000245 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000246 if (select == 0)
Ted Kremenek3306ec12012-02-27 22:55:11 +0000247 S.Diag(ND->getLocation(), diag::note_method_declared_at)
248 << ND->getDeclName();
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000249 else
250 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
251 }
252}
253
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000254/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
255/// pool.
256void Sema::AddAnyMethodToGlobalPool(Decl *D) {
257 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
258
259 // If we don't have a valid method decl, simply return.
260 if (!MDecl)
261 return;
262 if (MDecl->isInstanceMethod())
263 AddInstanceMethodToGlobalPool(MDecl, true);
264 else
265 AddFactoryMethodToGlobalPool(MDecl, true);
266}
267
Steve Naroffebf64432009-02-28 16:59:13 +0000268/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000269/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000270void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000271 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000272 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000273
Steve Naroff394f3f42008-07-25 17:57:26 +0000274 // If we don't have a valid method decl, simply return.
275 if (!MDecl)
276 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000277
Chris Lattner4d391482007-12-12 07:09:47 +0000278 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000279 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000280 PushFunctionScope();
281
Chris Lattner4d391482007-12-12 07:09:47 +0000282 // Create Decl objects for each parameter, entrring them in the scope for
283 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000284
285 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000286 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000287
Daniel Dunbar451318c2008-08-26 06:07:48 +0000288 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
289 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000290
Chris Lattner8123a952008-04-10 02:22:51 +0000291 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000292 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000293 E = MDecl->param_end(); PI != E; ++PI) {
294 ParmVarDecl *Param = (*PI);
295 if (!Param->isInvalidDecl() &&
296 RequireCompleteType(Param->getLocation(), Param->getType(),
297 diag::err_typecheck_decl_incomplete_type))
298 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000299 if ((*PI)->getIdentifier())
300 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000301 }
John McCallf85e1932011-06-15 23:02:42 +0000302
303 // In ARC, disallow definition of retain/release/autorelease/retainCount
David Blaikie4e4d0842012-03-11 07:00:24 +0000304 if (getLangOpts().ObjCAutoRefCount) {
John McCallf85e1932011-06-15 23:02:42 +0000305 switch (MDecl->getMethodFamily()) {
306 case OMF_retain:
307 case OMF_retainCount:
308 case OMF_release:
309 case OMF_autorelease:
310 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
311 << MDecl->getSelector();
312 break;
313
314 case OMF_None:
315 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000316 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000317 case OMF_alloc:
318 case OMF_init:
319 case OMF_mutableCopy:
320 case OMF_copy:
321 case OMF_new:
322 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000323 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000324 break;
325 }
326 }
327
Nico Weber9a1ecf02011-08-22 17:25:57 +0000328 // Warn on deprecated methods under -Wdeprecated-implementations,
329 // and prepare for warning on missing super calls.
330 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000331 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000332 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000333 DiagnoseObjCImplementedDeprecations(*this,
334 dyn_cast<NamedDecl>(IMD),
335 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000336
Nico Weber80cb6e62011-08-28 22:35:17 +0000337 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000338 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
339 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
340 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000341 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000342 ObjCShouldCallSuperDealloc =
David Blaikie4e4d0842012-03-11 07:00:24 +0000343 !(Context.getLangOpts().ObjCAutoRefCount ||
344 Context.getLangOpts().getGC() == LangOptions::GCOnly) &&
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000345 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000346 ObjCShouldCallSuperFinalize =
David Blaikie4e4d0842012-03-11 07:00:24 +0000347 Context.getLangOpts().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000348 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000349 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000350 }
Chris Lattner4d391482007-12-12 07:09:47 +0000351}
352
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000353namespace {
354
355// Callback to only accept typo corrections that are Objective-C classes.
356// If an ObjCInterfaceDecl* is given to the constructor, then the validation
357// function will reject corrections to that class.
358class ObjCInterfaceValidatorCCC : public CorrectionCandidateCallback {
359 public:
360 ObjCInterfaceValidatorCCC() : CurrentIDecl(0) {}
361 explicit ObjCInterfaceValidatorCCC(ObjCInterfaceDecl *IDecl)
362 : CurrentIDecl(IDecl) {}
363
364 virtual bool ValidateCandidate(const TypoCorrection &candidate) {
365 ObjCInterfaceDecl *ID = candidate.getCorrectionDeclAs<ObjCInterfaceDecl>();
366 return ID && !declaresSameEntity(ID, CurrentIDecl);
367 }
368
369 private:
370 ObjCInterfaceDecl *CurrentIDecl;
371};
372
373}
374
John McCalld226f652010-08-21 09:40:31 +0000375Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000376ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
377 IdentifierInfo *ClassName, SourceLocation ClassLoc,
378 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000379 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000380 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000381 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000382 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000383
Chris Lattner4d391482007-12-12 07:09:47 +0000384 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000385 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000386 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000387
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000388 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000389 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000390 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000391 }
Mike Stump1eb44332009-09-09 15:08:12 +0000392
Douglas Gregor7723fec2011-12-15 20:29:51 +0000393 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000394 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000395 ObjCInterfaceDecl *IDecl
396 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000397 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000398
Douglas Gregor7723fec2011-12-15 20:29:51 +0000399 if (PrevIDecl) {
400 // Class already seen. Was it a definition?
401 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
402 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
403 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000404 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000405 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000406 }
Chris Lattner4d391482007-12-12 07:09:47 +0000407 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000408
409 if (AttrList)
410 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
411 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000412
Douglas Gregor7723fec2011-12-15 20:29:51 +0000413 // Start the definition of this class. If we're in a redefinition case, there
414 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000415 if (!IDecl->hasDefinition())
416 IDecl->startDefinition();
417
Chris Lattner4d391482007-12-12 07:09:47 +0000418 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000419 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000420 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
421 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000422
423 if (!PrevDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000424 // Try to correct for a typo in the superclass name without correcting
425 // to the class we're defining.
426 ObjCInterfaceValidatorCCC Validator(IDecl);
427 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000428 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000429 NULL, Validator)) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000430 PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
431 Diag(SuperLoc, diag::err_undef_superclass_suggest)
432 << SuperName << ClassName << PrevDecl->getDeclName();
433 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
434 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000435 }
436 }
437
Douglas Gregor60ef3082011-12-15 00:29:59 +0000438 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000439 Diag(SuperLoc, diag::err_recursive_superclass)
440 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000441 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000442 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000443 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000444 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000445
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000446 // Diagnose classes that inherit from deprecated classes.
447 if (SuperClassDecl)
448 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000450 if (PrevDecl && SuperClassDecl == 0) {
451 // The previous declaration was not a class decl. Check if we have a
452 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000453 if (const TypedefNameDecl *TDecl =
454 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000455 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000456 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000457 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
458 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000459 }
460 }
Mike Stump1eb44332009-09-09 15:08:12 +0000461
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000462 // This handles the following case:
463 //
464 // typedef int SuperClass;
465 // @interface MyClass : SuperClass {} @end
466 //
467 if (!SuperClassDecl) {
468 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
469 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000470 }
471 }
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Richard Smith162e1c12011-04-15 14:24:37 +0000473 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000474 if (!SuperClassDecl)
475 Diag(SuperLoc, diag::err_undef_superclass)
476 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000477 else if (RequireCompleteType(SuperLoc,
Douglas Gregord10099e2012-05-04 16:32:21 +0000478 Context.getObjCInterfaceType(SuperClassDecl),
479 diag::err_forward_superclass,
480 SuperClassDecl->getDeclName(),
481 ClassName,
482 SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000483 SuperClassDecl = 0;
484 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000485 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000486 IDecl->setSuperClass(SuperClassDecl);
487 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000488 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000489 }
Chris Lattner4d391482007-12-12 07:09:47 +0000490 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000491 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000492 }
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Sebastian Redl0b17c612010-08-13 00:28:03 +0000494 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000495 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000496 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000497 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000498 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000499 }
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Anders Carlsson15281452008-11-04 16:57:32 +0000501 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000502 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000503}
504
505/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000506/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000507Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
508 IdentifierInfo *AliasName,
509 SourceLocation AliasLocation,
510 IdentifierInfo *ClassName,
511 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000512 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000513 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000514 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000515 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000516 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000517 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000518 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000519 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000520 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000521 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000522 }
523 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000524 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000525 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000526 if (const TypedefNameDecl *TDecl =
527 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000528 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000529 if (T->isObjCObjectType()) {
530 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000531 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000532 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000533 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000534 }
535 }
536 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000537 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
538 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000539 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000540 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000541 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000542 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000543 }
Mike Stump1eb44332009-09-09 15:08:12 +0000544
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000545 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000546 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000547 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Anders Carlsson15281452008-11-04 16:57:32 +0000549 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000550 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000551
John McCalld226f652010-08-21 09:40:31 +0000552 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000553}
554
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000555bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000556 IdentifierInfo *PName,
557 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000558 const ObjCList<ObjCProtocolDecl> &PList) {
559
560 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000561 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
562 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000563 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
564 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000565 if (PDecl->getIdentifier() == PName) {
566 Diag(Ploc, diag::err_protocol_has_circular_dependency);
567 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000568 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000569 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000570
571 if (!PDecl->hasDefinition())
572 continue;
573
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000574 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
575 PDecl->getLocation(), PDecl->getReferencedProtocols()))
576 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000577 }
578 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000579 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000580}
581
John McCalld226f652010-08-21 09:40:31 +0000582Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000583Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
584 IdentifierInfo *ProtocolName,
585 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000586 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000587 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000588 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000589 SourceLocation EndProtoLoc,
590 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000591 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000592 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000593 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000594 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
595 ForRedeclaration);
596 ObjCProtocolDecl *PDecl = 0;
597 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
598 // If we already have a definition, complain.
599 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
600 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000601
Douglas Gregor27c6da22012-01-01 20:30:41 +0000602 // Create a new protocol that is completely distinct from previous
603 // declarations, and do not make this protocol available for name lookup.
604 // That way, we'll end up completely ignoring the duplicate.
605 // FIXME: Can we turn this into an error?
606 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
607 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000608 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000609 PDecl->startDefinition();
610 } else {
611 if (PrevDecl) {
612 // Check for circular dependencies among protocol declarations. This can
613 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000614 ObjCList<ObjCProtocolDecl> PList;
615 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
616 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000617 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000618 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000619
620 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000621 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000622 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000623 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000624
Douglas Gregor6e378de2009-04-23 23:18:26 +0000625 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000626 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000627 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000628
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000629 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000630 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000631
632 // Merge attributes from previous declarations.
633 if (PrevDecl)
634 mergeDeclAttributes(PDecl, PrevDecl);
635
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000636 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000637 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000638 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
639 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000640 }
Mike Stump1eb44332009-09-09 15:08:12 +0000641
642 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000643 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000644}
645
646/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000647/// issues an error if they are not declared. It returns list of
648/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000649void
Chris Lattnere13b9592008-07-26 04:03:38 +0000650Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000651 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000652 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000653 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000654 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000655 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
656 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000657 if (!PDecl) {
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000658 DeclFilterCCC<ObjCProtocolDecl> Validator;
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000659 TypoCorrection Corrected = CorrectTypo(
660 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000661 LookupObjCProtocolName, TUScope, NULL, Validator);
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000662 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000663 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000664 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000665 Diag(PDecl->getLocation(), diag::note_previous_decl)
666 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000667 }
668 }
669
670 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000671 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000672 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000673 continue;
674 }
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000676 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000677
678 // If this is a forward declaration and we are supposed to warn in this
679 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000680 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000681 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000682 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000683 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000684 }
685}
686
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000687/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000688/// a class method in its extension.
689///
Mike Stump1eb44332009-09-09 15:08:12 +0000690void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000691 ObjCInterfaceDecl *ID) {
692 if (!ID)
693 return; // Possibly due to previous error
694
695 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000696 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
697 e = ID->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000698 ObjCMethodDecl *MD = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000699 MethodMap[MD->getSelector()] = MD;
700 }
701
702 if (MethodMap.empty())
703 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000704 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
705 e = CAT->meth_end(); i != e; ++i) {
David Blaikie581deb32012-06-06 20:45:41 +0000706 ObjCMethodDecl *Method = *i;
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000707 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
708 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
709 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
710 << Method->getDeclName();
711 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
712 }
713 }
714}
715
Chris Lattner58fe03b2009-04-12 08:43:13 +0000716/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000717Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000718Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000719 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000720 unsigned NumElts,
721 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000722 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000723 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000724 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000725 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
726 ForRedeclaration);
727 ObjCProtocolDecl *PDecl
728 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
729 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000730 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000731
732 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000733 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000734
Douglas Gregor3937f872012-01-01 20:33:24 +0000735 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000736 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000737
738 if (PrevDecl)
739 mergeDeclAttributes(PDecl, PrevDecl);
740
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000741 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000744 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000745}
746
John McCalld226f652010-08-21 09:40:31 +0000747Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000748ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
749 IdentifierInfo *ClassName, SourceLocation ClassLoc,
750 IdentifierInfo *CategoryName,
751 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000752 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000753 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000754 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000755 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000756 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000757 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000758
759 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000760
761 if (!IDecl
762 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
Douglas Gregord10099e2012-05-04 16:32:21 +0000763 diag::err_category_forward_interface,
764 CategoryName == 0)) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000765 // Create an invalid ObjCCategoryDecl to serve as context for
766 // the enclosing method declarations. We mark the decl invalid
767 // to make it clear that this isn't a valid AST.
768 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000769 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000770 CDecl->setInvalidDecl();
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +0000771 CurContext->addDecl(CDecl);
Douglas Gregorb3029962011-11-14 22:10:01 +0000772
773 if (!IDecl)
774 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000775 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000776 }
777
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000778 if (!CategoryName && IDecl->getImplementation()) {
779 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
780 Diag(IDecl->getImplementation()->getLocation(),
781 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000782 }
783
Fariborz Jahanian25760612010-02-15 21:55:26 +0000784 if (CategoryName) {
785 /// Check for duplicate interface declaration for this category
786 ObjCCategoryDecl *CDeclChain;
787 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
788 CDeclChain = CDeclChain->getNextClassCategory()) {
789 if (CDeclChain->getIdentifier() == CategoryName) {
790 // Class extensions can be declared multiple times.
791 Diag(CategoryLoc, diag::warn_dup_category_def)
792 << ClassName << CategoryName;
793 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
794 break;
795 }
Chris Lattner70f19542009-02-16 21:26:43 +0000796 }
797 }
Chris Lattner70f19542009-02-16 21:26:43 +0000798
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000799 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
800 ClassLoc, CategoryLoc, CategoryName, IDecl);
801 // FIXME: PushOnScopeChains?
802 CurContext->addDecl(CDecl);
803
Chris Lattner4d391482007-12-12 07:09:47 +0000804 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000805 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000806 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000807 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000808 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000809 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000810 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000811 }
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Anders Carlsson15281452008-11-04 16:57:32 +0000813 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000814 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000815}
816
817/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000818/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000819/// object.
John McCalld226f652010-08-21 09:40:31 +0000820Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000821 SourceLocation AtCatImplLoc,
822 IdentifierInfo *ClassName, SourceLocation ClassLoc,
823 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000824 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000825 ObjCCategoryDecl *CatIDecl = 0;
Argyrios Kyrtzidis5a61e0c2012-03-02 19:14:29 +0000826 if (IDecl && IDecl->hasDefinition()) {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000827 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
828 if (!CatIDecl) {
829 // Category @implementation with no corresponding @interface.
830 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000831 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
832 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000833 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000834 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000835 }
836 }
837
Mike Stump1eb44332009-09-09 15:08:12 +0000838 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000839 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000840 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000841 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000842 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000843 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000844 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000845 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
846 diag::err_undef_interface)) {
847 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000848 }
Chris Lattner4d391482007-12-12 07:09:47 +0000849
Douglas Gregord0434102009-01-09 00:49:46 +0000850 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000851 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000852
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000853 // If the interface is deprecated/unavailable, warn/error about it.
854 if (IDecl)
855 DiagnoseUseOfDecl(IDecl, ClassLoc);
856
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000857 /// Check that CatName, category name, is not used in another implementation.
858 if (CatIDecl) {
859 if (CatIDecl->getImplementation()) {
860 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
861 << CatName;
862 Diag(CatIDecl->getImplementation()->getLocation(),
863 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000864 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000865 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000866 // Warn on implementating category of deprecated class under
867 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000868 DiagnoseObjCImplementedDeprecations(*this,
869 dyn_cast<NamedDecl>(IDecl),
870 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000871 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000872 }
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Anders Carlsson15281452008-11-04 16:57:32 +0000874 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000875 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000876}
877
John McCalld226f652010-08-21 09:40:31 +0000878Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000879 SourceLocation AtClassImplLoc,
880 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000881 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000882 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000883 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000884 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000885 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000886 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
887 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000888 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000889 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000890 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000891 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000892 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
893 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000894 } else {
895 // We did not find anything with the name ClassName; try to correct for
896 // typos in the class name.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000897 ObjCInterfaceValidatorCCC Validator;
898 if (TypoCorrection Corrected = CorrectTypo(
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000899 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
Kaelyn Uhrain16e46dd2012-01-31 23:49:25 +0000900 NULL, Validator)) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000901 // Suggest the (potentially) correct interface name. However, put the
902 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000903 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000904 // provide a code-modification hint or use the typo name for recovery,
905 // because this is just a warning. The program may actually be correct.
Kaelyn Uhrain2f4d88f2012-01-13 01:32:50 +0000906 IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>();
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000907 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000908 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000909 << ClassName << CorrectedName;
910 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
911 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000912 IDecl = 0;
913 } else {
914 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
915 }
Chris Lattner4d391482007-12-12 07:09:47 +0000916 }
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Chris Lattner4d391482007-12-12 07:09:47 +0000918 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000919 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000920 if (SuperClassname) {
921 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000922 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
923 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000924 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000925 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
926 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000927 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000928 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000929 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Argyrios Kyrtzidiscd707ab2012-03-13 01:09:36 +0000930 if (SDecl && !SDecl->hasDefinition())
931 SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000932 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000933 Diag(SuperClassLoc, diag::err_undef_superclass)
934 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000935 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000936 // This implementation and its interface do not have the same
937 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000938 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000939 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000940 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000941 }
942 }
943 }
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Chris Lattner4d391482007-12-12 07:09:47 +0000945 if (!IDecl) {
946 // Legacy case of @implementation with no corresponding @interface.
947 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000948
Mike Stump390b4cc2009-05-16 07:39:55 +0000949 // FIXME: Do we support attributes on the @implementation? If so we should
950 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000951 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000952 ClassName, /*PrevDecl=*/0, ClassLoc,
953 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000954 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000955 if (SDecl) {
956 IDecl->setSuperClass(SDecl);
957 IDecl->setSuperClassLoc(SuperClassLoc);
958 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
959 } else {
960 IDecl->setEndOfDefinitionLoc(ClassLoc);
961 }
962
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000963 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000964 } else {
965 // Mark the interface as being completed, even if it was just as
966 // @class ....;
967 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000968 if (!IDecl->hasDefinition())
969 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000970 }
Mike Stump1eb44332009-09-09 15:08:12 +0000971
972 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000973 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
974 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Anders Carlsson15281452008-11-04 16:57:32 +0000976 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000977 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Chris Lattner4d391482007-12-12 07:09:47 +0000979 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000980 if (IDecl->getImplementation()) {
981 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000982 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000983 Diag(IDecl->getImplementation()->getLocation(),
984 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000985 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000986 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000987 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000988 // Warn on implementating deprecated class under
989 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000990 DiagnoseObjCImplementedDeprecations(*this,
991 dyn_cast<NamedDecl>(IDecl),
992 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000993 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000994 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000995}
996
Argyrios Kyrtzidis644af7b2012-02-23 21:11:20 +0000997Sema::DeclGroupPtrTy
998Sema::ActOnFinishObjCImplementation(Decl *ObjCImpDecl, ArrayRef<Decl *> Decls) {
999 SmallVector<Decl *, 64> DeclsInGroup;
1000 DeclsInGroup.reserve(Decls.size() + 1);
1001
1002 for (unsigned i = 0, e = Decls.size(); i != e; ++i) {
1003 Decl *Dcl = Decls[i];
1004 if (!Dcl)
1005 continue;
1006 if (Dcl->getDeclContext()->isFileContext())
1007 Dcl->setTopLevelDeclInObjCContainer();
1008 DeclsInGroup.push_back(Dcl);
1009 }
1010
1011 DeclsInGroup.push_back(ObjCImpDecl);
1012
1013 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
1014}
1015
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001016void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
1017 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +00001018 SourceLocation RBrace) {
1019 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001020 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +00001021 if (!IDecl)
1022 return;
1023 /// Check case of non-existing @interface decl.
1024 /// (legacy objective-c @implementation decl without an @interface decl).
1025 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +00001026 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +00001027 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001028 // Add ivar's to class's DeclContext.
1029 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001030 ivars[i]->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001031 IDecl->makeDeclVisibleInContext(ivars[i]);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001032 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001033 }
1034
Chris Lattner4d391482007-12-12 07:09:47 +00001035 return;
1036 }
1037 // If implementation has empty ivar list, just return.
1038 if (numIvars == 0)
1039 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Chris Lattner4d391482007-12-12 07:09:47 +00001041 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001042 if (LangOpts.ObjCNonFragileABI2) {
1043 if (ImpDecl->getSuperClass())
1044 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1045 for (unsigned i = 0; i < numIvars; i++) {
1046 ObjCIvarDecl* ImplIvar = ivars[i];
1047 if (const ObjCIvarDecl *ClsIvar =
1048 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1049 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1050 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1051 continue;
1052 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001053 // Instance ivar to Implementation's DeclContext.
1054 ImplIvar->setLexicalDeclContext(ImpDecl);
Richard Smith1b7f9cb2012-03-13 03:12:56 +00001055 IDecl->makeDeclVisibleInContext(ImplIvar);
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001056 ImpDecl->addDecl(ImplIvar);
1057 }
1058 return;
1059 }
Chris Lattner4d391482007-12-12 07:09:47 +00001060 // Check interface's Ivar list against those in the implementation.
1061 // names and types must match.
1062 //
Chris Lattner4d391482007-12-12 07:09:47 +00001063 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001064 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001065 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1066 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001067 ObjCIvarDecl* ImplIvar = ivars[j++];
David Blaikie581deb32012-06-06 20:45:41 +00001068 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001069 assert (ImplIvar && "missing implementation ivar");
1070 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Steve Naroffca331292009-03-03 14:49:36 +00001072 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001073 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001074 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001075 << ImplIvar->getIdentifier()
1076 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001077 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001078 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1079 ImplIvar->getBitWidthValue(Context) !=
1080 ClsIvar->getBitWidthValue(Context)) {
1081 Diag(ImplIvar->getBitWidth()->getLocStart(),
1082 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1083 Diag(ClsIvar->getBitWidth()->getLocStart(),
1084 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001085 }
Steve Naroffca331292009-03-03 14:49:36 +00001086 // Make sure the names are identical.
1087 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001088 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001089 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001090 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001091 }
1092 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001093 }
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Chris Lattner609e4c72007-12-12 18:11:49 +00001095 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001096 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001097 else if (IVI != IVE)
David Blaikie262bc182012-04-30 02:36:29 +00001098 Diag(IVI->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001099}
1100
Steve Naroff3c2eb662008-02-10 21:38:56 +00001101void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001102 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001103 // No point warning no definition of method which is 'unavailable'.
1104 if (method->hasAttr<UnavailableAttr>())
1105 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001106 if (!IncompleteImpl) {
1107 Diag(ImpLoc, diag::warn_incomplete_impl);
1108 IncompleteImpl = true;
1109 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001110 if (DiagID == diag::warn_unimplemented_protocol_method)
1111 Diag(ImpLoc, DiagID) << method->getDeclName();
1112 else
1113 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001114}
1115
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001116/// Determines if type B can be substituted for type A. Returns true if we can
1117/// guarantee that anything that the user will do to an object of type A can
1118/// also be done to an object of type B. This is trivially true if the two
1119/// types are the same, or if B is a subclass of A. It becomes more complex
1120/// in cases where protocols are involved.
1121///
1122/// Object types in Objective-C describe the minimum requirements for an
1123/// object, rather than providing a complete description of a type. For
1124/// example, if A is a subclass of B, then B* may refer to an instance of A.
1125/// The principle of substitutability means that we may use an instance of A
1126/// anywhere that we may use an instance of B - it will implement all of the
1127/// ivars of B and all of the methods of B.
1128///
1129/// This substitutability is important when type checking methods, because
1130/// the implementation may have stricter type definitions than the interface.
1131/// The interface specifies minimum requirements, but the implementation may
1132/// have more accurate ones. For example, a method may privately accept
1133/// instances of B, but only publish that it accepts instances of A. Any
1134/// object passed to it will be type checked against B, and so will implicitly
1135/// by a valid A*. Similarly, a method may return a subclass of the class that
1136/// it is declared as returning.
1137///
1138/// This is most important when considering subclassing. A method in a
1139/// subclass must accept any object as an argument that its superclass's
1140/// implementation accepts. It may, however, accept a more general type
1141/// without breaking substitutability (i.e. you can still use the subclass
1142/// anywhere that you can use the superclass, but not vice versa). The
1143/// converse requirement applies to return types: the return type for a
1144/// subclass method must be a valid object of the kind that the superclass
1145/// advertises, but it may be specified more accurately. This avoids the need
1146/// for explicit down-casting by callers.
1147///
1148/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001149static bool isObjCTypeSubstitutable(ASTContext &Context,
1150 const ObjCObjectPointerType *A,
1151 const ObjCObjectPointerType *B,
1152 bool rejectId) {
1153 // Reject a protocol-unqualified id.
1154 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001155
1156 // If B is a qualified id, then A must also be a qualified id and it must
1157 // implement all of the protocols in B. It may not be a qualified class.
1158 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1159 // stricter definition so it is not substitutable for id<A>.
1160 if (B->isObjCQualifiedIdType()) {
1161 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001162 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1163 QualType(B,0),
1164 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001165 }
1166
1167 /*
1168 // id is a special type that bypasses type checking completely. We want a
1169 // warning when it is used in one place but not another.
1170 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1171
1172
1173 // If B is a qualified id, then A must also be a qualified id (which it isn't
1174 // if we've got this far)
1175 if (B->isObjCQualifiedIdType()) return false;
1176 */
1177
1178 // Now we know that A and B are (potentially-qualified) class types. The
1179 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001180 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001181}
1182
John McCall10302c02010-10-28 02:34:38 +00001183static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1184 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1185}
1186
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001187static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001188 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001189 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001190 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001191 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001192 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001193 if (IsProtocolMethodDecl &&
1194 (MethodDecl->getObjCDeclQualifier() !=
1195 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001196 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001197 S.Diag(MethodImpl->getLocation(),
1198 (IsOverridingMode ?
1199 diag::warn_conflicting_overriding_ret_type_modifiers
1200 : diag::warn_conflicting_ret_type_modifiers))
1201 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001202 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1203 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1204 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1205 }
1206 else
1207 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001208 }
1209
John McCall10302c02010-10-28 02:34:38 +00001210 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001211 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001212 return true;
1213 if (!Warn)
1214 return false;
John McCall10302c02010-10-28 02:34:38 +00001215
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001216 unsigned DiagID =
1217 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1218 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001219
1220 // Mismatches between ObjC pointers go into a different warning
1221 // category, and sometimes they're even completely whitelisted.
1222 if (const ObjCObjectPointerType *ImplPtrTy =
1223 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1224 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001225 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001226 // Allow non-matching return types as long as they don't violate
1227 // the principle of substitutability. Specifically, we permit
1228 // return types that are subclasses of the declared return type,
1229 // or that are more-qualified versions of the declared type.
1230 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001231 return false;
John McCall10302c02010-10-28 02:34:38 +00001232
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001233 DiagID =
1234 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1235 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001236 }
1237 }
1238
1239 S.Diag(MethodImpl->getLocation(), DiagID)
1240 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001241 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001242 << MethodImpl->getResultType()
1243 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001244 S.Diag(MethodDecl->getLocation(),
1245 IsOverridingMode ? diag::note_previous_declaration
1246 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001247 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001248 return false;
John McCall10302c02010-10-28 02:34:38 +00001249}
1250
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001251static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001252 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001253 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001254 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001255 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001256 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001257 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001258 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001259 if (IsProtocolMethodDecl &&
1260 (ImplVar->getObjCDeclQualifier() !=
1261 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001262 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001263 if (IsOverridingMode)
1264 S.Diag(ImplVar->getLocation(),
1265 diag::warn_conflicting_overriding_param_modifiers)
1266 << getTypeRange(ImplVar->getTypeSourceInfo())
1267 << MethodImpl->getDeclName();
1268 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001269 diag::warn_conflicting_param_modifiers)
1270 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001271 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001272 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1273 << getTypeRange(IfaceVar->getTypeSourceInfo());
1274 }
1275 else
1276 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001277 }
1278
John McCall10302c02010-10-28 02:34:38 +00001279 QualType ImplTy = ImplVar->getType();
1280 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001281
John McCall10302c02010-10-28 02:34:38 +00001282 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001283 return true;
1284
1285 if (!Warn)
1286 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001287 unsigned DiagID =
1288 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1289 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001290
1291 // Mismatches between ObjC pointers go into a different warning
1292 // category, and sometimes they're even completely whitelisted.
1293 if (const ObjCObjectPointerType *ImplPtrTy =
1294 ImplTy->getAs<ObjCObjectPointerType>()) {
1295 if (const ObjCObjectPointerType *IfacePtrTy =
1296 IfaceTy->getAs<ObjCObjectPointerType>()) {
1297 // Allow non-matching argument types as long as they don't
1298 // violate the principle of substitutability. Specifically, the
1299 // implementation must accept any objects that the superclass
1300 // accepts, however it may also accept others.
1301 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001302 return false;
John McCall10302c02010-10-28 02:34:38 +00001303
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001304 DiagID =
1305 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1306 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001307 }
1308 }
1309
1310 S.Diag(ImplVar->getLocation(), DiagID)
1311 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001312 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1313 S.Diag(IfaceVar->getLocation(),
1314 (IsOverridingMode ? diag::note_previous_declaration
1315 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001316 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001317 return false;
John McCall10302c02010-10-28 02:34:38 +00001318}
John McCallf85e1932011-06-15 23:02:42 +00001319
1320/// In ARC, check whether the conventional meanings of the two methods
1321/// match. If they don't, it's a hard error.
1322static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1323 ObjCMethodDecl *decl) {
1324 ObjCMethodFamily implFamily = impl->getMethodFamily();
1325 ObjCMethodFamily declFamily = decl->getMethodFamily();
1326 if (implFamily == declFamily) return false;
1327
1328 // Since conventions are sorted by selector, the only possibility is
1329 // that the types differ enough to cause one selector or the other
1330 // to fall out of the family.
1331 assert(implFamily == OMF_None || declFamily == OMF_None);
1332
1333 // No further diagnostics required on invalid declarations.
1334 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1335
1336 const ObjCMethodDecl *unmatched = impl;
1337 ObjCMethodFamily family = declFamily;
1338 unsigned errorID = diag::err_arc_lost_method_convention;
1339 unsigned noteID = diag::note_arc_lost_method_convention;
1340 if (declFamily == OMF_None) {
1341 unmatched = decl;
1342 family = implFamily;
1343 errorID = diag::err_arc_gained_method_convention;
1344 noteID = diag::note_arc_gained_method_convention;
1345 }
1346
1347 // Indexes into a %select clause in the diagnostic.
1348 enum FamilySelector {
1349 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1350 };
1351 FamilySelector familySelector = FamilySelector();
1352
1353 switch (family) {
1354 case OMF_None: llvm_unreachable("logic error, no method convention");
1355 case OMF_retain:
1356 case OMF_release:
1357 case OMF_autorelease:
1358 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001359 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001360 case OMF_retainCount:
1361 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001362 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001363 // Mismatches for these methods don't change ownership
1364 // conventions, so we don't care.
1365 return false;
1366
1367 case OMF_init: familySelector = F_init; break;
1368 case OMF_alloc: familySelector = F_alloc; break;
1369 case OMF_copy: familySelector = F_copy; break;
1370 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1371 case OMF_new: familySelector = F_new; break;
1372 }
1373
1374 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1375 ReasonSelector reasonSelector;
1376
1377 // The only reason these methods don't fall within their families is
1378 // due to unusual result types.
1379 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1380 reasonSelector = R_UnrelatedReturn;
1381 } else {
1382 reasonSelector = R_NonObjectReturn;
1383 }
1384
1385 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1386 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1387
1388 return true;
1389}
John McCall10302c02010-10-28 02:34:38 +00001390
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001391void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001392 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001393 bool IsProtocolMethodDecl) {
David Blaikie4e4d0842012-03-11 07:00:24 +00001394 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001395 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1396 return;
1397
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001398 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001399 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001400 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001401
Chris Lattner3aff9192009-04-11 19:58:42 +00001402 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001403 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1404 EF = MethodDecl->param_end();
1405 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001406 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001407 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001408 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001409
Fariborz Jahanian21121902011-08-08 18:03:17 +00001410 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001411 Diag(ImpMethodDecl->getLocation(),
1412 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001413 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001414 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001415}
1416
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001417void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1418 ObjCMethodDecl *Overridden,
1419 bool IsProtocolMethodDecl) {
1420
1421 CheckMethodOverrideReturn(*this, Method, Overridden,
1422 IsProtocolMethodDecl, true,
1423 true);
1424
1425 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001426 IF = Overridden->param_begin(), EM = Method->param_end(),
1427 EF = Overridden->param_end();
1428 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001429 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1430 IsProtocolMethodDecl, true, true);
1431 }
1432
1433 if (Method->isVariadic() != Overridden->isVariadic()) {
1434 Diag(Method->getLocation(),
1435 diag::warn_conflicting_overriding_variadic);
1436 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1437 }
1438}
1439
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001440/// WarnExactTypedMethods - This routine issues a warning if method
1441/// implementation declaration matches exactly that of its declaration.
1442void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1443 ObjCMethodDecl *MethodDecl,
1444 bool IsProtocolMethodDecl) {
1445 // don't issue warning when protocol method is optional because primary
1446 // class is not required to implement it and it is safe for protocol
1447 // to implement it.
1448 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1449 return;
1450 // don't issue warning when primary class's method is
1451 // depecated/unavailable.
1452 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1453 MethodDecl->hasAttr<DeprecatedAttr>())
1454 return;
1455
1456 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1457 IsProtocolMethodDecl, false, false);
1458 if (match)
1459 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001460 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end(),
1461 EF = MethodDecl->param_end();
1462 IM != EM && IF != EF; ++IM, ++IF) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001463 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1464 *IM, *IF,
1465 IsProtocolMethodDecl, false, false);
1466 if (!match)
1467 break;
1468 }
1469 if (match)
1470 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001471 if (match)
1472 match = !(MethodDecl->isClassMethod() &&
1473 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001474
1475 if (match) {
1476 Diag(ImpMethodDecl->getLocation(),
1477 diag::warn_category_method_impl_match);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001478 Diag(MethodDecl->getLocation(), diag::note_method_declared_at)
1479 << MethodDecl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001480 }
1481}
1482
Mike Stump390b4cc2009-05-16 07:39:55 +00001483/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1484/// improve the efficiency of selector lookups and type checking by associating
1485/// with each protocol / interface / category the flattened instance tables. If
1486/// we used an immutable set to keep the table then it wouldn't add significant
1487/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001488
Steve Naroffefe7f362008-02-08 22:06:17 +00001489/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001490/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001491void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1492 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001493 bool& IncompleteImpl,
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001494 const SelectorSet &InsMap,
1495 const SelectorSet &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001496 ObjCContainerDecl *CDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001497 ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl);
1498 ObjCInterfaceDecl *IDecl = C ? C->getClassInterface()
1499 : dyn_cast<ObjCInterfaceDecl>(CDecl);
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001500 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1501
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001502 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001503 ObjCInterfaceDecl *NSIDecl = 0;
David Blaikie4e4d0842012-03-11 07:00:24 +00001504 if (getLangOpts().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001505 // check to see if class implements forwardInvocation method and objects
1506 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001507 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001508 // Under such conditions, which means that every method possible is
1509 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001510 // found" warnings.
1511 // FIXME: Use a general GetUnarySelector method for this.
1512 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1513 Selector fISelector = Context.Selectors.getSelector(1, &II);
1514 if (InsMap.count(fISelector))
1515 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1516 // need be implemented in the implementation.
1517 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1518 }
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001520 // If a method lookup fails locally we still need to look and see if
1521 // the method was implemented by a base class or an inherited
1522 // protocol. This lookup is slow, but occurs rarely in correct code
1523 // and otherwise would terminate in a warning.
1524
Chris Lattner4d391482007-12-12 07:09:47 +00001525 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001526 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001527 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001528 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001529 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001530 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001531 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001532 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001533 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001534 // If a method is not implemented in the category implementation but
1535 // has been declared in its primary class, superclass,
1536 // or in one of their protocols, no need to issue the warning.
1537 // This is because method will be implemented in the primary class
1538 // or one of its super class implementation.
1539
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001540 // Ugly, but necessary. Method declared in protcol might have
1541 // have been synthesized due to a property declared in the class which
1542 // uses the protocol.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001543 if (ObjCMethodDecl *MethodInClass =
1544 IDecl->lookupInstanceMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001545 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001546 if (C || MethodInClass->isSynthesized())
1547 continue;
1548 unsigned DIAG = diag::warn_unimplemented_protocol_method;
1549 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
1550 != DiagnosticsEngine::Ignored) {
1551 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001552 Diag(method->getLocation(), diag::note_method_declared_at)
1553 << method->getDeclName();
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001554 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1555 << PDecl->getDeclName();
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001556 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001557 }
1558 }
Chris Lattner4d391482007-12-12 07:09:47 +00001559 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001560 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001561 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001562 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001563 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001564 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1565 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001566 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001567 // See above comment for instance method lookups.
1568 if (C && IDecl->lookupClassMethod(method->getSelector(),
Fariborz Jahanianbf393be2012-04-05 22:14:12 +00001569 true /*shallowCategoryLookup*/))
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001570 continue;
Fariborz Jahanian52146832010-03-31 18:23:33 +00001571 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001572 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1573 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001574 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Ted Kremenek3306ec12012-02-27 22:55:11 +00001575 Diag(method->getLocation(), diag::note_method_declared_at)
1576 << method->getDeclName();
Fariborz Jahanian52146832010-03-31 18:23:33 +00001577 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1578 PDecl->getDeclName();
1579 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001580 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001581 }
Chris Lattner780f3292008-07-21 21:32:27 +00001582 // Check on this protocols's referenced protocols, recursively.
1583 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1584 E = PDecl->protocol_end(); PI != E; ++PI)
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001585 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001586}
1587
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001588/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001589/// or protocol against those declared in their implementations.
1590///
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001591void Sema::MatchAllMethodDeclarations(const SelectorSet &InsMap,
1592 const SelectorSet &ClsMap,
1593 SelectorSet &InsMapSeen,
1594 SelectorSet &ClsMapSeen,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001595 ObjCImplDecl* IMPDecl,
1596 ObjCContainerDecl* CDecl,
1597 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001598 bool ImmediateClass,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001599 bool WarnCategoryMethodImpl) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001600 // Check and see if instance methods in class interface have been
1601 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001602 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1603 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001604 if (InsMapSeen.count((*I)->getSelector()))
1605 continue;
1606 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001607 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001608 !InsMap.count((*I)->getSelector())) {
1609 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001610 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1611 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001612 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001613 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001614 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001615 IMPDecl->getInstanceMethod((*I)->getSelector());
1616 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1617 "Expected to find the method through lookup as well");
1618 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001619 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001620 if (ImpMethodDecl) {
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001621 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001622 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1623 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001624 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001625 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001626 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001627 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001628 }
1629 }
Mike Stump1eb44332009-09-09 15:08:12 +00001630
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001631 // Check and see if class methods in class interface have been
1632 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001633 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001634 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001635 if (ClsMapSeen.count((*I)->getSelector()))
1636 continue;
1637 ClsMapSeen.insert((*I)->getSelector());
1638 if (!ClsMap.count((*I)->getSelector())) {
1639 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001640 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1641 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001642 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001643 ObjCMethodDecl *ImpMethodDecl =
1644 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001645 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1646 "Expected to find the method through lookup as well");
1647 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001648 if (!WarnCategoryMethodImpl)
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001649 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1650 isa<ObjCProtocolDecl>(CDecl));
1651 else
1652 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001653 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001654 }
1655 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001656
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001657 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001658 // Also methods in class extensions need be looked at next.
1659 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1660 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1661 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1662 IMPDecl,
1663 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001664 IncompleteImpl, false,
1665 WarnCategoryMethodImpl);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001666
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001667 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001668 for (ObjCInterfaceDecl::all_protocol_iterator
1669 PI = I->all_referenced_protocol_begin(),
1670 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001671 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1672 IMPDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001673 (*PI), IncompleteImpl, false,
1674 WarnCategoryMethodImpl);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001675
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001676 // FIXME. For now, we are not checking for extact match of methods
1677 // in category implementation and its primary class's super class.
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001678 if (!WarnCategoryMethodImpl && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001679 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001680 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001681 I->getSuperClass(), IncompleteImpl, false);
1682 }
1683}
1684
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001685/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1686/// category matches with those implemented in its primary class and
1687/// warns each time an exact match is found.
1688void Sema::CheckCategoryVsClassMethodMatches(
1689 ObjCCategoryImplDecl *CatIMPDecl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001690 SelectorSet InsMap, ClsMap;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001691
1692 for (ObjCImplementationDecl::instmeth_iterator
1693 I = CatIMPDecl->instmeth_begin(),
1694 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1695 InsMap.insert((*I)->getSelector());
1696
1697 for (ObjCImplementationDecl::classmeth_iterator
1698 I = CatIMPDecl->classmeth_begin(),
1699 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1700 ClsMap.insert((*I)->getSelector());
1701 if (InsMap.empty() && ClsMap.empty())
1702 return;
1703
1704 // Get category's primary class.
1705 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1706 if (!CatDecl)
1707 return;
1708 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1709 if (!IDecl)
1710 return;
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001711 SelectorSet InsMapSeen, ClsMapSeen;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001712 bool IncompleteImpl = false;
1713 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1714 CatIMPDecl, IDecl,
Fariborz Jahanianbb3d14e2012-02-09 21:30:24 +00001715 IncompleteImpl, false,
1716 true /*WarnCategoryMethodImpl*/);
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001717}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001718
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001719void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001720 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001721 bool IncompleteImpl) {
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001722 SelectorSet InsMap;
Chris Lattner4d391482007-12-12 07:09:47 +00001723 // Check and see if instance methods in class interface have been
1724 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001725 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001726 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001727 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001729 // Check and see if properties declared in the interface have either 1)
1730 // an implementation or 2) there is a @synthesize/@dynamic implementation
1731 // of the property in the @implementation.
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001732 if (const ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl))
1733 if (!(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2) ||
Ted Kremenek71207fc2012-01-05 22:47:47 +00001734 IDecl->isObjCRequiresPropertyDefs())
Fariborz Jahanianeb4f2c52012-01-03 19:46:00 +00001735 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001736
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001737 SelectorSet ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001738 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001739 I = IMPDecl->classmeth_begin(),
1740 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001741 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001742
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001743 // Check for type conflict of methods declared in a class/protocol and
1744 // its implementation; if any.
Benjamin Kramer811bfcd2012-05-27 13:28:52 +00001745 SelectorSet InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001746 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1747 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001748 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001749
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001750 // check all methods implemented in category against those declared
1751 // in its primary class.
1752 if (ObjCCategoryImplDecl *CatDecl =
1753 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1754 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001755
Chris Lattner4d391482007-12-12 07:09:47 +00001756 // Check the protocol list for unimplemented methods in the @implementation
1757 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001758 // Check and see if class methods in class interface have been
1759 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Chris Lattnercddc8882009-03-01 00:56:52 +00001761 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001762 for (ObjCInterfaceDecl::all_protocol_iterator
1763 PI = I->all_referenced_protocol_begin(),
1764 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001765 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001766 InsMap, ClsMap, I);
1767 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001768 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1769 Categories; Categories = Categories->getNextClassExtension())
1770 ImplMethodsVsClassMethods(S, IMPDecl,
1771 const_cast<ObjCCategoryDecl*>(Categories),
1772 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001773 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001774 // For extended class, unimplemented methods in its protocols will
1775 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001776 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001777 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1778 E = C->protocol_end(); PI != E; ++PI)
1779 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001780 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001781 // Report unimplemented properties in the category as well.
1782 // When reporting on missing setter/getters, do not report when
1783 // setter/getter is implemented in category's primary class
1784 // implementation.
1785 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1786 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1787 for (ObjCImplementationDecl::instmeth_iterator
1788 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1789 InsMap.insert((*I)->getSelector());
1790 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001791 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001792 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001793 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001794 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001795}
1796
Mike Stump1eb44332009-09-09 15:08:12 +00001797/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001798Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001799Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001800 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001801 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001802 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001803 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001804 for (unsigned i = 0; i != NumElts; ++i) {
1805 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001806 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001807 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001808 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001809 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001810 // Maybe we will complain about the shadowed template parameter.
1811 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1812 // Just pretend that we didn't see the previous declaration.
1813 PrevDecl = 0;
1814 }
1815
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001816 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001817 // GCC apparently allows the following idiom:
1818 //
1819 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1820 // @class XCElementToggler;
1821 //
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001822 // Here we have chosen to ignore the forward class declaration
1823 // with a warning. Since this is the implied behavior.
Richard Smith162e1c12011-04-15 14:24:37 +00001824 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001825 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001826 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001827 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001828 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001829 // a forward class declaration matching a typedef name of a class refers
Fariborz Jahaniane42670b2012-01-24 00:40:15 +00001830 // to the underlying class. Just ignore the forward class with a warning
1831 // as this will force the intended behavior which is to lookup the typedef
1832 // name.
1833 if (isa<ObjCObjectType>(TDD->getUnderlyingType())) {
1834 Diag(AtClassLoc, diag::warn_forward_class_redefinition) << IdentList[i];
1835 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
1836 continue;
1837 }
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001838 }
Chris Lattner4d391482007-12-12 07:09:47 +00001839 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001840
1841 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001842 ObjCInterfaceDecl *PrevIDecl
1843 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001844 ObjCInterfaceDecl *IDecl
1845 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001846 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001847 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001848
Douglas Gregor7723fec2011-12-15 20:29:51 +00001849 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001850 CheckObjCDeclScope(IDecl);
1851 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001852 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001853
1854 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001855}
1856
John McCall0f4c4c42011-06-16 01:15:19 +00001857static bool tryMatchRecordTypes(ASTContext &Context,
1858 Sema::MethodMatchStrategy strategy,
1859 const Type *left, const Type *right);
1860
John McCallf85e1932011-06-15 23:02:42 +00001861static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1862 QualType leftQT, QualType rightQT) {
1863 const Type *left =
1864 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1865 const Type *right =
1866 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1867
1868 if (left == right) return true;
1869
1870 // If we're doing a strict match, the types have to match exactly.
1871 if (strategy == Sema::MMS_strict) return false;
1872
1873 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1874
1875 // Otherwise, use this absurdly complicated algorithm to try to
1876 // validate the basic, low-level compatibility of the two types.
1877
1878 // As a minimum, require the sizes and alignments to match.
1879 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1880 return false;
1881
1882 // Consider all the kinds of non-dependent canonical types:
1883 // - functions and arrays aren't possible as return and parameter types
1884
1885 // - vector types of equal size can be arbitrarily mixed
1886 if (isa<VectorType>(left)) return isa<VectorType>(right);
1887 if (isa<VectorType>(right)) return false;
1888
1889 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001890 // - structs, unions, and Objective-C objects must match more-or-less
1891 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001892 // - everything else should be a scalar
1893 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001894 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001895
John McCall1d9b3b22011-09-09 05:25:32 +00001896 // Make scalars agree in kind, except count bools as chars, and group
1897 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001898 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1899 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1900 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1901 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001902 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1903 leftSK = Type::STK_ObjCObjectPointer;
1904 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1905 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001906
1907 // Note that data member pointers and function member pointers don't
1908 // intermix because of the size differences.
1909
1910 return (leftSK == rightSK);
1911}
Chris Lattner4d391482007-12-12 07:09:47 +00001912
John McCall0f4c4c42011-06-16 01:15:19 +00001913static bool tryMatchRecordTypes(ASTContext &Context,
1914 Sema::MethodMatchStrategy strategy,
1915 const Type *lt, const Type *rt) {
1916 assert(lt && rt && lt != rt);
1917
1918 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1919 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1920 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1921
1922 // Require union-hood to match.
1923 if (left->isUnion() != right->isUnion()) return false;
1924
1925 // Require an exact match if either is non-POD.
1926 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1927 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1928 return false;
1929
1930 // Require size and alignment to match.
1931 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1932
1933 // Require fields to match.
1934 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1935 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1936 for (; li != le && ri != re; ++li, ++ri) {
1937 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1938 return false;
1939 }
1940 return (li == le && ri == re);
1941}
1942
Chris Lattner4d391482007-12-12 07:09:47 +00001943/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1944/// returns true, or false, accordingly.
1945/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001946bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1947 const ObjCMethodDecl *right,
1948 MethodMatchStrategy strategy) {
1949 if (!matchTypes(Context, strategy,
1950 left->getResultType(), right->getResultType()))
1951 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001952
David Blaikie4e4d0842012-03-11 07:00:24 +00001953 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001954 (left->hasAttr<NSReturnsRetainedAttr>()
1955 != right->hasAttr<NSReturnsRetainedAttr>() ||
1956 left->hasAttr<NSConsumesSelfAttr>()
1957 != right->hasAttr<NSConsumesSelfAttr>()))
1958 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001959
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001960 ObjCMethodDecl::param_const_iterator
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001961 li = left->param_begin(), le = left->param_end(), ri = right->param_begin(),
1962 re = right->param_end();
Mike Stump1eb44332009-09-09 15:08:12 +00001963
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00001964 for (; li != le && ri != re; ++li, ++ri) {
John McCallf85e1932011-06-15 23:02:42 +00001965 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001966 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001967
1968 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1969 return false;
1970
David Blaikie4e4d0842012-03-11 07:00:24 +00001971 if (getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001972 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1973 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001974 }
1975 return true;
1976}
1977
Douglas Gregorff310c72012-05-01 23:37:00 +00001978void Sema::addMethodToGlobalList(ObjCMethodList *List, ObjCMethodDecl *Method) {
Douglas Gregor44fae522012-01-25 00:19:56 +00001979 // If the list is empty, make it a singleton list.
1980 if (List->Method == 0) {
1981 List->Method = Method;
1982 List->Next = 0;
Douglas Gregorff310c72012-05-01 23:37:00 +00001983 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00001984 }
1985
1986 // We've seen a method with this name, see if we have already seen this type
1987 // signature.
1988 ObjCMethodList *Previous = List;
1989 for (; List; Previous = List, List = List->Next) {
Douglas Gregor5ac4b692012-01-25 00:49:42 +00001990 if (!MatchTwoMethodDeclarations(Method, List->Method))
Douglas Gregor44fae522012-01-25 00:19:56 +00001991 continue;
1992
1993 ObjCMethodDecl *PrevObjCMethod = List->Method;
1994
1995 // Propagate the 'defined' bit.
1996 if (Method->isDefined())
1997 PrevObjCMethod->setDefined(true);
1998
1999 // If a method is deprecated, push it in the global pool.
2000 // This is used for better diagnostics.
2001 if (Method->isDeprecated()) {
2002 if (!PrevObjCMethod->isDeprecated())
2003 List->Method = Method;
2004 }
2005 // If new method is unavailable, push it into global pool
2006 // unless previous one is deprecated.
2007 if (Method->isUnavailable()) {
2008 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
2009 List->Method = Method;
2010 }
2011
Douglas Gregorff310c72012-05-01 23:37:00 +00002012 return;
Douglas Gregor44fae522012-01-25 00:19:56 +00002013 }
2014
2015 // We have a new signature for an existing method - add it.
2016 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002017 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
Douglas Gregor44fae522012-01-25 00:19:56 +00002018 Previous->Next = new (Mem) ObjCMethodList(Method, 0);
2019}
2020
Sebastian Redldb9d2142010-08-02 23:18:59 +00002021/// \brief Read the contents of the method pool for a given selector from
2022/// external storage.
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002023void Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002024 assert(ExternalSource && "We need an external AST source");
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002025 ExternalSource->ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002026}
2027
Douglas Gregorff310c72012-05-01 23:37:00 +00002028void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002029 bool instance) {
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002030 // Ignore methods of invalid containers.
2031 if (cast<Decl>(Method->getDeclContext())->isInvalidDecl())
Douglas Gregorff310c72012-05-01 23:37:00 +00002032 return;
Argyrios Kyrtzidis9a0b6b42012-03-12 18:34:26 +00002033
Douglas Gregor0d266d62012-01-25 00:59:09 +00002034 if (ExternalSource)
2035 ReadMethodPool(Method->getSelector());
2036
Sebastian Redldb9d2142010-08-02 23:18:59 +00002037 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
Douglas Gregor0d266d62012-01-25 00:59:09 +00002038 if (Pos == MethodPool.end())
2039 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
2040 GlobalMethods())).first;
Douglas Gregor44fae522012-01-25 00:19:56 +00002041
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002042 Method->setDefined(impl);
Douglas Gregor44fae522012-01-25 00:19:56 +00002043
Sebastian Redldb9d2142010-08-02 23:18:59 +00002044 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Douglas Gregorff310c72012-05-01 23:37:00 +00002045 addMethodToGlobalList(&Entry, Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002046}
2047
John McCallf85e1932011-06-15 23:02:42 +00002048/// Determines if this is an "acceptable" loose mismatch in the global
2049/// method pool. This exists mostly as a hack to get around certain
2050/// global mismatches which we can't afford to make warnings / errors.
2051/// Really, what we want is a way to take a method out of the global
2052/// method pool.
2053static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
2054 ObjCMethodDecl *other) {
2055 if (!chosen->isInstanceMethod())
2056 return false;
2057
2058 Selector sel = chosen->getSelector();
2059 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2060 return false;
2061
2062 // Don't complain about mismatches for -length if the method we
2063 // chose has an integral result type.
2064 return (chosen->getResultType()->isIntegerType());
2065}
2066
Sebastian Redldb9d2142010-08-02 23:18:59 +00002067ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002068 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002069 bool warn, bool instance) {
Douglas Gregor0d266d62012-01-25 00:59:09 +00002070 if (ExternalSource)
2071 ReadMethodPool(Sel);
2072
Sebastian Redldb9d2142010-08-02 23:18:59 +00002073 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
Douglas Gregor0d266d62012-01-25 00:59:09 +00002074 if (Pos == MethodPool.end())
2075 return 0;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002076
Sebastian Redldb9d2142010-08-02 23:18:59 +00002077 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002078
Sebastian Redldb9d2142010-08-02 23:18:59 +00002079 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002080 bool issueDiagnostic = false, issueError = false;
2081
2082 // We support a warning which complains about *any* difference in
2083 // method signature.
2084 bool strictSelectorMatch =
2085 (receiverIdOrClass && warn &&
2086 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2087 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002088 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002089 if (strictSelectorMatch)
2090 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002091 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2092 MMS_strict)) {
2093 issueDiagnostic = true;
2094 break;
2095 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002096 }
2097
John McCallf85e1932011-06-15 23:02:42 +00002098 // If we didn't see any strict differences, we won't see any loose
2099 // differences. In ARC, however, we also need to check for loose
2100 // mismatches, because most of them are errors.
2101 if (!strictSelectorMatch ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002102 (issueDiagnostic && getLangOpts().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002103 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002104 // This checks if the methods differ in type mismatch.
2105 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2106 MMS_loose) &&
2107 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2108 issueDiagnostic = true;
David Blaikie4e4d0842012-03-11 07:00:24 +00002109 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002110 issueError = true;
2111 break;
2112 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002113 }
2114
John McCallf85e1932011-06-15 23:02:42 +00002115 if (issueDiagnostic) {
2116 if (issueError)
2117 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2118 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002119 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2120 else
2121 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002122
2123 Diag(MethList.Method->getLocStart(),
2124 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002125 << MethList.Method->getSourceRange();
2126 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2127 Diag(Next->Method->getLocStart(), diag::note_also_found)
2128 << Next->Method->getSourceRange();
2129 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002130 }
2131 return MethList.Method;
2132}
2133
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002134ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002135 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2136 if (Pos == MethodPool.end())
2137 return 0;
2138
2139 GlobalMethods &Methods = Pos->second;
2140
2141 if (Methods.first.Method && Methods.first.Method->isDefined())
2142 return Methods.first.Method;
2143 if (Methods.second.Method && Methods.second.Method->isDefined())
2144 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002145 return 0;
2146}
2147
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002148/// DiagnoseDuplicateIvars -
2149/// Check for duplicate ivars in the entire class at the start of
2150/// @implementation. This becomes necesssary because class extension can
2151/// add ivars to a class in random order which will not be known until
2152/// class's @implementation is seen.
2153void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2154 ObjCInterfaceDecl *SID) {
2155 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2156 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
David Blaikie581deb32012-06-06 20:45:41 +00002157 ObjCIvarDecl* Ivar = *IVI;
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002158 if (Ivar->isInvalidDecl())
2159 continue;
2160 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2161 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2162 if (prevIvar) {
2163 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2164 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2165 Ivar->setInvalidDecl();
2166 }
2167 }
2168 }
2169}
2170
Erik Verbruggend64251f2011-12-06 09:25:23 +00002171Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2172 switch (CurContext->getDeclKind()) {
2173 case Decl::ObjCInterface:
2174 return Sema::OCK_Interface;
2175 case Decl::ObjCProtocol:
2176 return Sema::OCK_Protocol;
2177 case Decl::ObjCCategory:
2178 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2179 return Sema::OCK_ClassExtension;
2180 else
2181 return Sema::OCK_Category;
2182 case Decl::ObjCImplementation:
2183 return Sema::OCK_Implementation;
2184 case Decl::ObjCCategoryImpl:
2185 return Sema::OCK_CategoryImplementation;
2186
2187 default:
2188 return Sema::OCK_None;
2189 }
2190}
2191
Steve Naroffa56f6162007-12-18 01:30:32 +00002192// Note: For class/category implemenations, allMethods/allProperties is
2193// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002194Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2195 Decl **allMethods, unsigned allNum,
2196 Decl **allProperties, unsigned pNum,
2197 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002198
Erik Verbruggend64251f2011-12-06 09:25:23 +00002199 if (getObjCContainerKind() == Sema::OCK_None)
2200 return 0;
2201
2202 assert(AtEnd.isValid() && "Invalid location for '@end'");
2203
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002204 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2205 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002206
Mike Stump1eb44332009-09-09 15:08:12 +00002207 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002208 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2209 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002210 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002211
Steve Naroff0701bbb2009-01-08 17:28:14 +00002212 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2213 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2214 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2215
Chris Lattner4d391482007-12-12 07:09:47 +00002216 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002217 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002218 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002219
2220 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002221 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002222 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002223 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002224 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002225 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002226 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002227 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002228 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002229 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002230 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002231 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002232 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002233 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002234 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002235 if (!Context.getSourceManager().isInSystemHeader(
2236 Method->getLocation()))
2237 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2238 << Method->getDeclName();
2239 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2240 }
Chris Lattner4d391482007-12-12 07:09:47 +00002241 InsMap[Method->getSelector()] = Method;
2242 /// The following allows us to typecheck messages to "id".
Douglas Gregorff310c72012-05-01 23:37:00 +00002243 AddInstanceMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002244 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002245 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002246 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002247 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002248 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002249 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002250 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002251 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002252 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002253 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002254 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002255 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002256 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002257 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002258 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002259 if (!Context.getSourceManager().isInSystemHeader(
2260 Method->getLocation()))
2261 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2262 << Method->getDeclName();
2263 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2264 }
Chris Lattner4d391482007-12-12 07:09:47 +00002265 ClsMap[Method->getSelector()] = Method;
Douglas Gregorff310c72012-05-01 23:37:00 +00002266 AddFactoryMethodToGlobalPool(Method);
Chris Lattner4d391482007-12-12 07:09:47 +00002267 }
2268 }
2269 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002270 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002271 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002272 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002273 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002274 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002275 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002276 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002277 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002278 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002279
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002280 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002281 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002282 if (C->IsClassExtension()) {
2283 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2284 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002285 }
Chris Lattner4d391482007-12-12 07:09:47 +00002286 }
Steve Naroff09c47192009-01-09 15:36:25 +00002287 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002288 if (CDecl->getIdentifier())
2289 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2290 // user-defined setter/getter. It also synthesizes setter/getter methods
2291 // and adds them to the DeclContext and global method pools.
2292 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2293 E = CDecl->prop_end();
2294 I != E; ++I)
David Blaikie581deb32012-06-06 20:45:41 +00002295 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002296 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002297 }
2298 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002299 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002300 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002301 // Any property declared in a class extension might have user
2302 // declared setter or getter in current class extension or one
2303 // of the other class extensions. Mark them as synthesized as
2304 // property will be synthesized when property with same name is
2305 // seen in the @implementation.
2306 for (const ObjCCategoryDecl *ClsExtDecl =
2307 IDecl->getFirstClassExtension();
2308 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2309 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2310 E = ClsExtDecl->prop_end(); I != E; ++I) {
David Blaikie581deb32012-06-06 20:45:41 +00002311 ObjCPropertyDecl *Property = *I;
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002312 // Skip over properties declared @dynamic
2313 if (const ObjCPropertyImplDecl *PIDecl
2314 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2315 if (PIDecl->getPropertyImplementation()
2316 == ObjCPropertyImplDecl::Dynamic)
2317 continue;
2318
2319 for (const ObjCCategoryDecl *CExtDecl =
2320 IDecl->getFirstClassExtension();
2321 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2322 if (ObjCMethodDecl *GetterMethod =
2323 CExtDecl->getInstanceMethod(Property->getGetterName()))
2324 GetterMethod->setSynthesized(true);
2325 if (!Property->isReadOnly())
2326 if (ObjCMethodDecl *SetterMethod =
2327 CExtDecl->getInstanceMethod(Property->getSetterName()))
2328 SetterMethod->setSynthesized(true);
2329 }
2330 }
2331 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002332 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002333 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002334 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002335
Patrick Beardb2f68202012-04-06 18:12:22 +00002336 bool HasRootClassAttr = IDecl->hasAttr<ObjCRootClassAttr>();
2337 if (IDecl->getSuperClass() == NULL) {
2338 // This class has no superclass, so check that it has been marked with
2339 // __attribute((objc_root_class)).
2340 if (!HasRootClassAttr) {
2341 SourceLocation DeclLoc(IDecl->getLocation());
2342 SourceLocation SuperClassLoc(PP.getLocForEndOfToken(DeclLoc));
2343 Diag(DeclLoc, diag::warn_objc_root_class_missing)
2344 << IDecl->getIdentifier();
2345 // See if NSObject is in the current scope, and if it is, suggest
2346 // adding " : NSObject " to the class declaration.
2347 NamedDecl *IF = LookupSingleName(TUScope,
2348 NSAPIObj->getNSClassId(NSAPI::ClassId_NSObject),
2349 DeclLoc, LookupOrdinaryName);
2350 ObjCInterfaceDecl *NSObjectDecl = dyn_cast_or_null<ObjCInterfaceDecl>(IF);
2351 if (NSObjectDecl && NSObjectDecl->getDefinition()) {
2352 Diag(SuperClassLoc, diag::note_objc_needs_superclass)
2353 << FixItHint::CreateInsertion(SuperClassLoc, " : NSObject ");
2354 } else {
2355 Diag(SuperClassLoc, diag::note_objc_needs_superclass);
2356 }
2357 }
2358 } else if (HasRootClassAttr) {
2359 // Complain that only root classes may have this attribute.
2360 Diag(IDecl->getLocation(), diag::err_objc_root_class_subclass);
2361 }
2362
2363 if (LangOpts.ObjCNonFragileABI2) {
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002364 while (IDecl->getSuperClass()) {
2365 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2366 IDecl = IDecl->getSuperClass();
2367 }
Patrick Beardb2f68202012-04-06 18:12:22 +00002368 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002369 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002370 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002371 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002372 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002373 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002374
Chris Lattner4d391482007-12-12 07:09:47 +00002375 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002376 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002377 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002378 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002379 Categories; Categories = Categories->getNextClassCategory()) {
2380 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002381 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002382 break;
2383 }
2384 }
2385 }
2386 }
Chris Lattner682bf922009-03-29 16:50:03 +00002387 if (isInterfaceDeclKind) {
2388 // Reject invalid vardecls.
2389 for (unsigned i = 0; i != tuvNum; i++) {
2390 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2391 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2392 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002393 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002394 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002395 }
Chris Lattner682bf922009-03-29 16:50:03 +00002396 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002397 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002398 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002399
2400 for (unsigned i = 0; i != tuvNum; i++) {
2401 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002402 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2403 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002404 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2405 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002406
2407 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002408}
2409
2410
2411/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2412/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002413static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002414CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002415 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002416}
2417
Ted Kremenek422bae72010-04-18 04:59:38 +00002418static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002419bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2420 const AttrVec &A) {
2421 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002422 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002423 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002424 return false;
2425
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002426 // method declared in interface has no attribute.
2427 // But implementation has attributes. This is invalid
2428 if (!IMD->hasAttrs())
2429 return true;
2430
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002431 const AttrVec &D = IMD->getAttrs();
2432 if (D.size() != A.size())
2433 return true;
2434
2435 // attributes on method declaration and definition must match exactly.
2436 // Note that we have at most a couple of attributes on methods, so this
2437 // n*n search is good enough.
2438 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2439 bool match = false;
2440 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2441 if ((*i)->getKind() == (*i1)->getKind()) {
2442 match = true;
2443 break;
2444 }
2445 }
2446 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002447 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002448 }
Sean Huntcf807c42010-08-18 23:23:40 +00002449 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002450}
2451
Douglas Gregor926df6c2011-06-11 01:09:30 +00002452/// \brief Check whether the declared result type of the given Objective-C
2453/// method declaration is compatible with the method's class.
2454///
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002455static Sema::ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002456CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2457 ObjCInterfaceDecl *CurrentClass) {
2458 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002459
2460 // If an Objective-C method inherits its related result type, then its
2461 // declared result type must be compatible with its own class type. The
2462 // declared result type is compatible if:
2463 if (const ObjCObjectPointerType *ResultObjectType
2464 = ResultType->getAs<ObjCObjectPointerType>()) {
2465 // - it is id or qualified id, or
2466 if (ResultObjectType->isObjCIdType() ||
2467 ResultObjectType->isObjCQualifiedIdType())
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002468 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002469
2470 if (CurrentClass) {
2471 if (ObjCInterfaceDecl *ResultClass
2472 = ResultObjectType->getInterfaceDecl()) {
2473 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002474 if (declaresSameEntity(CurrentClass, ResultClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002475 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002476
2477 // - it is a superclass of the method's class type
2478 if (ResultClass->isSuperClassOf(CurrentClass))
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002479 return Sema::RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002480 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002481 } else {
2482 // Any Objective-C pointer type might be acceptable for a protocol
2483 // method; we just don't know.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002484 return Sema::RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002485 }
2486 }
2487
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002488 return Sema::RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002489}
2490
John McCall6c2c2502011-07-22 02:45:48 +00002491namespace {
2492/// A helper class for searching for methods which a particular method
2493/// overrides.
2494class OverrideSearch {
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002495public:
John McCall6c2c2502011-07-22 02:45:48 +00002496 Sema &S;
2497 ObjCMethodDecl *Method;
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002498 llvm::SmallPtrSet<ObjCMethodDecl*, 4> Overridden;
John McCall6c2c2502011-07-22 02:45:48 +00002499 bool Recursive;
2500
2501public:
2502 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2503 Selector selector = method->getSelector();
2504
2505 // Bypass this search if we've never seen an instance/class method
2506 // with this selector before.
2507 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2508 if (it == S.MethodPool.end()) {
2509 if (!S.ExternalSource) return;
Douglas Gregor5ac4b692012-01-25 00:49:42 +00002510 S.ReadMethodPool(selector);
2511
2512 it = S.MethodPool.find(selector);
2513 if (it == S.MethodPool.end())
2514 return;
John McCall6c2c2502011-07-22 02:45:48 +00002515 }
2516 ObjCMethodList &list =
2517 method->isInstanceMethod() ? it->second.first : it->second.second;
2518 if (!list.Method) return;
2519
2520 ObjCContainerDecl *container
2521 = cast<ObjCContainerDecl>(method->getDeclContext());
2522
2523 // Prevent the search from reaching this container again. This is
2524 // important with categories, which override methods from the
2525 // interface and each other.
Douglas Gregorc9683342012-05-03 21:25:24 +00002526 if (ObjCCategoryDecl *Category = dyn_cast<ObjCCategoryDecl>(container)) {
2527 searchFromContainer(container);
Douglas Gregordd872242012-05-17 22:39:14 +00002528 if (ObjCInterfaceDecl *Interface = Category->getClassInterface())
2529 searchFromContainer(Interface);
Douglas Gregorc9683342012-05-03 21:25:24 +00002530 } else {
2531 searchFromContainer(container);
2532 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002533 }
John McCall6c2c2502011-07-22 02:45:48 +00002534
Daniel Dunbarb732fce2012-02-29 03:04:05 +00002535 typedef llvm::SmallPtrSet<ObjCMethodDecl*, 128>::iterator iterator;
John McCall6c2c2502011-07-22 02:45:48 +00002536 iterator begin() const { return Overridden.begin(); }
2537 iterator end() const { return Overridden.end(); }
2538
2539private:
2540 void searchFromContainer(ObjCContainerDecl *container) {
2541 if (container->isInvalidDecl()) return;
2542
2543 switch (container->getDeclKind()) {
2544#define OBJCCONTAINER(type, base) \
2545 case Decl::type: \
2546 searchFrom(cast<type##Decl>(container)); \
2547 break;
2548#define ABSTRACT_DECL(expansion)
2549#define DECL(type, base) \
2550 case Decl::type:
2551#include "clang/AST/DeclNodes.inc"
2552 llvm_unreachable("not an ObjC container!");
2553 }
2554 }
2555
2556 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002557 if (!protocol->hasDefinition())
2558 return;
2559
John McCall6c2c2502011-07-22 02:45:48 +00002560 // A method in a protocol declaration overrides declarations from
2561 // referenced ("parent") protocols.
2562 search(protocol->getReferencedProtocols());
2563 }
2564
2565 void searchFrom(ObjCCategoryDecl *category) {
2566 // A method in a category declaration overrides declarations from
2567 // the main class and from protocols the category references.
Douglas Gregorc9683342012-05-03 21:25:24 +00002568 // The main class is handled in the constructor.
John McCall6c2c2502011-07-22 02:45:48 +00002569 search(category->getReferencedProtocols());
2570 }
2571
2572 void searchFrom(ObjCCategoryImplDecl *impl) {
2573 // A method in a category definition that has a category
2574 // declaration overrides declarations from the category
2575 // declaration.
2576 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2577 search(category);
Douglas Gregordd872242012-05-17 22:39:14 +00002578 if (ObjCInterfaceDecl *Interface = category->getClassInterface())
2579 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002580
2581 // Otherwise it overrides declarations from the class.
Douglas Gregordd872242012-05-17 22:39:14 +00002582 } else if (ObjCInterfaceDecl *Interface = impl->getClassInterface()) {
2583 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002584 }
2585 }
2586
2587 void searchFrom(ObjCInterfaceDecl *iface) {
2588 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002589 if (!iface->hasDefinition())
2590 return;
2591
John McCall6c2c2502011-07-22 02:45:48 +00002592 // - categories,
2593 for (ObjCCategoryDecl *category = iface->getCategoryList();
2594 category; category = category->getNextClassCategory())
2595 search(category);
2596
2597 // - the super class, and
2598 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2599 search(super);
2600
2601 // - any referenced protocols.
2602 search(iface->getReferencedProtocols());
2603 }
2604
2605 void searchFrom(ObjCImplementationDecl *impl) {
2606 // A method in a class implementation overrides declarations from
2607 // the class interface.
Douglas Gregordd872242012-05-17 22:39:14 +00002608 if (ObjCInterfaceDecl *Interface = impl->getClassInterface())
2609 search(Interface);
John McCall6c2c2502011-07-22 02:45:48 +00002610 }
2611
2612
2613 void search(const ObjCProtocolList &protocols) {
2614 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2615 i != e; ++i)
2616 search(*i);
2617 }
2618
2619 void search(ObjCContainerDecl *container) {
John McCall6c2c2502011-07-22 02:45:48 +00002620 // Check for a method in this container which matches this selector.
2621 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2622 Method->isInstanceMethod());
2623
2624 // If we find one, record it and bail out.
2625 if (meth) {
2626 Overridden.insert(meth);
2627 return;
2628 }
2629
2630 // Otherwise, search for methods that a hypothetical method here
2631 // would have overridden.
2632
2633 // Note that we're now in a recursive case.
2634 Recursive = true;
2635
2636 searchFromContainer(container);
2637 }
2638};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002639}
2640
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002641void Sema::CheckObjCMethodOverrides(ObjCMethodDecl *ObjCMethod,
2642 ObjCInterfaceDecl *CurrentClass,
2643 ResultTypeCompatibilityKind RTC) {
2644 // Search for overridden methods and merge information down from them.
2645 OverrideSearch overrides(*this, ObjCMethod);
2646 // Keep track if the method overrides any method in the class's base classes,
2647 // its protocols, or its categories' protocols; we will keep that info
2648 // in the ObjCMethodDecl.
2649 // For this info, a method in an implementation is not considered as
2650 // overriding the same method in the interface or its categories.
2651 bool hasOverriddenMethodsInBaseOrProtocol = false;
2652 for (OverrideSearch::iterator
2653 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2654 ObjCMethodDecl *overridden = *i;
2655
2656 if (isa<ObjCProtocolDecl>(overridden->getDeclContext()) ||
2657 CurrentClass != overridden->getClassInterface() ||
2658 overridden->isOverriding())
2659 hasOverriddenMethodsInBaseOrProtocol = true;
2660
2661 // Propagate down the 'related result type' bit from overridden methods.
2662 if (RTC != Sema::RTC_Incompatible && overridden->hasRelatedResultType())
2663 ObjCMethod->SetRelatedResultType();
2664
2665 // Then merge the declarations.
2666 mergeObjCMethodDecls(ObjCMethod, overridden);
2667
2668 if (ObjCMethod->isImplicit() && overridden->isImplicit())
2669 continue; // Conflicting properties are detected elsewhere.
2670
2671 // Check for overriding methods
2672 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2673 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2674 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2675 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
2676
2677 if (CurrentClass && overridden->getDeclContext() != CurrentClass &&
2678 isa<ObjCInterfaceDecl>(overridden->getDeclContext())) {
2679 ObjCMethodDecl::param_iterator ParamI = ObjCMethod->param_begin(),
2680 E = ObjCMethod->param_end();
Douglas Gregor0a4a23a2012-05-17 23:13:29 +00002681 ObjCMethodDecl::param_iterator PrevI = overridden->param_begin(),
2682 PrevE = overridden->param_end();
2683 for (; ParamI != E && PrevI != PrevE; ++ParamI, ++PrevI) {
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002684 assert(PrevI != overridden->param_end() && "Param mismatch");
2685 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2686 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
2687 // If type of argument of method in this class does not match its
2688 // respective argument type in the super class method, issue warning;
2689 if (!Context.typesAreCompatible(T1, T2)) {
2690 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
2691 << T1 << T2;
2692 Diag(overridden->getLocation(), diag::note_previous_declaration);
2693 break;
2694 }
2695 }
2696 }
2697 }
2698
2699 ObjCMethod->setOverriding(hasOverriddenMethodsInBaseOrProtocol);
2700}
2701
John McCalld226f652010-08-21 09:40:31 +00002702Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002703 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002704 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002705 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002706 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002707 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002708 Selector Sel,
2709 // optional arguments. The number of types/arguments is obtained
2710 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002711 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002712 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002713 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002714 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002715 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002716 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002717 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002718 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002719 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002720 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2721 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002722 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002723
Douglas Gregore97179c2011-09-08 01:46:34 +00002724 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002725 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002726 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002727 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002728
Steve Naroffccef3712009-02-20 22:59:16 +00002729 // Methods cannot return interface types. All ObjC objects are
2730 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002731 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002732 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2733 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002734 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002735 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002736
2737 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002738 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002739 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002740 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002741 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002742 }
Mike Stump1eb44332009-09-09 15:08:12 +00002743
2744 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002745 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002746 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002747 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002748 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002749 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002750 /*isSynthesized=*/false,
2751 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002752 MethodDeclKind == tok::objc_optional
2753 ? ObjCMethodDecl::Optional
2754 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002755 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002756
Chris Lattner5f9e2722011-07-23 10:55:15 +00002757 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002758
Chris Lattner7db638d2009-04-11 19:42:43 +00002759 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002760 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002761 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002762
Chris Lattnere294d3f2009-04-11 18:57:04 +00002763 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002764 ArgType = Context.getObjCIdType();
2765 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002766 } else {
John McCall58e46772009-10-23 21:48:59 +00002767 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002768 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002769 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002770 }
Mike Stump1eb44332009-09-09 15:08:12 +00002771
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002772 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2773 LookupOrdinaryName, ForRedeclaration);
2774 LookupName(R, S);
2775 if (R.isSingleResult()) {
2776 NamedDecl *PrevDecl = R.getFoundDecl();
2777 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002778 Diag(ArgInfo[i].NameLoc,
2779 (MethodDefinition ? diag::warn_method_param_redefinition
2780 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002781 << ArgInfo[i].Name;
2782 Diag(PrevDecl->getLocation(),
2783 diag::note_previous_declaration);
2784 }
2785 }
2786
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002787 SourceLocation StartLoc = DI
2788 ? DI->getTypeLoc().getBeginLoc()
2789 : ArgInfo[i].NameLoc;
2790
John McCall81ef3e62011-04-23 02:46:06 +00002791 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2792 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2793 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002794
John McCall70798862011-05-02 00:30:12 +00002795 Param->setObjCMethodScopeInfo(i);
2796
Chris Lattner0ed844b2008-04-04 06:12:32 +00002797 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002798 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002799
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002800 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002801 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Fariborz Jahanian47b1d962012-01-14 18:44:35 +00002803 if (Param->hasAttr<BlocksAttr>()) {
2804 Diag(Param->getLocation(), diag::err_block_on_nonlocal);
2805 Param->setInvalidDecl();
2806 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002807 S->AddDecl(Param);
2808 IdResolver.AddDecl(Param);
2809
Chris Lattner0ed844b2008-04-04 06:12:32 +00002810 Params.push_back(Param);
2811 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002812
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002813 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002814 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002815 QualType ArgType = Param->getType();
2816 if (ArgType.isNull())
2817 ArgType = Context.getObjCIdType();
2818 else
2819 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002820 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002821 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002822 Diag(Param->getLocation(),
2823 diag::err_object_cannot_be_passed_returned_by_value)
2824 << 1 << ArgType;
2825 Param->setInvalidDecl();
2826 }
2827 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002828
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002829 Params.push_back(Param);
2830 }
2831
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002832 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002833 ObjCMethod->setObjCDeclQualifier(
2834 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002835
2836 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002837 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002838
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002839 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002840 const ObjCMethodDecl *PrevMethod = 0;
2841 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002842 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002843 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2844 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002845 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002846 PrevMethod = ImpDecl->getClassMethod(Sel);
2847 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002848 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002849
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002850 ObjCMethodDecl *IMD = 0;
2851 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2852 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2853 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002854 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002855 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002856 SourceLocation MethodLoc = IMD->getLocation();
2857 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2858 Diag(EndLoc, diag::warn_attribute_method_def);
Ted Kremenek3306ec12012-02-27 22:55:11 +00002859 Diag(MethodLoc, diag::note_method_declared_at)
2860 << ObjCMethod->getDeclName();
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002861 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002862 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002863 } else {
2864 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002865 }
John McCall6c2c2502011-07-22 02:45:48 +00002866
Chris Lattner4d391482007-12-12 07:09:47 +00002867 if (PrevMethod) {
2868 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002869 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002870 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002871 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002872 }
John McCall54abf7d2009-11-04 02:18:39 +00002873
Douglas Gregor926df6c2011-06-11 01:09:30 +00002874 // If this Objective-C method does not have a related result type, but we
2875 // are allowed to infer related result types, try to do so based on the
2876 // method family.
2877 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2878 if (!CurrentClass) {
2879 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2880 CurrentClass = Cat->getClassInterface();
2881 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2882 CurrentClass = Impl->getClassInterface();
2883 else if (ObjCCategoryImplDecl *CatImpl
2884 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2885 CurrentClass = CatImpl->getClassInterface();
2886 }
John McCall6c2c2502011-07-22 02:45:48 +00002887
Douglas Gregore97179c2011-09-08 01:46:34 +00002888 ResultTypeCompatibilityKind RTC
2889 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002890
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002891 CheckObjCMethodOverrides(ObjCMethod, CurrentClass, RTC);
John McCall6c2c2502011-07-22 02:45:48 +00002892
John McCallf85e1932011-06-15 23:02:42 +00002893 bool ARCError = false;
David Blaikie4e4d0842012-03-11 07:00:24 +00002894 if (getLangOpts().ObjCAutoRefCount)
John McCallf85e1932011-06-15 23:02:42 +00002895 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2896
Douglas Gregore97179c2011-09-08 01:46:34 +00002897 // Infer the related result type when possible.
Argyrios Kyrtzidise15db6f2012-05-09 16:12:57 +00002898 if (!ARCError && RTC == Sema::RTC_Compatible &&
Douglas Gregore97179c2011-09-08 01:46:34 +00002899 !ObjCMethod->hasRelatedResultType() &&
2900 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002901 bool InferRelatedResultType = false;
2902 switch (ObjCMethod->getMethodFamily()) {
2903 case OMF_None:
2904 case OMF_copy:
2905 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002906 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002907 case OMF_mutableCopy:
2908 case OMF_release:
2909 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002910 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002911 break;
2912
2913 case OMF_alloc:
2914 case OMF_new:
2915 InferRelatedResultType = ObjCMethod->isClassMethod();
2916 break;
2917
2918 case OMF_init:
2919 case OMF_autorelease:
2920 case OMF_retain:
2921 case OMF_self:
2922 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2923 break;
2924 }
2925
John McCall6c2c2502011-07-22 02:45:48 +00002926 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002927 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002928 }
2929
John McCalld226f652010-08-21 09:40:31 +00002930 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002931}
2932
Chris Lattnercc98eac2008-12-17 07:13:27 +00002933bool Sema::CheckObjCDeclScope(Decl *D) {
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002934 // Following is also an error. But it is caused by a missing @end
2935 // and diagnostic is issued elsewhere.
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00002936 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext()))
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002937 return false;
Argyrios Kyrtzidisfce79eb2012-03-23 23:24:23 +00002938
2939 // If we switched context to translation unit while we are still lexically in
2940 // an objc container, it means the parser missed emitting an error.
2941 if (isa<TranslationUnitDecl>(getCurLexicalContext()->getRedeclContext()))
2942 return false;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002943
Anders Carlsson15281452008-11-04 16:57:32 +00002944 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2945 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002946
Anders Carlsson15281452008-11-04 16:57:32 +00002947 return true;
2948}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002949
Chris Lattnercc98eac2008-12-17 07:13:27 +00002950/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2951/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002952void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002953 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002954 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002955 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002956 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002957 if (!Class) {
2958 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2959 return;
2960 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002961 if (LangOpts.ObjCNonFragileABI) {
2962 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2963 return;
2964 }
Mike Stump1eb44332009-09-09 15:08:12 +00002965
Chris Lattnercc98eac2008-12-17 07:13:27 +00002966 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002967 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002968 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002969 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002970 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002971 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002972 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002973 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2974 /*FIXME: StartL=*/ID->getLocation(),
2975 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002976 ID->getIdentifier(), ID->getType(),
2977 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002978 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002979 }
Mike Stump1eb44332009-09-09 15:08:12 +00002980
Chris Lattnercc98eac2008-12-17 07:13:27 +00002981 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002982 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002983 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002984 FieldDecl *FD = cast<FieldDecl>(*D);
David Blaikie4e4d0842012-03-11 07:00:24 +00002985 if (getLangOpts().CPlusPlus)
Chris Lattnercc98eac2008-12-17 07:13:27 +00002986 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002987 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002988 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002989 }
2990}
2991
Douglas Gregor160b5632010-04-26 17:32:49 +00002992/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002993VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2994 SourceLocation StartLoc,
2995 SourceLocation IdLoc,
2996 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002997 bool Invalid) {
2998 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2999 // duration shall not be qualified by an address-space qualifier."
3000 // Since all parameters have automatic store duration, they can not have
3001 // an address space.
3002 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003003 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00003004 Invalid = true;
3005 }
3006
3007 // An @catch parameter must be an unqualified object pointer type;
3008 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
3009 if (Invalid) {
3010 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00003011 } else if (T->isDependentType()) {
3012 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00003013 } else if (!T->isObjCObjectPointerType()) {
3014 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003015 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00003016 } else if (T->isObjCQualifiedIdType()) {
3017 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003018 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00003019 }
3020
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003021 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
3022 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00003023 New->setExceptionVariable(true);
3024
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003025 // In ARC, infer 'retaining' for variables of retainable type.
David Blaikie4e4d0842012-03-11 07:00:24 +00003026 if (getLangOpts().ObjCAutoRefCount && inferObjCARCLifetime(New))
Douglas Gregor9aab9c42011-12-10 01:22:52 +00003027 Invalid = true;
3028
Douglas Gregor160b5632010-04-26 17:32:49 +00003029 if (Invalid)
3030 New->setInvalidDecl();
3031 return New;
3032}
3033
John McCalld226f652010-08-21 09:40:31 +00003034Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00003035 const DeclSpec &DS = D.getDeclSpec();
3036
3037 // We allow the "register" storage class on exception variables because
3038 // GCC did, but we drop it completely. Any other storage class is an error.
3039 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
3040 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
3041 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
3042 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
3043 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
3044 << DS.getStorageClassSpec();
3045 }
3046 if (D.getDeclSpec().isThreadSpecified())
3047 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
3048 D.getMutableDeclSpec().ClearStorageClassSpecs();
3049
3050 DiagnoseFunctionSpecifiers(D);
3051
3052 // Check that there are no default arguments inside the type of this
3053 // exception object (C++ only).
David Blaikie4e4d0842012-03-11 07:00:24 +00003054 if (getLangOpts().CPlusPlus)
Douglas Gregor160b5632010-04-26 17:32:49 +00003055 CheckExtraCXXDefaultArguments(D);
3056
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00003057 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00003058 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00003059
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00003060 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
3061 D.getSourceRange().getBegin(),
3062 D.getIdentifierLoc(),
3063 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00003064 D.isInvalidType());
3065
3066 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
3067 if (D.getCXXScopeSpec().isSet()) {
3068 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
3069 << D.getCXXScopeSpec().getRange();
3070 New->setInvalidDecl();
3071 }
3072
3073 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00003074 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00003075 if (D.getIdentifier())
3076 IdResolver.AddDecl(New);
3077
3078 ProcessDeclAttributes(S, New, D);
3079
3080 if (New->hasAttr<BlocksAttr>())
3081 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00003082 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00003083}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003084
3085/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003086/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003087void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003088 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003089 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
3090 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003091 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00003092 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003093 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003094 }
3095}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003096
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003097void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003098 // Load referenced selectors from the external source.
3099 if (ExternalSource) {
3100 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3101 ExternalSource->ReadReferencedSelectors(Sels);
3102 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3103 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3104 }
3105
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003106 // Warning will be issued only when selector table is
3107 // generated (which means there is at lease one implementation
3108 // in the TU). This is to match gcc's behavior.
3109 if (ReferencedSelectors.empty() ||
3110 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003111 return;
3112 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3113 ReferencedSelectors.begin(),
3114 E = ReferencedSelectors.end(); S != E; ++S) {
3115 Selector Sel = (*S).first;
3116 if (!LookupImplementedMethodInGlobalPool(Sel))
3117 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3118 }
3119 return;
3120}