blob: e76971188ff9f06236789cc2de573b9b608392ce [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"
John McCall50df6ae2010-08-25 07:03:20 +000027#include "llvm/ADT/DenseSet.h"
28
Chris Lattner4d391482007-12-12 07:09:47 +000029using namespace clang;
30
John McCallf85e1932011-06-15 23:02:42 +000031/// Check whether the given method, which must be in the 'init'
32/// family, is a valid member of that family.
33///
34/// \param receiverTypeIfCall - if null, check this as if declaring it;
35/// if non-null, check this as if making a call to it with the given
36/// receiver type
37///
38/// \return true to indicate that there was an error and appropriate
39/// actions were taken
40bool Sema::checkInitMethod(ObjCMethodDecl *method,
41 QualType receiverTypeIfCall) {
42 if (method->isInvalidDecl()) return true;
43
44 // This castAs is safe: methods that don't return an object
45 // pointer won't be inferred as inits and will reject an explicit
46 // objc_method_family(init).
47
48 // We ignore protocols here. Should we? What about Class?
49
50 const ObjCObjectType *result = method->getResultType()
51 ->castAs<ObjCObjectPointerType>()->getObjectType();
52
53 if (result->isObjCId()) {
54 return false;
55 } else if (result->isObjCClass()) {
56 // fall through: always an error
57 } else {
58 ObjCInterfaceDecl *resultClass = result->getInterface();
59 assert(resultClass && "unexpected object type!");
60
61 // It's okay for the result type to still be a forward declaration
62 // if we're checking an interface declaration.
Douglas Gregor7723fec2011-12-15 20:29:51 +000063 if (!resultClass->hasDefinition()) {
John McCallf85e1932011-06-15 23:02:42 +000064 if (receiverTypeIfCall.isNull() &&
65 !isa<ObjCImplementationDecl>(method->getDeclContext()))
66 return false;
67
68 // Otherwise, we try to compare class types.
69 } else {
70 // If this method was declared in a protocol, we can't check
71 // anything unless we have a receiver type that's an interface.
72 const ObjCInterfaceDecl *receiverClass = 0;
73 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
74 if (receiverTypeIfCall.isNull())
75 return false;
76
77 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
78 ->getInterfaceDecl();
79
80 // This can be null for calls to e.g. id<Foo>.
81 if (!receiverClass) return false;
82 } else {
83 receiverClass = method->getClassInterface();
84 assert(receiverClass && "method not associated with a class!");
85 }
86
87 // If either class is a subclass of the other, it's fine.
88 if (receiverClass->isSuperClassOf(resultClass) ||
89 resultClass->isSuperClassOf(receiverClass))
90 return false;
91 }
92 }
93
94 SourceLocation loc = method->getLocation();
95
96 // If we're in a system header, and this is not a call, just make
97 // the method unusable.
98 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
99 method->addAttr(new (Context) UnavailableAttr(loc, Context,
100 "init method returns a type unrelated to its receiver type"));
101 return true;
102 }
103
104 // Otherwise, it's an error.
105 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
106 method->setInvalidDecl();
107 return true;
108}
109
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000110void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000111 const ObjCMethodDecl *Overridden,
112 bool IsImplementation) {
113 if (Overridden->hasRelatedResultType() &&
114 !NewMethod->hasRelatedResultType()) {
115 // This can only happen when the method follows a naming convention that
116 // implies a related result type, and the original (overridden) method has
117 // a suitable return type, but the new (overriding) method does not have
118 // a suitable return type.
119 QualType ResultType = NewMethod->getResultType();
120 SourceRange ResultTypeRange;
121 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000122 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000123 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
124
125 // Figure out which class this method is part of, if any.
126 ObjCInterfaceDecl *CurrentClass
127 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
128 if (!CurrentClass) {
129 DeclContext *DC = NewMethod->getDeclContext();
130 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
131 CurrentClass = Cat->getClassInterface();
132 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
133 CurrentClass = Impl->getClassInterface();
134 else if (ObjCCategoryImplDecl *CatImpl
135 = dyn_cast<ObjCCategoryImplDecl>(DC))
136 CurrentClass = CatImpl->getClassInterface();
137 }
138
139 if (CurrentClass) {
140 Diag(NewMethod->getLocation(),
141 diag::warn_related_result_type_compatibility_class)
142 << Context.getObjCInterfaceType(CurrentClass)
143 << ResultType
144 << ResultTypeRange;
145 } else {
146 Diag(NewMethod->getLocation(),
147 diag::warn_related_result_type_compatibility_protocol)
148 << ResultType
149 << ResultTypeRange;
150 }
151
Douglas Gregore97179c2011-09-08 01:46:34 +0000152 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
153 Diag(Overridden->getLocation(),
154 diag::note_related_result_type_overridden_family)
155 << Family;
156 else
157 Diag(Overridden->getLocation(),
158 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000159 }
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000160 if (getLangOptions().ObjCAutoRefCount) {
161 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
162 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
163 Diag(NewMethod->getLocation(),
164 diag::err_nsreturns_retained_attribute_mismatch) << 1;
165 Diag(Overridden->getLocation(), diag::note_previous_decl)
166 << "method";
167 }
168 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
169 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
170 Diag(NewMethod->getLocation(),
171 diag::err_nsreturns_retained_attribute_mismatch) << 0;
172 Diag(Overridden->getLocation(), diag::note_previous_decl)
173 << "method";
174 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000175 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin();
176 for (ObjCMethodDecl::param_iterator
177 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000178 ni != ne; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000179 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000180 ParmVarDecl *newDecl = (*ni);
181 if (newDecl->hasAttr<NSConsumedAttr>() !=
182 oldDecl->hasAttr<NSConsumedAttr>()) {
183 Diag(newDecl->getLocation(),
184 diag::err_nsconsumed_attribute_mismatch);
185 Diag(oldDecl->getLocation(), diag::note_previous_decl)
186 << "parameter";
187 }
188 }
189 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000190}
191
John McCallf85e1932011-06-15 23:02:42 +0000192/// \brief Check a method declaration for compatibility with the Objective-C
193/// ARC conventions.
194static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
195 ObjCMethodFamily family = method->getMethodFamily();
196 switch (family) {
197 case OMF_None:
198 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000199 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000200 case OMF_retain:
201 case OMF_release:
202 case OMF_autorelease:
203 case OMF_retainCount:
204 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000205 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000206 return false;
207
208 case OMF_init:
209 // If the method doesn't obey the init rules, don't bother annotating it.
210 if (S.checkInitMethod(method, QualType()))
211 return true;
212
213 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
214 S.Context));
215
216 // Don't add a second copy of this attribute, but otherwise don't
217 // let it be suppressed.
218 if (method->hasAttr<NSReturnsRetainedAttr>())
219 return false;
220 break;
221
222 case OMF_alloc:
223 case OMF_copy:
224 case OMF_mutableCopy:
225 case OMF_new:
226 if (method->hasAttr<NSReturnsRetainedAttr>() ||
227 method->hasAttr<NSReturnsNotRetainedAttr>() ||
228 method->hasAttr<NSReturnsAutoreleasedAttr>())
229 return false;
230 break;
231 }
232
233 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
234 S.Context));
235 return false;
236}
237
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000238static void DiagnoseObjCImplementedDeprecations(Sema &S,
239 NamedDecl *ND,
240 SourceLocation ImplLoc,
241 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000242 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000243 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000244 if (select == 0)
245 S.Diag(ND->getLocation(), diag::note_method_declared_at);
246 else
247 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
248 }
249}
250
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000251/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
252/// pool.
253void Sema::AddAnyMethodToGlobalPool(Decl *D) {
254 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
255
256 // If we don't have a valid method decl, simply return.
257 if (!MDecl)
258 return;
259 if (MDecl->isInstanceMethod())
260 AddInstanceMethodToGlobalPool(MDecl, true);
261 else
262 AddFactoryMethodToGlobalPool(MDecl, true);
263}
264
Steve Naroffebf64432009-02-28 16:59:13 +0000265/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000266/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000267void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000268 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000269 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Steve Naroff394f3f42008-07-25 17:57:26 +0000271 // If we don't have a valid method decl, simply return.
272 if (!MDecl)
273 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000274
Chris Lattner4d391482007-12-12 07:09:47 +0000275 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000276 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000277 PushFunctionScope();
278
Chris Lattner4d391482007-12-12 07:09:47 +0000279 // Create Decl objects for each parameter, entrring them in the scope for
280 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000281
282 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000283 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Daniel Dunbar451318c2008-08-26 06:07:48 +0000285 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
286 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000287
Chris Lattner8123a952008-04-10 02:22:51 +0000288 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000289 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000290 E = MDecl->param_end(); PI != E; ++PI) {
291 ParmVarDecl *Param = (*PI);
292 if (!Param->isInvalidDecl() &&
293 RequireCompleteType(Param->getLocation(), Param->getType(),
294 diag::err_typecheck_decl_incomplete_type))
295 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000296 if ((*PI)->getIdentifier())
297 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000298 }
John McCallf85e1932011-06-15 23:02:42 +0000299
300 // In ARC, disallow definition of retain/release/autorelease/retainCount
301 if (getLangOptions().ObjCAutoRefCount) {
302 switch (MDecl->getMethodFamily()) {
303 case OMF_retain:
304 case OMF_retainCount:
305 case OMF_release:
306 case OMF_autorelease:
307 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
308 << MDecl->getSelector();
309 break;
310
311 case OMF_None:
312 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000313 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000314 case OMF_alloc:
315 case OMF_init:
316 case OMF_mutableCopy:
317 case OMF_copy:
318 case OMF_new:
319 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000320 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000321 break;
322 }
323 }
324
Nico Weber9a1ecf02011-08-22 17:25:57 +0000325 // Warn on deprecated methods under -Wdeprecated-implementations,
326 // and prepare for warning on missing super calls.
327 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000328 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000329 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000330 DiagnoseObjCImplementedDeprecations(*this,
331 dyn_cast<NamedDecl>(IMD),
332 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000333
Nico Weber80cb6e62011-08-28 22:35:17 +0000334 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000335 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
336 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
337 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000338 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000339 ObjCShouldCallSuperDealloc =
Ted Kremenek8cd8de42011-09-28 19:32:29 +0000340 !(Context.getLangOptions().ObjCAutoRefCount ||
341 Context.getLangOptions().getGC() == LangOptions::GCOnly) &&
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000342 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000343 ObjCShouldCallSuperFinalize =
Ted Kremenek8cd8de42011-09-28 19:32:29 +0000344 Context.getLangOptions().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000345 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000346 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000347 }
Chris Lattner4d391482007-12-12 07:09:47 +0000348}
349
John McCalld226f652010-08-21 09:40:31 +0000350Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000351ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
352 IdentifierInfo *ClassName, SourceLocation ClassLoc,
353 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000354 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000355 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000356 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000357 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattner4d391482007-12-12 07:09:47 +0000359 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000360 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000361 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000362
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000363 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000364 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000365 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Douglas Gregor7723fec2011-12-15 20:29:51 +0000368 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000369 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000370 ObjCInterfaceDecl *IDecl
371 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000372 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000373
Douglas Gregor7723fec2011-12-15 20:29:51 +0000374 if (PrevIDecl) {
375 // Class already seen. Was it a definition?
376 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
377 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
378 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000379 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000380 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000381 }
Chris Lattner4d391482007-12-12 07:09:47 +0000382 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000383
384 if (AttrList)
385 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
386 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Douglas Gregor7723fec2011-12-15 20:29:51 +0000388 // Start the definition of this class. If we're in a redefinition case, there
389 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000390 if (!IDecl->hasDefinition())
391 IDecl->startDefinition();
392
Chris Lattner4d391482007-12-12 07:09:47 +0000393 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000394 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000395 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
396 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000397
398 if (!PrevDecl) {
399 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000400 TypoCorrection Corrected = CorrectTypo(
401 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
402 NULL, NULL, false, CTC_NoKeywords);
403 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregor60ef3082011-12-15 00:29:59 +0000404 if (declaresSameEntity(PrevDecl, IDecl)) {
Douglas Gregora38c4732011-12-01 15:37:53 +0000405 // Don't correct to the class we're defining.
406 PrevDecl = 0;
407 } else {
408 Diag(SuperLoc, diag::err_undef_superclass_suggest)
409 << SuperName << ClassName << PrevDecl->getDeclName();
410 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
411 << PrevDecl->getDeclName();
412 }
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000413 }
414 }
415
Douglas Gregor60ef3082011-12-15 00:29:59 +0000416 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000417 Diag(SuperLoc, diag::err_recursive_superclass)
418 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000419 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000420 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000421 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000422 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000423
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000424 // Diagnose classes that inherit from deprecated classes.
425 if (SuperClassDecl)
426 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000428 if (PrevDecl && SuperClassDecl == 0) {
429 // The previous declaration was not a class decl. Check if we have a
430 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000431 if (const TypedefNameDecl *TDecl =
432 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000433 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000434 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000435 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
436 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000437 }
438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000440 // This handles the following case:
441 //
442 // typedef int SuperClass;
443 // @interface MyClass : SuperClass {} @end
444 //
445 if (!SuperClassDecl) {
446 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
447 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000448 }
449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Richard Smith162e1c12011-04-15 14:24:37 +0000451 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000452 if (!SuperClassDecl)
453 Diag(SuperLoc, diag::err_undef_superclass)
454 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000455 else if (RequireCompleteType(SuperLoc,
456 Context.getObjCInterfaceType(SuperClassDecl),
457 PDiag(diag::err_forward_superclass)
458 << SuperClassDecl->getDeclName()
459 << ClassName
460 << SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000461 SuperClassDecl = 0;
462 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000463 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000464 IDecl->setSuperClass(SuperClassDecl);
465 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000466 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000467 }
Chris Lattner4d391482007-12-12 07:09:47 +0000468 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000469 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000470 }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Sebastian Redl0b17c612010-08-13 00:28:03 +0000472 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000473 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000474 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000475 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000476 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Anders Carlsson15281452008-11-04 16:57:32 +0000479 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000480 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000481}
482
483/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000484/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000485Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
486 IdentifierInfo *AliasName,
487 SourceLocation AliasLocation,
488 IdentifierInfo *ClassName,
489 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000490 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000491 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000492 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000493 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000494 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000495 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000496 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000497 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000498 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000499 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000500 }
501 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000502 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000503 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000504 if (const TypedefNameDecl *TDecl =
505 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000506 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000507 if (T->isObjCObjectType()) {
508 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000509 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000510 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000511 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000512 }
513 }
514 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000515 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
516 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000517 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000518 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000519 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000520 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000523 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000524 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000525 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Anders Carlsson15281452008-11-04 16:57:32 +0000527 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000528 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000529
John McCalld226f652010-08-21 09:40:31 +0000530 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000531}
532
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000533bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000534 IdentifierInfo *PName,
535 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000536 const ObjCList<ObjCProtocolDecl> &PList) {
537
538 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000539 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
540 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000541 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
542 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000543 if (PDecl->getIdentifier() == PName) {
544 Diag(Ploc, diag::err_protocol_has_circular_dependency);
545 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000546 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000547 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000548
549 if (!PDecl->hasDefinition())
550 continue;
551
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000552 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
553 PDecl->getLocation(), PDecl->getReferencedProtocols()))
554 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000555 }
556 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000557 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000558}
559
John McCalld226f652010-08-21 09:40:31 +0000560Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000561Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
562 IdentifierInfo *ProtocolName,
563 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000564 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000565 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000566 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000567 SourceLocation EndProtoLoc,
568 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000569 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000570 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000571 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000572 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
573 ForRedeclaration);
574 ObjCProtocolDecl *PDecl = 0;
575 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
576 // If we already have a definition, complain.
577 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
578 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Douglas Gregor27c6da22012-01-01 20:30:41 +0000580 // Create a new protocol that is completely distinct from previous
581 // declarations, and do not make this protocol available for name lookup.
582 // That way, we'll end up completely ignoring the duplicate.
583 // FIXME: Can we turn this into an error?
584 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
585 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000586 /*PrevDecl=*/0);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000587 PDecl->startDefinition();
588 } else {
589 if (PrevDecl) {
590 // Check for circular dependencies among protocol declarations. This can
591 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000592 ObjCList<ObjCProtocolDecl> PList;
593 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
594 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000595 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000596 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000597
598 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000599 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000600 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000601 /*PrevDecl=*/PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000602
Douglas Gregor6e378de2009-04-23 23:18:26 +0000603 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000604 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000605 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000606
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000607 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000608 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000609
610 // Merge attributes from previous declarations.
611 if (PrevDecl)
612 mergeDeclAttributes(PDecl, PrevDecl);
613
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000614 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000615 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000616 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
617 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000618 PDecl->setLocEnd(EndProtoLoc);
619 }
Mike Stump1eb44332009-09-09 15:08:12 +0000620
621 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000622 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000623}
624
625/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000626/// issues an error if they are not declared. It returns list of
627/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000628void
Chris Lattnere13b9592008-07-26 04:03:38 +0000629Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000630 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000631 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000632 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000633 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000634 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
635 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000636 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000637 TypoCorrection Corrected = CorrectTypo(
638 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
639 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
640 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000641 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000642 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000643 Diag(PDecl->getLocation(), diag::note_previous_decl)
644 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000645 }
646 }
647
648 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000649 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000650 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000651 continue;
652 }
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000654 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000655
656 // If this is a forward declaration and we are supposed to warn in this
657 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000658 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000659 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000660 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000661 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000662 }
663}
664
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000665/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000666/// a class method in its extension.
667///
Mike Stump1eb44332009-09-09 15:08:12 +0000668void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000669 ObjCInterfaceDecl *ID) {
670 if (!ID)
671 return; // Possibly due to previous error
672
673 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000674 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
675 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000676 ObjCMethodDecl *MD = *i;
677 MethodMap[MD->getSelector()] = MD;
678 }
679
680 if (MethodMap.empty())
681 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000682 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
683 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000684 ObjCMethodDecl *Method = *i;
685 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
686 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
687 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
688 << Method->getDeclName();
689 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
690 }
691 }
692}
693
Chris Lattner58fe03b2009-04-12 08:43:13 +0000694/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000695Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000696Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000697 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000698 unsigned NumElts,
699 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000700 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000701 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000702 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000703 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
704 ForRedeclaration);
705 ObjCProtocolDecl *PDecl
706 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
707 IdentList[i].second, AtProtocolLoc,
Douglas Gregorc9d3c7e2012-01-01 22:06:18 +0000708 PrevDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000709
710 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000711 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000712
Douglas Gregor3937f872012-01-01 20:33:24 +0000713 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000714 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000715
716 if (PrevDecl)
717 mergeDeclAttributes(PDecl, PrevDecl);
718
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000719 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000720 }
Mike Stump1eb44332009-09-09 15:08:12 +0000721
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000722 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000723}
724
John McCalld226f652010-08-21 09:40:31 +0000725Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000726ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
727 IdentifierInfo *ClassName, SourceLocation ClassLoc,
728 IdentifierInfo *CategoryName,
729 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000730 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000731 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000732 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000733 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000734 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000735 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000736
737 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000738
739 if (!IDecl
740 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
741 PDiag(diag::err_category_forward_interface)
742 << (CategoryName == 0))) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000743 // Create an invalid ObjCCategoryDecl to serve as context for
744 // the enclosing method declarations. We mark the decl invalid
745 // to make it clear that this isn't a valid AST.
746 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000747 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000748 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000749
750 if (!IDecl)
751 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000752 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000753 }
754
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000755 if (!CategoryName && IDecl->getImplementation()) {
756 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
757 Diag(IDecl->getImplementation()->getLocation(),
758 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000759 }
760
Fariborz Jahanian25760612010-02-15 21:55:26 +0000761 if (CategoryName) {
762 /// Check for duplicate interface declaration for this category
763 ObjCCategoryDecl *CDeclChain;
764 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
765 CDeclChain = CDeclChain->getNextClassCategory()) {
766 if (CDeclChain->getIdentifier() == CategoryName) {
767 // Class extensions can be declared multiple times.
768 Diag(CategoryLoc, diag::warn_dup_category_def)
769 << ClassName << CategoryName;
770 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
771 break;
772 }
Chris Lattner70f19542009-02-16 21:26:43 +0000773 }
774 }
Chris Lattner70f19542009-02-16 21:26:43 +0000775
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000776 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
777 ClassLoc, CategoryLoc, CategoryName, IDecl);
778 // FIXME: PushOnScopeChains?
779 CurContext->addDecl(CDecl);
780
Chris Lattner4d391482007-12-12 07:09:47 +0000781 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000782 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000783 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000784 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000785 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000786 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000787 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000788 }
Mike Stump1eb44332009-09-09 15:08:12 +0000789
Anders Carlsson15281452008-11-04 16:57:32 +0000790 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000791 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000792}
793
794/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000795/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000796/// object.
John McCalld226f652010-08-21 09:40:31 +0000797Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000798 SourceLocation AtCatImplLoc,
799 IdentifierInfo *ClassName, SourceLocation ClassLoc,
800 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000801 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000802 ObjCCategoryDecl *CatIDecl = 0;
803 if (IDecl) {
804 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
805 if (!CatIDecl) {
806 // Category @implementation with no corresponding @interface.
807 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000808 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
809 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000810 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000811 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000812 }
813 }
814
Mike Stump1eb44332009-09-09 15:08:12 +0000815 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000816 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000817 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000818 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000819 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000820 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000821 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000822 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
823 diag::err_undef_interface)) {
824 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000825 }
Chris Lattner4d391482007-12-12 07:09:47 +0000826
Douglas Gregord0434102009-01-09 00:49:46 +0000827 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000828 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000829
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000830 // If the interface is deprecated/unavailable, warn/error about it.
831 if (IDecl)
832 DiagnoseUseOfDecl(IDecl, ClassLoc);
833
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000834 /// Check that CatName, category name, is not used in another implementation.
835 if (CatIDecl) {
836 if (CatIDecl->getImplementation()) {
837 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
838 << CatName;
839 Diag(CatIDecl->getImplementation()->getLocation(),
840 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000841 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000842 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000843 // Warn on implementating category of deprecated class under
844 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000845 DiagnoseObjCImplementedDeprecations(*this,
846 dyn_cast<NamedDecl>(IDecl),
847 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000848 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000849 }
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Anders Carlsson15281452008-11-04 16:57:32 +0000851 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000852 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000853}
854
John McCalld226f652010-08-21 09:40:31 +0000855Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000856 SourceLocation AtClassImplLoc,
857 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000858 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000859 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000860 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000861 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000862 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000863 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
864 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000865 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000866 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000867 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000868 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000869 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
870 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000871 } else {
872 // We did not find anything with the name ClassName; try to correct for
873 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000874 TypoCorrection Corrected = CorrectTypo(
875 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
876 NULL, NULL, false, CTC_NoKeywords);
877 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000878 // Suggest the (potentially) correct interface name. However, put the
879 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000880 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000881 // provide a code-modification hint or use the typo name for recovery,
882 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000883 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000884 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000885 << ClassName << CorrectedName;
886 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
887 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000888 IDecl = 0;
889 } else {
890 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
891 }
Chris Lattner4d391482007-12-12 07:09:47 +0000892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Chris Lattner4d391482007-12-12 07:09:47 +0000894 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000895 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000896 if (SuperClassname) {
897 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000898 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
899 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000900 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000901 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
902 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000903 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000904 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000905 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000906 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000907 Diag(SuperClassLoc, diag::err_undef_superclass)
908 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000909 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000910 // This implementation and its interface do not have the same
911 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000912 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000913 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000914 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000915 }
916 }
917 }
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Chris Lattner4d391482007-12-12 07:09:47 +0000919 if (!IDecl) {
920 // Legacy case of @implementation with no corresponding @interface.
921 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000922
Mike Stump390b4cc2009-05-16 07:39:55 +0000923 // FIXME: Do we support attributes on the @implementation? If so we should
924 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000925 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000926 ClassName, /*PrevDecl=*/0, ClassLoc,
927 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000928 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000929 if (SDecl) {
930 IDecl->setSuperClass(SDecl);
931 IDecl->setSuperClassLoc(SuperClassLoc);
932 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
933 } else {
934 IDecl->setEndOfDefinitionLoc(ClassLoc);
935 }
936
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000937 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000938 } else {
939 // Mark the interface as being completed, even if it was just as
940 // @class ....;
941 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000942 if (!IDecl->hasDefinition())
943 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000944 }
Mike Stump1eb44332009-09-09 15:08:12 +0000945
946 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000947 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
948 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Anders Carlsson15281452008-11-04 16:57:32 +0000950 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000951 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000952
Chris Lattner4d391482007-12-12 07:09:47 +0000953 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000954 if (IDecl->getImplementation()) {
955 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000956 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000957 Diag(IDecl->getImplementation()->getLocation(),
958 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000959 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000960 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000961 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000962 // Warn on implementating deprecated class under
963 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000964 DiagnoseObjCImplementedDeprecations(*this,
965 dyn_cast<NamedDecl>(IDecl),
966 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000967 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000968 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000969}
970
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000971void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
972 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000973 SourceLocation RBrace) {
974 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000975 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000976 if (!IDecl)
977 return;
978 /// Check case of non-existing @interface decl.
979 /// (legacy objective-c @implementation decl without an @interface decl).
980 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000981 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +0000982 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000983 // Add ivar's to class's DeclContext.
984 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000985 ivars[i]->setLexicalDeclContext(ImpDecl);
986 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000987 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000988 }
989
Chris Lattner4d391482007-12-12 07:09:47 +0000990 return;
991 }
992 // If implementation has empty ivar list, just return.
993 if (numIvars == 0)
994 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Chris Lattner4d391482007-12-12 07:09:47 +0000996 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000997 if (LangOpts.ObjCNonFragileABI2) {
998 if (ImpDecl->getSuperClass())
999 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1000 for (unsigned i = 0; i < numIvars; i++) {
1001 ObjCIvarDecl* ImplIvar = ivars[i];
1002 if (const ObjCIvarDecl *ClsIvar =
1003 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1004 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1005 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1006 continue;
1007 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001008 // Instance ivar to Implementation's DeclContext.
1009 ImplIvar->setLexicalDeclContext(ImpDecl);
1010 IDecl->makeDeclVisibleInContext(ImplIvar, false);
1011 ImpDecl->addDecl(ImplIvar);
1012 }
1013 return;
1014 }
Chris Lattner4d391482007-12-12 07:09:47 +00001015 // Check interface's Ivar list against those in the implementation.
1016 // names and types must match.
1017 //
Chris Lattner4d391482007-12-12 07:09:47 +00001018 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001019 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001020 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1021 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001022 ObjCIvarDecl* ImplIvar = ivars[j++];
1023 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001024 assert (ImplIvar && "missing implementation ivar");
1025 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Steve Naroffca331292009-03-03 14:49:36 +00001027 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001028 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001029 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001030 << ImplIvar->getIdentifier()
1031 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001032 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001033 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1034 ImplIvar->getBitWidthValue(Context) !=
1035 ClsIvar->getBitWidthValue(Context)) {
1036 Diag(ImplIvar->getBitWidth()->getLocStart(),
1037 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1038 Diag(ClsIvar->getBitWidth()->getLocStart(),
1039 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001040 }
Steve Naroffca331292009-03-03 14:49:36 +00001041 // Make sure the names are identical.
1042 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001043 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001044 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001045 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001046 }
1047 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001048 }
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Chris Lattner609e4c72007-12-12 18:11:49 +00001050 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001051 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001052 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001053 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001054}
1055
Steve Naroff3c2eb662008-02-10 21:38:56 +00001056void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001057 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001058 // No point warning no definition of method which is 'unavailable'.
1059 if (method->hasAttr<UnavailableAttr>())
1060 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001061 if (!IncompleteImpl) {
1062 Diag(ImpLoc, diag::warn_incomplete_impl);
1063 IncompleteImpl = true;
1064 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001065 if (DiagID == diag::warn_unimplemented_protocol_method)
1066 Diag(ImpLoc, DiagID) << method->getDeclName();
1067 else
1068 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001069}
1070
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001071/// Determines if type B can be substituted for type A. Returns true if we can
1072/// guarantee that anything that the user will do to an object of type A can
1073/// also be done to an object of type B. This is trivially true if the two
1074/// types are the same, or if B is a subclass of A. It becomes more complex
1075/// in cases where protocols are involved.
1076///
1077/// Object types in Objective-C describe the minimum requirements for an
1078/// object, rather than providing a complete description of a type. For
1079/// example, if A is a subclass of B, then B* may refer to an instance of A.
1080/// The principle of substitutability means that we may use an instance of A
1081/// anywhere that we may use an instance of B - it will implement all of the
1082/// ivars of B and all of the methods of B.
1083///
1084/// This substitutability is important when type checking methods, because
1085/// the implementation may have stricter type definitions than the interface.
1086/// The interface specifies minimum requirements, but the implementation may
1087/// have more accurate ones. For example, a method may privately accept
1088/// instances of B, but only publish that it accepts instances of A. Any
1089/// object passed to it will be type checked against B, and so will implicitly
1090/// by a valid A*. Similarly, a method may return a subclass of the class that
1091/// it is declared as returning.
1092///
1093/// This is most important when considering subclassing. A method in a
1094/// subclass must accept any object as an argument that its superclass's
1095/// implementation accepts. It may, however, accept a more general type
1096/// without breaking substitutability (i.e. you can still use the subclass
1097/// anywhere that you can use the superclass, but not vice versa). The
1098/// converse requirement applies to return types: the return type for a
1099/// subclass method must be a valid object of the kind that the superclass
1100/// advertises, but it may be specified more accurately. This avoids the need
1101/// for explicit down-casting by callers.
1102///
1103/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001104static bool isObjCTypeSubstitutable(ASTContext &Context,
1105 const ObjCObjectPointerType *A,
1106 const ObjCObjectPointerType *B,
1107 bool rejectId) {
1108 // Reject a protocol-unqualified id.
1109 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001110
1111 // If B is a qualified id, then A must also be a qualified id and it must
1112 // implement all of the protocols in B. It may not be a qualified class.
1113 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1114 // stricter definition so it is not substitutable for id<A>.
1115 if (B->isObjCQualifiedIdType()) {
1116 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001117 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1118 QualType(B,0),
1119 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001120 }
1121
1122 /*
1123 // id is a special type that bypasses type checking completely. We want a
1124 // warning when it is used in one place but not another.
1125 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1126
1127
1128 // If B is a qualified id, then A must also be a qualified id (which it isn't
1129 // if we've got this far)
1130 if (B->isObjCQualifiedIdType()) return false;
1131 */
1132
1133 // Now we know that A and B are (potentially-qualified) class types. The
1134 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001135 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001136}
1137
John McCall10302c02010-10-28 02:34:38 +00001138static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1139 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1140}
1141
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001142static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001143 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001144 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001145 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001146 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001147 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001148 if (IsProtocolMethodDecl &&
1149 (MethodDecl->getObjCDeclQualifier() !=
1150 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001151 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001152 S.Diag(MethodImpl->getLocation(),
1153 (IsOverridingMode ?
1154 diag::warn_conflicting_overriding_ret_type_modifiers
1155 : diag::warn_conflicting_ret_type_modifiers))
1156 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001157 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1158 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1159 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1160 }
1161 else
1162 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001163 }
1164
John McCall10302c02010-10-28 02:34:38 +00001165 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001166 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001167 return true;
1168 if (!Warn)
1169 return false;
John McCall10302c02010-10-28 02:34:38 +00001170
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001171 unsigned DiagID =
1172 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1173 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001174
1175 // Mismatches between ObjC pointers go into a different warning
1176 // category, and sometimes they're even completely whitelisted.
1177 if (const ObjCObjectPointerType *ImplPtrTy =
1178 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1179 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001180 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001181 // Allow non-matching return types as long as they don't violate
1182 // the principle of substitutability. Specifically, we permit
1183 // return types that are subclasses of the declared return type,
1184 // or that are more-qualified versions of the declared type.
1185 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001186 return false;
John McCall10302c02010-10-28 02:34:38 +00001187
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001188 DiagID =
1189 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1190 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001191 }
1192 }
1193
1194 S.Diag(MethodImpl->getLocation(), DiagID)
1195 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001196 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001197 << MethodImpl->getResultType()
1198 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001199 S.Diag(MethodDecl->getLocation(),
1200 IsOverridingMode ? diag::note_previous_declaration
1201 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001202 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001203 return false;
John McCall10302c02010-10-28 02:34:38 +00001204}
1205
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001206static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001207 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001208 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001209 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001210 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001211 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001212 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001213 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001214 if (IsProtocolMethodDecl &&
1215 (ImplVar->getObjCDeclQualifier() !=
1216 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001217 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001218 if (IsOverridingMode)
1219 S.Diag(ImplVar->getLocation(),
1220 diag::warn_conflicting_overriding_param_modifiers)
1221 << getTypeRange(ImplVar->getTypeSourceInfo())
1222 << MethodImpl->getDeclName();
1223 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001224 diag::warn_conflicting_param_modifiers)
1225 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001226 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001227 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1228 << getTypeRange(IfaceVar->getTypeSourceInfo());
1229 }
1230 else
1231 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001232 }
1233
John McCall10302c02010-10-28 02:34:38 +00001234 QualType ImplTy = ImplVar->getType();
1235 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001236
John McCall10302c02010-10-28 02:34:38 +00001237 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001238 return true;
1239
1240 if (!Warn)
1241 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001242 unsigned DiagID =
1243 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1244 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001245
1246 // Mismatches between ObjC pointers go into a different warning
1247 // category, and sometimes they're even completely whitelisted.
1248 if (const ObjCObjectPointerType *ImplPtrTy =
1249 ImplTy->getAs<ObjCObjectPointerType>()) {
1250 if (const ObjCObjectPointerType *IfacePtrTy =
1251 IfaceTy->getAs<ObjCObjectPointerType>()) {
1252 // Allow non-matching argument types as long as they don't
1253 // violate the principle of substitutability. Specifically, the
1254 // implementation must accept any objects that the superclass
1255 // accepts, however it may also accept others.
1256 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001257 return false;
John McCall10302c02010-10-28 02:34:38 +00001258
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001259 DiagID =
1260 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1261 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001262 }
1263 }
1264
1265 S.Diag(ImplVar->getLocation(), DiagID)
1266 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001267 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1268 S.Diag(IfaceVar->getLocation(),
1269 (IsOverridingMode ? diag::note_previous_declaration
1270 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001271 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001272 return false;
John McCall10302c02010-10-28 02:34:38 +00001273}
John McCallf85e1932011-06-15 23:02:42 +00001274
1275/// In ARC, check whether the conventional meanings of the two methods
1276/// match. If they don't, it's a hard error.
1277static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1278 ObjCMethodDecl *decl) {
1279 ObjCMethodFamily implFamily = impl->getMethodFamily();
1280 ObjCMethodFamily declFamily = decl->getMethodFamily();
1281 if (implFamily == declFamily) return false;
1282
1283 // Since conventions are sorted by selector, the only possibility is
1284 // that the types differ enough to cause one selector or the other
1285 // to fall out of the family.
1286 assert(implFamily == OMF_None || declFamily == OMF_None);
1287
1288 // No further diagnostics required on invalid declarations.
1289 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1290
1291 const ObjCMethodDecl *unmatched = impl;
1292 ObjCMethodFamily family = declFamily;
1293 unsigned errorID = diag::err_arc_lost_method_convention;
1294 unsigned noteID = diag::note_arc_lost_method_convention;
1295 if (declFamily == OMF_None) {
1296 unmatched = decl;
1297 family = implFamily;
1298 errorID = diag::err_arc_gained_method_convention;
1299 noteID = diag::note_arc_gained_method_convention;
1300 }
1301
1302 // Indexes into a %select clause in the diagnostic.
1303 enum FamilySelector {
1304 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1305 };
1306 FamilySelector familySelector = FamilySelector();
1307
1308 switch (family) {
1309 case OMF_None: llvm_unreachable("logic error, no method convention");
1310 case OMF_retain:
1311 case OMF_release:
1312 case OMF_autorelease:
1313 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001314 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001315 case OMF_retainCount:
1316 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001317 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001318 // Mismatches for these methods don't change ownership
1319 // conventions, so we don't care.
1320 return false;
1321
1322 case OMF_init: familySelector = F_init; break;
1323 case OMF_alloc: familySelector = F_alloc; break;
1324 case OMF_copy: familySelector = F_copy; break;
1325 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1326 case OMF_new: familySelector = F_new; break;
1327 }
1328
1329 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1330 ReasonSelector reasonSelector;
1331
1332 // The only reason these methods don't fall within their families is
1333 // due to unusual result types.
1334 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1335 reasonSelector = R_UnrelatedReturn;
1336 } else {
1337 reasonSelector = R_NonObjectReturn;
1338 }
1339
1340 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1341 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1342
1343 return true;
1344}
John McCall10302c02010-10-28 02:34:38 +00001345
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001346void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001347 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001348 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001349 if (getLangOptions().ObjCAutoRefCount &&
1350 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1351 return;
1352
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001353 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001354 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001355 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001356
Chris Lattner3aff9192009-04-11 19:58:42 +00001357 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001358 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001359 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001360 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001361 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001362 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001363
Fariborz Jahanian21121902011-08-08 18:03:17 +00001364 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001365 Diag(ImpMethodDecl->getLocation(),
1366 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001367 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001368 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001369}
1370
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001371void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1372 ObjCMethodDecl *Overridden,
1373 bool IsProtocolMethodDecl) {
1374
1375 CheckMethodOverrideReturn(*this, Method, Overridden,
1376 IsProtocolMethodDecl, true,
1377 true);
1378
1379 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
1380 IF = Overridden->param_begin(), EM = Method->param_end();
1381 IM != EM; ++IM, ++IF) {
1382 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1383 IsProtocolMethodDecl, true, true);
1384 }
1385
1386 if (Method->isVariadic() != Overridden->isVariadic()) {
1387 Diag(Method->getLocation(),
1388 diag::warn_conflicting_overriding_variadic);
1389 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1390 }
1391}
1392
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001393/// WarnExactTypedMethods - This routine issues a warning if method
1394/// implementation declaration matches exactly that of its declaration.
1395void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1396 ObjCMethodDecl *MethodDecl,
1397 bool IsProtocolMethodDecl) {
1398 // don't issue warning when protocol method is optional because primary
1399 // class is not required to implement it and it is safe for protocol
1400 // to implement it.
1401 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1402 return;
1403 // don't issue warning when primary class's method is
1404 // depecated/unavailable.
1405 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1406 MethodDecl->hasAttr<DeprecatedAttr>())
1407 return;
1408
1409 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1410 IsProtocolMethodDecl, false, false);
1411 if (match)
1412 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1413 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1414 IM != EM; ++IM, ++IF) {
1415 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1416 *IM, *IF,
1417 IsProtocolMethodDecl, false, false);
1418 if (!match)
1419 break;
1420 }
1421 if (match)
1422 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001423 if (match)
1424 match = !(MethodDecl->isClassMethod() &&
1425 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001426
1427 if (match) {
1428 Diag(ImpMethodDecl->getLocation(),
1429 diag::warn_category_method_impl_match);
1430 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1431 }
1432}
1433
Mike Stump390b4cc2009-05-16 07:39:55 +00001434/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1435/// improve the efficiency of selector lookups and type checking by associating
1436/// with each protocol / interface / category the flattened instance tables. If
1437/// we used an immutable set to keep the table then it wouldn't add significant
1438/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001439
Steve Naroffefe7f362008-02-08 22:06:17 +00001440/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001441/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001442void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1443 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001444 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001445 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001446 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001447 ObjCContainerDecl *CDecl) {
1448 ObjCInterfaceDecl *IDecl;
1449 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1450 IDecl = C->getClassInterface();
1451 else
1452 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1453 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1454
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001455 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001456 ObjCInterfaceDecl *NSIDecl = 0;
1457 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001458 // check to see if class implements forwardInvocation method and objects
1459 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001460 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001461 // Under such conditions, which means that every method possible is
1462 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001463 // found" warnings.
1464 // FIXME: Use a general GetUnarySelector method for this.
1465 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1466 Selector fISelector = Context.Selectors.getSelector(1, &II);
1467 if (InsMap.count(fISelector))
1468 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1469 // need be implemented in the implementation.
1470 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1471 }
Mike Stump1eb44332009-09-09 15:08:12 +00001472
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001473 // If a method lookup fails locally we still need to look and see if
1474 // the method was implemented by a base class or an inherited
1475 // protocol. This lookup is slow, but occurs rarely in correct code
1476 // and otherwise would terminate in a warning.
1477
Chris Lattner4d391482007-12-12 07:09:47 +00001478 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001479 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001480 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001481 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001482 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001483 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001484 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001485 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001486 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001487 // Ugly, but necessary. Method declared in protcol might have
1488 // have been synthesized due to a property declared in the class which
1489 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001490 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001491 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001492 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001493 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001494 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
David Blaikied6471f72011-09-25 23:23:43 +00001495 != DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001496 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001497 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001498 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1499 << PDecl->getDeclName();
1500 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001501 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001502 }
1503 }
Chris Lattner4d391482007-12-12 07:09:47 +00001504 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001505 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001506 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001507 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001508 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001509 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1510 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001511 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001512 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001513 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1514 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001515 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001516 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001517 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1518 PDecl->getDeclName();
1519 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001520 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001521 }
Chris Lattner780f3292008-07-21 21:32:27 +00001522 // Check on this protocols's referenced protocols, recursively.
1523 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1524 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001525 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001526}
1527
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001528/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001529/// or protocol against those declared in their implementations.
1530///
1531void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1532 const llvm::DenseSet<Selector> &ClsMap,
1533 llvm::DenseSet<Selector> &InsMapSeen,
1534 llvm::DenseSet<Selector> &ClsMapSeen,
1535 ObjCImplDecl* IMPDecl,
1536 ObjCContainerDecl* CDecl,
1537 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001538 bool ImmediateClass,
1539 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001540 // Check and see if instance methods in class interface have been
1541 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001542 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1543 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001544 if (InsMapSeen.count((*I)->getSelector()))
1545 continue;
1546 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001547 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001548 !InsMap.count((*I)->getSelector())) {
1549 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001550 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1551 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001552 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001553 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001554 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001555 IMPDecl->getInstanceMethod((*I)->getSelector());
1556 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1557 "Expected to find the method through lookup as well");
1558 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001559 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001560 if (ImpMethodDecl) {
1561 if (!WarnExactMatch)
1562 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1563 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001564 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001565 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1566 isa<ObjCProtocolDecl>(CDecl));
1567 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001568 }
1569 }
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001571 // Check and see if class methods in class interface have been
1572 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001573 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001574 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001575 if (ClsMapSeen.count((*I)->getSelector()))
1576 continue;
1577 ClsMapSeen.insert((*I)->getSelector());
1578 if (!ClsMap.count((*I)->getSelector())) {
1579 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001580 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1581 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001582 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001583 ObjCMethodDecl *ImpMethodDecl =
1584 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001585 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1586 "Expected to find the method through lookup as well");
1587 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001588 if (!WarnExactMatch)
1589 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1590 isa<ObjCProtocolDecl>(CDecl));
1591 else
1592 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1593 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001594 }
1595 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001596
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001597 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001598 // Also methods in class extensions need be looked at next.
1599 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1600 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1601 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1602 IMPDecl,
1603 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001604 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001605
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001606 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001607 for (ObjCInterfaceDecl::all_protocol_iterator
1608 PI = I->all_referenced_protocol_begin(),
1609 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001610 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1611 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001612 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001613
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001614 // FIXME. For now, we are not checking for extact match of methods
1615 // in category implementation and its primary class's super class.
1616 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001617 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001618 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001619 I->getSuperClass(), IncompleteImpl, false);
1620 }
1621}
1622
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001623/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1624/// category matches with those implemented in its primary class and
1625/// warns each time an exact match is found.
1626void Sema::CheckCategoryVsClassMethodMatches(
1627 ObjCCategoryImplDecl *CatIMPDecl) {
1628 llvm::DenseSet<Selector> InsMap, ClsMap;
1629
1630 for (ObjCImplementationDecl::instmeth_iterator
1631 I = CatIMPDecl->instmeth_begin(),
1632 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1633 InsMap.insert((*I)->getSelector());
1634
1635 for (ObjCImplementationDecl::classmeth_iterator
1636 I = CatIMPDecl->classmeth_begin(),
1637 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1638 ClsMap.insert((*I)->getSelector());
1639 if (InsMap.empty() && ClsMap.empty())
1640 return;
1641
1642 // Get category's primary class.
1643 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1644 if (!CatDecl)
1645 return;
1646 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1647 if (!IDecl)
1648 return;
1649 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1650 bool IncompleteImpl = false;
1651 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1652 CatIMPDecl, IDecl,
1653 IncompleteImpl, false, true /*WarnExactMatch*/);
1654}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001655
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001656void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001657 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001658 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001659 llvm::DenseSet<Selector> InsMap;
1660 // Check and see if instance methods in class interface have been
1661 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001662 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001663 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001664 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001666 // Check and see if properties declared in the interface have either 1)
1667 // an implementation or 2) there is a @synthesize/@dynamic implementation
1668 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001669 if (isa<ObjCInterfaceDecl>(CDecl) &&
1670 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001671 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001672
Chris Lattner4d391482007-12-12 07:09:47 +00001673 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001674 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001675 I = IMPDecl->classmeth_begin(),
1676 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001677 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001678
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001679 // Check for type conflict of methods declared in a class/protocol and
1680 // its implementation; if any.
1681 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001682 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1683 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001684 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001685
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001686 // check all methods implemented in category against those declared
1687 // in its primary class.
1688 if (ObjCCategoryImplDecl *CatDecl =
1689 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1690 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Chris Lattner4d391482007-12-12 07:09:47 +00001692 // Check the protocol list for unimplemented methods in the @implementation
1693 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001694 // Check and see if class methods in class interface have been
1695 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Chris Lattnercddc8882009-03-01 00:56:52 +00001697 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001698 for (ObjCInterfaceDecl::all_protocol_iterator
1699 PI = I->all_referenced_protocol_begin(),
1700 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001701 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001702 InsMap, ClsMap, I);
1703 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001704 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1705 Categories; Categories = Categories->getNextClassExtension())
1706 ImplMethodsVsClassMethods(S, IMPDecl,
1707 const_cast<ObjCCategoryDecl*>(Categories),
1708 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001709 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001710 // For extended class, unimplemented methods in its protocols will
1711 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001712 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001713 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1714 E = C->protocol_end(); PI != E; ++PI)
1715 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001716 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001717 // Report unimplemented properties in the category as well.
1718 // When reporting on missing setter/getters, do not report when
1719 // setter/getter is implemented in category's primary class
1720 // implementation.
1721 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1722 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1723 for (ObjCImplementationDecl::instmeth_iterator
1724 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1725 InsMap.insert((*I)->getSelector());
1726 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001727 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001728 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001729 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001730 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001731}
1732
Mike Stump1eb44332009-09-09 15:08:12 +00001733/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001734Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001735Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001736 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001737 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001738 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001739 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001740 for (unsigned i = 0; i != NumElts; ++i) {
1741 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001742 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001743 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001744 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001745 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001746 // Maybe we will complain about the shadowed template parameter.
1747 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1748 // Just pretend that we didn't see the previous declaration.
1749 PrevDecl = 0;
1750 }
1751
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001752 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001753 // GCC apparently allows the following idiom:
1754 //
1755 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1756 // @class XCElementToggler;
1757 //
Mike Stump1eb44332009-09-09 15:08:12 +00001758 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001759 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001760 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001761 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001762 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001763 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001764 // a forward class declaration matching a typedef name of a class refers
1765 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001766 if (const ObjCObjectType *OI =
1767 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1768 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001769 }
Chris Lattner4d391482007-12-12 07:09:47 +00001770 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001771
1772 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001773 ObjCInterfaceDecl *PrevIDecl
1774 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001775 ObjCInterfaceDecl *IDecl
1776 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001777 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001778 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001779
Douglas Gregor7723fec2011-12-15 20:29:51 +00001780 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001781 CheckObjCDeclScope(IDecl);
1782 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001783 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001784
1785 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001786}
1787
John McCall0f4c4c42011-06-16 01:15:19 +00001788static bool tryMatchRecordTypes(ASTContext &Context,
1789 Sema::MethodMatchStrategy strategy,
1790 const Type *left, const Type *right);
1791
John McCallf85e1932011-06-15 23:02:42 +00001792static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1793 QualType leftQT, QualType rightQT) {
1794 const Type *left =
1795 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1796 const Type *right =
1797 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1798
1799 if (left == right) return true;
1800
1801 // If we're doing a strict match, the types have to match exactly.
1802 if (strategy == Sema::MMS_strict) return false;
1803
1804 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1805
1806 // Otherwise, use this absurdly complicated algorithm to try to
1807 // validate the basic, low-level compatibility of the two types.
1808
1809 // As a minimum, require the sizes and alignments to match.
1810 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1811 return false;
1812
1813 // Consider all the kinds of non-dependent canonical types:
1814 // - functions and arrays aren't possible as return and parameter types
1815
1816 // - vector types of equal size can be arbitrarily mixed
1817 if (isa<VectorType>(left)) return isa<VectorType>(right);
1818 if (isa<VectorType>(right)) return false;
1819
1820 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001821 // - structs, unions, and Objective-C objects must match more-or-less
1822 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001823 // - everything else should be a scalar
1824 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001825 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001826
John McCall1d9b3b22011-09-09 05:25:32 +00001827 // Make scalars agree in kind, except count bools as chars, and group
1828 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001829 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1830 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1831 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1832 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001833 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1834 leftSK = Type::STK_ObjCObjectPointer;
1835 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1836 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001837
1838 // Note that data member pointers and function member pointers don't
1839 // intermix because of the size differences.
1840
1841 return (leftSK == rightSK);
1842}
Chris Lattner4d391482007-12-12 07:09:47 +00001843
John McCall0f4c4c42011-06-16 01:15:19 +00001844static bool tryMatchRecordTypes(ASTContext &Context,
1845 Sema::MethodMatchStrategy strategy,
1846 const Type *lt, const Type *rt) {
1847 assert(lt && rt && lt != rt);
1848
1849 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1850 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1851 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1852
1853 // Require union-hood to match.
1854 if (left->isUnion() != right->isUnion()) return false;
1855
1856 // Require an exact match if either is non-POD.
1857 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1858 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1859 return false;
1860
1861 // Require size and alignment to match.
1862 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1863
1864 // Require fields to match.
1865 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1866 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1867 for (; li != le && ri != re; ++li, ++ri) {
1868 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1869 return false;
1870 }
1871 return (li == le && ri == re);
1872}
1873
Chris Lattner4d391482007-12-12 07:09:47 +00001874/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1875/// returns true, or false, accordingly.
1876/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001877bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1878 const ObjCMethodDecl *right,
1879 MethodMatchStrategy strategy) {
1880 if (!matchTypes(Context, strategy,
1881 left->getResultType(), right->getResultType()))
1882 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001883
John McCallf85e1932011-06-15 23:02:42 +00001884 if (getLangOptions().ObjCAutoRefCount &&
1885 (left->hasAttr<NSReturnsRetainedAttr>()
1886 != right->hasAttr<NSReturnsRetainedAttr>() ||
1887 left->hasAttr<NSConsumesSelfAttr>()
1888 != right->hasAttr<NSConsumesSelfAttr>()))
1889 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001890
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001891 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001892 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001893
John McCallf85e1932011-06-15 23:02:42 +00001894 for (; li != le; ++li, ++ri) {
1895 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001896 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001897
1898 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1899 return false;
1900
1901 if (getLangOptions().ObjCAutoRefCount &&
1902 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1903 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001904 }
1905 return true;
1906}
1907
Sebastian Redldb9d2142010-08-02 23:18:59 +00001908/// \brief Read the contents of the method pool for a given selector from
1909/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001910///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001911/// This routine should only be called once, when the method pool has no entry
1912/// for this selector.
1913Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001914 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001915 assert(MethodPool.find(Sel) == MethodPool.end() &&
1916 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001917
1918 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001919 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Sebastian Redldb9d2142010-08-02 23:18:59 +00001921 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001922}
1923
Sebastian Redldb9d2142010-08-02 23:18:59 +00001924void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1925 bool instance) {
1926 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1927 if (Pos == MethodPool.end()) {
1928 if (ExternalSource)
1929 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001930 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001931 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1932 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001933 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001934 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001935 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001936 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001937 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001938 Entry.Method = Method;
1939 Entry.Next = 0;
1940 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001941 }
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Chris Lattnerb25df352009-03-04 05:16:45 +00001943 // We've seen a method with this name, see if we have already seen this type
1944 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001945 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1946 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1947
1948 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001949 ObjCMethodDecl *PrevObjCMethod = List->Method;
1950 PrevObjCMethod->setDefined(impl);
1951 // If a method is deprecated, push it in the global pool.
1952 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001953 if (Method->isDeprecated()) {
1954 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001955 List->Method = Method;
1956 }
1957 // If new method is unavailable, push it into global pool
1958 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001959 if (Method->isUnavailable()) {
1960 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001961 List->Method = Method;
1962 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001963 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001964 }
John McCallf85e1932011-06-15 23:02:42 +00001965 }
Mike Stump1eb44332009-09-09 15:08:12 +00001966
Chris Lattnerb25df352009-03-04 05:16:45 +00001967 // We have a new signature for an existing method - add it.
1968 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001969 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1970 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001971}
1972
John McCallf85e1932011-06-15 23:02:42 +00001973/// Determines if this is an "acceptable" loose mismatch in the global
1974/// method pool. This exists mostly as a hack to get around certain
1975/// global mismatches which we can't afford to make warnings / errors.
1976/// Really, what we want is a way to take a method out of the global
1977/// method pool.
1978static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1979 ObjCMethodDecl *other) {
1980 if (!chosen->isInstanceMethod())
1981 return false;
1982
1983 Selector sel = chosen->getSelector();
1984 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1985 return false;
1986
1987 // Don't complain about mismatches for -length if the method we
1988 // chose has an integral result type.
1989 return (chosen->getResultType()->isIntegerType());
1990}
1991
Sebastian Redldb9d2142010-08-02 23:18:59 +00001992ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001993 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001994 bool warn, bool instance) {
1995 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1996 if (Pos == MethodPool.end()) {
1997 if (ExternalSource)
1998 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001999 else
2000 return 0;
2001 }
2002
Sebastian Redldb9d2142010-08-02 23:18:59 +00002003 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002004
Sebastian Redldb9d2142010-08-02 23:18:59 +00002005 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002006 bool issueDiagnostic = false, issueError = false;
2007
2008 // We support a warning which complains about *any* difference in
2009 // method signature.
2010 bool strictSelectorMatch =
2011 (receiverIdOrClass && warn &&
2012 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2013 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002014 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002015 if (strictSelectorMatch)
2016 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002017 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2018 MMS_strict)) {
2019 issueDiagnostic = true;
2020 break;
2021 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002022 }
2023
John McCallf85e1932011-06-15 23:02:42 +00002024 // If we didn't see any strict differences, we won't see any loose
2025 // differences. In ARC, however, we also need to check for loose
2026 // mismatches, because most of them are errors.
2027 if (!strictSelectorMatch ||
2028 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002029 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002030 // This checks if the methods differ in type mismatch.
2031 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2032 MMS_loose) &&
2033 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2034 issueDiagnostic = true;
2035 if (getLangOptions().ObjCAutoRefCount)
2036 issueError = true;
2037 break;
2038 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002039 }
2040
John McCallf85e1932011-06-15 23:02:42 +00002041 if (issueDiagnostic) {
2042 if (issueError)
2043 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2044 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002045 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2046 else
2047 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002048
2049 Diag(MethList.Method->getLocStart(),
2050 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002051 << MethList.Method->getSourceRange();
2052 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2053 Diag(Next->Method->getLocStart(), diag::note_also_found)
2054 << Next->Method->getSourceRange();
2055 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002056 }
2057 return MethList.Method;
2058}
2059
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002060ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002061 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2062 if (Pos == MethodPool.end())
2063 return 0;
2064
2065 GlobalMethods &Methods = Pos->second;
2066
2067 if (Methods.first.Method && Methods.first.Method->isDefined())
2068 return Methods.first.Method;
2069 if (Methods.second.Method && Methods.second.Method->isDefined())
2070 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002071 return 0;
2072}
2073
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002074/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2075/// identical selector names in current and its super classes and issues
2076/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002077void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2078 ObjCMethodDecl *Method,
2079 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002080 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2081 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002082
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002083 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002084 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002085 SD->lookupMethod(Method->getSelector(), IsInstance);
2086 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002087 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002088 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002089 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002090 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2091 E = Method->param_end();
2092 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2093 for (; ParamI != E; ++ParamI, ++PrevI) {
2094 // Number of parameters are the same and is guaranteed by selector match.
2095 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2096 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2097 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002098 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002099 // respective argument type in the super class method, issue warning;
2100 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002101 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002102 << T1 << T2;
2103 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2104 return;
2105 }
2106 }
2107 ID = SD;
2108 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002109}
2110
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002111/// DiagnoseDuplicateIvars -
2112/// Check for duplicate ivars in the entire class at the start of
2113/// @implementation. This becomes necesssary because class extension can
2114/// add ivars to a class in random order which will not be known until
2115/// class's @implementation is seen.
2116void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2117 ObjCInterfaceDecl *SID) {
2118 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2119 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2120 ObjCIvarDecl* Ivar = (*IVI);
2121 if (Ivar->isInvalidDecl())
2122 continue;
2123 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2124 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2125 if (prevIvar) {
2126 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2127 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2128 Ivar->setInvalidDecl();
2129 }
2130 }
2131 }
2132}
2133
Erik Verbruggend64251f2011-12-06 09:25:23 +00002134Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2135 switch (CurContext->getDeclKind()) {
2136 case Decl::ObjCInterface:
2137 return Sema::OCK_Interface;
2138 case Decl::ObjCProtocol:
2139 return Sema::OCK_Protocol;
2140 case Decl::ObjCCategory:
2141 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2142 return Sema::OCK_ClassExtension;
2143 else
2144 return Sema::OCK_Category;
2145 case Decl::ObjCImplementation:
2146 return Sema::OCK_Implementation;
2147 case Decl::ObjCCategoryImpl:
2148 return Sema::OCK_CategoryImplementation;
2149
2150 default:
2151 return Sema::OCK_None;
2152 }
2153}
2154
Steve Naroffa56f6162007-12-18 01:30:32 +00002155// Note: For class/category implemenations, allMethods/allProperties is
2156// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002157Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2158 Decl **allMethods, unsigned allNum,
2159 Decl **allProperties, unsigned pNum,
2160 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002161
Erik Verbruggend64251f2011-12-06 09:25:23 +00002162 if (getObjCContainerKind() == Sema::OCK_None)
2163 return 0;
2164
2165 assert(AtEnd.isValid() && "Invalid location for '@end'");
2166
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002167 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2168 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002169
Mike Stump1eb44332009-09-09 15:08:12 +00002170 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002171 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2172 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002173 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002174
Steve Naroff0701bbb2009-01-08 17:28:14 +00002175 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2176 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2177 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2178
Chris Lattner4d391482007-12-12 07:09:47 +00002179 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002180 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002181 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002182
2183 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002184 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002185 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002186 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002187 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002188 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002189 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002190 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002191 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002192 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002193 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002194 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002195 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002196 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002197 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002198 if (!Context.getSourceManager().isInSystemHeader(
2199 Method->getLocation()))
2200 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2201 << Method->getDeclName();
2202 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2203 }
Chris Lattner4d391482007-12-12 07:09:47 +00002204 InsMap[Method->getSelector()] = Method;
2205 /// The following allows us to typecheck messages to "id".
2206 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002207 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002208 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002209 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002210 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002211 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002212 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002213 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002214 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002215 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002216 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002217 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002218 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002219 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002220 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002221 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002222 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002223 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002224 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002225 if (!Context.getSourceManager().isInSystemHeader(
2226 Method->getLocation()))
2227 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2228 << Method->getDeclName();
2229 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2230 }
Chris Lattner4d391482007-12-12 07:09:47 +00002231 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002232 /// The following allows us to typecheck messages to "Class".
2233 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002234 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002235 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002236 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002237 }
2238 }
2239 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002240 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002241 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002242 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002243 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002244 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002245 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002246 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002247 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002248 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002249
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002250 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002251 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002252 if (C->IsClassExtension()) {
2253 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2254 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002255 }
Chris Lattner4d391482007-12-12 07:09:47 +00002256 }
Steve Naroff09c47192009-01-09 15:36:25 +00002257 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002258 if (CDecl->getIdentifier())
2259 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2260 // user-defined setter/getter. It also synthesizes setter/getter methods
2261 // and adds them to the DeclContext and global method pools.
2262 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2263 E = CDecl->prop_end();
2264 I != E; ++I)
2265 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002266 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002267 }
2268 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002269 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002270 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002271 // Any property declared in a class extension might have user
2272 // declared setter or getter in current class extension or one
2273 // of the other class extensions. Mark them as synthesized as
2274 // property will be synthesized when property with same name is
2275 // seen in the @implementation.
2276 for (const ObjCCategoryDecl *ClsExtDecl =
2277 IDecl->getFirstClassExtension();
2278 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2279 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2280 E = ClsExtDecl->prop_end(); I != E; ++I) {
2281 ObjCPropertyDecl *Property = (*I);
2282 // Skip over properties declared @dynamic
2283 if (const ObjCPropertyImplDecl *PIDecl
2284 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2285 if (PIDecl->getPropertyImplementation()
2286 == ObjCPropertyImplDecl::Dynamic)
2287 continue;
2288
2289 for (const ObjCCategoryDecl *CExtDecl =
2290 IDecl->getFirstClassExtension();
2291 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2292 if (ObjCMethodDecl *GetterMethod =
2293 CExtDecl->getInstanceMethod(Property->getGetterName()))
2294 GetterMethod->setSynthesized(true);
2295 if (!Property->isReadOnly())
2296 if (ObjCMethodDecl *SetterMethod =
2297 CExtDecl->getInstanceMethod(Property->getSetterName()))
2298 SetterMethod->setSynthesized(true);
2299 }
2300 }
2301 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002302 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002303 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002304 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002305
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002306 if (LangOpts.ObjCNonFragileABI2)
2307 while (IDecl->getSuperClass()) {
2308 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2309 IDecl = IDecl->getSuperClass();
2310 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002311 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002312 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002313 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002314 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002315 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002316
Chris Lattner4d391482007-12-12 07:09:47 +00002317 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002318 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002319 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002320 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002321 Categories; Categories = Categories->getNextClassCategory()) {
2322 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002323 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002324 break;
2325 }
2326 }
2327 }
2328 }
Chris Lattner682bf922009-03-29 16:50:03 +00002329 if (isInterfaceDeclKind) {
2330 // Reject invalid vardecls.
2331 for (unsigned i = 0; i != tuvNum; i++) {
2332 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2333 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2334 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002335 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002336 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002337 }
Chris Lattner682bf922009-03-29 16:50:03 +00002338 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002339 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002340 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002341
2342 for (unsigned i = 0; i != tuvNum; i++) {
2343 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002344 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2345 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002346 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2347 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002348
2349 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002350}
2351
2352
2353/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2354/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002355static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002356CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002357 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002358}
2359
Ted Kremenek422bae72010-04-18 04:59:38 +00002360static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002361bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2362 const AttrVec &A) {
2363 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002364 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002365 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002366 return false;
2367
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002368 // method declared in interface has no attribute.
2369 // But implementation has attributes. This is invalid
2370 if (!IMD->hasAttrs())
2371 return true;
2372
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002373 const AttrVec &D = IMD->getAttrs();
2374 if (D.size() != A.size())
2375 return true;
2376
2377 // attributes on method declaration and definition must match exactly.
2378 // Note that we have at most a couple of attributes on methods, so this
2379 // n*n search is good enough.
2380 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2381 bool match = false;
2382 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2383 if ((*i)->getKind() == (*i1)->getKind()) {
2384 match = true;
2385 break;
2386 }
2387 }
2388 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002389 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002390 }
Sean Huntcf807c42010-08-18 23:23:40 +00002391 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002392}
2393
Douglas Gregore97179c2011-09-08 01:46:34 +00002394namespace {
2395 /// \brief Describes the compatibility of a result type with its method.
2396 enum ResultTypeCompatibilityKind {
2397 RTC_Compatible,
2398 RTC_Incompatible,
2399 RTC_Unknown
2400 };
2401}
2402
Douglas Gregor926df6c2011-06-11 01:09:30 +00002403/// \brief Check whether the declared result type of the given Objective-C
2404/// method declaration is compatible with the method's class.
2405///
Douglas Gregore97179c2011-09-08 01:46:34 +00002406static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002407CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2408 ObjCInterfaceDecl *CurrentClass) {
2409 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002410
2411 // If an Objective-C method inherits its related result type, then its
2412 // declared result type must be compatible with its own class type. The
2413 // declared result type is compatible if:
2414 if (const ObjCObjectPointerType *ResultObjectType
2415 = ResultType->getAs<ObjCObjectPointerType>()) {
2416 // - it is id or qualified id, or
2417 if (ResultObjectType->isObjCIdType() ||
2418 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002419 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002420
2421 if (CurrentClass) {
2422 if (ObjCInterfaceDecl *ResultClass
2423 = ResultObjectType->getInterfaceDecl()) {
2424 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002425 if (declaresSameEntity(CurrentClass, ResultClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002426 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002427
2428 // - it is a superclass of the method's class type
2429 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002430 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002431 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002432 } else {
2433 // Any Objective-C pointer type might be acceptable for a protocol
2434 // method; we just don't know.
2435 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002436 }
2437 }
2438
Douglas Gregore97179c2011-09-08 01:46:34 +00002439 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002440}
2441
John McCall6c2c2502011-07-22 02:45:48 +00002442namespace {
2443/// A helper class for searching for methods which a particular method
2444/// overrides.
2445class OverrideSearch {
2446 Sema &S;
2447 ObjCMethodDecl *Method;
2448 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2449 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2450 bool Recursive;
2451
2452public:
2453 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2454 Selector selector = method->getSelector();
2455
2456 // Bypass this search if we've never seen an instance/class method
2457 // with this selector before.
2458 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2459 if (it == S.MethodPool.end()) {
2460 if (!S.ExternalSource) return;
2461 it = S.ReadMethodPool(selector);
2462 }
2463 ObjCMethodList &list =
2464 method->isInstanceMethod() ? it->second.first : it->second.second;
2465 if (!list.Method) return;
2466
2467 ObjCContainerDecl *container
2468 = cast<ObjCContainerDecl>(method->getDeclContext());
2469
2470 // Prevent the search from reaching this container again. This is
2471 // important with categories, which override methods from the
2472 // interface and each other.
2473 Searched.insert(container);
2474 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002475 }
John McCall6c2c2502011-07-22 02:45:48 +00002476
2477 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2478 iterator begin() const { return Overridden.begin(); }
2479 iterator end() const { return Overridden.end(); }
2480
2481private:
2482 void searchFromContainer(ObjCContainerDecl *container) {
2483 if (container->isInvalidDecl()) return;
2484
2485 switch (container->getDeclKind()) {
2486#define OBJCCONTAINER(type, base) \
2487 case Decl::type: \
2488 searchFrom(cast<type##Decl>(container)); \
2489 break;
2490#define ABSTRACT_DECL(expansion)
2491#define DECL(type, base) \
2492 case Decl::type:
2493#include "clang/AST/DeclNodes.inc"
2494 llvm_unreachable("not an ObjC container!");
2495 }
2496 }
2497
2498 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002499 if (!protocol->hasDefinition())
2500 return;
2501
John McCall6c2c2502011-07-22 02:45:48 +00002502 // A method in a protocol declaration overrides declarations from
2503 // referenced ("parent") protocols.
2504 search(protocol->getReferencedProtocols());
2505 }
2506
2507 void searchFrom(ObjCCategoryDecl *category) {
2508 // A method in a category declaration overrides declarations from
2509 // the main class and from protocols the category references.
2510 search(category->getClassInterface());
2511 search(category->getReferencedProtocols());
2512 }
2513
2514 void searchFrom(ObjCCategoryImplDecl *impl) {
2515 // A method in a category definition that has a category
2516 // declaration overrides declarations from the category
2517 // declaration.
2518 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2519 search(category);
2520
2521 // Otherwise it overrides declarations from the class.
2522 } else {
2523 search(impl->getClassInterface());
2524 }
2525 }
2526
2527 void searchFrom(ObjCInterfaceDecl *iface) {
2528 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002529 if (!iface->hasDefinition())
2530 return;
2531
John McCall6c2c2502011-07-22 02:45:48 +00002532 // - categories,
2533 for (ObjCCategoryDecl *category = iface->getCategoryList();
2534 category; category = category->getNextClassCategory())
2535 search(category);
2536
2537 // - the super class, and
2538 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2539 search(super);
2540
2541 // - any referenced protocols.
2542 search(iface->getReferencedProtocols());
2543 }
2544
2545 void searchFrom(ObjCImplementationDecl *impl) {
2546 // A method in a class implementation overrides declarations from
2547 // the class interface.
2548 search(impl->getClassInterface());
2549 }
2550
2551
2552 void search(const ObjCProtocolList &protocols) {
2553 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2554 i != e; ++i)
2555 search(*i);
2556 }
2557
2558 void search(ObjCContainerDecl *container) {
2559 // Abort if we've already searched this container.
2560 if (!Searched.insert(container)) return;
2561
2562 // Check for a method in this container which matches this selector.
2563 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2564 Method->isInstanceMethod());
2565
2566 // If we find one, record it and bail out.
2567 if (meth) {
2568 Overridden.insert(meth);
2569 return;
2570 }
2571
2572 // Otherwise, search for methods that a hypothetical method here
2573 // would have overridden.
2574
2575 // Note that we're now in a recursive case.
2576 Recursive = true;
2577
2578 searchFromContainer(container);
2579 }
2580};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002581}
2582
John McCalld226f652010-08-21 09:40:31 +00002583Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002584 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002585 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002586 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002587 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002588 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002589 Selector Sel,
2590 // optional arguments. The number of types/arguments is obtained
2591 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002592 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002593 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002594 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002595 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002596 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002597 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002598 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002599 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002600 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002601 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2602 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002603 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002604
Douglas Gregore97179c2011-09-08 01:46:34 +00002605 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002606 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002607 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002608 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002609
Steve Naroffccef3712009-02-20 22:59:16 +00002610 // Methods cannot return interface types. All ObjC objects are
2611 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002612 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002613 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2614 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002615 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002616 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002617
2618 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002619 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002620 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002621 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002622 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002623 }
Mike Stump1eb44332009-09-09 15:08:12 +00002624
2625 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002626 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002627 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002628 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002629 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002630 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002631 /*isSynthesized=*/false,
2632 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002633 MethodDeclKind == tok::objc_optional
2634 ? ObjCMethodDecl::Optional
2635 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002636 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002637
Chris Lattner5f9e2722011-07-23 10:55:15 +00002638 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002639
Chris Lattner7db638d2009-04-11 19:42:43 +00002640 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002641 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002642 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002643
Chris Lattnere294d3f2009-04-11 18:57:04 +00002644 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002645 ArgType = Context.getObjCIdType();
2646 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002647 } else {
John McCall58e46772009-10-23 21:48:59 +00002648 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002649 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002650 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002651 }
Mike Stump1eb44332009-09-09 15:08:12 +00002652
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002653 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2654 LookupOrdinaryName, ForRedeclaration);
2655 LookupName(R, S);
2656 if (R.isSingleResult()) {
2657 NamedDecl *PrevDecl = R.getFoundDecl();
2658 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002659 Diag(ArgInfo[i].NameLoc,
2660 (MethodDefinition ? diag::warn_method_param_redefinition
2661 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002662 << ArgInfo[i].Name;
2663 Diag(PrevDecl->getLocation(),
2664 diag::note_previous_declaration);
2665 }
2666 }
2667
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002668 SourceLocation StartLoc = DI
2669 ? DI->getTypeLoc().getBeginLoc()
2670 : ArgInfo[i].NameLoc;
2671
John McCall81ef3e62011-04-23 02:46:06 +00002672 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2673 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2674 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002675
John McCall70798862011-05-02 00:30:12 +00002676 Param->setObjCMethodScopeInfo(i);
2677
Chris Lattner0ed844b2008-04-04 06:12:32 +00002678 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002679 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002680
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002681 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002682 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002683
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002684 S->AddDecl(Param);
2685 IdResolver.AddDecl(Param);
2686
Chris Lattner0ed844b2008-04-04 06:12:32 +00002687 Params.push_back(Param);
2688 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002689
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002690 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002691 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002692 QualType ArgType = Param->getType();
2693 if (ArgType.isNull())
2694 ArgType = Context.getObjCIdType();
2695 else
2696 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002697 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002698 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002699 Diag(Param->getLocation(),
2700 diag::err_object_cannot_be_passed_returned_by_value)
2701 << 1 << ArgType;
2702 Param->setInvalidDecl();
2703 }
2704 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002705
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002706 Params.push_back(Param);
2707 }
2708
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002709 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002710 ObjCMethod->setObjCDeclQualifier(
2711 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002712
2713 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002714 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002715
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002716 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002717 const ObjCMethodDecl *PrevMethod = 0;
2718 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002719 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002720 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2721 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002722 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002723 PrevMethod = ImpDecl->getClassMethod(Sel);
2724 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002725 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002726
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002727 ObjCMethodDecl *IMD = 0;
2728 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2729 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2730 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002731 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002732 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002733 SourceLocation MethodLoc = IMD->getLocation();
2734 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2735 Diag(EndLoc, diag::warn_attribute_method_def);
2736 Diag(MethodLoc, diag::note_method_declared_at);
2737 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002738 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002739 } else {
2740 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002741 }
John McCall6c2c2502011-07-22 02:45:48 +00002742
Chris Lattner4d391482007-12-12 07:09:47 +00002743 if (PrevMethod) {
2744 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002745 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002746 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002747 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002748 }
John McCall54abf7d2009-11-04 02:18:39 +00002749
Douglas Gregor926df6c2011-06-11 01:09:30 +00002750 // If this Objective-C method does not have a related result type, but we
2751 // are allowed to infer related result types, try to do so based on the
2752 // method family.
2753 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2754 if (!CurrentClass) {
2755 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2756 CurrentClass = Cat->getClassInterface();
2757 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2758 CurrentClass = Impl->getClassInterface();
2759 else if (ObjCCategoryImplDecl *CatImpl
2760 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2761 CurrentClass = CatImpl->getClassInterface();
2762 }
John McCall6c2c2502011-07-22 02:45:48 +00002763
Douglas Gregore97179c2011-09-08 01:46:34 +00002764 ResultTypeCompatibilityKind RTC
2765 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002766
2767 // Search for overridden methods and merge information down from them.
2768 OverrideSearch overrides(*this, ObjCMethod);
2769 for (OverrideSearch::iterator
2770 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2771 ObjCMethodDecl *overridden = *i;
2772
2773 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002774 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002775 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002776
2777 // Then merge the declarations.
2778 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002779
2780 // Check for overriding methods
2781 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00002782 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2783 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2784 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
Douglas Gregor926df6c2011-06-11 01:09:30 +00002785 }
2786
John McCallf85e1932011-06-15 23:02:42 +00002787 bool ARCError = false;
2788 if (getLangOptions().ObjCAutoRefCount)
2789 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2790
Douglas Gregore97179c2011-09-08 01:46:34 +00002791 // Infer the related result type when possible.
2792 if (!ARCError && RTC == RTC_Compatible &&
2793 !ObjCMethod->hasRelatedResultType() &&
2794 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002795 bool InferRelatedResultType = false;
2796 switch (ObjCMethod->getMethodFamily()) {
2797 case OMF_None:
2798 case OMF_copy:
2799 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002800 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002801 case OMF_mutableCopy:
2802 case OMF_release:
2803 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002804 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002805 break;
2806
2807 case OMF_alloc:
2808 case OMF_new:
2809 InferRelatedResultType = ObjCMethod->isClassMethod();
2810 break;
2811
2812 case OMF_init:
2813 case OMF_autorelease:
2814 case OMF_retain:
2815 case OMF_self:
2816 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2817 break;
2818 }
2819
John McCall6c2c2502011-07-22 02:45:48 +00002820 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002821 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002822 }
2823
John McCalld226f652010-08-21 09:40:31 +00002824 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002825}
2826
Chris Lattnercc98eac2008-12-17 07:13:27 +00002827bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002828 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002829 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002830 // Following is also an error. But it is caused by a missing @end
2831 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002832 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2833 return false;
2834 }
2835
Anders Carlsson15281452008-11-04 16:57:32 +00002836 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2837 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002838
Anders Carlsson15281452008-11-04 16:57:32 +00002839 return true;
2840}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002841
Chris Lattnercc98eac2008-12-17 07:13:27 +00002842/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2843/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002844void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002845 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002846 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002847 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002848 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002849 if (!Class) {
2850 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2851 return;
2852 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002853 if (LangOpts.ObjCNonFragileABI) {
2854 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2855 return;
2856 }
Mike Stump1eb44332009-09-09 15:08:12 +00002857
Chris Lattnercc98eac2008-12-17 07:13:27 +00002858 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002859 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002860 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002861 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002862 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002863 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002864 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002865 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2866 /*FIXME: StartL=*/ID->getLocation(),
2867 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002868 ID->getIdentifier(), ID->getType(),
2869 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002870 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002871 }
Mike Stump1eb44332009-09-09 15:08:12 +00002872
Chris Lattnercc98eac2008-12-17 07:13:27 +00002873 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002874 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002875 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002876 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002877 if (getLangOptions().CPlusPlus)
2878 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002879 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002880 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002881 }
2882}
2883
Douglas Gregor160b5632010-04-26 17:32:49 +00002884/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002885VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2886 SourceLocation StartLoc,
2887 SourceLocation IdLoc,
2888 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002889 bool Invalid) {
2890 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2891 // duration shall not be qualified by an address-space qualifier."
2892 // Since all parameters have automatic store duration, they can not have
2893 // an address space.
2894 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002895 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002896 Invalid = true;
2897 }
2898
2899 // An @catch parameter must be an unqualified object pointer type;
2900 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2901 if (Invalid) {
2902 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002903 } else if (T->isDependentType()) {
2904 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002905 } else if (!T->isObjCObjectPointerType()) {
2906 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002907 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002908 } else if (T->isObjCQualifiedIdType()) {
2909 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002910 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002911 }
2912
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002913 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2914 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002915 New->setExceptionVariable(true);
2916
Douglas Gregor9aab9c42011-12-10 01:22:52 +00002917 // In ARC, infer 'retaining' for variables of retainable type.
2918 if (getLangOptions().ObjCAutoRefCount && inferObjCARCLifetime(New))
2919 Invalid = true;
2920
Douglas Gregor160b5632010-04-26 17:32:49 +00002921 if (Invalid)
2922 New->setInvalidDecl();
2923 return New;
2924}
2925
John McCalld226f652010-08-21 09:40:31 +00002926Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002927 const DeclSpec &DS = D.getDeclSpec();
2928
2929 // We allow the "register" storage class on exception variables because
2930 // GCC did, but we drop it completely. Any other storage class is an error.
2931 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2932 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2933 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2934 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2935 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2936 << DS.getStorageClassSpec();
2937 }
2938 if (D.getDeclSpec().isThreadSpecified())
2939 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2940 D.getMutableDeclSpec().ClearStorageClassSpecs();
2941
2942 DiagnoseFunctionSpecifiers(D);
2943
2944 // Check that there are no default arguments inside the type of this
2945 // exception object (C++ only).
2946 if (getLangOptions().CPlusPlus)
2947 CheckExtraCXXDefaultArguments(D);
2948
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002949 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002950 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002951
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002952 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2953 D.getSourceRange().getBegin(),
2954 D.getIdentifierLoc(),
2955 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002956 D.isInvalidType());
2957
2958 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2959 if (D.getCXXScopeSpec().isSet()) {
2960 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2961 << D.getCXXScopeSpec().getRange();
2962 New->setInvalidDecl();
2963 }
2964
2965 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002966 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002967 if (D.getIdentifier())
2968 IdResolver.AddDecl(New);
2969
2970 ProcessDeclAttributes(S, New, D);
2971
2972 if (New->hasAttr<BlocksAttr>())
2973 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002974 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002975}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002976
2977/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002978/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002979void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002980 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002981 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2982 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002983 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002984 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002985 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002986 }
2987}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002988
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002989void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002990 // Load referenced selectors from the external source.
2991 if (ExternalSource) {
2992 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2993 ExternalSource->ReadReferencedSelectors(Sels);
2994 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2995 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2996 }
2997
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002998 // Warning will be issued only when selector table is
2999 // generated (which means there is at lease one implementation
3000 // in the TU). This is to match gcc's behavior.
3001 if (ReferencedSelectors.empty() ||
3002 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003003 return;
3004 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3005 ReferencedSelectors.begin(),
3006 E = ReferencedSelectors.end(); S != E; ++S) {
3007 Selector Sel = (*S).first;
3008 if (!LookupImplementedMethodInGlobalPool(Sel))
3009 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3010 }
3011 return;
3012}