blob: eb178612c8e90cfefc094828726872845f8f06c4 [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 Gregorc83c6872010-04-15 22:33:43 +0000572 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000573 if (PDecl) {
574 // Protocol already seen. Better be a forward protocol declaration
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000575 if (ObjCProtocolDecl *Def = PDecl->getDefinition()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000576 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000577 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000578
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000579 // Create a new protocol that is completely distinct from previous
580 // declarations, and do not make this protocol available for name lookup.
581 // That way, we'll end up completely ignoring the duplicate.
582 // FIXME: Can we turn this into an error?
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000583 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
584 ProtocolLoc, AtProtoInterfaceLoc,
585 /*isForwardDecl=*/false);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000586 PDecl->startDefinition();
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000587 } else {
588 ObjCList<ObjCProtocolDecl> PList;
589 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
590 err = CheckForwardProtocolDeclarationForCircularDependency(
591 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
592
593 // Make sure the cached decl gets a valid start location.
594 PDecl->setAtStartLoc(AtProtoInterfaceLoc);
595 PDecl->setLocation(ProtocolLoc);
596 // Since this ObjCProtocolDecl was created by a forward declaration,
597 // we now add it to the DeclContext since it wasn't added before
598 PDecl->setLexicalDeclContext(CurContext);
599 CurContext->addDecl(PDecl);
600 PDecl->completedForwardDecl();
601 }
Chris Lattner439e71f2008-03-16 01:25:17 +0000602 } else {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000603 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000604 ProtocolLoc, AtProtoInterfaceLoc,
605 /*isForwardDecl=*/false);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000606 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000607 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000608 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000609
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000610 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000611 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000612 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000613 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000614 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
615 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000616 PDecl->setLocEnd(EndProtoLoc);
617 }
Mike Stump1eb44332009-09-09 15:08:12 +0000618
619 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000620 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000621}
622
623/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000624/// issues an error if they are not declared. It returns list of
625/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000626void
Chris Lattnere13b9592008-07-26 04:03:38 +0000627Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000628 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000629 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000630 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000631 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000632 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
633 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000634 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000635 TypoCorrection Corrected = CorrectTypo(
636 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
637 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
638 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000639 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000640 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000641 Diag(PDecl->getLocation(), diag::note_previous_decl)
642 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000643 }
644 }
645
646 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000647 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000648 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000649 continue;
650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000652 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000653
654 // If this is a forward declaration and we are supposed to warn in this
655 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000656 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000657 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000658 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000659 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000660 }
661}
662
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000663/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000664/// a class method in its extension.
665///
Mike Stump1eb44332009-09-09 15:08:12 +0000666void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000667 ObjCInterfaceDecl *ID) {
668 if (!ID)
669 return; // Possibly due to previous error
670
671 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000672 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
673 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000674 ObjCMethodDecl *MD = *i;
675 MethodMap[MD->getSelector()] = MD;
676 }
677
678 if (MethodMap.empty())
679 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000680 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
681 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000682 ObjCMethodDecl *Method = *i;
683 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
684 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
685 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
686 << Method->getDeclName();
687 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
688 }
689 }
690}
691
Chris Lattner58fe03b2009-04-12 08:43:13 +0000692/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000693Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000694Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000695 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000696 unsigned NumElts,
697 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000698 SmallVector<ObjCProtocolDecl*, 32> Protocols;
699 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000700
Chris Lattner4d391482007-12-12 07:09:47 +0000701 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000702 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000703 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000704 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000705 if (PDecl == 0) { // Not already seen?
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000706 PDecl = ObjCProtocolDecl::Create(Context, CurContext, Ident,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000707 IdentList[i].second, AtProtocolLoc,
708 /*isForwardDecl=*/true);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000709 PushOnScopeChains(PDecl, TUScope, false);
710 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000711 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000712 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000713 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +0000714 if (!isNew) {
715 if (ASTMutationListener *L = Context.getASTMutationListener())
716 L->UpdatedAttributeList(PDecl);
717 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000718 }
Chris Lattner4d391482007-12-12 07:09:47 +0000719 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000720 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000721 }
Mike Stump1eb44332009-09-09 15:08:12 +0000722
723 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000724 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000725 Protocols.data(), Protocols.size(),
726 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000727 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000728 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000729 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000730}
731
John McCalld226f652010-08-21 09:40:31 +0000732Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000733ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
734 IdentifierInfo *ClassName, SourceLocation ClassLoc,
735 IdentifierInfo *CategoryName,
736 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000737 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000738 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000739 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000740 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000741 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000742 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000743
744 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000745
746 if (!IDecl
747 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
748 PDiag(diag::err_category_forward_interface)
749 << (CategoryName == 0))) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000750 // Create an invalid ObjCCategoryDecl to serve as context for
751 // the enclosing method declarations. We mark the decl invalid
752 // to make it clear that this isn't a valid AST.
753 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000754 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000755 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000756
757 if (!IDecl)
758 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000759 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000760 }
761
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000762 if (!CategoryName && IDecl->getImplementation()) {
763 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
764 Diag(IDecl->getImplementation()->getLocation(),
765 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000766 }
767
Fariborz Jahanian25760612010-02-15 21:55:26 +0000768 if (CategoryName) {
769 /// Check for duplicate interface declaration for this category
770 ObjCCategoryDecl *CDeclChain;
771 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
772 CDeclChain = CDeclChain->getNextClassCategory()) {
773 if (CDeclChain->getIdentifier() == CategoryName) {
774 // Class extensions can be declared multiple times.
775 Diag(CategoryLoc, diag::warn_dup_category_def)
776 << ClassName << CategoryName;
777 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
778 break;
779 }
Chris Lattner70f19542009-02-16 21:26:43 +0000780 }
781 }
Chris Lattner70f19542009-02-16 21:26:43 +0000782
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000783 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
784 ClassLoc, CategoryLoc, CategoryName, IDecl);
785 // FIXME: PushOnScopeChains?
786 CurContext->addDecl(CDecl);
787
Chris Lattner4d391482007-12-12 07:09:47 +0000788 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000789 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000790 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000791 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000792 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000793 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000794 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000795 }
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Anders Carlsson15281452008-11-04 16:57:32 +0000797 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000798 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000799}
800
801/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000802/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000803/// object.
John McCalld226f652010-08-21 09:40:31 +0000804Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000805 SourceLocation AtCatImplLoc,
806 IdentifierInfo *ClassName, SourceLocation ClassLoc,
807 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000808 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000809 ObjCCategoryDecl *CatIDecl = 0;
810 if (IDecl) {
811 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
812 if (!CatIDecl) {
813 // Category @implementation with no corresponding @interface.
814 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000815 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
816 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000817 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000818 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000819 }
820 }
821
Mike Stump1eb44332009-09-09 15:08:12 +0000822 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000823 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000824 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000825 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000826 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000827 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000828 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000829 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
830 diag::err_undef_interface)) {
831 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000832 }
Chris Lattner4d391482007-12-12 07:09:47 +0000833
Douglas Gregord0434102009-01-09 00:49:46 +0000834 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000835 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000836
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000837 // If the interface is deprecated/unavailable, warn/error about it.
838 if (IDecl)
839 DiagnoseUseOfDecl(IDecl, ClassLoc);
840
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000841 /// Check that CatName, category name, is not used in another implementation.
842 if (CatIDecl) {
843 if (CatIDecl->getImplementation()) {
844 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
845 << CatName;
846 Diag(CatIDecl->getImplementation()->getLocation(),
847 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000848 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000849 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000850 // Warn on implementating category of deprecated class under
851 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000852 DiagnoseObjCImplementedDeprecations(*this,
853 dyn_cast<NamedDecl>(IDecl),
854 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000855 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000856 }
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Anders Carlsson15281452008-11-04 16:57:32 +0000858 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000859 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000860}
861
John McCalld226f652010-08-21 09:40:31 +0000862Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000863 SourceLocation AtClassImplLoc,
864 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000865 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000866 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000867 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000868 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000869 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000870 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
871 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000872 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000873 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000874 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000875 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000876 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
877 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000878 } else {
879 // We did not find anything with the name ClassName; try to correct for
880 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000881 TypoCorrection Corrected = CorrectTypo(
882 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
883 NULL, NULL, false, CTC_NoKeywords);
884 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000885 // Suggest the (potentially) correct interface name. However, put the
886 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000887 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000888 // provide a code-modification hint or use the typo name for recovery,
889 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000890 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000891 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000892 << ClassName << CorrectedName;
893 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
894 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000895 IDecl = 0;
896 } else {
897 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
898 }
Chris Lattner4d391482007-12-12 07:09:47 +0000899 }
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Chris Lattner4d391482007-12-12 07:09:47 +0000901 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000902 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000903 if (SuperClassname) {
904 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000905 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
906 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000907 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000908 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
909 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000910 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000911 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000912 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000913 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000914 Diag(SuperClassLoc, diag::err_undef_superclass)
915 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000916 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000917 // This implementation and its interface do not have the same
918 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000919 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000920 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000921 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000922 }
923 }
924 }
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Chris Lattner4d391482007-12-12 07:09:47 +0000926 if (!IDecl) {
927 // Legacy case of @implementation with no corresponding @interface.
928 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000929
Mike Stump390b4cc2009-05-16 07:39:55 +0000930 // FIXME: Do we support attributes on the @implementation? If so we should
931 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000932 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000933 ClassName, /*PrevDecl=*/0, ClassLoc,
934 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000935 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000936 if (SDecl) {
937 IDecl->setSuperClass(SDecl);
938 IDecl->setSuperClassLoc(SuperClassLoc);
939 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
940 } else {
941 IDecl->setEndOfDefinitionLoc(ClassLoc);
942 }
943
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000944 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000945 } else {
946 // Mark the interface as being completed, even if it was just as
947 // @class ....;
948 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000949 if (!IDecl->hasDefinition())
950 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000951 }
Mike Stump1eb44332009-09-09 15:08:12 +0000952
953 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000954 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
955 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Anders Carlsson15281452008-11-04 16:57:32 +0000957 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000958 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Chris Lattner4d391482007-12-12 07:09:47 +0000960 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000961 if (IDecl->getImplementation()) {
962 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000963 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000964 Diag(IDecl->getImplementation()->getLocation(),
965 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000966 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000967 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000968 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000969 // Warn on implementating deprecated class under
970 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000971 DiagnoseObjCImplementedDeprecations(*this,
972 dyn_cast<NamedDecl>(IDecl),
973 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000974 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000975 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000976}
977
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000978void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
979 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000980 SourceLocation RBrace) {
981 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000982 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000983 if (!IDecl)
984 return;
985 /// Check case of non-existing @interface decl.
986 /// (legacy objective-c @implementation decl without an @interface decl).
987 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000988 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +0000989 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000990 // Add ivar's to class's DeclContext.
991 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000992 ivars[i]->setLexicalDeclContext(ImpDecl);
993 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000994 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000995 }
996
Chris Lattner4d391482007-12-12 07:09:47 +0000997 return;
998 }
999 // If implementation has empty ivar list, just return.
1000 if (numIvars == 0)
1001 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Chris Lattner4d391482007-12-12 07:09:47 +00001003 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001004 if (LangOpts.ObjCNonFragileABI2) {
1005 if (ImpDecl->getSuperClass())
1006 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1007 for (unsigned i = 0; i < numIvars; i++) {
1008 ObjCIvarDecl* ImplIvar = ivars[i];
1009 if (const ObjCIvarDecl *ClsIvar =
1010 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1011 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1012 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1013 continue;
1014 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001015 // Instance ivar to Implementation's DeclContext.
1016 ImplIvar->setLexicalDeclContext(ImpDecl);
1017 IDecl->makeDeclVisibleInContext(ImplIvar, false);
1018 ImpDecl->addDecl(ImplIvar);
1019 }
1020 return;
1021 }
Chris Lattner4d391482007-12-12 07:09:47 +00001022 // Check interface's Ivar list against those in the implementation.
1023 // names and types must match.
1024 //
Chris Lattner4d391482007-12-12 07:09:47 +00001025 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001026 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001027 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1028 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001029 ObjCIvarDecl* ImplIvar = ivars[j++];
1030 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001031 assert (ImplIvar && "missing implementation ivar");
1032 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Steve Naroffca331292009-03-03 14:49:36 +00001034 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001035 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001036 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001037 << ImplIvar->getIdentifier()
1038 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001039 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001040 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1041 ImplIvar->getBitWidthValue(Context) !=
1042 ClsIvar->getBitWidthValue(Context)) {
1043 Diag(ImplIvar->getBitWidth()->getLocStart(),
1044 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1045 Diag(ClsIvar->getBitWidth()->getLocStart(),
1046 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001047 }
Steve Naroffca331292009-03-03 14:49:36 +00001048 // Make sure the names are identical.
1049 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001050 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001051 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001052 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001053 }
1054 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001055 }
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Chris Lattner609e4c72007-12-12 18:11:49 +00001057 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001058 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001059 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001060 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001061}
1062
Steve Naroff3c2eb662008-02-10 21:38:56 +00001063void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001064 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001065 // No point warning no definition of method which is 'unavailable'.
1066 if (method->hasAttr<UnavailableAttr>())
1067 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001068 if (!IncompleteImpl) {
1069 Diag(ImpLoc, diag::warn_incomplete_impl);
1070 IncompleteImpl = true;
1071 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001072 if (DiagID == diag::warn_unimplemented_protocol_method)
1073 Diag(ImpLoc, DiagID) << method->getDeclName();
1074 else
1075 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001076}
1077
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001078/// Determines if type B can be substituted for type A. Returns true if we can
1079/// guarantee that anything that the user will do to an object of type A can
1080/// also be done to an object of type B. This is trivially true if the two
1081/// types are the same, or if B is a subclass of A. It becomes more complex
1082/// in cases where protocols are involved.
1083///
1084/// Object types in Objective-C describe the minimum requirements for an
1085/// object, rather than providing a complete description of a type. For
1086/// example, if A is a subclass of B, then B* may refer to an instance of A.
1087/// The principle of substitutability means that we may use an instance of A
1088/// anywhere that we may use an instance of B - it will implement all of the
1089/// ivars of B and all of the methods of B.
1090///
1091/// This substitutability is important when type checking methods, because
1092/// the implementation may have stricter type definitions than the interface.
1093/// The interface specifies minimum requirements, but the implementation may
1094/// have more accurate ones. For example, a method may privately accept
1095/// instances of B, but only publish that it accepts instances of A. Any
1096/// object passed to it will be type checked against B, and so will implicitly
1097/// by a valid A*. Similarly, a method may return a subclass of the class that
1098/// it is declared as returning.
1099///
1100/// This is most important when considering subclassing. A method in a
1101/// subclass must accept any object as an argument that its superclass's
1102/// implementation accepts. It may, however, accept a more general type
1103/// without breaking substitutability (i.e. you can still use the subclass
1104/// anywhere that you can use the superclass, but not vice versa). The
1105/// converse requirement applies to return types: the return type for a
1106/// subclass method must be a valid object of the kind that the superclass
1107/// advertises, but it may be specified more accurately. This avoids the need
1108/// for explicit down-casting by callers.
1109///
1110/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001111static bool isObjCTypeSubstitutable(ASTContext &Context,
1112 const ObjCObjectPointerType *A,
1113 const ObjCObjectPointerType *B,
1114 bool rejectId) {
1115 // Reject a protocol-unqualified id.
1116 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001117
1118 // If B is a qualified id, then A must also be a qualified id and it must
1119 // implement all of the protocols in B. It may not be a qualified class.
1120 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1121 // stricter definition so it is not substitutable for id<A>.
1122 if (B->isObjCQualifiedIdType()) {
1123 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001124 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1125 QualType(B,0),
1126 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001127 }
1128
1129 /*
1130 // id is a special type that bypasses type checking completely. We want a
1131 // warning when it is used in one place but not another.
1132 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1133
1134
1135 // If B is a qualified id, then A must also be a qualified id (which it isn't
1136 // if we've got this far)
1137 if (B->isObjCQualifiedIdType()) return false;
1138 */
1139
1140 // Now we know that A and B are (potentially-qualified) class types. The
1141 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001142 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001143}
1144
John McCall10302c02010-10-28 02:34:38 +00001145static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1146 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1147}
1148
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001149static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001150 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001151 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001152 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001153 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001154 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001155 if (IsProtocolMethodDecl &&
1156 (MethodDecl->getObjCDeclQualifier() !=
1157 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001158 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001159 S.Diag(MethodImpl->getLocation(),
1160 (IsOverridingMode ?
1161 diag::warn_conflicting_overriding_ret_type_modifiers
1162 : diag::warn_conflicting_ret_type_modifiers))
1163 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001164 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1165 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1166 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1167 }
1168 else
1169 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001170 }
1171
John McCall10302c02010-10-28 02:34:38 +00001172 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001173 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001174 return true;
1175 if (!Warn)
1176 return false;
John McCall10302c02010-10-28 02:34:38 +00001177
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001178 unsigned DiagID =
1179 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1180 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001181
1182 // Mismatches between ObjC pointers go into a different warning
1183 // category, and sometimes they're even completely whitelisted.
1184 if (const ObjCObjectPointerType *ImplPtrTy =
1185 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1186 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001187 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001188 // Allow non-matching return types as long as they don't violate
1189 // the principle of substitutability. Specifically, we permit
1190 // return types that are subclasses of the declared return type,
1191 // or that are more-qualified versions of the declared type.
1192 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001193 return false;
John McCall10302c02010-10-28 02:34:38 +00001194
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001195 DiagID =
1196 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1197 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001198 }
1199 }
1200
1201 S.Diag(MethodImpl->getLocation(), DiagID)
1202 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001203 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001204 << MethodImpl->getResultType()
1205 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001206 S.Diag(MethodDecl->getLocation(),
1207 IsOverridingMode ? diag::note_previous_declaration
1208 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001209 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001210 return false;
John McCall10302c02010-10-28 02:34:38 +00001211}
1212
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001213static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001214 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001215 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001216 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001217 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001218 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001219 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001220 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001221 if (IsProtocolMethodDecl &&
1222 (ImplVar->getObjCDeclQualifier() !=
1223 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001224 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001225 if (IsOverridingMode)
1226 S.Diag(ImplVar->getLocation(),
1227 diag::warn_conflicting_overriding_param_modifiers)
1228 << getTypeRange(ImplVar->getTypeSourceInfo())
1229 << MethodImpl->getDeclName();
1230 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001231 diag::warn_conflicting_param_modifiers)
1232 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001233 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001234 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1235 << getTypeRange(IfaceVar->getTypeSourceInfo());
1236 }
1237 else
1238 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001239 }
1240
John McCall10302c02010-10-28 02:34:38 +00001241 QualType ImplTy = ImplVar->getType();
1242 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001243
John McCall10302c02010-10-28 02:34:38 +00001244 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001245 return true;
1246
1247 if (!Warn)
1248 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001249 unsigned DiagID =
1250 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1251 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001252
1253 // Mismatches between ObjC pointers go into a different warning
1254 // category, and sometimes they're even completely whitelisted.
1255 if (const ObjCObjectPointerType *ImplPtrTy =
1256 ImplTy->getAs<ObjCObjectPointerType>()) {
1257 if (const ObjCObjectPointerType *IfacePtrTy =
1258 IfaceTy->getAs<ObjCObjectPointerType>()) {
1259 // Allow non-matching argument types as long as they don't
1260 // violate the principle of substitutability. Specifically, the
1261 // implementation must accept any objects that the superclass
1262 // accepts, however it may also accept others.
1263 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001264 return false;
John McCall10302c02010-10-28 02:34:38 +00001265
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001266 DiagID =
1267 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1268 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001269 }
1270 }
1271
1272 S.Diag(ImplVar->getLocation(), DiagID)
1273 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001274 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1275 S.Diag(IfaceVar->getLocation(),
1276 (IsOverridingMode ? diag::note_previous_declaration
1277 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001278 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001279 return false;
John McCall10302c02010-10-28 02:34:38 +00001280}
John McCallf85e1932011-06-15 23:02:42 +00001281
1282/// In ARC, check whether the conventional meanings of the two methods
1283/// match. If they don't, it's a hard error.
1284static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1285 ObjCMethodDecl *decl) {
1286 ObjCMethodFamily implFamily = impl->getMethodFamily();
1287 ObjCMethodFamily declFamily = decl->getMethodFamily();
1288 if (implFamily == declFamily) return false;
1289
1290 // Since conventions are sorted by selector, the only possibility is
1291 // that the types differ enough to cause one selector or the other
1292 // to fall out of the family.
1293 assert(implFamily == OMF_None || declFamily == OMF_None);
1294
1295 // No further diagnostics required on invalid declarations.
1296 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1297
1298 const ObjCMethodDecl *unmatched = impl;
1299 ObjCMethodFamily family = declFamily;
1300 unsigned errorID = diag::err_arc_lost_method_convention;
1301 unsigned noteID = diag::note_arc_lost_method_convention;
1302 if (declFamily == OMF_None) {
1303 unmatched = decl;
1304 family = implFamily;
1305 errorID = diag::err_arc_gained_method_convention;
1306 noteID = diag::note_arc_gained_method_convention;
1307 }
1308
1309 // Indexes into a %select clause in the diagnostic.
1310 enum FamilySelector {
1311 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1312 };
1313 FamilySelector familySelector = FamilySelector();
1314
1315 switch (family) {
1316 case OMF_None: llvm_unreachable("logic error, no method convention");
1317 case OMF_retain:
1318 case OMF_release:
1319 case OMF_autorelease:
1320 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001321 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001322 case OMF_retainCount:
1323 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001324 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001325 // Mismatches for these methods don't change ownership
1326 // conventions, so we don't care.
1327 return false;
1328
1329 case OMF_init: familySelector = F_init; break;
1330 case OMF_alloc: familySelector = F_alloc; break;
1331 case OMF_copy: familySelector = F_copy; break;
1332 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1333 case OMF_new: familySelector = F_new; break;
1334 }
1335
1336 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1337 ReasonSelector reasonSelector;
1338
1339 // The only reason these methods don't fall within their families is
1340 // due to unusual result types.
1341 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1342 reasonSelector = R_UnrelatedReturn;
1343 } else {
1344 reasonSelector = R_NonObjectReturn;
1345 }
1346
1347 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1348 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1349
1350 return true;
1351}
John McCall10302c02010-10-28 02:34:38 +00001352
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001353void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001354 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001355 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001356 if (getLangOptions().ObjCAutoRefCount &&
1357 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1358 return;
1359
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001360 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001361 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001362 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Chris Lattner3aff9192009-04-11 19:58:42 +00001364 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001365 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001366 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001367 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001368 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001369 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001370
Fariborz Jahanian21121902011-08-08 18:03:17 +00001371 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001372 Diag(ImpMethodDecl->getLocation(),
1373 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001374 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001375 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001376}
1377
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001378void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1379 ObjCMethodDecl *Overridden,
1380 bool IsProtocolMethodDecl) {
1381
1382 CheckMethodOverrideReturn(*this, Method, Overridden,
1383 IsProtocolMethodDecl, true,
1384 true);
1385
1386 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
1387 IF = Overridden->param_begin(), EM = Method->param_end();
1388 IM != EM; ++IM, ++IF) {
1389 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1390 IsProtocolMethodDecl, true, true);
1391 }
1392
1393 if (Method->isVariadic() != Overridden->isVariadic()) {
1394 Diag(Method->getLocation(),
1395 diag::warn_conflicting_overriding_variadic);
1396 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1397 }
1398}
1399
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001400/// WarnExactTypedMethods - This routine issues a warning if method
1401/// implementation declaration matches exactly that of its declaration.
1402void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1403 ObjCMethodDecl *MethodDecl,
1404 bool IsProtocolMethodDecl) {
1405 // don't issue warning when protocol method is optional because primary
1406 // class is not required to implement it and it is safe for protocol
1407 // to implement it.
1408 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1409 return;
1410 // don't issue warning when primary class's method is
1411 // depecated/unavailable.
1412 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1413 MethodDecl->hasAttr<DeprecatedAttr>())
1414 return;
1415
1416 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1417 IsProtocolMethodDecl, false, false);
1418 if (match)
1419 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1420 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1421 IM != EM; ++IM, ++IF) {
1422 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1423 *IM, *IF,
1424 IsProtocolMethodDecl, false, false);
1425 if (!match)
1426 break;
1427 }
1428 if (match)
1429 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001430 if (match)
1431 match = !(MethodDecl->isClassMethod() &&
1432 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001433
1434 if (match) {
1435 Diag(ImpMethodDecl->getLocation(),
1436 diag::warn_category_method_impl_match);
1437 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1438 }
1439}
1440
Mike Stump390b4cc2009-05-16 07:39:55 +00001441/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1442/// improve the efficiency of selector lookups and type checking by associating
1443/// with each protocol / interface / category the flattened instance tables. If
1444/// we used an immutable set to keep the table then it wouldn't add significant
1445/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001446
Steve Naroffefe7f362008-02-08 22:06:17 +00001447/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001448/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001449void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1450 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001451 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001452 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001453 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001454 ObjCContainerDecl *CDecl) {
1455 ObjCInterfaceDecl *IDecl;
1456 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1457 IDecl = C->getClassInterface();
1458 else
1459 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1460 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1461
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001462 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001463 ObjCInterfaceDecl *NSIDecl = 0;
1464 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001465 // check to see if class implements forwardInvocation method and objects
1466 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001467 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001468 // Under such conditions, which means that every method possible is
1469 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001470 // found" warnings.
1471 // FIXME: Use a general GetUnarySelector method for this.
1472 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1473 Selector fISelector = Context.Selectors.getSelector(1, &II);
1474 if (InsMap.count(fISelector))
1475 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1476 // need be implemented in the implementation.
1477 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1478 }
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001480 // If a method lookup fails locally we still need to look and see if
1481 // the method was implemented by a base class or an inherited
1482 // protocol. This lookup is slow, but occurs rarely in correct code
1483 // and otherwise would terminate in a warning.
1484
Chris Lattner4d391482007-12-12 07:09:47 +00001485 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001486 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001487 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001488 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001489 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001490 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001491 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001492 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001493 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001494 // Ugly, but necessary. Method declared in protcol might have
1495 // have been synthesized due to a property declared in the class which
1496 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001497 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001498 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001499 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001500 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001501 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
David Blaikied6471f72011-09-25 23:23:43 +00001502 != DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001503 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001504 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001505 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1506 << PDecl->getDeclName();
1507 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001508 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001509 }
1510 }
Chris Lattner4d391482007-12-12 07:09:47 +00001511 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001512 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001513 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001514 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001515 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001516 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1517 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001518 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001519 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001520 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1521 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001522 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001523 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001524 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1525 PDecl->getDeclName();
1526 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001527 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001528 }
Chris Lattner780f3292008-07-21 21:32:27 +00001529 // Check on this protocols's referenced protocols, recursively.
1530 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1531 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001532 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001533}
1534
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001535/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001536/// or protocol against those declared in their implementations.
1537///
1538void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1539 const llvm::DenseSet<Selector> &ClsMap,
1540 llvm::DenseSet<Selector> &InsMapSeen,
1541 llvm::DenseSet<Selector> &ClsMapSeen,
1542 ObjCImplDecl* IMPDecl,
1543 ObjCContainerDecl* CDecl,
1544 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001545 bool ImmediateClass,
1546 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001547 // Check and see if instance methods in class interface have been
1548 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001549 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1550 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001551 if (InsMapSeen.count((*I)->getSelector()))
1552 continue;
1553 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001554 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001555 !InsMap.count((*I)->getSelector())) {
1556 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001557 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1558 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001559 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001560 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001561 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001562 IMPDecl->getInstanceMethod((*I)->getSelector());
1563 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1564 "Expected to find the method through lookup as well");
1565 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001566 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001567 if (ImpMethodDecl) {
1568 if (!WarnExactMatch)
1569 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1570 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001571 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001572 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1573 isa<ObjCProtocolDecl>(CDecl));
1574 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001575 }
1576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001578 // Check and see if class methods in class interface have been
1579 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001580 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001581 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001582 if (ClsMapSeen.count((*I)->getSelector()))
1583 continue;
1584 ClsMapSeen.insert((*I)->getSelector());
1585 if (!ClsMap.count((*I)->getSelector())) {
1586 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001587 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1588 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001589 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001590 ObjCMethodDecl *ImpMethodDecl =
1591 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001592 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1593 "Expected to find the method through lookup as well");
1594 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001595 if (!WarnExactMatch)
1596 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1597 isa<ObjCProtocolDecl>(CDecl));
1598 else
1599 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1600 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001601 }
1602 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001603
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001604 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001605 // Also methods in class extensions need be looked at next.
1606 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1607 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1608 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1609 IMPDecl,
1610 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001611 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001612
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001613 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001614 for (ObjCInterfaceDecl::all_protocol_iterator
1615 PI = I->all_referenced_protocol_begin(),
1616 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001617 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1618 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001619 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001620
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001621 // FIXME. For now, we are not checking for extact match of methods
1622 // in category implementation and its primary class's super class.
1623 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001624 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001625 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001626 I->getSuperClass(), IncompleteImpl, false);
1627 }
1628}
1629
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001630/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1631/// category matches with those implemented in its primary class and
1632/// warns each time an exact match is found.
1633void Sema::CheckCategoryVsClassMethodMatches(
1634 ObjCCategoryImplDecl *CatIMPDecl) {
1635 llvm::DenseSet<Selector> InsMap, ClsMap;
1636
1637 for (ObjCImplementationDecl::instmeth_iterator
1638 I = CatIMPDecl->instmeth_begin(),
1639 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1640 InsMap.insert((*I)->getSelector());
1641
1642 for (ObjCImplementationDecl::classmeth_iterator
1643 I = CatIMPDecl->classmeth_begin(),
1644 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1645 ClsMap.insert((*I)->getSelector());
1646 if (InsMap.empty() && ClsMap.empty())
1647 return;
1648
1649 // Get category's primary class.
1650 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1651 if (!CatDecl)
1652 return;
1653 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1654 if (!IDecl)
1655 return;
1656 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1657 bool IncompleteImpl = false;
1658 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1659 CatIMPDecl, IDecl,
1660 IncompleteImpl, false, true /*WarnExactMatch*/);
1661}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001662
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001663void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001664 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001665 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001666 llvm::DenseSet<Selector> InsMap;
1667 // Check and see if instance methods in class interface have been
1668 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001669 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001670 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001671 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001673 // Check and see if properties declared in the interface have either 1)
1674 // an implementation or 2) there is a @synthesize/@dynamic implementation
1675 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001676 if (isa<ObjCInterfaceDecl>(CDecl) &&
1677 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001678 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001679
Chris Lattner4d391482007-12-12 07:09:47 +00001680 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001681 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001682 I = IMPDecl->classmeth_begin(),
1683 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001684 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001686 // Check for type conflict of methods declared in a class/protocol and
1687 // its implementation; if any.
1688 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001689 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1690 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001691 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001692
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001693 // check all methods implemented in category against those declared
1694 // in its primary class.
1695 if (ObjCCategoryImplDecl *CatDecl =
1696 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1697 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Chris Lattner4d391482007-12-12 07:09:47 +00001699 // Check the protocol list for unimplemented methods in the @implementation
1700 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001701 // Check and see if class methods in class interface have been
1702 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001703
Chris Lattnercddc8882009-03-01 00:56:52 +00001704 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001705 for (ObjCInterfaceDecl::all_protocol_iterator
1706 PI = I->all_referenced_protocol_begin(),
1707 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001708 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001709 InsMap, ClsMap, I);
1710 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001711 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1712 Categories; Categories = Categories->getNextClassExtension())
1713 ImplMethodsVsClassMethods(S, IMPDecl,
1714 const_cast<ObjCCategoryDecl*>(Categories),
1715 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001716 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001717 // For extended class, unimplemented methods in its protocols will
1718 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001719 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001720 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1721 E = C->protocol_end(); PI != E; ++PI)
1722 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001723 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001724 // Report unimplemented properties in the category as well.
1725 // When reporting on missing setter/getters, do not report when
1726 // setter/getter is implemented in category's primary class
1727 // implementation.
1728 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1729 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1730 for (ObjCImplementationDecl::instmeth_iterator
1731 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1732 InsMap.insert((*I)->getSelector());
1733 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001734 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001735 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001736 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001737 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001738}
1739
Mike Stump1eb44332009-09-09 15:08:12 +00001740/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001741Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001742Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001743 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001744 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001745 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001746 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001747 for (unsigned i = 0; i != NumElts; ++i) {
1748 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001749 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001750 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001751 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001752 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001753 // Maybe we will complain about the shadowed template parameter.
1754 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1755 // Just pretend that we didn't see the previous declaration.
1756 PrevDecl = 0;
1757 }
1758
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001759 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001760 // GCC apparently allows the following idiom:
1761 //
1762 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1763 // @class XCElementToggler;
1764 //
Mike Stump1eb44332009-09-09 15:08:12 +00001765 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001766 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001767 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001768 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001769 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001770 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001771 // a forward class declaration matching a typedef name of a class refers
1772 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001773 if (const ObjCObjectType *OI =
1774 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1775 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001776 }
Chris Lattner4d391482007-12-12 07:09:47 +00001777 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001778
1779 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001780 ObjCInterfaceDecl *PrevIDecl
1781 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001782 ObjCInterfaceDecl *IDecl
1783 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001784 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001785 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001786
Douglas Gregor7723fec2011-12-15 20:29:51 +00001787 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001788 CheckObjCDeclScope(IDecl);
1789 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001790 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001791
1792 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001793}
1794
John McCall0f4c4c42011-06-16 01:15:19 +00001795static bool tryMatchRecordTypes(ASTContext &Context,
1796 Sema::MethodMatchStrategy strategy,
1797 const Type *left, const Type *right);
1798
John McCallf85e1932011-06-15 23:02:42 +00001799static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1800 QualType leftQT, QualType rightQT) {
1801 const Type *left =
1802 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1803 const Type *right =
1804 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1805
1806 if (left == right) return true;
1807
1808 // If we're doing a strict match, the types have to match exactly.
1809 if (strategy == Sema::MMS_strict) return false;
1810
1811 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1812
1813 // Otherwise, use this absurdly complicated algorithm to try to
1814 // validate the basic, low-level compatibility of the two types.
1815
1816 // As a minimum, require the sizes and alignments to match.
1817 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1818 return false;
1819
1820 // Consider all the kinds of non-dependent canonical types:
1821 // - functions and arrays aren't possible as return and parameter types
1822
1823 // - vector types of equal size can be arbitrarily mixed
1824 if (isa<VectorType>(left)) return isa<VectorType>(right);
1825 if (isa<VectorType>(right)) return false;
1826
1827 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001828 // - structs, unions, and Objective-C objects must match more-or-less
1829 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001830 // - everything else should be a scalar
1831 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001832 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001833
John McCall1d9b3b22011-09-09 05:25:32 +00001834 // Make scalars agree in kind, except count bools as chars, and group
1835 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001836 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1837 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1838 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1839 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001840 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1841 leftSK = Type::STK_ObjCObjectPointer;
1842 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1843 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001844
1845 // Note that data member pointers and function member pointers don't
1846 // intermix because of the size differences.
1847
1848 return (leftSK == rightSK);
1849}
Chris Lattner4d391482007-12-12 07:09:47 +00001850
John McCall0f4c4c42011-06-16 01:15:19 +00001851static bool tryMatchRecordTypes(ASTContext &Context,
1852 Sema::MethodMatchStrategy strategy,
1853 const Type *lt, const Type *rt) {
1854 assert(lt && rt && lt != rt);
1855
1856 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1857 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1858 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1859
1860 // Require union-hood to match.
1861 if (left->isUnion() != right->isUnion()) return false;
1862
1863 // Require an exact match if either is non-POD.
1864 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1865 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1866 return false;
1867
1868 // Require size and alignment to match.
1869 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1870
1871 // Require fields to match.
1872 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1873 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1874 for (; li != le && ri != re; ++li, ++ri) {
1875 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1876 return false;
1877 }
1878 return (li == le && ri == re);
1879}
1880
Chris Lattner4d391482007-12-12 07:09:47 +00001881/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1882/// returns true, or false, accordingly.
1883/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001884bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1885 const ObjCMethodDecl *right,
1886 MethodMatchStrategy strategy) {
1887 if (!matchTypes(Context, strategy,
1888 left->getResultType(), right->getResultType()))
1889 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001890
John McCallf85e1932011-06-15 23:02:42 +00001891 if (getLangOptions().ObjCAutoRefCount &&
1892 (left->hasAttr<NSReturnsRetainedAttr>()
1893 != right->hasAttr<NSReturnsRetainedAttr>() ||
1894 left->hasAttr<NSConsumesSelfAttr>()
1895 != right->hasAttr<NSConsumesSelfAttr>()))
1896 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001897
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001898 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001899 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001900
John McCallf85e1932011-06-15 23:02:42 +00001901 for (; li != le; ++li, ++ri) {
1902 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001903 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001904
1905 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1906 return false;
1907
1908 if (getLangOptions().ObjCAutoRefCount &&
1909 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1910 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001911 }
1912 return true;
1913}
1914
Sebastian Redldb9d2142010-08-02 23:18:59 +00001915/// \brief Read the contents of the method pool for a given selector from
1916/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001917///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001918/// This routine should only be called once, when the method pool has no entry
1919/// for this selector.
1920Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001921 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001922 assert(MethodPool.find(Sel) == MethodPool.end() &&
1923 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001924
1925 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001926 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001927
Sebastian Redldb9d2142010-08-02 23:18:59 +00001928 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001929}
1930
Sebastian Redldb9d2142010-08-02 23:18:59 +00001931void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1932 bool instance) {
1933 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1934 if (Pos == MethodPool.end()) {
1935 if (ExternalSource)
1936 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001937 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001938 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1939 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001940 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001941 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001942 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001943 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001944 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001945 Entry.Method = Method;
1946 Entry.Next = 0;
1947 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001948 }
Mike Stump1eb44332009-09-09 15:08:12 +00001949
Chris Lattnerb25df352009-03-04 05:16:45 +00001950 // We've seen a method with this name, see if we have already seen this type
1951 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001952 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1953 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1954
1955 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001956 ObjCMethodDecl *PrevObjCMethod = List->Method;
1957 PrevObjCMethod->setDefined(impl);
1958 // If a method is deprecated, push it in the global pool.
1959 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001960 if (Method->isDeprecated()) {
1961 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001962 List->Method = Method;
1963 }
1964 // If new method is unavailable, push it into global pool
1965 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001966 if (Method->isUnavailable()) {
1967 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001968 List->Method = Method;
1969 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001970 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001971 }
John McCallf85e1932011-06-15 23:02:42 +00001972 }
Mike Stump1eb44332009-09-09 15:08:12 +00001973
Chris Lattnerb25df352009-03-04 05:16:45 +00001974 // We have a new signature for an existing method - add it.
1975 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001976 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1977 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001978}
1979
John McCallf85e1932011-06-15 23:02:42 +00001980/// Determines if this is an "acceptable" loose mismatch in the global
1981/// method pool. This exists mostly as a hack to get around certain
1982/// global mismatches which we can't afford to make warnings / errors.
1983/// Really, what we want is a way to take a method out of the global
1984/// method pool.
1985static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1986 ObjCMethodDecl *other) {
1987 if (!chosen->isInstanceMethod())
1988 return false;
1989
1990 Selector sel = chosen->getSelector();
1991 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1992 return false;
1993
1994 // Don't complain about mismatches for -length if the method we
1995 // chose has an integral result type.
1996 return (chosen->getResultType()->isIntegerType());
1997}
1998
Sebastian Redldb9d2142010-08-02 23:18:59 +00001999ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002000 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002001 bool warn, bool instance) {
2002 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2003 if (Pos == MethodPool.end()) {
2004 if (ExternalSource)
2005 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002006 else
2007 return 0;
2008 }
2009
Sebastian Redldb9d2142010-08-02 23:18:59 +00002010 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Sebastian Redldb9d2142010-08-02 23:18:59 +00002012 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002013 bool issueDiagnostic = false, issueError = false;
2014
2015 // We support a warning which complains about *any* difference in
2016 // method signature.
2017 bool strictSelectorMatch =
2018 (receiverIdOrClass && warn &&
2019 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2020 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002021 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002022 if (strictSelectorMatch)
2023 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002024 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2025 MMS_strict)) {
2026 issueDiagnostic = true;
2027 break;
2028 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002029 }
2030
John McCallf85e1932011-06-15 23:02:42 +00002031 // If we didn't see any strict differences, we won't see any loose
2032 // differences. In ARC, however, we also need to check for loose
2033 // mismatches, because most of them are errors.
2034 if (!strictSelectorMatch ||
2035 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002036 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002037 // This checks if the methods differ in type mismatch.
2038 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2039 MMS_loose) &&
2040 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2041 issueDiagnostic = true;
2042 if (getLangOptions().ObjCAutoRefCount)
2043 issueError = true;
2044 break;
2045 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002046 }
2047
John McCallf85e1932011-06-15 23:02:42 +00002048 if (issueDiagnostic) {
2049 if (issueError)
2050 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2051 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002052 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2053 else
2054 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002055
2056 Diag(MethList.Method->getLocStart(),
2057 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002058 << MethList.Method->getSourceRange();
2059 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2060 Diag(Next->Method->getLocStart(), diag::note_also_found)
2061 << Next->Method->getSourceRange();
2062 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002063 }
2064 return MethList.Method;
2065}
2066
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002067ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002068 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2069 if (Pos == MethodPool.end())
2070 return 0;
2071
2072 GlobalMethods &Methods = Pos->second;
2073
2074 if (Methods.first.Method && Methods.first.Method->isDefined())
2075 return Methods.first.Method;
2076 if (Methods.second.Method && Methods.second.Method->isDefined())
2077 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002078 return 0;
2079}
2080
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002081/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2082/// identical selector names in current and its super classes and issues
2083/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002084void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2085 ObjCMethodDecl *Method,
2086 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002087 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2088 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002089
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002090 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002091 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002092 SD->lookupMethod(Method->getSelector(), IsInstance);
2093 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002094 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002095 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002096 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002097 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2098 E = Method->param_end();
2099 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2100 for (; ParamI != E; ++ParamI, ++PrevI) {
2101 // Number of parameters are the same and is guaranteed by selector match.
2102 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2103 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2104 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002105 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002106 // respective argument type in the super class method, issue warning;
2107 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002108 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002109 << T1 << T2;
2110 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2111 return;
2112 }
2113 }
2114 ID = SD;
2115 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002116}
2117
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002118/// DiagnoseDuplicateIvars -
2119/// Check for duplicate ivars in the entire class at the start of
2120/// @implementation. This becomes necesssary because class extension can
2121/// add ivars to a class in random order which will not be known until
2122/// class's @implementation is seen.
2123void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2124 ObjCInterfaceDecl *SID) {
2125 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2126 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2127 ObjCIvarDecl* Ivar = (*IVI);
2128 if (Ivar->isInvalidDecl())
2129 continue;
2130 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2131 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2132 if (prevIvar) {
2133 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2134 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2135 Ivar->setInvalidDecl();
2136 }
2137 }
2138 }
2139}
2140
Erik Verbruggend64251f2011-12-06 09:25:23 +00002141Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2142 switch (CurContext->getDeclKind()) {
2143 case Decl::ObjCInterface:
2144 return Sema::OCK_Interface;
2145 case Decl::ObjCProtocol:
2146 return Sema::OCK_Protocol;
2147 case Decl::ObjCCategory:
2148 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2149 return Sema::OCK_ClassExtension;
2150 else
2151 return Sema::OCK_Category;
2152 case Decl::ObjCImplementation:
2153 return Sema::OCK_Implementation;
2154 case Decl::ObjCCategoryImpl:
2155 return Sema::OCK_CategoryImplementation;
2156
2157 default:
2158 return Sema::OCK_None;
2159 }
2160}
2161
Steve Naroffa56f6162007-12-18 01:30:32 +00002162// Note: For class/category implemenations, allMethods/allProperties is
2163// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002164Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2165 Decl **allMethods, unsigned allNum,
2166 Decl **allProperties, unsigned pNum,
2167 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002168
Erik Verbruggend64251f2011-12-06 09:25:23 +00002169 if (getObjCContainerKind() == Sema::OCK_None)
2170 return 0;
2171
2172 assert(AtEnd.isValid() && "Invalid location for '@end'");
2173
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002174 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2175 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002176
Mike Stump1eb44332009-09-09 15:08:12 +00002177 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002178 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2179 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002180 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002181
Steve Naroff0701bbb2009-01-08 17:28:14 +00002182 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2183 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2184 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2185
Chris Lattner4d391482007-12-12 07:09:47 +00002186 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002187 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002188 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002189
2190 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002191 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002192 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002193 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002194 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002195 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002196 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002197 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002198 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002199 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002200 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002201 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002202 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002203 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002204 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002205 if (!Context.getSourceManager().isInSystemHeader(
2206 Method->getLocation()))
2207 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2208 << Method->getDeclName();
2209 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2210 }
Chris Lattner4d391482007-12-12 07:09:47 +00002211 InsMap[Method->getSelector()] = Method;
2212 /// The following allows us to typecheck messages to "id".
2213 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002214 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002215 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002216 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002217 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002218 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002219 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002220 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002221 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002222 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002223 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002224 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002225 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002226 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002227 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002228 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002229 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002230 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002231 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002232 if (!Context.getSourceManager().isInSystemHeader(
2233 Method->getLocation()))
2234 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2235 << Method->getDeclName();
2236 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2237 }
Chris Lattner4d391482007-12-12 07:09:47 +00002238 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002239 /// The following allows us to typecheck messages to "Class".
2240 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002241 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002242 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002243 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002244 }
2245 }
2246 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002247 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002248 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002249 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002250 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002251 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002252 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002253 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002254 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002255 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002256
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002257 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002258 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002259 if (C->IsClassExtension()) {
2260 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2261 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002262 }
Chris Lattner4d391482007-12-12 07:09:47 +00002263 }
Steve Naroff09c47192009-01-09 15:36:25 +00002264 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002265 if (CDecl->getIdentifier())
2266 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2267 // user-defined setter/getter. It also synthesizes setter/getter methods
2268 // and adds them to the DeclContext and global method pools.
2269 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2270 E = CDecl->prop_end();
2271 I != E; ++I)
2272 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002273 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002274 }
2275 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002276 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002277 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002278 // Any property declared in a class extension might have user
2279 // declared setter or getter in current class extension or one
2280 // of the other class extensions. Mark them as synthesized as
2281 // property will be synthesized when property with same name is
2282 // seen in the @implementation.
2283 for (const ObjCCategoryDecl *ClsExtDecl =
2284 IDecl->getFirstClassExtension();
2285 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2286 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2287 E = ClsExtDecl->prop_end(); I != E; ++I) {
2288 ObjCPropertyDecl *Property = (*I);
2289 // Skip over properties declared @dynamic
2290 if (const ObjCPropertyImplDecl *PIDecl
2291 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2292 if (PIDecl->getPropertyImplementation()
2293 == ObjCPropertyImplDecl::Dynamic)
2294 continue;
2295
2296 for (const ObjCCategoryDecl *CExtDecl =
2297 IDecl->getFirstClassExtension();
2298 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2299 if (ObjCMethodDecl *GetterMethod =
2300 CExtDecl->getInstanceMethod(Property->getGetterName()))
2301 GetterMethod->setSynthesized(true);
2302 if (!Property->isReadOnly())
2303 if (ObjCMethodDecl *SetterMethod =
2304 CExtDecl->getInstanceMethod(Property->getSetterName()))
2305 SetterMethod->setSynthesized(true);
2306 }
2307 }
2308 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002309 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002310 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002311 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002312
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002313 if (LangOpts.ObjCNonFragileABI2)
2314 while (IDecl->getSuperClass()) {
2315 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2316 IDecl = IDecl->getSuperClass();
2317 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002318 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002319 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002320 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002321 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002322 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002323
Chris Lattner4d391482007-12-12 07:09:47 +00002324 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002325 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002326 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002327 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002328 Categories; Categories = Categories->getNextClassCategory()) {
2329 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002330 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002331 break;
2332 }
2333 }
2334 }
2335 }
Chris Lattner682bf922009-03-29 16:50:03 +00002336 if (isInterfaceDeclKind) {
2337 // Reject invalid vardecls.
2338 for (unsigned i = 0; i != tuvNum; i++) {
2339 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2340 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2341 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002342 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002343 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002344 }
Chris Lattner682bf922009-03-29 16:50:03 +00002345 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002346 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002347 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002348
2349 for (unsigned i = 0; i != tuvNum; i++) {
2350 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002351 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2352 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002353 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2354 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002355
2356 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002357}
2358
2359
2360/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2361/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002362static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002363CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002364 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002365}
2366
Ted Kremenek422bae72010-04-18 04:59:38 +00002367static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002368bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2369 const AttrVec &A) {
2370 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002371 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002372 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002373 return false;
2374
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002375 // method declared in interface has no attribute.
2376 // But implementation has attributes. This is invalid
2377 if (!IMD->hasAttrs())
2378 return true;
2379
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002380 const AttrVec &D = IMD->getAttrs();
2381 if (D.size() != A.size())
2382 return true;
2383
2384 // attributes on method declaration and definition must match exactly.
2385 // Note that we have at most a couple of attributes on methods, so this
2386 // n*n search is good enough.
2387 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2388 bool match = false;
2389 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2390 if ((*i)->getKind() == (*i1)->getKind()) {
2391 match = true;
2392 break;
2393 }
2394 }
2395 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002396 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002397 }
Sean Huntcf807c42010-08-18 23:23:40 +00002398 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002399}
2400
Douglas Gregore97179c2011-09-08 01:46:34 +00002401namespace {
2402 /// \brief Describes the compatibility of a result type with its method.
2403 enum ResultTypeCompatibilityKind {
2404 RTC_Compatible,
2405 RTC_Incompatible,
2406 RTC_Unknown
2407 };
2408}
2409
Douglas Gregor926df6c2011-06-11 01:09:30 +00002410/// \brief Check whether the declared result type of the given Objective-C
2411/// method declaration is compatible with the method's class.
2412///
Douglas Gregore97179c2011-09-08 01:46:34 +00002413static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002414CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2415 ObjCInterfaceDecl *CurrentClass) {
2416 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002417
2418 // If an Objective-C method inherits its related result type, then its
2419 // declared result type must be compatible with its own class type. The
2420 // declared result type is compatible if:
2421 if (const ObjCObjectPointerType *ResultObjectType
2422 = ResultType->getAs<ObjCObjectPointerType>()) {
2423 // - it is id or qualified id, or
2424 if (ResultObjectType->isObjCIdType() ||
2425 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002426 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002427
2428 if (CurrentClass) {
2429 if (ObjCInterfaceDecl *ResultClass
2430 = ResultObjectType->getInterfaceDecl()) {
2431 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002432 if (declaresSameEntity(CurrentClass, ResultClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002433 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002434
2435 // - it is a superclass of the method's class type
2436 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002437 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002438 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002439 } else {
2440 // Any Objective-C pointer type might be acceptable for a protocol
2441 // method; we just don't know.
2442 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002443 }
2444 }
2445
Douglas Gregore97179c2011-09-08 01:46:34 +00002446 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002447}
2448
John McCall6c2c2502011-07-22 02:45:48 +00002449namespace {
2450/// A helper class for searching for methods which a particular method
2451/// overrides.
2452class OverrideSearch {
2453 Sema &S;
2454 ObjCMethodDecl *Method;
2455 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2456 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2457 bool Recursive;
2458
2459public:
2460 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2461 Selector selector = method->getSelector();
2462
2463 // Bypass this search if we've never seen an instance/class method
2464 // with this selector before.
2465 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2466 if (it == S.MethodPool.end()) {
2467 if (!S.ExternalSource) return;
2468 it = S.ReadMethodPool(selector);
2469 }
2470 ObjCMethodList &list =
2471 method->isInstanceMethod() ? it->second.first : it->second.second;
2472 if (!list.Method) return;
2473
2474 ObjCContainerDecl *container
2475 = cast<ObjCContainerDecl>(method->getDeclContext());
2476
2477 // Prevent the search from reaching this container again. This is
2478 // important with categories, which override methods from the
2479 // interface and each other.
2480 Searched.insert(container);
2481 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002482 }
John McCall6c2c2502011-07-22 02:45:48 +00002483
2484 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2485 iterator begin() const { return Overridden.begin(); }
2486 iterator end() const { return Overridden.end(); }
2487
2488private:
2489 void searchFromContainer(ObjCContainerDecl *container) {
2490 if (container->isInvalidDecl()) return;
2491
2492 switch (container->getDeclKind()) {
2493#define OBJCCONTAINER(type, base) \
2494 case Decl::type: \
2495 searchFrom(cast<type##Decl>(container)); \
2496 break;
2497#define ABSTRACT_DECL(expansion)
2498#define DECL(type, base) \
2499 case Decl::type:
2500#include "clang/AST/DeclNodes.inc"
2501 llvm_unreachable("not an ObjC container!");
2502 }
2503 }
2504
2505 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002506 if (!protocol->hasDefinition())
2507 return;
2508
John McCall6c2c2502011-07-22 02:45:48 +00002509 // A method in a protocol declaration overrides declarations from
2510 // referenced ("parent") protocols.
2511 search(protocol->getReferencedProtocols());
2512 }
2513
2514 void searchFrom(ObjCCategoryDecl *category) {
2515 // A method in a category declaration overrides declarations from
2516 // the main class and from protocols the category references.
2517 search(category->getClassInterface());
2518 search(category->getReferencedProtocols());
2519 }
2520
2521 void searchFrom(ObjCCategoryImplDecl *impl) {
2522 // A method in a category definition that has a category
2523 // declaration overrides declarations from the category
2524 // declaration.
2525 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2526 search(category);
2527
2528 // Otherwise it overrides declarations from the class.
2529 } else {
2530 search(impl->getClassInterface());
2531 }
2532 }
2533
2534 void searchFrom(ObjCInterfaceDecl *iface) {
2535 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002536 if (!iface->hasDefinition())
2537 return;
2538
John McCall6c2c2502011-07-22 02:45:48 +00002539 // - categories,
2540 for (ObjCCategoryDecl *category = iface->getCategoryList();
2541 category; category = category->getNextClassCategory())
2542 search(category);
2543
2544 // - the super class, and
2545 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2546 search(super);
2547
2548 // - any referenced protocols.
2549 search(iface->getReferencedProtocols());
2550 }
2551
2552 void searchFrom(ObjCImplementationDecl *impl) {
2553 // A method in a class implementation overrides declarations from
2554 // the class interface.
2555 search(impl->getClassInterface());
2556 }
2557
2558
2559 void search(const ObjCProtocolList &protocols) {
2560 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2561 i != e; ++i)
2562 search(*i);
2563 }
2564
2565 void search(ObjCContainerDecl *container) {
2566 // Abort if we've already searched this container.
2567 if (!Searched.insert(container)) return;
2568
2569 // Check for a method in this container which matches this selector.
2570 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2571 Method->isInstanceMethod());
2572
2573 // If we find one, record it and bail out.
2574 if (meth) {
2575 Overridden.insert(meth);
2576 return;
2577 }
2578
2579 // Otherwise, search for methods that a hypothetical method here
2580 // would have overridden.
2581
2582 // Note that we're now in a recursive case.
2583 Recursive = true;
2584
2585 searchFromContainer(container);
2586 }
2587};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002588}
2589
John McCalld226f652010-08-21 09:40:31 +00002590Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002591 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002592 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002593 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002594 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002595 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002596 Selector Sel,
2597 // optional arguments. The number of types/arguments is obtained
2598 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002599 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002600 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002601 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002602 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002603 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002604 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002605 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002606 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002607 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002608 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2609 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002610 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002611
Douglas Gregore97179c2011-09-08 01:46:34 +00002612 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002613 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002614 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002615 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002616
Steve Naroffccef3712009-02-20 22:59:16 +00002617 // Methods cannot return interface types. All ObjC objects are
2618 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002619 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002620 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2621 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002622 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002623 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002624
2625 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002626 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002627 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002628 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002629 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002630 }
Mike Stump1eb44332009-09-09 15:08:12 +00002631
2632 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002633 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002634 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002635 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002636 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002637 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002638 /*isSynthesized=*/false,
2639 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002640 MethodDeclKind == tok::objc_optional
2641 ? ObjCMethodDecl::Optional
2642 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002643 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002644
Chris Lattner5f9e2722011-07-23 10:55:15 +00002645 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002646
Chris Lattner7db638d2009-04-11 19:42:43 +00002647 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002648 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002649 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002650
Chris Lattnere294d3f2009-04-11 18:57:04 +00002651 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002652 ArgType = Context.getObjCIdType();
2653 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002654 } else {
John McCall58e46772009-10-23 21:48:59 +00002655 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002656 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002657 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002658 }
Mike Stump1eb44332009-09-09 15:08:12 +00002659
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002660 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2661 LookupOrdinaryName, ForRedeclaration);
2662 LookupName(R, S);
2663 if (R.isSingleResult()) {
2664 NamedDecl *PrevDecl = R.getFoundDecl();
2665 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002666 Diag(ArgInfo[i].NameLoc,
2667 (MethodDefinition ? diag::warn_method_param_redefinition
2668 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002669 << ArgInfo[i].Name;
2670 Diag(PrevDecl->getLocation(),
2671 diag::note_previous_declaration);
2672 }
2673 }
2674
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002675 SourceLocation StartLoc = DI
2676 ? DI->getTypeLoc().getBeginLoc()
2677 : ArgInfo[i].NameLoc;
2678
John McCall81ef3e62011-04-23 02:46:06 +00002679 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2680 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2681 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002682
John McCall70798862011-05-02 00:30:12 +00002683 Param->setObjCMethodScopeInfo(i);
2684
Chris Lattner0ed844b2008-04-04 06:12:32 +00002685 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002686 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002687
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002688 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002689 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002690
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002691 S->AddDecl(Param);
2692 IdResolver.AddDecl(Param);
2693
Chris Lattner0ed844b2008-04-04 06:12:32 +00002694 Params.push_back(Param);
2695 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002696
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002697 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002698 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002699 QualType ArgType = Param->getType();
2700 if (ArgType.isNull())
2701 ArgType = Context.getObjCIdType();
2702 else
2703 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002704 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002705 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002706 Diag(Param->getLocation(),
2707 diag::err_object_cannot_be_passed_returned_by_value)
2708 << 1 << ArgType;
2709 Param->setInvalidDecl();
2710 }
2711 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002712
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002713 Params.push_back(Param);
2714 }
2715
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002716 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002717 ObjCMethod->setObjCDeclQualifier(
2718 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002719
2720 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002721 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002723 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002724 const ObjCMethodDecl *PrevMethod = 0;
2725 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002726 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002727 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2728 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002729 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002730 PrevMethod = ImpDecl->getClassMethod(Sel);
2731 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002732 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002733
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002734 ObjCMethodDecl *IMD = 0;
2735 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2736 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2737 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002738 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002739 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002740 SourceLocation MethodLoc = IMD->getLocation();
2741 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2742 Diag(EndLoc, diag::warn_attribute_method_def);
2743 Diag(MethodLoc, diag::note_method_declared_at);
2744 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002745 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002746 } else {
2747 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002748 }
John McCall6c2c2502011-07-22 02:45:48 +00002749
Chris Lattner4d391482007-12-12 07:09:47 +00002750 if (PrevMethod) {
2751 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002752 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002753 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002754 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002755 }
John McCall54abf7d2009-11-04 02:18:39 +00002756
Douglas Gregor926df6c2011-06-11 01:09:30 +00002757 // If this Objective-C method does not have a related result type, but we
2758 // are allowed to infer related result types, try to do so based on the
2759 // method family.
2760 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2761 if (!CurrentClass) {
2762 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2763 CurrentClass = Cat->getClassInterface();
2764 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2765 CurrentClass = Impl->getClassInterface();
2766 else if (ObjCCategoryImplDecl *CatImpl
2767 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2768 CurrentClass = CatImpl->getClassInterface();
2769 }
John McCall6c2c2502011-07-22 02:45:48 +00002770
Douglas Gregore97179c2011-09-08 01:46:34 +00002771 ResultTypeCompatibilityKind RTC
2772 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002773
2774 // Search for overridden methods and merge information down from them.
2775 OverrideSearch overrides(*this, ObjCMethod);
2776 for (OverrideSearch::iterator
2777 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2778 ObjCMethodDecl *overridden = *i;
2779
2780 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002781 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002782 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002783
2784 // Then merge the declarations.
2785 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002786
2787 // Check for overriding methods
2788 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00002789 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2790 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2791 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
Douglas Gregor926df6c2011-06-11 01:09:30 +00002792 }
2793
John McCallf85e1932011-06-15 23:02:42 +00002794 bool ARCError = false;
2795 if (getLangOptions().ObjCAutoRefCount)
2796 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2797
Douglas Gregore97179c2011-09-08 01:46:34 +00002798 // Infer the related result type when possible.
2799 if (!ARCError && RTC == RTC_Compatible &&
2800 !ObjCMethod->hasRelatedResultType() &&
2801 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002802 bool InferRelatedResultType = false;
2803 switch (ObjCMethod->getMethodFamily()) {
2804 case OMF_None:
2805 case OMF_copy:
2806 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002807 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002808 case OMF_mutableCopy:
2809 case OMF_release:
2810 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002811 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002812 break;
2813
2814 case OMF_alloc:
2815 case OMF_new:
2816 InferRelatedResultType = ObjCMethod->isClassMethod();
2817 break;
2818
2819 case OMF_init:
2820 case OMF_autorelease:
2821 case OMF_retain:
2822 case OMF_self:
2823 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2824 break;
2825 }
2826
John McCall6c2c2502011-07-22 02:45:48 +00002827 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002828 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002829 }
2830
John McCalld226f652010-08-21 09:40:31 +00002831 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002832}
2833
Chris Lattnercc98eac2008-12-17 07:13:27 +00002834bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002835 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002836 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002837 // Following is also an error. But it is caused by a missing @end
2838 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002839 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2840 return false;
2841 }
2842
Anders Carlsson15281452008-11-04 16:57:32 +00002843 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2844 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002845
Anders Carlsson15281452008-11-04 16:57:32 +00002846 return true;
2847}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002848
Chris Lattnercc98eac2008-12-17 07:13:27 +00002849/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2850/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002851void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002852 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002853 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002854 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002855 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002856 if (!Class) {
2857 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2858 return;
2859 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002860 if (LangOpts.ObjCNonFragileABI) {
2861 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2862 return;
2863 }
Mike Stump1eb44332009-09-09 15:08:12 +00002864
Chris Lattnercc98eac2008-12-17 07:13:27 +00002865 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002866 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002867 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002868 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002869 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002870 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002871 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002872 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2873 /*FIXME: StartL=*/ID->getLocation(),
2874 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002875 ID->getIdentifier(), ID->getType(),
2876 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002877 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002878 }
Mike Stump1eb44332009-09-09 15:08:12 +00002879
Chris Lattnercc98eac2008-12-17 07:13:27 +00002880 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002881 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002882 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002883 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002884 if (getLangOptions().CPlusPlus)
2885 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002886 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002887 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002888 }
2889}
2890
Douglas Gregor160b5632010-04-26 17:32:49 +00002891/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002892VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2893 SourceLocation StartLoc,
2894 SourceLocation IdLoc,
2895 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002896 bool Invalid) {
2897 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2898 // duration shall not be qualified by an address-space qualifier."
2899 // Since all parameters have automatic store duration, they can not have
2900 // an address space.
2901 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002902 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002903 Invalid = true;
2904 }
2905
2906 // An @catch parameter must be an unqualified object pointer type;
2907 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2908 if (Invalid) {
2909 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002910 } else if (T->isDependentType()) {
2911 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002912 } else if (!T->isObjCObjectPointerType()) {
2913 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002914 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002915 } else if (T->isObjCQualifiedIdType()) {
2916 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002917 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002918 }
2919
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002920 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2921 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002922 New->setExceptionVariable(true);
2923
Douglas Gregor9aab9c42011-12-10 01:22:52 +00002924 // In ARC, infer 'retaining' for variables of retainable type.
2925 if (getLangOptions().ObjCAutoRefCount && inferObjCARCLifetime(New))
2926 Invalid = true;
2927
Douglas Gregor160b5632010-04-26 17:32:49 +00002928 if (Invalid)
2929 New->setInvalidDecl();
2930 return New;
2931}
2932
John McCalld226f652010-08-21 09:40:31 +00002933Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002934 const DeclSpec &DS = D.getDeclSpec();
2935
2936 // We allow the "register" storage class on exception variables because
2937 // GCC did, but we drop it completely. Any other storage class is an error.
2938 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2939 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2940 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2941 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2942 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2943 << DS.getStorageClassSpec();
2944 }
2945 if (D.getDeclSpec().isThreadSpecified())
2946 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2947 D.getMutableDeclSpec().ClearStorageClassSpecs();
2948
2949 DiagnoseFunctionSpecifiers(D);
2950
2951 // Check that there are no default arguments inside the type of this
2952 // exception object (C++ only).
2953 if (getLangOptions().CPlusPlus)
2954 CheckExtraCXXDefaultArguments(D);
2955
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002956 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002957 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002958
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002959 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2960 D.getSourceRange().getBegin(),
2961 D.getIdentifierLoc(),
2962 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002963 D.isInvalidType());
2964
2965 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2966 if (D.getCXXScopeSpec().isSet()) {
2967 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2968 << D.getCXXScopeSpec().getRange();
2969 New->setInvalidDecl();
2970 }
2971
2972 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002973 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002974 if (D.getIdentifier())
2975 IdResolver.AddDecl(New);
2976
2977 ProcessDeclAttributes(S, New, D);
2978
2979 if (New->hasAttr<BlocksAttr>())
2980 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002981 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002982}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002983
2984/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002985/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002986void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002987 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002988 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2989 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002990 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002991 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002992 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002993 }
2994}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002995
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002996void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002997 // Load referenced selectors from the external source.
2998 if (ExternalSource) {
2999 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3000 ExternalSource->ReadReferencedSelectors(Sels);
3001 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3002 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3003 }
3004
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003005 // Warning will be issued only when selector table is
3006 // generated (which means there is at lease one implementation
3007 // in the TU). This is to match gcc's behavior.
3008 if (ReferencedSelectors.empty() ||
3009 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003010 return;
3011 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3012 ReferencedSelectors.begin(),
3013 E = ReferencedSelectors.end(); S != E; ++S) {
3014 Selector Sel = (*S).first;
3015 if (!LookupImplementedMethodInGlobalPool(Sel))
3016 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3017 }
3018 return;
3019}