blob: 51d282a546c3e387cb23d86aef861de850dc324e [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,
586 /*PrevDecl=*/0,
587 /*isForwardDecl=*/false);
588 PDecl->startDefinition();
589 } else {
590 if (PrevDecl) {
591 // Check for circular dependencies among protocol declarations. This can
592 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000593 ObjCList<ObjCProtocolDecl> PList;
594 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
595 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000596 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000597 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000598
599 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000600 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000601 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregor27c6da22012-01-01 20:30:41 +0000602 /*PrevDecl=*/PrevDecl,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000603 /*isForwardDecl=*/false);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000604
Douglas Gregor6e378de2009-04-23 23:18:26 +0000605 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000606 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000607 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000608
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000609 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000610 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000611
612 // Merge attributes from previous declarations.
613 if (PrevDecl)
614 mergeDeclAttributes(PDecl, PrevDecl);
615
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000616 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000617 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000618 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
619 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000620 PDecl->setLocEnd(EndProtoLoc);
621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
623 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000624 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000625}
626
627/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000628/// issues an error if they are not declared. It returns list of
629/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000630void
Chris Lattnere13b9592008-07-26 04:03:38 +0000631Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000632 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000633 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000634 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000635 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000636 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
637 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000638 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000639 TypoCorrection Corrected = CorrectTypo(
640 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
641 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
642 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000643 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000644 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000645 Diag(PDecl->getLocation(), diag::note_previous_decl)
646 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000647 }
648 }
649
650 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000651 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000652 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000653 continue;
654 }
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000656 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000657
658 // If this is a forward declaration and we are supposed to warn in this
659 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000660 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000661 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000662 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000663 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000664 }
665}
666
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000667/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000668/// a class method in its extension.
669///
Mike Stump1eb44332009-09-09 15:08:12 +0000670void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000671 ObjCInterfaceDecl *ID) {
672 if (!ID)
673 return; // Possibly due to previous error
674
675 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000676 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
677 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000678 ObjCMethodDecl *MD = *i;
679 MethodMap[MD->getSelector()] = MD;
680 }
681
682 if (MethodMap.empty())
683 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000684 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
685 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000686 ObjCMethodDecl *Method = *i;
687 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
688 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
689 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
690 << Method->getDeclName();
691 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
692 }
693 }
694}
695
Chris Lattner58fe03b2009-04-12 08:43:13 +0000696/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000697Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +0000698Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000699 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000700 unsigned NumElts,
701 AttributeList *attrList) {
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000702 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +0000703 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000704 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000705 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
706 ForRedeclaration);
707 ObjCProtocolDecl *PDecl
708 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
709 IdentList[i].second, AtProtocolLoc,
710 PrevDecl, /*isForwardDecl=*/true);
711
712 PushOnScopeChains(PDecl, TUScope);
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000713 CheckObjCDeclScope(PDecl);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000714
Douglas Gregor3937f872012-01-01 20:33:24 +0000715 if (attrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000716 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000717
718 if (PrevDecl)
719 mergeDeclAttributes(PDecl, PrevDecl);
720
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000721 DeclsInGroup.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000722 }
Mike Stump1eb44332009-09-09 15:08:12 +0000723
Douglas Gregorbd9482d2012-01-01 21:23:57 +0000724 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +0000725}
726
John McCalld226f652010-08-21 09:40:31 +0000727Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000728ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
729 IdentifierInfo *ClassName, SourceLocation ClassLoc,
730 IdentifierInfo *CategoryName,
731 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000732 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000733 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000734 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000735 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000736 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000737 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000738
739 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000740
741 if (!IDecl
742 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
743 PDiag(diag::err_category_forward_interface)
744 << (CategoryName == 0))) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000745 // Create an invalid ObjCCategoryDecl to serve as context for
746 // the enclosing method declarations. We mark the decl invalid
747 // to make it clear that this isn't a valid AST.
748 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000749 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000750 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000751
752 if (!IDecl)
753 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000754 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000755 }
756
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000757 if (!CategoryName && IDecl->getImplementation()) {
758 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
759 Diag(IDecl->getImplementation()->getLocation(),
760 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000761 }
762
Fariborz Jahanian25760612010-02-15 21:55:26 +0000763 if (CategoryName) {
764 /// Check for duplicate interface declaration for this category
765 ObjCCategoryDecl *CDeclChain;
766 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
767 CDeclChain = CDeclChain->getNextClassCategory()) {
768 if (CDeclChain->getIdentifier() == CategoryName) {
769 // Class extensions can be declared multiple times.
770 Diag(CategoryLoc, diag::warn_dup_category_def)
771 << ClassName << CategoryName;
772 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
773 break;
774 }
Chris Lattner70f19542009-02-16 21:26:43 +0000775 }
776 }
Chris Lattner70f19542009-02-16 21:26:43 +0000777
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000778 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
779 ClassLoc, CategoryLoc, CategoryName, IDecl);
780 // FIXME: PushOnScopeChains?
781 CurContext->addDecl(CDecl);
782
Chris Lattner4d391482007-12-12 07:09:47 +0000783 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000784 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000785 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000786 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000787 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000788 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000789 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000790 }
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Anders Carlsson15281452008-11-04 16:57:32 +0000792 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000793 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000794}
795
796/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000797/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000798/// object.
John McCalld226f652010-08-21 09:40:31 +0000799Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000800 SourceLocation AtCatImplLoc,
801 IdentifierInfo *ClassName, SourceLocation ClassLoc,
802 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000803 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000804 ObjCCategoryDecl *CatIDecl = 0;
805 if (IDecl) {
806 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
807 if (!CatIDecl) {
808 // Category @implementation with no corresponding @interface.
809 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000810 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
811 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000812 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000813 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000814 }
815 }
816
Mike Stump1eb44332009-09-09 15:08:12 +0000817 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000818 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000819 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000820 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000821 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000822 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000823 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000824 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
825 diag::err_undef_interface)) {
826 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000827 }
Chris Lattner4d391482007-12-12 07:09:47 +0000828
Douglas Gregord0434102009-01-09 00:49:46 +0000829 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000830 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000831
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000832 // If the interface is deprecated/unavailable, warn/error about it.
833 if (IDecl)
834 DiagnoseUseOfDecl(IDecl, ClassLoc);
835
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000836 /// Check that CatName, category name, is not used in another implementation.
837 if (CatIDecl) {
838 if (CatIDecl->getImplementation()) {
839 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
840 << CatName;
841 Diag(CatIDecl->getImplementation()->getLocation(),
842 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000843 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000844 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000845 // Warn on implementating category of deprecated class under
846 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000847 DiagnoseObjCImplementedDeprecations(*this,
848 dyn_cast<NamedDecl>(IDecl),
849 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000850 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000851 }
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Anders Carlsson15281452008-11-04 16:57:32 +0000853 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000854 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000855}
856
John McCalld226f652010-08-21 09:40:31 +0000857Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000858 SourceLocation AtClassImplLoc,
859 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000860 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000861 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000862 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000863 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000864 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000865 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
866 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000867 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000868 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000869 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000870 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000871 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
872 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000873 } else {
874 // We did not find anything with the name ClassName; try to correct for
875 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000876 TypoCorrection Corrected = CorrectTypo(
877 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
878 NULL, NULL, false, CTC_NoKeywords);
879 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000880 // Suggest the (potentially) correct interface name. However, put the
881 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000882 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000883 // provide a code-modification hint or use the typo name for recovery,
884 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000885 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000886 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000887 << ClassName << CorrectedName;
888 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
889 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000890 IDecl = 0;
891 } else {
892 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
893 }
Chris Lattner4d391482007-12-12 07:09:47 +0000894 }
Mike Stump1eb44332009-09-09 15:08:12 +0000895
Chris Lattner4d391482007-12-12 07:09:47 +0000896 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000897 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000898 if (SuperClassname) {
899 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000900 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
901 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000902 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000903 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
904 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000905 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000906 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000907 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000908 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000909 Diag(SuperClassLoc, diag::err_undef_superclass)
910 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000911 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000912 // This implementation and its interface do not have the same
913 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000914 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000915 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000916 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000917 }
918 }
919 }
Mike Stump1eb44332009-09-09 15:08:12 +0000920
Chris Lattner4d391482007-12-12 07:09:47 +0000921 if (!IDecl) {
922 // Legacy case of @implementation with no corresponding @interface.
923 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000924
Mike Stump390b4cc2009-05-16 07:39:55 +0000925 // FIXME: Do we support attributes on the @implementation? If so we should
926 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000927 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000928 ClassName, /*PrevDecl=*/0, ClassLoc,
929 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000930 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000931 if (SDecl) {
932 IDecl->setSuperClass(SDecl);
933 IDecl->setSuperClassLoc(SuperClassLoc);
934 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
935 } else {
936 IDecl->setEndOfDefinitionLoc(ClassLoc);
937 }
938
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000939 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000940 } else {
941 // Mark the interface as being completed, even if it was just as
942 // @class ....;
943 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000944 if (!IDecl->hasDefinition())
945 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000946 }
Mike Stump1eb44332009-09-09 15:08:12 +0000947
948 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000949 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
950 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Anders Carlsson15281452008-11-04 16:57:32 +0000952 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000953 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000954
Chris Lattner4d391482007-12-12 07:09:47 +0000955 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000956 if (IDecl->getImplementation()) {
957 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000958 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000959 Diag(IDecl->getImplementation()->getLocation(),
960 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000961 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000962 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000963 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000964 // Warn on implementating deprecated class under
965 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000966 DiagnoseObjCImplementedDeprecations(*this,
967 dyn_cast<NamedDecl>(IDecl),
968 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000969 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000970 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000971}
972
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000973void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
974 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000975 SourceLocation RBrace) {
976 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000977 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000978 if (!IDecl)
979 return;
980 /// Check case of non-existing @interface decl.
981 /// (legacy objective-c @implementation decl without an @interface decl).
982 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000983 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +0000984 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000985 // Add ivar's to class's DeclContext.
986 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000987 ivars[i]->setLexicalDeclContext(ImpDecl);
988 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000989 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000990 }
991
Chris Lattner4d391482007-12-12 07:09:47 +0000992 return;
993 }
994 // If implementation has empty ivar list, just return.
995 if (numIvars == 0)
996 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000997
Chris Lattner4d391482007-12-12 07:09:47 +0000998 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000999 if (LangOpts.ObjCNonFragileABI2) {
1000 if (ImpDecl->getSuperClass())
1001 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1002 for (unsigned i = 0; i < numIvars; i++) {
1003 ObjCIvarDecl* ImplIvar = ivars[i];
1004 if (const ObjCIvarDecl *ClsIvar =
1005 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1006 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1007 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1008 continue;
1009 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001010 // Instance ivar to Implementation's DeclContext.
1011 ImplIvar->setLexicalDeclContext(ImpDecl);
1012 IDecl->makeDeclVisibleInContext(ImplIvar, false);
1013 ImpDecl->addDecl(ImplIvar);
1014 }
1015 return;
1016 }
Chris Lattner4d391482007-12-12 07:09:47 +00001017 // Check interface's Ivar list against those in the implementation.
1018 // names and types must match.
1019 //
Chris Lattner4d391482007-12-12 07:09:47 +00001020 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001021 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001022 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1023 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001024 ObjCIvarDecl* ImplIvar = ivars[j++];
1025 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001026 assert (ImplIvar && "missing implementation ivar");
1027 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Steve Naroffca331292009-03-03 14:49:36 +00001029 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001030 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001031 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001032 << ImplIvar->getIdentifier()
1033 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001034 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001035 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1036 ImplIvar->getBitWidthValue(Context) !=
1037 ClsIvar->getBitWidthValue(Context)) {
1038 Diag(ImplIvar->getBitWidth()->getLocStart(),
1039 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1040 Diag(ClsIvar->getBitWidth()->getLocStart(),
1041 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001042 }
Steve Naroffca331292009-03-03 14:49:36 +00001043 // Make sure the names are identical.
1044 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001045 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001046 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001047 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001048 }
1049 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001050 }
Mike Stump1eb44332009-09-09 15:08:12 +00001051
Chris Lattner609e4c72007-12-12 18:11:49 +00001052 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001053 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001054 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001055 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001056}
1057
Steve Naroff3c2eb662008-02-10 21:38:56 +00001058void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001059 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001060 // No point warning no definition of method which is 'unavailable'.
1061 if (method->hasAttr<UnavailableAttr>())
1062 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001063 if (!IncompleteImpl) {
1064 Diag(ImpLoc, diag::warn_incomplete_impl);
1065 IncompleteImpl = true;
1066 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001067 if (DiagID == diag::warn_unimplemented_protocol_method)
1068 Diag(ImpLoc, DiagID) << method->getDeclName();
1069 else
1070 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001071}
1072
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001073/// Determines if type B can be substituted for type A. Returns true if we can
1074/// guarantee that anything that the user will do to an object of type A can
1075/// also be done to an object of type B. This is trivially true if the two
1076/// types are the same, or if B is a subclass of A. It becomes more complex
1077/// in cases where protocols are involved.
1078///
1079/// Object types in Objective-C describe the minimum requirements for an
1080/// object, rather than providing a complete description of a type. For
1081/// example, if A is a subclass of B, then B* may refer to an instance of A.
1082/// The principle of substitutability means that we may use an instance of A
1083/// anywhere that we may use an instance of B - it will implement all of the
1084/// ivars of B and all of the methods of B.
1085///
1086/// This substitutability is important when type checking methods, because
1087/// the implementation may have stricter type definitions than the interface.
1088/// The interface specifies minimum requirements, but the implementation may
1089/// have more accurate ones. For example, a method may privately accept
1090/// instances of B, but only publish that it accepts instances of A. Any
1091/// object passed to it will be type checked against B, and so will implicitly
1092/// by a valid A*. Similarly, a method may return a subclass of the class that
1093/// it is declared as returning.
1094///
1095/// This is most important when considering subclassing. A method in a
1096/// subclass must accept any object as an argument that its superclass's
1097/// implementation accepts. It may, however, accept a more general type
1098/// without breaking substitutability (i.e. you can still use the subclass
1099/// anywhere that you can use the superclass, but not vice versa). The
1100/// converse requirement applies to return types: the return type for a
1101/// subclass method must be a valid object of the kind that the superclass
1102/// advertises, but it may be specified more accurately. This avoids the need
1103/// for explicit down-casting by callers.
1104///
1105/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001106static bool isObjCTypeSubstitutable(ASTContext &Context,
1107 const ObjCObjectPointerType *A,
1108 const ObjCObjectPointerType *B,
1109 bool rejectId) {
1110 // Reject a protocol-unqualified id.
1111 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001112
1113 // If B is a qualified id, then A must also be a qualified id and it must
1114 // implement all of the protocols in B. It may not be a qualified class.
1115 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1116 // stricter definition so it is not substitutable for id<A>.
1117 if (B->isObjCQualifiedIdType()) {
1118 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001119 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1120 QualType(B,0),
1121 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001122 }
1123
1124 /*
1125 // id is a special type that bypasses type checking completely. We want a
1126 // warning when it is used in one place but not another.
1127 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1128
1129
1130 // If B is a qualified id, then A must also be a qualified id (which it isn't
1131 // if we've got this far)
1132 if (B->isObjCQualifiedIdType()) return false;
1133 */
1134
1135 // Now we know that A and B are (potentially-qualified) class types. The
1136 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001137 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001138}
1139
John McCall10302c02010-10-28 02:34:38 +00001140static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1141 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1142}
1143
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001144static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001145 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001146 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001147 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001148 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001149 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001150 if (IsProtocolMethodDecl &&
1151 (MethodDecl->getObjCDeclQualifier() !=
1152 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001153 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001154 S.Diag(MethodImpl->getLocation(),
1155 (IsOverridingMode ?
1156 diag::warn_conflicting_overriding_ret_type_modifiers
1157 : diag::warn_conflicting_ret_type_modifiers))
1158 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001159 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1160 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1161 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1162 }
1163 else
1164 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001165 }
1166
John McCall10302c02010-10-28 02:34:38 +00001167 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001168 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001169 return true;
1170 if (!Warn)
1171 return false;
John McCall10302c02010-10-28 02:34:38 +00001172
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001173 unsigned DiagID =
1174 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1175 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001176
1177 // Mismatches between ObjC pointers go into a different warning
1178 // category, and sometimes they're even completely whitelisted.
1179 if (const ObjCObjectPointerType *ImplPtrTy =
1180 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1181 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001182 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001183 // Allow non-matching return types as long as they don't violate
1184 // the principle of substitutability. Specifically, we permit
1185 // return types that are subclasses of the declared return type,
1186 // or that are more-qualified versions of the declared type.
1187 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001188 return false;
John McCall10302c02010-10-28 02:34:38 +00001189
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001190 DiagID =
1191 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1192 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001193 }
1194 }
1195
1196 S.Diag(MethodImpl->getLocation(), DiagID)
1197 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001198 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001199 << MethodImpl->getResultType()
1200 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001201 S.Diag(MethodDecl->getLocation(),
1202 IsOverridingMode ? diag::note_previous_declaration
1203 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001204 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001205 return false;
John McCall10302c02010-10-28 02:34:38 +00001206}
1207
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001208static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001209 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001210 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001211 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001212 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001213 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001214 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001215 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001216 if (IsProtocolMethodDecl &&
1217 (ImplVar->getObjCDeclQualifier() !=
1218 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001219 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001220 if (IsOverridingMode)
1221 S.Diag(ImplVar->getLocation(),
1222 diag::warn_conflicting_overriding_param_modifiers)
1223 << getTypeRange(ImplVar->getTypeSourceInfo())
1224 << MethodImpl->getDeclName();
1225 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001226 diag::warn_conflicting_param_modifiers)
1227 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001228 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001229 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1230 << getTypeRange(IfaceVar->getTypeSourceInfo());
1231 }
1232 else
1233 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001234 }
1235
John McCall10302c02010-10-28 02:34:38 +00001236 QualType ImplTy = ImplVar->getType();
1237 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001238
John McCall10302c02010-10-28 02:34:38 +00001239 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001240 return true;
1241
1242 if (!Warn)
1243 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001244 unsigned DiagID =
1245 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1246 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001247
1248 // Mismatches between ObjC pointers go into a different warning
1249 // category, and sometimes they're even completely whitelisted.
1250 if (const ObjCObjectPointerType *ImplPtrTy =
1251 ImplTy->getAs<ObjCObjectPointerType>()) {
1252 if (const ObjCObjectPointerType *IfacePtrTy =
1253 IfaceTy->getAs<ObjCObjectPointerType>()) {
1254 // Allow non-matching argument types as long as they don't
1255 // violate the principle of substitutability. Specifically, the
1256 // implementation must accept any objects that the superclass
1257 // accepts, however it may also accept others.
1258 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001259 return false;
John McCall10302c02010-10-28 02:34:38 +00001260
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001261 DiagID =
1262 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1263 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001264 }
1265 }
1266
1267 S.Diag(ImplVar->getLocation(), DiagID)
1268 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001269 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1270 S.Diag(IfaceVar->getLocation(),
1271 (IsOverridingMode ? diag::note_previous_declaration
1272 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001273 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001274 return false;
John McCall10302c02010-10-28 02:34:38 +00001275}
John McCallf85e1932011-06-15 23:02:42 +00001276
1277/// In ARC, check whether the conventional meanings of the two methods
1278/// match. If they don't, it's a hard error.
1279static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1280 ObjCMethodDecl *decl) {
1281 ObjCMethodFamily implFamily = impl->getMethodFamily();
1282 ObjCMethodFamily declFamily = decl->getMethodFamily();
1283 if (implFamily == declFamily) return false;
1284
1285 // Since conventions are sorted by selector, the only possibility is
1286 // that the types differ enough to cause one selector or the other
1287 // to fall out of the family.
1288 assert(implFamily == OMF_None || declFamily == OMF_None);
1289
1290 // No further diagnostics required on invalid declarations.
1291 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1292
1293 const ObjCMethodDecl *unmatched = impl;
1294 ObjCMethodFamily family = declFamily;
1295 unsigned errorID = diag::err_arc_lost_method_convention;
1296 unsigned noteID = diag::note_arc_lost_method_convention;
1297 if (declFamily == OMF_None) {
1298 unmatched = decl;
1299 family = implFamily;
1300 errorID = diag::err_arc_gained_method_convention;
1301 noteID = diag::note_arc_gained_method_convention;
1302 }
1303
1304 // Indexes into a %select clause in the diagnostic.
1305 enum FamilySelector {
1306 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1307 };
1308 FamilySelector familySelector = FamilySelector();
1309
1310 switch (family) {
1311 case OMF_None: llvm_unreachable("logic error, no method convention");
1312 case OMF_retain:
1313 case OMF_release:
1314 case OMF_autorelease:
1315 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001316 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001317 case OMF_retainCount:
1318 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001319 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001320 // Mismatches for these methods don't change ownership
1321 // conventions, so we don't care.
1322 return false;
1323
1324 case OMF_init: familySelector = F_init; break;
1325 case OMF_alloc: familySelector = F_alloc; break;
1326 case OMF_copy: familySelector = F_copy; break;
1327 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1328 case OMF_new: familySelector = F_new; break;
1329 }
1330
1331 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1332 ReasonSelector reasonSelector;
1333
1334 // The only reason these methods don't fall within their families is
1335 // due to unusual result types.
1336 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1337 reasonSelector = R_UnrelatedReturn;
1338 } else {
1339 reasonSelector = R_NonObjectReturn;
1340 }
1341
1342 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1343 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1344
1345 return true;
1346}
John McCall10302c02010-10-28 02:34:38 +00001347
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001348void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001349 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001350 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001351 if (getLangOptions().ObjCAutoRefCount &&
1352 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1353 return;
1354
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001355 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001356 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001357 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001358
Chris Lattner3aff9192009-04-11 19:58:42 +00001359 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001360 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001361 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001362 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001363 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001364 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001365
Fariborz Jahanian21121902011-08-08 18:03:17 +00001366 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001367 Diag(ImpMethodDecl->getLocation(),
1368 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001369 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001370 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001371}
1372
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001373void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1374 ObjCMethodDecl *Overridden,
1375 bool IsProtocolMethodDecl) {
1376
1377 CheckMethodOverrideReturn(*this, Method, Overridden,
1378 IsProtocolMethodDecl, true,
1379 true);
1380
1381 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
1382 IF = Overridden->param_begin(), EM = Method->param_end();
1383 IM != EM; ++IM, ++IF) {
1384 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1385 IsProtocolMethodDecl, true, true);
1386 }
1387
1388 if (Method->isVariadic() != Overridden->isVariadic()) {
1389 Diag(Method->getLocation(),
1390 diag::warn_conflicting_overriding_variadic);
1391 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1392 }
1393}
1394
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001395/// WarnExactTypedMethods - This routine issues a warning if method
1396/// implementation declaration matches exactly that of its declaration.
1397void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1398 ObjCMethodDecl *MethodDecl,
1399 bool IsProtocolMethodDecl) {
1400 // don't issue warning when protocol method is optional because primary
1401 // class is not required to implement it and it is safe for protocol
1402 // to implement it.
1403 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1404 return;
1405 // don't issue warning when primary class's method is
1406 // depecated/unavailable.
1407 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1408 MethodDecl->hasAttr<DeprecatedAttr>())
1409 return;
1410
1411 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1412 IsProtocolMethodDecl, false, false);
1413 if (match)
1414 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1415 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1416 IM != EM; ++IM, ++IF) {
1417 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1418 *IM, *IF,
1419 IsProtocolMethodDecl, false, false);
1420 if (!match)
1421 break;
1422 }
1423 if (match)
1424 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001425 if (match)
1426 match = !(MethodDecl->isClassMethod() &&
1427 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001428
1429 if (match) {
1430 Diag(ImpMethodDecl->getLocation(),
1431 diag::warn_category_method_impl_match);
1432 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1433 }
1434}
1435
Mike Stump390b4cc2009-05-16 07:39:55 +00001436/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1437/// improve the efficiency of selector lookups and type checking by associating
1438/// with each protocol / interface / category the flattened instance tables. If
1439/// we used an immutable set to keep the table then it wouldn't add significant
1440/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001441
Steve Naroffefe7f362008-02-08 22:06:17 +00001442/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001443/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001444void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1445 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001446 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001447 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001448 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001449 ObjCContainerDecl *CDecl) {
1450 ObjCInterfaceDecl *IDecl;
1451 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1452 IDecl = C->getClassInterface();
1453 else
1454 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1455 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1456
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001457 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001458 ObjCInterfaceDecl *NSIDecl = 0;
1459 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001460 // check to see if class implements forwardInvocation method and objects
1461 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001462 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001463 // Under such conditions, which means that every method possible is
1464 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001465 // found" warnings.
1466 // FIXME: Use a general GetUnarySelector method for this.
1467 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1468 Selector fISelector = Context.Selectors.getSelector(1, &II);
1469 if (InsMap.count(fISelector))
1470 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1471 // need be implemented in the implementation.
1472 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1473 }
Mike Stump1eb44332009-09-09 15:08:12 +00001474
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001475 // If a method lookup fails locally we still need to look and see if
1476 // the method was implemented by a base class or an inherited
1477 // protocol. This lookup is slow, but occurs rarely in correct code
1478 // and otherwise would terminate in a warning.
1479
Chris Lattner4d391482007-12-12 07:09:47 +00001480 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001481 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001482 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001483 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001484 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001485 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001486 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001487 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001488 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001489 // Ugly, but necessary. Method declared in protcol might have
1490 // have been synthesized due to a property declared in the class which
1491 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001492 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001493 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001494 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001495 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001496 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
David Blaikied6471f72011-09-25 23:23:43 +00001497 != DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001498 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001499 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001500 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1501 << PDecl->getDeclName();
1502 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001503 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001504 }
1505 }
Chris Lattner4d391482007-12-12 07:09:47 +00001506 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001507 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001508 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001509 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001510 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001511 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1512 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001513 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001514 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001515 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1516 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001517 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001518 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001519 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1520 PDecl->getDeclName();
1521 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001522 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001523 }
Chris Lattner780f3292008-07-21 21:32:27 +00001524 // Check on this protocols's referenced protocols, recursively.
1525 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1526 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001527 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001528}
1529
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001530/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001531/// or protocol against those declared in their implementations.
1532///
1533void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1534 const llvm::DenseSet<Selector> &ClsMap,
1535 llvm::DenseSet<Selector> &InsMapSeen,
1536 llvm::DenseSet<Selector> &ClsMapSeen,
1537 ObjCImplDecl* IMPDecl,
1538 ObjCContainerDecl* CDecl,
1539 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001540 bool ImmediateClass,
1541 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001542 // Check and see if instance methods in class interface have been
1543 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001544 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1545 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001546 if (InsMapSeen.count((*I)->getSelector()))
1547 continue;
1548 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001549 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001550 !InsMap.count((*I)->getSelector())) {
1551 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001552 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1553 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001554 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001555 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001556 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001557 IMPDecl->getInstanceMethod((*I)->getSelector());
1558 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1559 "Expected to find the method through lookup as well");
1560 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001561 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001562 if (ImpMethodDecl) {
1563 if (!WarnExactMatch)
1564 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1565 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001566 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001567 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1568 isa<ObjCProtocolDecl>(CDecl));
1569 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001570 }
1571 }
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001573 // Check and see if class methods in class interface have been
1574 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001575 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001576 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001577 if (ClsMapSeen.count((*I)->getSelector()))
1578 continue;
1579 ClsMapSeen.insert((*I)->getSelector());
1580 if (!ClsMap.count((*I)->getSelector())) {
1581 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001582 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1583 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001584 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001585 ObjCMethodDecl *ImpMethodDecl =
1586 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001587 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1588 "Expected to find the method through lookup as well");
1589 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001590 if (!WarnExactMatch)
1591 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1592 isa<ObjCProtocolDecl>(CDecl));
1593 else
1594 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1595 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001596 }
1597 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001598
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001599 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001600 // Also methods in class extensions need be looked at next.
1601 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1602 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1603 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1604 IMPDecl,
1605 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001606 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001607
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001608 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001609 for (ObjCInterfaceDecl::all_protocol_iterator
1610 PI = I->all_referenced_protocol_begin(),
1611 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001612 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1613 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001614 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001615
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001616 // FIXME. For now, we are not checking for extact match of methods
1617 // in category implementation and its primary class's super class.
1618 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001619 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001620 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001621 I->getSuperClass(), IncompleteImpl, false);
1622 }
1623}
1624
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001625/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1626/// category matches with those implemented in its primary class and
1627/// warns each time an exact match is found.
1628void Sema::CheckCategoryVsClassMethodMatches(
1629 ObjCCategoryImplDecl *CatIMPDecl) {
1630 llvm::DenseSet<Selector> InsMap, ClsMap;
1631
1632 for (ObjCImplementationDecl::instmeth_iterator
1633 I = CatIMPDecl->instmeth_begin(),
1634 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1635 InsMap.insert((*I)->getSelector());
1636
1637 for (ObjCImplementationDecl::classmeth_iterator
1638 I = CatIMPDecl->classmeth_begin(),
1639 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1640 ClsMap.insert((*I)->getSelector());
1641 if (InsMap.empty() && ClsMap.empty())
1642 return;
1643
1644 // Get category's primary class.
1645 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1646 if (!CatDecl)
1647 return;
1648 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1649 if (!IDecl)
1650 return;
1651 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1652 bool IncompleteImpl = false;
1653 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1654 CatIMPDecl, IDecl,
1655 IncompleteImpl, false, true /*WarnExactMatch*/);
1656}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001657
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001658void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001659 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001660 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001661 llvm::DenseSet<Selector> InsMap;
1662 // Check and see if instance methods in class interface have been
1663 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001664 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001665 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001666 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001667
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001668 // Check and see if properties declared in the interface have either 1)
1669 // an implementation or 2) there is a @synthesize/@dynamic implementation
1670 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001671 if (isa<ObjCInterfaceDecl>(CDecl) &&
1672 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001673 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001674
Chris Lattner4d391482007-12-12 07:09:47 +00001675 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001676 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001677 I = IMPDecl->classmeth_begin(),
1678 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001679 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001681 // Check for type conflict of methods declared in a class/protocol and
1682 // its implementation; if any.
1683 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001684 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1685 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001686 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001687
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001688 // check all methods implemented in category against those declared
1689 // in its primary class.
1690 if (ObjCCategoryImplDecl *CatDecl =
1691 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1692 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Chris Lattner4d391482007-12-12 07:09:47 +00001694 // Check the protocol list for unimplemented methods in the @implementation
1695 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001696 // Check and see if class methods in class interface have been
1697 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Chris Lattnercddc8882009-03-01 00:56:52 +00001699 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001700 for (ObjCInterfaceDecl::all_protocol_iterator
1701 PI = I->all_referenced_protocol_begin(),
1702 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001703 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001704 InsMap, ClsMap, I);
1705 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001706 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1707 Categories; Categories = Categories->getNextClassExtension())
1708 ImplMethodsVsClassMethods(S, IMPDecl,
1709 const_cast<ObjCCategoryDecl*>(Categories),
1710 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001711 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001712 // For extended class, unimplemented methods in its protocols will
1713 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001714 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001715 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1716 E = C->protocol_end(); PI != E; ++PI)
1717 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001718 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001719 // Report unimplemented properties in the category as well.
1720 // When reporting on missing setter/getters, do not report when
1721 // setter/getter is implemented in category's primary class
1722 // implementation.
1723 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1724 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1725 for (ObjCImplementationDecl::instmeth_iterator
1726 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1727 InsMap.insert((*I)->getSelector());
1728 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001729 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001730 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001731 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001732 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001733}
1734
Mike Stump1eb44332009-09-09 15:08:12 +00001735/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001736Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001737Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001738 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001739 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001740 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001741 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001742 for (unsigned i = 0; i != NumElts; ++i) {
1743 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001744 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001745 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001746 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001747 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001748 // Maybe we will complain about the shadowed template parameter.
1749 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1750 // Just pretend that we didn't see the previous declaration.
1751 PrevDecl = 0;
1752 }
1753
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001754 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001755 // GCC apparently allows the following idiom:
1756 //
1757 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1758 // @class XCElementToggler;
1759 //
Mike Stump1eb44332009-09-09 15:08:12 +00001760 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001761 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001762 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001763 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001764 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001765 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001766 // a forward class declaration matching a typedef name of a class refers
1767 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001768 if (const ObjCObjectType *OI =
1769 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1770 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001771 }
Chris Lattner4d391482007-12-12 07:09:47 +00001772 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001773
1774 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001775 ObjCInterfaceDecl *PrevIDecl
1776 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001777 ObjCInterfaceDecl *IDecl
1778 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001779 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001780 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001781
Douglas Gregor7723fec2011-12-15 20:29:51 +00001782 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001783 CheckObjCDeclScope(IDecl);
1784 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001785 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001786
1787 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001788}
1789
John McCall0f4c4c42011-06-16 01:15:19 +00001790static bool tryMatchRecordTypes(ASTContext &Context,
1791 Sema::MethodMatchStrategy strategy,
1792 const Type *left, const Type *right);
1793
John McCallf85e1932011-06-15 23:02:42 +00001794static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1795 QualType leftQT, QualType rightQT) {
1796 const Type *left =
1797 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1798 const Type *right =
1799 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1800
1801 if (left == right) return true;
1802
1803 // If we're doing a strict match, the types have to match exactly.
1804 if (strategy == Sema::MMS_strict) return false;
1805
1806 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1807
1808 // Otherwise, use this absurdly complicated algorithm to try to
1809 // validate the basic, low-level compatibility of the two types.
1810
1811 // As a minimum, require the sizes and alignments to match.
1812 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1813 return false;
1814
1815 // Consider all the kinds of non-dependent canonical types:
1816 // - functions and arrays aren't possible as return and parameter types
1817
1818 // - vector types of equal size can be arbitrarily mixed
1819 if (isa<VectorType>(left)) return isa<VectorType>(right);
1820 if (isa<VectorType>(right)) return false;
1821
1822 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001823 // - structs, unions, and Objective-C objects must match more-or-less
1824 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001825 // - everything else should be a scalar
1826 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001827 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001828
John McCall1d9b3b22011-09-09 05:25:32 +00001829 // Make scalars agree in kind, except count bools as chars, and group
1830 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001831 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1832 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1833 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1834 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001835 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1836 leftSK = Type::STK_ObjCObjectPointer;
1837 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1838 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001839
1840 // Note that data member pointers and function member pointers don't
1841 // intermix because of the size differences.
1842
1843 return (leftSK == rightSK);
1844}
Chris Lattner4d391482007-12-12 07:09:47 +00001845
John McCall0f4c4c42011-06-16 01:15:19 +00001846static bool tryMatchRecordTypes(ASTContext &Context,
1847 Sema::MethodMatchStrategy strategy,
1848 const Type *lt, const Type *rt) {
1849 assert(lt && rt && lt != rt);
1850
1851 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1852 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1853 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1854
1855 // Require union-hood to match.
1856 if (left->isUnion() != right->isUnion()) return false;
1857
1858 // Require an exact match if either is non-POD.
1859 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1860 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1861 return false;
1862
1863 // Require size and alignment to match.
1864 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1865
1866 // Require fields to match.
1867 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1868 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1869 for (; li != le && ri != re; ++li, ++ri) {
1870 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1871 return false;
1872 }
1873 return (li == le && ri == re);
1874}
1875
Chris Lattner4d391482007-12-12 07:09:47 +00001876/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1877/// returns true, or false, accordingly.
1878/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001879bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1880 const ObjCMethodDecl *right,
1881 MethodMatchStrategy strategy) {
1882 if (!matchTypes(Context, strategy,
1883 left->getResultType(), right->getResultType()))
1884 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001885
John McCallf85e1932011-06-15 23:02:42 +00001886 if (getLangOptions().ObjCAutoRefCount &&
1887 (left->hasAttr<NSReturnsRetainedAttr>()
1888 != right->hasAttr<NSReturnsRetainedAttr>() ||
1889 left->hasAttr<NSConsumesSelfAttr>()
1890 != right->hasAttr<NSConsumesSelfAttr>()))
1891 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001893 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001894 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001895
John McCallf85e1932011-06-15 23:02:42 +00001896 for (; li != le; ++li, ++ri) {
1897 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001898 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001899
1900 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1901 return false;
1902
1903 if (getLangOptions().ObjCAutoRefCount &&
1904 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1905 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001906 }
1907 return true;
1908}
1909
Sebastian Redldb9d2142010-08-02 23:18:59 +00001910/// \brief Read the contents of the method pool for a given selector from
1911/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001912///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001913/// This routine should only be called once, when the method pool has no entry
1914/// for this selector.
1915Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001916 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001917 assert(MethodPool.find(Sel) == MethodPool.end() &&
1918 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001919
1920 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001921 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Sebastian Redldb9d2142010-08-02 23:18:59 +00001923 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001924}
1925
Sebastian Redldb9d2142010-08-02 23:18:59 +00001926void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1927 bool instance) {
1928 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1929 if (Pos == MethodPool.end()) {
1930 if (ExternalSource)
1931 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001932 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001933 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1934 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001935 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001936 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001937 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001938 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001939 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001940 Entry.Method = Method;
1941 Entry.Next = 0;
1942 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001943 }
Mike Stump1eb44332009-09-09 15:08:12 +00001944
Chris Lattnerb25df352009-03-04 05:16:45 +00001945 // We've seen a method with this name, see if we have already seen this type
1946 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001947 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1948 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1949
1950 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001951 ObjCMethodDecl *PrevObjCMethod = List->Method;
1952 PrevObjCMethod->setDefined(impl);
1953 // If a method is deprecated, push it in the global pool.
1954 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001955 if (Method->isDeprecated()) {
1956 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001957 List->Method = Method;
1958 }
1959 // If new method is unavailable, push it into global pool
1960 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001961 if (Method->isUnavailable()) {
1962 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001963 List->Method = Method;
1964 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001965 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001966 }
John McCallf85e1932011-06-15 23:02:42 +00001967 }
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Chris Lattnerb25df352009-03-04 05:16:45 +00001969 // We have a new signature for an existing method - add it.
1970 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001971 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1972 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001973}
1974
John McCallf85e1932011-06-15 23:02:42 +00001975/// Determines if this is an "acceptable" loose mismatch in the global
1976/// method pool. This exists mostly as a hack to get around certain
1977/// global mismatches which we can't afford to make warnings / errors.
1978/// Really, what we want is a way to take a method out of the global
1979/// method pool.
1980static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1981 ObjCMethodDecl *other) {
1982 if (!chosen->isInstanceMethod())
1983 return false;
1984
1985 Selector sel = chosen->getSelector();
1986 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1987 return false;
1988
1989 // Don't complain about mismatches for -length if the method we
1990 // chose has an integral result type.
1991 return (chosen->getResultType()->isIntegerType());
1992}
1993
Sebastian Redldb9d2142010-08-02 23:18:59 +00001994ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001995 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001996 bool warn, bool instance) {
1997 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1998 if (Pos == MethodPool.end()) {
1999 if (ExternalSource)
2000 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002001 else
2002 return 0;
2003 }
2004
Sebastian Redldb9d2142010-08-02 23:18:59 +00002005 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Sebastian Redldb9d2142010-08-02 23:18:59 +00002007 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002008 bool issueDiagnostic = false, issueError = false;
2009
2010 // We support a warning which complains about *any* difference in
2011 // method signature.
2012 bool strictSelectorMatch =
2013 (receiverIdOrClass && warn &&
2014 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2015 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002016 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002017 if (strictSelectorMatch)
2018 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002019 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2020 MMS_strict)) {
2021 issueDiagnostic = true;
2022 break;
2023 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002024 }
2025
John McCallf85e1932011-06-15 23:02:42 +00002026 // If we didn't see any strict differences, we won't see any loose
2027 // differences. In ARC, however, we also need to check for loose
2028 // mismatches, because most of them are errors.
2029 if (!strictSelectorMatch ||
2030 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002031 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002032 // This checks if the methods differ in type mismatch.
2033 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2034 MMS_loose) &&
2035 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2036 issueDiagnostic = true;
2037 if (getLangOptions().ObjCAutoRefCount)
2038 issueError = true;
2039 break;
2040 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002041 }
2042
John McCallf85e1932011-06-15 23:02:42 +00002043 if (issueDiagnostic) {
2044 if (issueError)
2045 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2046 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002047 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2048 else
2049 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002050
2051 Diag(MethList.Method->getLocStart(),
2052 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002053 << MethList.Method->getSourceRange();
2054 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2055 Diag(Next->Method->getLocStart(), diag::note_also_found)
2056 << Next->Method->getSourceRange();
2057 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002058 }
2059 return MethList.Method;
2060}
2061
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002062ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002063 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2064 if (Pos == MethodPool.end())
2065 return 0;
2066
2067 GlobalMethods &Methods = Pos->second;
2068
2069 if (Methods.first.Method && Methods.first.Method->isDefined())
2070 return Methods.first.Method;
2071 if (Methods.second.Method && Methods.second.Method->isDefined())
2072 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002073 return 0;
2074}
2075
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002076/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2077/// identical selector names in current and its super classes and issues
2078/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002079void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2080 ObjCMethodDecl *Method,
2081 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002082 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2083 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002084
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002085 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002086 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002087 SD->lookupMethod(Method->getSelector(), IsInstance);
2088 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002089 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002090 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002091 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002092 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2093 E = Method->param_end();
2094 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2095 for (; ParamI != E; ++ParamI, ++PrevI) {
2096 // Number of parameters are the same and is guaranteed by selector match.
2097 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2098 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2099 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002100 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002101 // respective argument type in the super class method, issue warning;
2102 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002103 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002104 << T1 << T2;
2105 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2106 return;
2107 }
2108 }
2109 ID = SD;
2110 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002111}
2112
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002113/// DiagnoseDuplicateIvars -
2114/// Check for duplicate ivars in the entire class at the start of
2115/// @implementation. This becomes necesssary because class extension can
2116/// add ivars to a class in random order which will not be known until
2117/// class's @implementation is seen.
2118void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2119 ObjCInterfaceDecl *SID) {
2120 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2121 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2122 ObjCIvarDecl* Ivar = (*IVI);
2123 if (Ivar->isInvalidDecl())
2124 continue;
2125 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2126 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2127 if (prevIvar) {
2128 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2129 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2130 Ivar->setInvalidDecl();
2131 }
2132 }
2133 }
2134}
2135
Erik Verbruggend64251f2011-12-06 09:25:23 +00002136Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2137 switch (CurContext->getDeclKind()) {
2138 case Decl::ObjCInterface:
2139 return Sema::OCK_Interface;
2140 case Decl::ObjCProtocol:
2141 return Sema::OCK_Protocol;
2142 case Decl::ObjCCategory:
2143 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2144 return Sema::OCK_ClassExtension;
2145 else
2146 return Sema::OCK_Category;
2147 case Decl::ObjCImplementation:
2148 return Sema::OCK_Implementation;
2149 case Decl::ObjCCategoryImpl:
2150 return Sema::OCK_CategoryImplementation;
2151
2152 default:
2153 return Sema::OCK_None;
2154 }
2155}
2156
Steve Naroffa56f6162007-12-18 01:30:32 +00002157// Note: For class/category implemenations, allMethods/allProperties is
2158// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002159Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2160 Decl **allMethods, unsigned allNum,
2161 Decl **allProperties, unsigned pNum,
2162 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002163
Erik Verbruggend64251f2011-12-06 09:25:23 +00002164 if (getObjCContainerKind() == Sema::OCK_None)
2165 return 0;
2166
2167 assert(AtEnd.isValid() && "Invalid location for '@end'");
2168
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002169 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2170 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002171
Mike Stump1eb44332009-09-09 15:08:12 +00002172 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002173 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2174 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002175 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002176
Steve Naroff0701bbb2009-01-08 17:28:14 +00002177 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2178 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2179 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2180
Chris Lattner4d391482007-12-12 07:09:47 +00002181 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002182 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002183 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002184
2185 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002186 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002187 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002188 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002189 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002190 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002191 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002192 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002193 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002194 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002195 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002196 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002197 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002198 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002199 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002200 if (!Context.getSourceManager().isInSystemHeader(
2201 Method->getLocation()))
2202 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2203 << Method->getDeclName();
2204 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2205 }
Chris Lattner4d391482007-12-12 07:09:47 +00002206 InsMap[Method->getSelector()] = Method;
2207 /// The following allows us to typecheck messages to "id".
2208 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002209 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002210 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002211 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002212 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002213 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002214 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002215 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002216 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002217 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002218 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002219 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002220 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002221 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002222 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002223 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002224 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002225 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002226 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002227 if (!Context.getSourceManager().isInSystemHeader(
2228 Method->getLocation()))
2229 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2230 << Method->getDeclName();
2231 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2232 }
Chris Lattner4d391482007-12-12 07:09:47 +00002233 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002234 /// The following allows us to typecheck messages to "Class".
2235 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002236 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002237 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002238 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002239 }
2240 }
2241 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002242 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002243 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002244 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002245 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002246 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002247 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002248 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002249 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002250 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002251
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002252 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002253 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002254 if (C->IsClassExtension()) {
2255 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2256 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002257 }
Chris Lattner4d391482007-12-12 07:09:47 +00002258 }
Steve Naroff09c47192009-01-09 15:36:25 +00002259 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002260 if (CDecl->getIdentifier())
2261 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2262 // user-defined setter/getter. It also synthesizes setter/getter methods
2263 // and adds them to the DeclContext and global method pools.
2264 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2265 E = CDecl->prop_end();
2266 I != E; ++I)
2267 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002268 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002269 }
2270 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002271 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002272 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002273 // Any property declared in a class extension might have user
2274 // declared setter or getter in current class extension or one
2275 // of the other class extensions. Mark them as synthesized as
2276 // property will be synthesized when property with same name is
2277 // seen in the @implementation.
2278 for (const ObjCCategoryDecl *ClsExtDecl =
2279 IDecl->getFirstClassExtension();
2280 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2281 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2282 E = ClsExtDecl->prop_end(); I != E; ++I) {
2283 ObjCPropertyDecl *Property = (*I);
2284 // Skip over properties declared @dynamic
2285 if (const ObjCPropertyImplDecl *PIDecl
2286 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2287 if (PIDecl->getPropertyImplementation()
2288 == ObjCPropertyImplDecl::Dynamic)
2289 continue;
2290
2291 for (const ObjCCategoryDecl *CExtDecl =
2292 IDecl->getFirstClassExtension();
2293 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2294 if (ObjCMethodDecl *GetterMethod =
2295 CExtDecl->getInstanceMethod(Property->getGetterName()))
2296 GetterMethod->setSynthesized(true);
2297 if (!Property->isReadOnly())
2298 if (ObjCMethodDecl *SetterMethod =
2299 CExtDecl->getInstanceMethod(Property->getSetterName()))
2300 SetterMethod->setSynthesized(true);
2301 }
2302 }
2303 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002304 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002305 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002306 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002307
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002308 if (LangOpts.ObjCNonFragileABI2)
2309 while (IDecl->getSuperClass()) {
2310 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2311 IDecl = IDecl->getSuperClass();
2312 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002313 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002314 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002315 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002316 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002317 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002318
Chris Lattner4d391482007-12-12 07:09:47 +00002319 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002320 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002321 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002322 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002323 Categories; Categories = Categories->getNextClassCategory()) {
2324 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002325 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002326 break;
2327 }
2328 }
2329 }
2330 }
Chris Lattner682bf922009-03-29 16:50:03 +00002331 if (isInterfaceDeclKind) {
2332 // Reject invalid vardecls.
2333 for (unsigned i = 0; i != tuvNum; i++) {
2334 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2335 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2336 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002337 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002338 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002339 }
Chris Lattner682bf922009-03-29 16:50:03 +00002340 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002341 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002342 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002343
2344 for (unsigned i = 0; i != tuvNum; i++) {
2345 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002346 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2347 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002348 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2349 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002350
2351 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002352}
2353
2354
2355/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2356/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002357static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002358CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002359 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002360}
2361
Ted Kremenek422bae72010-04-18 04:59:38 +00002362static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002363bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2364 const AttrVec &A) {
2365 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002366 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002367 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002368 return false;
2369
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002370 // method declared in interface has no attribute.
2371 // But implementation has attributes. This is invalid
2372 if (!IMD->hasAttrs())
2373 return true;
2374
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002375 const AttrVec &D = IMD->getAttrs();
2376 if (D.size() != A.size())
2377 return true;
2378
2379 // attributes on method declaration and definition must match exactly.
2380 // Note that we have at most a couple of attributes on methods, so this
2381 // n*n search is good enough.
2382 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2383 bool match = false;
2384 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2385 if ((*i)->getKind() == (*i1)->getKind()) {
2386 match = true;
2387 break;
2388 }
2389 }
2390 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002391 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002392 }
Sean Huntcf807c42010-08-18 23:23:40 +00002393 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002394}
2395
Douglas Gregore97179c2011-09-08 01:46:34 +00002396namespace {
2397 /// \brief Describes the compatibility of a result type with its method.
2398 enum ResultTypeCompatibilityKind {
2399 RTC_Compatible,
2400 RTC_Incompatible,
2401 RTC_Unknown
2402 };
2403}
2404
Douglas Gregor926df6c2011-06-11 01:09:30 +00002405/// \brief Check whether the declared result type of the given Objective-C
2406/// method declaration is compatible with the method's class.
2407///
Douglas Gregore97179c2011-09-08 01:46:34 +00002408static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002409CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2410 ObjCInterfaceDecl *CurrentClass) {
2411 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002412
2413 // If an Objective-C method inherits its related result type, then its
2414 // declared result type must be compatible with its own class type. The
2415 // declared result type is compatible if:
2416 if (const ObjCObjectPointerType *ResultObjectType
2417 = ResultType->getAs<ObjCObjectPointerType>()) {
2418 // - it is id or qualified id, or
2419 if (ResultObjectType->isObjCIdType() ||
2420 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002421 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002422
2423 if (CurrentClass) {
2424 if (ObjCInterfaceDecl *ResultClass
2425 = ResultObjectType->getInterfaceDecl()) {
2426 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002427 if (declaresSameEntity(CurrentClass, ResultClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002428 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002429
2430 // - it is a superclass of the method's class type
2431 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002432 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002433 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002434 } else {
2435 // Any Objective-C pointer type might be acceptable for a protocol
2436 // method; we just don't know.
2437 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002438 }
2439 }
2440
Douglas Gregore97179c2011-09-08 01:46:34 +00002441 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002442}
2443
John McCall6c2c2502011-07-22 02:45:48 +00002444namespace {
2445/// A helper class for searching for methods which a particular method
2446/// overrides.
2447class OverrideSearch {
2448 Sema &S;
2449 ObjCMethodDecl *Method;
2450 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2451 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2452 bool Recursive;
2453
2454public:
2455 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2456 Selector selector = method->getSelector();
2457
2458 // Bypass this search if we've never seen an instance/class method
2459 // with this selector before.
2460 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2461 if (it == S.MethodPool.end()) {
2462 if (!S.ExternalSource) return;
2463 it = S.ReadMethodPool(selector);
2464 }
2465 ObjCMethodList &list =
2466 method->isInstanceMethod() ? it->second.first : it->second.second;
2467 if (!list.Method) return;
2468
2469 ObjCContainerDecl *container
2470 = cast<ObjCContainerDecl>(method->getDeclContext());
2471
2472 // Prevent the search from reaching this container again. This is
2473 // important with categories, which override methods from the
2474 // interface and each other.
2475 Searched.insert(container);
2476 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002477 }
John McCall6c2c2502011-07-22 02:45:48 +00002478
2479 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2480 iterator begin() const { return Overridden.begin(); }
2481 iterator end() const { return Overridden.end(); }
2482
2483private:
2484 void searchFromContainer(ObjCContainerDecl *container) {
2485 if (container->isInvalidDecl()) return;
2486
2487 switch (container->getDeclKind()) {
2488#define OBJCCONTAINER(type, base) \
2489 case Decl::type: \
2490 searchFrom(cast<type##Decl>(container)); \
2491 break;
2492#define ABSTRACT_DECL(expansion)
2493#define DECL(type, base) \
2494 case Decl::type:
2495#include "clang/AST/DeclNodes.inc"
2496 llvm_unreachable("not an ObjC container!");
2497 }
2498 }
2499
2500 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002501 if (!protocol->hasDefinition())
2502 return;
2503
John McCall6c2c2502011-07-22 02:45:48 +00002504 // A method in a protocol declaration overrides declarations from
2505 // referenced ("parent") protocols.
2506 search(protocol->getReferencedProtocols());
2507 }
2508
2509 void searchFrom(ObjCCategoryDecl *category) {
2510 // A method in a category declaration overrides declarations from
2511 // the main class and from protocols the category references.
2512 search(category->getClassInterface());
2513 search(category->getReferencedProtocols());
2514 }
2515
2516 void searchFrom(ObjCCategoryImplDecl *impl) {
2517 // A method in a category definition that has a category
2518 // declaration overrides declarations from the category
2519 // declaration.
2520 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2521 search(category);
2522
2523 // Otherwise it overrides declarations from the class.
2524 } else {
2525 search(impl->getClassInterface());
2526 }
2527 }
2528
2529 void searchFrom(ObjCInterfaceDecl *iface) {
2530 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002531 if (!iface->hasDefinition())
2532 return;
2533
John McCall6c2c2502011-07-22 02:45:48 +00002534 // - categories,
2535 for (ObjCCategoryDecl *category = iface->getCategoryList();
2536 category; category = category->getNextClassCategory())
2537 search(category);
2538
2539 // - the super class, and
2540 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2541 search(super);
2542
2543 // - any referenced protocols.
2544 search(iface->getReferencedProtocols());
2545 }
2546
2547 void searchFrom(ObjCImplementationDecl *impl) {
2548 // A method in a class implementation overrides declarations from
2549 // the class interface.
2550 search(impl->getClassInterface());
2551 }
2552
2553
2554 void search(const ObjCProtocolList &protocols) {
2555 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2556 i != e; ++i)
2557 search(*i);
2558 }
2559
2560 void search(ObjCContainerDecl *container) {
2561 // Abort if we've already searched this container.
2562 if (!Searched.insert(container)) return;
2563
2564 // Check for a method in this container which matches this selector.
2565 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2566 Method->isInstanceMethod());
2567
2568 // If we find one, record it and bail out.
2569 if (meth) {
2570 Overridden.insert(meth);
2571 return;
2572 }
2573
2574 // Otherwise, search for methods that a hypothetical method here
2575 // would have overridden.
2576
2577 // Note that we're now in a recursive case.
2578 Recursive = true;
2579
2580 searchFromContainer(container);
2581 }
2582};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002583}
2584
John McCalld226f652010-08-21 09:40:31 +00002585Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002586 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002587 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002588 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002589 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002590 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002591 Selector Sel,
2592 // optional arguments. The number of types/arguments is obtained
2593 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002594 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002595 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002596 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002597 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002598 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002599 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002600 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002601 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002602 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002603 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2604 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002605 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002606
Douglas Gregore97179c2011-09-08 01:46:34 +00002607 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002608 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002609 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002610 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002611
Steve Naroffccef3712009-02-20 22:59:16 +00002612 // Methods cannot return interface types. All ObjC objects are
2613 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002614 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002615 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2616 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002617 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002618 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002619
2620 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002621 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002622 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002623 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002624 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002625 }
Mike Stump1eb44332009-09-09 15:08:12 +00002626
2627 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002628 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002629 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002630 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002631 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002632 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002633 /*isSynthesized=*/false,
2634 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002635 MethodDeclKind == tok::objc_optional
2636 ? ObjCMethodDecl::Optional
2637 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002638 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002639
Chris Lattner5f9e2722011-07-23 10:55:15 +00002640 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002641
Chris Lattner7db638d2009-04-11 19:42:43 +00002642 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002643 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002644 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002645
Chris Lattnere294d3f2009-04-11 18:57:04 +00002646 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002647 ArgType = Context.getObjCIdType();
2648 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002649 } else {
John McCall58e46772009-10-23 21:48:59 +00002650 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002651 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002652 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002653 }
Mike Stump1eb44332009-09-09 15:08:12 +00002654
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002655 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2656 LookupOrdinaryName, ForRedeclaration);
2657 LookupName(R, S);
2658 if (R.isSingleResult()) {
2659 NamedDecl *PrevDecl = R.getFoundDecl();
2660 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002661 Diag(ArgInfo[i].NameLoc,
2662 (MethodDefinition ? diag::warn_method_param_redefinition
2663 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002664 << ArgInfo[i].Name;
2665 Diag(PrevDecl->getLocation(),
2666 diag::note_previous_declaration);
2667 }
2668 }
2669
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002670 SourceLocation StartLoc = DI
2671 ? DI->getTypeLoc().getBeginLoc()
2672 : ArgInfo[i].NameLoc;
2673
John McCall81ef3e62011-04-23 02:46:06 +00002674 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2675 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2676 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002677
John McCall70798862011-05-02 00:30:12 +00002678 Param->setObjCMethodScopeInfo(i);
2679
Chris Lattner0ed844b2008-04-04 06:12:32 +00002680 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002681 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002682
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002683 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002684 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002685
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002686 S->AddDecl(Param);
2687 IdResolver.AddDecl(Param);
2688
Chris Lattner0ed844b2008-04-04 06:12:32 +00002689 Params.push_back(Param);
2690 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002691
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002692 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002693 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002694 QualType ArgType = Param->getType();
2695 if (ArgType.isNull())
2696 ArgType = Context.getObjCIdType();
2697 else
2698 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002699 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002700 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002701 Diag(Param->getLocation(),
2702 diag::err_object_cannot_be_passed_returned_by_value)
2703 << 1 << ArgType;
2704 Param->setInvalidDecl();
2705 }
2706 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002707
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002708 Params.push_back(Param);
2709 }
2710
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002711 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002712 ObjCMethod->setObjCDeclQualifier(
2713 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002714
2715 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002716 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002717
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002718 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002719 const ObjCMethodDecl *PrevMethod = 0;
2720 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002721 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002722 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2723 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002724 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002725 PrevMethod = ImpDecl->getClassMethod(Sel);
2726 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002727 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002728
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002729 ObjCMethodDecl *IMD = 0;
2730 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2731 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2732 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002733 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002734 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002735 SourceLocation MethodLoc = IMD->getLocation();
2736 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2737 Diag(EndLoc, diag::warn_attribute_method_def);
2738 Diag(MethodLoc, diag::note_method_declared_at);
2739 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002740 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002741 } else {
2742 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002743 }
John McCall6c2c2502011-07-22 02:45:48 +00002744
Chris Lattner4d391482007-12-12 07:09:47 +00002745 if (PrevMethod) {
2746 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002747 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002748 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002749 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002750 }
John McCall54abf7d2009-11-04 02:18:39 +00002751
Douglas Gregor926df6c2011-06-11 01:09:30 +00002752 // If this Objective-C method does not have a related result type, but we
2753 // are allowed to infer related result types, try to do so based on the
2754 // method family.
2755 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2756 if (!CurrentClass) {
2757 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2758 CurrentClass = Cat->getClassInterface();
2759 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2760 CurrentClass = Impl->getClassInterface();
2761 else if (ObjCCategoryImplDecl *CatImpl
2762 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2763 CurrentClass = CatImpl->getClassInterface();
2764 }
John McCall6c2c2502011-07-22 02:45:48 +00002765
Douglas Gregore97179c2011-09-08 01:46:34 +00002766 ResultTypeCompatibilityKind RTC
2767 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002768
2769 // Search for overridden methods and merge information down from them.
2770 OverrideSearch overrides(*this, ObjCMethod);
2771 for (OverrideSearch::iterator
2772 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2773 ObjCMethodDecl *overridden = *i;
2774
2775 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002776 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002777 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002778
2779 // Then merge the declarations.
2780 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002781
2782 // Check for overriding methods
2783 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00002784 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2785 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2786 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
Douglas Gregor926df6c2011-06-11 01:09:30 +00002787 }
2788
John McCallf85e1932011-06-15 23:02:42 +00002789 bool ARCError = false;
2790 if (getLangOptions().ObjCAutoRefCount)
2791 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2792
Douglas Gregore97179c2011-09-08 01:46:34 +00002793 // Infer the related result type when possible.
2794 if (!ARCError && RTC == RTC_Compatible &&
2795 !ObjCMethod->hasRelatedResultType() &&
2796 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002797 bool InferRelatedResultType = false;
2798 switch (ObjCMethod->getMethodFamily()) {
2799 case OMF_None:
2800 case OMF_copy:
2801 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002802 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002803 case OMF_mutableCopy:
2804 case OMF_release:
2805 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002806 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002807 break;
2808
2809 case OMF_alloc:
2810 case OMF_new:
2811 InferRelatedResultType = ObjCMethod->isClassMethod();
2812 break;
2813
2814 case OMF_init:
2815 case OMF_autorelease:
2816 case OMF_retain:
2817 case OMF_self:
2818 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2819 break;
2820 }
2821
John McCall6c2c2502011-07-22 02:45:48 +00002822 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002823 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002824 }
2825
John McCalld226f652010-08-21 09:40:31 +00002826 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002827}
2828
Chris Lattnercc98eac2008-12-17 07:13:27 +00002829bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002830 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002831 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002832 // Following is also an error. But it is caused by a missing @end
2833 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002834 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2835 return false;
2836 }
2837
Anders Carlsson15281452008-11-04 16:57:32 +00002838 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2839 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002840
Anders Carlsson15281452008-11-04 16:57:32 +00002841 return true;
2842}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002843
Chris Lattnercc98eac2008-12-17 07:13:27 +00002844/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2845/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002846void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002847 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002848 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002849 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002850 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002851 if (!Class) {
2852 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2853 return;
2854 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002855 if (LangOpts.ObjCNonFragileABI) {
2856 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2857 return;
2858 }
Mike Stump1eb44332009-09-09 15:08:12 +00002859
Chris Lattnercc98eac2008-12-17 07:13:27 +00002860 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002861 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002862 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002863 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002864 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002865 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002866 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002867 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2868 /*FIXME: StartL=*/ID->getLocation(),
2869 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002870 ID->getIdentifier(), ID->getType(),
2871 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002872 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002873 }
Mike Stump1eb44332009-09-09 15:08:12 +00002874
Chris Lattnercc98eac2008-12-17 07:13:27 +00002875 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002876 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002877 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002878 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002879 if (getLangOptions().CPlusPlus)
2880 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002881 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002882 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002883 }
2884}
2885
Douglas Gregor160b5632010-04-26 17:32:49 +00002886/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002887VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2888 SourceLocation StartLoc,
2889 SourceLocation IdLoc,
2890 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002891 bool Invalid) {
2892 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2893 // duration shall not be qualified by an address-space qualifier."
2894 // Since all parameters have automatic store duration, they can not have
2895 // an address space.
2896 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002897 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002898 Invalid = true;
2899 }
2900
2901 // An @catch parameter must be an unqualified object pointer type;
2902 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2903 if (Invalid) {
2904 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002905 } else if (T->isDependentType()) {
2906 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002907 } else if (!T->isObjCObjectPointerType()) {
2908 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002909 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002910 } else if (T->isObjCQualifiedIdType()) {
2911 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002912 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002913 }
2914
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002915 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2916 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002917 New->setExceptionVariable(true);
2918
Douglas Gregor9aab9c42011-12-10 01:22:52 +00002919 // In ARC, infer 'retaining' for variables of retainable type.
2920 if (getLangOptions().ObjCAutoRefCount && inferObjCARCLifetime(New))
2921 Invalid = true;
2922
Douglas Gregor160b5632010-04-26 17:32:49 +00002923 if (Invalid)
2924 New->setInvalidDecl();
2925 return New;
2926}
2927
John McCalld226f652010-08-21 09:40:31 +00002928Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002929 const DeclSpec &DS = D.getDeclSpec();
2930
2931 // We allow the "register" storage class on exception variables because
2932 // GCC did, but we drop it completely. Any other storage class is an error.
2933 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2934 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2935 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2936 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2937 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2938 << DS.getStorageClassSpec();
2939 }
2940 if (D.getDeclSpec().isThreadSpecified())
2941 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2942 D.getMutableDeclSpec().ClearStorageClassSpecs();
2943
2944 DiagnoseFunctionSpecifiers(D);
2945
2946 // Check that there are no default arguments inside the type of this
2947 // exception object (C++ only).
2948 if (getLangOptions().CPlusPlus)
2949 CheckExtraCXXDefaultArguments(D);
2950
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002951 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002952 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002953
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002954 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2955 D.getSourceRange().getBegin(),
2956 D.getIdentifierLoc(),
2957 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002958 D.isInvalidType());
2959
2960 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2961 if (D.getCXXScopeSpec().isSet()) {
2962 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2963 << D.getCXXScopeSpec().getRange();
2964 New->setInvalidDecl();
2965 }
2966
2967 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002968 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002969 if (D.getIdentifier())
2970 IdResolver.AddDecl(New);
2971
2972 ProcessDeclAttributes(S, New, D);
2973
2974 if (New->hasAttr<BlocksAttr>())
2975 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002976 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002977}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002978
2979/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002980/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002981void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002982 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002983 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2984 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002985 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002986 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002987 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002988 }
2989}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002990
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002991void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002992 // Load referenced selectors from the external source.
2993 if (ExternalSource) {
2994 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2995 ExternalSource->ReadReferencedSelectors(Sels);
2996 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2997 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2998 }
2999
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003000 // Warning will be issued only when selector table is
3001 // generated (which means there is at lease one implementation
3002 // in the TU). This is to match gcc's behavior.
3003 if (ReferencedSelectors.empty() ||
3004 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003005 return;
3006 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3007 ReferencedSelectors.begin(),
3008 E = ReferencedSelectors.end(); S != E; ++S) {
3009 Selector Sel = (*S).first;
3010 if (!LookupImplementedMethodInGlobalPool(Sel))
3011 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3012 }
3013 return;
3014}