blob: c4261ecbb81196a0b7233c533d31138abf309f39 [file] [log] [blame]
Chris Lattner4d391482007-12-12 07:09:47 +00001//===--- SemaDeclObjC.cpp - Semantic Analysis for ObjC Declarations -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4d391482007-12-12 07:09:47 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements semantic analysis for Objective C declarations.
11//
12//===----------------------------------------------------------------------===//
13
John McCall2d887082010-08-25 22:03:47 +000014#include "clang/Sema/SemaInternal.h"
Douglas Gregore737f502010-08-12 20:07:10 +000015#include "clang/Sema/Lookup.h"
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +000016#include "clang/Sema/ExternalSemaSource.h"
John McCall5f1e0942010-08-24 08:50:51 +000017#include "clang/Sema/Scope.h"
John McCall781472f2010-08-25 08:40:02 +000018#include "clang/Sema/ScopeInfo.h"
John McCallf85e1932011-06-15 23:02:42 +000019#include "clang/AST/ASTConsumer.h"
Steve Naroffca331292009-03-03 14:49:36 +000020#include "clang/AST/Expr.h"
John McCallf85e1932011-06-15 23:02:42 +000021#include "clang/AST/ExprObjC.h"
Chris Lattner4d391482007-12-12 07:09:47 +000022#include "clang/AST/ASTContext.h"
23#include "clang/AST/DeclObjC.h"
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +000024#include "clang/AST/ASTMutationListener.h"
John McCallf85e1932011-06-15 23:02:42 +000025#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000026#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000027#include "llvm/ADT/DenseSet.h"
28
Chris Lattner4d391482007-12-12 07:09:47 +000029using namespace clang;
30
John McCallf85e1932011-06-15 23:02:42 +000031/// Check whether the given method, which must be in the 'init'
32/// family, is a valid member of that family.
33///
34/// \param receiverTypeIfCall - if null, check this as if declaring it;
35/// if non-null, check this as if making a call to it with the given
36/// receiver type
37///
38/// \return true to indicate that there was an error and appropriate
39/// actions were taken
40bool Sema::checkInitMethod(ObjCMethodDecl *method,
41 QualType receiverTypeIfCall) {
42 if (method->isInvalidDecl()) return true;
43
44 // This castAs is safe: methods that don't return an object
45 // pointer won't be inferred as inits and will reject an explicit
46 // objc_method_family(init).
47
48 // We ignore protocols here. Should we? What about Class?
49
50 const ObjCObjectType *result = method->getResultType()
51 ->castAs<ObjCObjectPointerType>()->getObjectType();
52
53 if (result->isObjCId()) {
54 return false;
55 } else if (result->isObjCClass()) {
56 // fall through: always an error
57 } else {
58 ObjCInterfaceDecl *resultClass = result->getInterface();
59 assert(resultClass && "unexpected object type!");
60
61 // It's okay for the result type to still be a forward declaration
62 // if we're checking an interface declaration.
Douglas Gregor7723fec2011-12-15 20:29:51 +000063 if (!resultClass->hasDefinition()) {
John McCallf85e1932011-06-15 23:02:42 +000064 if (receiverTypeIfCall.isNull() &&
65 !isa<ObjCImplementationDecl>(method->getDeclContext()))
66 return false;
67
68 // Otherwise, we try to compare class types.
69 } else {
70 // If this method was declared in a protocol, we can't check
71 // anything unless we have a receiver type that's an interface.
72 const ObjCInterfaceDecl *receiverClass = 0;
73 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
74 if (receiverTypeIfCall.isNull())
75 return false;
76
77 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
78 ->getInterfaceDecl();
79
80 // This can be null for calls to e.g. id<Foo>.
81 if (!receiverClass) return false;
82 } else {
83 receiverClass = method->getClassInterface();
84 assert(receiverClass && "method not associated with a class!");
85 }
86
87 // If either class is a subclass of the other, it's fine.
88 if (receiverClass->isSuperClassOf(resultClass) ||
89 resultClass->isSuperClassOf(receiverClass))
90 return false;
91 }
92 }
93
94 SourceLocation loc = method->getLocation();
95
96 // If we're in a system header, and this is not a call, just make
97 // the method unusable.
98 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
99 method->addAttr(new (Context) UnavailableAttr(loc, Context,
100 "init method returns a type unrelated to its receiver type"));
101 return true;
102 }
103
104 // Otherwise, it's an error.
105 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
106 method->setInvalidDecl();
107 return true;
108}
109
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000110void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000111 const ObjCMethodDecl *Overridden,
112 bool IsImplementation) {
113 if (Overridden->hasRelatedResultType() &&
114 !NewMethod->hasRelatedResultType()) {
115 // This can only happen when the method follows a naming convention that
116 // implies a related result type, and the original (overridden) method has
117 // a suitable return type, but the new (overriding) method does not have
118 // a suitable return type.
119 QualType ResultType = NewMethod->getResultType();
120 SourceRange ResultTypeRange;
121 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000122 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000123 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
124
125 // Figure out which class this method is part of, if any.
126 ObjCInterfaceDecl *CurrentClass
127 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
128 if (!CurrentClass) {
129 DeclContext *DC = NewMethod->getDeclContext();
130 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
131 CurrentClass = Cat->getClassInterface();
132 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
133 CurrentClass = Impl->getClassInterface();
134 else if (ObjCCategoryImplDecl *CatImpl
135 = dyn_cast<ObjCCategoryImplDecl>(DC))
136 CurrentClass = CatImpl->getClassInterface();
137 }
138
139 if (CurrentClass) {
140 Diag(NewMethod->getLocation(),
141 diag::warn_related_result_type_compatibility_class)
142 << Context.getObjCInterfaceType(CurrentClass)
143 << ResultType
144 << ResultTypeRange;
145 } else {
146 Diag(NewMethod->getLocation(),
147 diag::warn_related_result_type_compatibility_protocol)
148 << ResultType
149 << ResultTypeRange;
150 }
151
Douglas Gregore97179c2011-09-08 01:46:34 +0000152 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
153 Diag(Overridden->getLocation(),
154 diag::note_related_result_type_overridden_family)
155 << Family;
156 else
157 Diag(Overridden->getLocation(),
158 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000159 }
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000160 if (getLangOptions().ObjCAutoRefCount) {
161 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
162 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
163 Diag(NewMethod->getLocation(),
164 diag::err_nsreturns_retained_attribute_mismatch) << 1;
165 Diag(Overridden->getLocation(), diag::note_previous_decl)
166 << "method";
167 }
168 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
169 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
170 Diag(NewMethod->getLocation(),
171 diag::err_nsreturns_retained_attribute_mismatch) << 0;
172 Diag(Overridden->getLocation(), diag::note_previous_decl)
173 << "method";
174 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000175 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin();
176 for (ObjCMethodDecl::param_iterator
177 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000178 ni != ne; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000179 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000180 ParmVarDecl *newDecl = (*ni);
181 if (newDecl->hasAttr<NSConsumedAttr>() !=
182 oldDecl->hasAttr<NSConsumedAttr>()) {
183 Diag(newDecl->getLocation(),
184 diag::err_nsconsumed_attribute_mismatch);
185 Diag(oldDecl->getLocation(), diag::note_previous_decl)
186 << "parameter";
187 }
188 }
189 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000190}
191
John McCallf85e1932011-06-15 23:02:42 +0000192/// \brief Check a method declaration for compatibility with the Objective-C
193/// ARC conventions.
194static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
195 ObjCMethodFamily family = method->getMethodFamily();
196 switch (family) {
197 case OMF_None:
198 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000199 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000200 case OMF_retain:
201 case OMF_release:
202 case OMF_autorelease:
203 case OMF_retainCount:
204 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000205 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000206 return false;
207
208 case OMF_init:
209 // If the method doesn't obey the init rules, don't bother annotating it.
210 if (S.checkInitMethod(method, QualType()))
211 return true;
212
213 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
214 S.Context));
215
216 // Don't add a second copy of this attribute, but otherwise don't
217 // let it be suppressed.
218 if (method->hasAttr<NSReturnsRetainedAttr>())
219 return false;
220 break;
221
222 case OMF_alloc:
223 case OMF_copy:
224 case OMF_mutableCopy:
225 case OMF_new:
226 if (method->hasAttr<NSReturnsRetainedAttr>() ||
227 method->hasAttr<NSReturnsNotRetainedAttr>() ||
228 method->hasAttr<NSReturnsAutoreleasedAttr>())
229 return false;
230 break;
231 }
232
233 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
234 S.Context));
235 return false;
236}
237
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000238static void DiagnoseObjCImplementedDeprecations(Sema &S,
239 NamedDecl *ND,
240 SourceLocation ImplLoc,
241 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000242 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000243 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000244 if (select == 0)
245 S.Diag(ND->getLocation(), diag::note_method_declared_at);
246 else
247 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
248 }
249}
250
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000251/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
252/// pool.
253void Sema::AddAnyMethodToGlobalPool(Decl *D) {
254 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
255
256 // If we don't have a valid method decl, simply return.
257 if (!MDecl)
258 return;
259 if (MDecl->isInstanceMethod())
260 AddInstanceMethodToGlobalPool(MDecl, true);
261 else
262 AddFactoryMethodToGlobalPool(MDecl, true);
263}
264
Steve Naroffebf64432009-02-28 16:59:13 +0000265/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000266/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000267void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000268 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000269 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Steve Naroff394f3f42008-07-25 17:57:26 +0000271 // If we don't have a valid method decl, simply return.
272 if (!MDecl)
273 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000274
Chris Lattner4d391482007-12-12 07:09:47 +0000275 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000276 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000277 PushFunctionScope();
278
Chris Lattner4d391482007-12-12 07:09:47 +0000279 // Create Decl objects for each parameter, entrring them in the scope for
280 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000281
282 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000283 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000284
Daniel Dunbar451318c2008-08-26 06:07:48 +0000285 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
286 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000287
Chris Lattner8123a952008-04-10 02:22:51 +0000288 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000289 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000290 E = MDecl->param_end(); PI != E; ++PI) {
291 ParmVarDecl *Param = (*PI);
292 if (!Param->isInvalidDecl() &&
293 RequireCompleteType(Param->getLocation(), Param->getType(),
294 diag::err_typecheck_decl_incomplete_type))
295 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000296 if ((*PI)->getIdentifier())
297 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000298 }
John McCallf85e1932011-06-15 23:02:42 +0000299
300 // In ARC, disallow definition of retain/release/autorelease/retainCount
301 if (getLangOptions().ObjCAutoRefCount) {
302 switch (MDecl->getMethodFamily()) {
303 case OMF_retain:
304 case OMF_retainCount:
305 case OMF_release:
306 case OMF_autorelease:
307 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
308 << MDecl->getSelector();
309 break;
310
311 case OMF_None:
312 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000313 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000314 case OMF_alloc:
315 case OMF_init:
316 case OMF_mutableCopy:
317 case OMF_copy:
318 case OMF_new:
319 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000320 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000321 break;
322 }
323 }
324
Nico Weber9a1ecf02011-08-22 17:25:57 +0000325 // Warn on deprecated methods under -Wdeprecated-implementations,
326 // and prepare for warning on missing super calls.
327 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000328 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000329 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000330 DiagnoseObjCImplementedDeprecations(*this,
331 dyn_cast<NamedDecl>(IMD),
332 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000333
Nico Weber80cb6e62011-08-28 22:35:17 +0000334 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000335 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
336 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
337 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000338 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000339 ObjCShouldCallSuperDealloc =
Ted Kremenek8cd8de42011-09-28 19:32:29 +0000340 !(Context.getLangOptions().ObjCAutoRefCount ||
341 Context.getLangOptions().getGC() == LangOptions::GCOnly) &&
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000342 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000343 ObjCShouldCallSuperFinalize =
Ted Kremenek8cd8de42011-09-28 19:32:29 +0000344 Context.getLangOptions().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000345 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000346 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000347 }
Chris Lattner4d391482007-12-12 07:09:47 +0000348}
349
John McCalld226f652010-08-21 09:40:31 +0000350Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000351ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
352 IdentifierInfo *ClassName, SourceLocation ClassLoc,
353 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000354 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000355 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000356 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000357 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattner4d391482007-12-12 07:09:47 +0000359 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000360 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000361 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000362
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000363 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000364 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000365 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000366 }
Mike Stump1eb44332009-09-09 15:08:12 +0000367
Douglas Gregor7723fec2011-12-15 20:29:51 +0000368 // Create a declaration to describe this @interface.
Douglas Gregor0af55012011-12-16 03:12:41 +0000369 ObjCInterfaceDecl* PrevIDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000370 ObjCInterfaceDecl *IDecl
371 = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc, ClassName,
Douglas Gregor0af55012011-12-16 03:12:41 +0000372 PrevIDecl, ClassLoc);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000373
Douglas Gregor7723fec2011-12-15 20:29:51 +0000374 if (PrevIDecl) {
375 // Class already seen. Was it a definition?
376 if (ObjCInterfaceDecl *Def = PrevIDecl->getDefinition()) {
377 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)
378 << PrevIDecl->getDeclName();
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000379 Diag(Def->getLocation(), diag::note_previous_definition);
Douglas Gregor7723fec2011-12-15 20:29:51 +0000380 IDecl->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000381 }
Chris Lattner4d391482007-12-12 07:09:47 +0000382 }
Douglas Gregor7723fec2011-12-15 20:29:51 +0000383
384 if (AttrList)
385 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
386 PushOnScopeChains(IDecl, TUScope);
Mike Stump1eb44332009-09-09 15:08:12 +0000387
Douglas Gregor7723fec2011-12-15 20:29:51 +0000388 // Start the definition of this class. If we're in a redefinition case, there
389 // may already be a definition, so we'll end up adding to it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000390 if (!IDecl->hasDefinition())
391 IDecl->startDefinition();
392
Chris Lattner4d391482007-12-12 07:09:47 +0000393 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000394 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000395 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
396 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000397
398 if (!PrevDecl) {
399 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000400 TypoCorrection Corrected = CorrectTypo(
401 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
402 NULL, NULL, false, CTC_NoKeywords);
403 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregor60ef3082011-12-15 00:29:59 +0000404 if (declaresSameEntity(PrevDecl, IDecl)) {
Douglas Gregora38c4732011-12-01 15:37:53 +0000405 // Don't correct to the class we're defining.
406 PrevDecl = 0;
407 } else {
408 Diag(SuperLoc, diag::err_undef_superclass_suggest)
409 << SuperName << ClassName << PrevDecl->getDeclName();
410 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
411 << PrevDecl->getDeclName();
412 }
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000413 }
414 }
415
Douglas Gregor60ef3082011-12-15 00:29:59 +0000416 if (declaresSameEntity(PrevDecl, IDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000417 Diag(SuperLoc, diag::err_recursive_superclass)
418 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000419 IDecl->setEndOfDefinitionLoc(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000420 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000421 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000422 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000423
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000424 // Diagnose classes that inherit from deprecated classes.
425 if (SuperClassDecl)
426 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000427
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000428 if (PrevDecl && SuperClassDecl == 0) {
429 // The previous declaration was not a class decl. Check if we have a
430 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000431 if (const TypedefNameDecl *TDecl =
432 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000433 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000434 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000435 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
436 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000437 }
438 }
Mike Stump1eb44332009-09-09 15:08:12 +0000439
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000440 // This handles the following case:
441 //
442 // typedef int SuperClass;
443 // @interface MyClass : SuperClass {} @end
444 //
445 if (!SuperClassDecl) {
446 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
447 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000448 }
449 }
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Richard Smith162e1c12011-04-15 14:24:37 +0000451 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000452 if (!SuperClassDecl)
453 Diag(SuperLoc, diag::err_undef_superclass)
454 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Douglas Gregorb3029962011-11-14 22:10:01 +0000455 else if (RequireCompleteType(SuperLoc,
456 Context.getObjCInterfaceType(SuperClassDecl),
457 PDiag(diag::err_forward_superclass)
458 << SuperClassDecl->getDeclName()
459 << ClassName
460 << SourceRange(AtInterfaceLoc, ClassLoc))) {
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000461 SuperClassDecl = 0;
462 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000463 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000464 IDecl->setSuperClass(SuperClassDecl);
465 IDecl->setSuperClassLoc(SuperLoc);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000466 IDecl->setEndOfDefinitionLoc(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000467 }
Chris Lattner4d391482007-12-12 07:09:47 +0000468 } else { // we have a root class.
Douglas Gregor05c272f2011-12-15 22:34:59 +0000469 IDecl->setEndOfDefinitionLoc(ClassLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000470 }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Sebastian Redl0b17c612010-08-13 00:28:03 +0000472 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000473 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000474 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000475 ProtoLocs, Context);
Douglas Gregor05c272f2011-12-15 22:34:59 +0000476 IDecl->setEndOfDefinitionLoc(EndProtoLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000477 }
Mike Stump1eb44332009-09-09 15:08:12 +0000478
Anders Carlsson15281452008-11-04 16:57:32 +0000479 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000480 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000481}
482
483/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000484/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000485Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
486 IdentifierInfo *AliasName,
487 SourceLocation AliasLocation,
488 IdentifierInfo *ClassName,
489 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000490 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000491 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000492 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000493 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000494 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000495 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000496 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000497 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000498 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000499 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000500 }
501 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000502 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000503 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000504 if (const TypedefNameDecl *TDecl =
505 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000506 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000507 if (T->isObjCObjectType()) {
508 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000509 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000510 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000511 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000512 }
513 }
514 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000515 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
516 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000517 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000518 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000519 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000520 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000523 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000524 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000525 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Anders Carlsson15281452008-11-04 16:57:32 +0000527 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000528 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000529
John McCalld226f652010-08-21 09:40:31 +0000530 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000531}
532
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000533bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000534 IdentifierInfo *PName,
535 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000536 const ObjCList<ObjCProtocolDecl> &PList) {
537
538 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000539 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
540 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000541 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
542 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000543 if (PDecl->getIdentifier() == PName) {
544 Diag(Ploc, diag::err_protocol_has_circular_dependency);
545 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000546 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000547 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000548
549 if (!PDecl->hasDefinition())
550 continue;
551
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000552 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
553 PDecl->getLocation(), PDecl->getReferencedProtocols()))
554 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000555 }
556 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000557 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000558}
559
John McCalld226f652010-08-21 09:40:31 +0000560Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000561Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
562 IdentifierInfo *ProtocolName,
563 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000564 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000565 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000566 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000567 SourceLocation EndProtoLoc,
568 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000569 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000570 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000571 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregor27c6da22012-01-01 20:30:41 +0000572 ObjCProtocolDecl *PrevDecl = LookupProtocol(ProtocolName, ProtocolLoc,
573 ForRedeclaration);
574 ObjCProtocolDecl *PDecl = 0;
575 if (ObjCProtocolDecl *Def = PrevDecl? PrevDecl->getDefinition() : 0) {
576 // If we already have a definition, complain.
577 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
578 Diag(Def->getLocation(), diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +0000579
Douglas Gregor27c6da22012-01-01 20:30:41 +0000580 // Create a new protocol that is completely distinct from previous
581 // declarations, and do not make this protocol available for name lookup.
582 // That way, we'll end up completely ignoring the duplicate.
583 // FIXME: Can we turn this into an error?
584 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
585 ProtocolLoc, AtProtoInterfaceLoc,
586 /*PrevDecl=*/0,
587 /*isForwardDecl=*/false);
588 PDecl->startDefinition();
589 } else {
590 if (PrevDecl) {
591 // Check for circular dependencies among protocol declarations. This can
592 // only happen if this protocol was forward-declared.
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000593 ObjCList<ObjCProtocolDecl> PList;
594 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
595 err = CheckForwardProtocolDeclarationForCircularDependency(
Douglas Gregor27c6da22012-01-01 20:30:41 +0000596 ProtocolName, ProtocolLoc, PrevDecl->getLocation(), PList);
Argyrios Kyrtzidis4fc04da2011-11-13 22:08:30 +0000597 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000598
599 // Create the new declaration.
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000600 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000601 ProtocolLoc, AtProtoInterfaceLoc,
Douglas Gregor27c6da22012-01-01 20:30:41 +0000602 /*PrevDecl=*/PrevDecl,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000603 /*isForwardDecl=*/false);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000604
Douglas Gregor6e378de2009-04-23 23:18:26 +0000605 PushOnScopeChains(PDecl, TUScope);
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000606 PDecl->startDefinition();
Chris Lattnercca59d72008-03-16 01:23:04 +0000607 }
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000608
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000609 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000610 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000611
612 // Merge attributes from previous declarations.
613 if (PrevDecl)
614 mergeDeclAttributes(PDecl, PrevDecl);
615
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000616 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000617 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000618 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
619 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000620 PDecl->setLocEnd(EndProtoLoc);
621 }
Mike Stump1eb44332009-09-09 15:08:12 +0000622
623 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000624 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000625}
626
627/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000628/// issues an error if they are not declared. It returns list of
629/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000630void
Chris Lattnere13b9592008-07-26 04:03:38 +0000631Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000632 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000633 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000634 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000635 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000636 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
637 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000638 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000639 TypoCorrection Corrected = CorrectTypo(
640 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
641 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
642 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000643 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000644 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000645 Diag(PDecl->getLocation(), diag::note_previous_decl)
646 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000647 }
648 }
649
650 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000651 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000652 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000653 continue;
654 }
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000656 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000657
658 // If this is a forward declaration and we are supposed to warn in this
659 // case, do it.
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +0000660 if (WarnOnDeclarations && !PDecl->hasDefinition())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000661 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000662 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000663 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000664 }
665}
666
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000667/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000668/// a class method in its extension.
669///
Mike Stump1eb44332009-09-09 15:08:12 +0000670void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000671 ObjCInterfaceDecl *ID) {
672 if (!ID)
673 return; // Possibly due to previous error
674
675 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000676 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
677 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000678 ObjCMethodDecl *MD = *i;
679 MethodMap[MD->getSelector()] = MD;
680 }
681
682 if (MethodMap.empty())
683 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000684 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
685 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000686 ObjCMethodDecl *Method = *i;
687 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
688 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
689 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
690 << Method->getDeclName();
691 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
692 }
693 }
694}
695
Chris Lattner58fe03b2009-04-12 08:43:13 +0000696/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000697Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000698Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000699 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000700 unsigned NumElts,
701 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000702 SmallVector<ObjCProtocolDecl*, 32> Protocols;
703 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000704
Chris Lattner4d391482007-12-12 07:09:47 +0000705 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000706 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregor27c6da22012-01-01 20:30:41 +0000707 ObjCProtocolDecl *PrevDecl = LookupProtocol(Ident, IdentList[i].second,
708 ForRedeclaration);
709 ObjCProtocolDecl *PDecl
710 = ObjCProtocolDecl::Create(Context, CurContext, Ident,
711 IdentList[i].second, AtProtocolLoc,
712 PrevDecl, /*isForwardDecl=*/true);
713
714 PushOnScopeChains(PDecl, TUScope);
715
Sebastian Redl0b17c612010-08-13 00:28:03 +0000716 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000717 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Douglas Gregor27c6da22012-01-01 20:30:41 +0000718 if (PrevDecl) {
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +0000719 if (ASTMutationListener *L = Context.getASTMutationListener())
720 L->UpdatedAttributeList(PDecl);
721 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000722 }
Douglas Gregor27c6da22012-01-01 20:30:41 +0000723
724 if (PrevDecl)
725 mergeDeclAttributes(PDecl, PrevDecl);
726
Chris Lattner4d391482007-12-12 07:09:47 +0000727 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000728 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000729 }
Mike Stump1eb44332009-09-09 15:08:12 +0000730
731 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000732 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000733 Protocols.data(), Protocols.size(),
734 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000735 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000736 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000737 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000738}
739
John McCalld226f652010-08-21 09:40:31 +0000740Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000741ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
742 IdentifierInfo *ClassName, SourceLocation ClassLoc,
743 IdentifierInfo *CategoryName,
744 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000745 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000746 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000747 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000748 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000749 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000750 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000751
752 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000753
754 if (!IDecl
755 || RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
756 PDiag(diag::err_category_forward_interface)
757 << (CategoryName == 0))) {
Ted Kremenek09b68972010-02-23 19:39:46 +0000758 // Create an invalid ObjCCategoryDecl to serve as context for
759 // the enclosing method declarations. We mark the decl invalid
760 // to make it clear that this isn't a valid AST.
761 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000762 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000763 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000764
765 if (!IDecl)
766 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000767 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000768 }
769
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000770 if (!CategoryName && IDecl->getImplementation()) {
771 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
772 Diag(IDecl->getImplementation()->getLocation(),
773 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000774 }
775
Fariborz Jahanian25760612010-02-15 21:55:26 +0000776 if (CategoryName) {
777 /// Check for duplicate interface declaration for this category
778 ObjCCategoryDecl *CDeclChain;
779 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
780 CDeclChain = CDeclChain->getNextClassCategory()) {
781 if (CDeclChain->getIdentifier() == CategoryName) {
782 // Class extensions can be declared multiple times.
783 Diag(CategoryLoc, diag::warn_dup_category_def)
784 << ClassName << CategoryName;
785 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
786 break;
787 }
Chris Lattner70f19542009-02-16 21:26:43 +0000788 }
789 }
Chris Lattner70f19542009-02-16 21:26:43 +0000790
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000791 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
792 ClassLoc, CategoryLoc, CategoryName, IDecl);
793 // FIXME: PushOnScopeChains?
794 CurContext->addDecl(CDecl);
795
Chris Lattner4d391482007-12-12 07:09:47 +0000796 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000797 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000798 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000799 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000800 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000801 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000802 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000803 }
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Anders Carlsson15281452008-11-04 16:57:32 +0000805 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000806 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000807}
808
809/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000810/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000811/// object.
John McCalld226f652010-08-21 09:40:31 +0000812Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000813 SourceLocation AtCatImplLoc,
814 IdentifierInfo *ClassName, SourceLocation ClassLoc,
815 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000816 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000817 ObjCCategoryDecl *CatIDecl = 0;
818 if (IDecl) {
819 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
820 if (!CatIDecl) {
821 // Category @implementation with no corresponding @interface.
822 // Create and install one.
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000823 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, AtCatImplLoc,
824 ClassLoc, CatLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000825 CatName, IDecl);
Argyrios Kyrtzidis37f40572011-11-23 20:27:26 +0000826 CatIDecl->setImplicit();
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000827 }
828 }
829
Mike Stump1eb44332009-09-09 15:08:12 +0000830 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000831 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
Argyrios Kyrtzidisc6994002011-12-09 00:31:40 +0000832 ClassLoc, AtCatImplLoc, CatLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000833 /// Check that class of this category is already completely declared.
Douglas Gregorb3029962011-11-14 22:10:01 +0000834 if (!IDecl) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000835 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000836 CDecl->setInvalidDecl();
Douglas Gregorb3029962011-11-14 22:10:01 +0000837 } else if (RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
838 diag::err_undef_interface)) {
839 CDecl->setInvalidDecl();
John McCall6c2c2502011-07-22 02:45:48 +0000840 }
Chris Lattner4d391482007-12-12 07:09:47 +0000841
Douglas Gregord0434102009-01-09 00:49:46 +0000842 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000843 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000844
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000845 // If the interface is deprecated/unavailable, warn/error about it.
846 if (IDecl)
847 DiagnoseUseOfDecl(IDecl, ClassLoc);
848
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000849 /// Check that CatName, category name, is not used in another implementation.
850 if (CatIDecl) {
851 if (CatIDecl->getImplementation()) {
852 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
853 << CatName;
854 Diag(CatIDecl->getImplementation()->getLocation(),
855 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000856 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000857 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000858 // Warn on implementating category of deprecated class under
859 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000860 DiagnoseObjCImplementedDeprecations(*this,
861 dyn_cast<NamedDecl>(IDecl),
862 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000863 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000864 }
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Anders Carlsson15281452008-11-04 16:57:32 +0000866 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000867 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000868}
869
John McCalld226f652010-08-21 09:40:31 +0000870Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000871 SourceLocation AtClassImplLoc,
872 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000873 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000874 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000875 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000876 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000877 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000878 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
879 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000880 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000881 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000882 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000883 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
Douglas Gregor0af55012011-12-16 03:12:41 +0000884 RequireCompleteType(ClassLoc, Context.getObjCInterfaceType(IDecl),
885 diag::warn_undef_interface);
Douglas Gregor95ff7422010-01-04 17:27:12 +0000886 } else {
887 // We did not find anything with the name ClassName; try to correct for
888 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000889 TypoCorrection Corrected = CorrectTypo(
890 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
891 NULL, NULL, false, CTC_NoKeywords);
892 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000893 // Suggest the (potentially) correct interface name. However, put the
894 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000895 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000896 // provide a code-modification hint or use the typo name for recovery,
897 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000898 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000899 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000900 << ClassName << CorrectedName;
901 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
902 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000903 IDecl = 0;
904 } else {
905 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
906 }
Chris Lattner4d391482007-12-12 07:09:47 +0000907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Chris Lattner4d391482007-12-12 07:09:47 +0000909 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000910 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000911 if (SuperClassname) {
912 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000913 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
914 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000915 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000916 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
917 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000918 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000919 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000920 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000921 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000922 Diag(SuperClassLoc, diag::err_undef_superclass)
923 << SuperClassname << ClassName;
Douglas Gregor60ef3082011-12-15 00:29:59 +0000924 else if (IDecl && !declaresSameEntity(IDecl->getSuperClass(), SDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +0000925 // This implementation and its interface do not have the same
926 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000927 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000928 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000929 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000930 }
931 }
932 }
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattner4d391482007-12-12 07:09:47 +0000934 if (!IDecl) {
935 // Legacy case of @implementation with no corresponding @interface.
936 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000937
Mike Stump390b4cc2009-05-16 07:39:55 +0000938 // FIXME: Do we support attributes on the @implementation? If so we should
939 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000940 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregor0af55012011-12-16 03:12:41 +0000941 ClassName, /*PrevDecl=*/0, ClassLoc,
942 true);
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000943 IDecl->startDefinition();
Douglas Gregor05c272f2011-12-15 22:34:59 +0000944 if (SDecl) {
945 IDecl->setSuperClass(SDecl);
946 IDecl->setSuperClassLoc(SuperClassLoc);
947 IDecl->setEndOfDefinitionLoc(SuperClassLoc);
948 } else {
949 IDecl->setEndOfDefinitionLoc(ClassLoc);
950 }
951
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000952 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000953 } else {
954 // Mark the interface as being completed, even if it was just as
955 // @class ....;
956 // declaration; the user cannot reopen it.
Douglas Gregor2e5c15b2011-12-15 05:27:12 +0000957 if (!IDecl->hasDefinition())
958 IDecl->startDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +0000959 }
Mike Stump1eb44332009-09-09 15:08:12 +0000960
961 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000962 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
963 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Anders Carlsson15281452008-11-04 16:57:32 +0000965 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000966 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Chris Lattner4d391482007-12-12 07:09:47 +0000968 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000969 if (IDecl->getImplementation()) {
970 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000971 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000972 Diag(IDecl->getImplementation()->getLocation(),
973 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000974 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000975 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000976 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000977 // Warn on implementating deprecated class under
978 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000979 DiagnoseObjCImplementedDeprecations(*this,
980 dyn_cast<NamedDecl>(IDecl),
981 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000982 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000983 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000984}
985
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000986void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
987 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000988 SourceLocation RBrace) {
989 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000990 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000991 if (!IDecl)
992 return;
993 /// Check case of non-existing @interface decl.
994 /// (legacy objective-c @implementation decl without an @interface decl).
995 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000996 if (IDecl->isImplicitInterfaceDecl()) {
Douglas Gregor05c272f2011-12-15 22:34:59 +0000997 IDecl->setEndOfDefinitionLoc(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000998 // Add ivar's to class's DeclContext.
999 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +00001000 ivars[i]->setLexicalDeclContext(ImpDecl);
1001 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +00001002 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +00001003 }
1004
Chris Lattner4d391482007-12-12 07:09:47 +00001005 return;
1006 }
1007 // If implementation has empty ivar list, just return.
1008 if (numIvars == 0)
1009 return;
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Chris Lattner4d391482007-12-12 07:09:47 +00001011 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001012 if (LangOpts.ObjCNonFragileABI2) {
1013 if (ImpDecl->getSuperClass())
1014 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
1015 for (unsigned i = 0; i < numIvars; i++) {
1016 ObjCIvarDecl* ImplIvar = ivars[i];
1017 if (const ObjCIvarDecl *ClsIvar =
1018 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
1019 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
1020 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
1021 continue;
1022 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +00001023 // Instance ivar to Implementation's DeclContext.
1024 ImplIvar->setLexicalDeclContext(ImpDecl);
1025 IDecl->makeDeclVisibleInContext(ImplIvar, false);
1026 ImpDecl->addDecl(ImplIvar);
1027 }
1028 return;
1029 }
Chris Lattner4d391482007-12-12 07:09:47 +00001030 // Check interface's Ivar list against those in the implementation.
1031 // names and types must match.
1032 //
Chris Lattner4d391482007-12-12 07:09:47 +00001033 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001034 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001035 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1036 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001037 ObjCIvarDecl* ImplIvar = ivars[j++];
1038 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001039 assert (ImplIvar && "missing implementation ivar");
1040 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Steve Naroffca331292009-03-03 14:49:36 +00001042 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001043 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001044 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001045 << ImplIvar->getIdentifier()
1046 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001047 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001048 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1049 ImplIvar->getBitWidthValue(Context) !=
1050 ClsIvar->getBitWidthValue(Context)) {
1051 Diag(ImplIvar->getBitWidth()->getLocStart(),
1052 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1053 Diag(ClsIvar->getBitWidth()->getLocStart(),
1054 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001055 }
Steve Naroffca331292009-03-03 14:49:36 +00001056 // Make sure the names are identical.
1057 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001058 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001059 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001060 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001061 }
1062 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Chris Lattner609e4c72007-12-12 18:11:49 +00001065 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001066 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001067 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001068 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001069}
1070
Steve Naroff3c2eb662008-02-10 21:38:56 +00001071void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001072 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001073 // No point warning no definition of method which is 'unavailable'.
1074 if (method->hasAttr<UnavailableAttr>())
1075 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001076 if (!IncompleteImpl) {
1077 Diag(ImpLoc, diag::warn_incomplete_impl);
1078 IncompleteImpl = true;
1079 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001080 if (DiagID == diag::warn_unimplemented_protocol_method)
1081 Diag(ImpLoc, DiagID) << method->getDeclName();
1082 else
1083 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001084}
1085
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001086/// Determines if type B can be substituted for type A. Returns true if we can
1087/// guarantee that anything that the user will do to an object of type A can
1088/// also be done to an object of type B. This is trivially true if the two
1089/// types are the same, or if B is a subclass of A. It becomes more complex
1090/// in cases where protocols are involved.
1091///
1092/// Object types in Objective-C describe the minimum requirements for an
1093/// object, rather than providing a complete description of a type. For
1094/// example, if A is a subclass of B, then B* may refer to an instance of A.
1095/// The principle of substitutability means that we may use an instance of A
1096/// anywhere that we may use an instance of B - it will implement all of the
1097/// ivars of B and all of the methods of B.
1098///
1099/// This substitutability is important when type checking methods, because
1100/// the implementation may have stricter type definitions than the interface.
1101/// The interface specifies minimum requirements, but the implementation may
1102/// have more accurate ones. For example, a method may privately accept
1103/// instances of B, but only publish that it accepts instances of A. Any
1104/// object passed to it will be type checked against B, and so will implicitly
1105/// by a valid A*. Similarly, a method may return a subclass of the class that
1106/// it is declared as returning.
1107///
1108/// This is most important when considering subclassing. A method in a
1109/// subclass must accept any object as an argument that its superclass's
1110/// implementation accepts. It may, however, accept a more general type
1111/// without breaking substitutability (i.e. you can still use the subclass
1112/// anywhere that you can use the superclass, but not vice versa). The
1113/// converse requirement applies to return types: the return type for a
1114/// subclass method must be a valid object of the kind that the superclass
1115/// advertises, but it may be specified more accurately. This avoids the need
1116/// for explicit down-casting by callers.
1117///
1118/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001119static bool isObjCTypeSubstitutable(ASTContext &Context,
1120 const ObjCObjectPointerType *A,
1121 const ObjCObjectPointerType *B,
1122 bool rejectId) {
1123 // Reject a protocol-unqualified id.
1124 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001125
1126 // If B is a qualified id, then A must also be a qualified id and it must
1127 // implement all of the protocols in B. It may not be a qualified class.
1128 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1129 // stricter definition so it is not substitutable for id<A>.
1130 if (B->isObjCQualifiedIdType()) {
1131 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001132 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1133 QualType(B,0),
1134 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001135 }
1136
1137 /*
1138 // id is a special type that bypasses type checking completely. We want a
1139 // warning when it is used in one place but not another.
1140 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1141
1142
1143 // If B is a qualified id, then A must also be a qualified id (which it isn't
1144 // if we've got this far)
1145 if (B->isObjCQualifiedIdType()) return false;
1146 */
1147
1148 // Now we know that A and B are (potentially-qualified) class types. The
1149 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001150 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001151}
1152
John McCall10302c02010-10-28 02:34:38 +00001153static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1154 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1155}
1156
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001157static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001158 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001159 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001160 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001161 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001162 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001163 if (IsProtocolMethodDecl &&
1164 (MethodDecl->getObjCDeclQualifier() !=
1165 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001166 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001167 S.Diag(MethodImpl->getLocation(),
1168 (IsOverridingMode ?
1169 diag::warn_conflicting_overriding_ret_type_modifiers
1170 : diag::warn_conflicting_ret_type_modifiers))
1171 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001172 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1173 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1174 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1175 }
1176 else
1177 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001178 }
1179
John McCall10302c02010-10-28 02:34:38 +00001180 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001181 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001182 return true;
1183 if (!Warn)
1184 return false;
John McCall10302c02010-10-28 02:34:38 +00001185
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001186 unsigned DiagID =
1187 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1188 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001189
1190 // Mismatches between ObjC pointers go into a different warning
1191 // category, and sometimes they're even completely whitelisted.
1192 if (const ObjCObjectPointerType *ImplPtrTy =
1193 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1194 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001195 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001196 // Allow non-matching return types as long as they don't violate
1197 // the principle of substitutability. Specifically, we permit
1198 // return types that are subclasses of the declared return type,
1199 // or that are more-qualified versions of the declared type.
1200 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001201 return false;
John McCall10302c02010-10-28 02:34:38 +00001202
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001203 DiagID =
1204 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1205 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001206 }
1207 }
1208
1209 S.Diag(MethodImpl->getLocation(), DiagID)
1210 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001211 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001212 << MethodImpl->getResultType()
1213 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001214 S.Diag(MethodDecl->getLocation(),
1215 IsOverridingMode ? diag::note_previous_declaration
1216 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001217 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001218 return false;
John McCall10302c02010-10-28 02:34:38 +00001219}
1220
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001221static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001222 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001223 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001224 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001225 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001226 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001227 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001228 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001229 if (IsProtocolMethodDecl &&
1230 (ImplVar->getObjCDeclQualifier() !=
1231 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001232 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001233 if (IsOverridingMode)
1234 S.Diag(ImplVar->getLocation(),
1235 diag::warn_conflicting_overriding_param_modifiers)
1236 << getTypeRange(ImplVar->getTypeSourceInfo())
1237 << MethodImpl->getDeclName();
1238 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001239 diag::warn_conflicting_param_modifiers)
1240 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001241 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001242 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1243 << getTypeRange(IfaceVar->getTypeSourceInfo());
1244 }
1245 else
1246 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001247 }
1248
John McCall10302c02010-10-28 02:34:38 +00001249 QualType ImplTy = ImplVar->getType();
1250 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001251
John McCall10302c02010-10-28 02:34:38 +00001252 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001253 return true;
1254
1255 if (!Warn)
1256 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001257 unsigned DiagID =
1258 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1259 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001260
1261 // Mismatches between ObjC pointers go into a different warning
1262 // category, and sometimes they're even completely whitelisted.
1263 if (const ObjCObjectPointerType *ImplPtrTy =
1264 ImplTy->getAs<ObjCObjectPointerType>()) {
1265 if (const ObjCObjectPointerType *IfacePtrTy =
1266 IfaceTy->getAs<ObjCObjectPointerType>()) {
1267 // Allow non-matching argument types as long as they don't
1268 // violate the principle of substitutability. Specifically, the
1269 // implementation must accept any objects that the superclass
1270 // accepts, however it may also accept others.
1271 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001272 return false;
John McCall10302c02010-10-28 02:34:38 +00001273
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001274 DiagID =
1275 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1276 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001277 }
1278 }
1279
1280 S.Diag(ImplVar->getLocation(), DiagID)
1281 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001282 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1283 S.Diag(IfaceVar->getLocation(),
1284 (IsOverridingMode ? diag::note_previous_declaration
1285 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001286 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001287 return false;
John McCall10302c02010-10-28 02:34:38 +00001288}
John McCallf85e1932011-06-15 23:02:42 +00001289
1290/// In ARC, check whether the conventional meanings of the two methods
1291/// match. If they don't, it's a hard error.
1292static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1293 ObjCMethodDecl *decl) {
1294 ObjCMethodFamily implFamily = impl->getMethodFamily();
1295 ObjCMethodFamily declFamily = decl->getMethodFamily();
1296 if (implFamily == declFamily) return false;
1297
1298 // Since conventions are sorted by selector, the only possibility is
1299 // that the types differ enough to cause one selector or the other
1300 // to fall out of the family.
1301 assert(implFamily == OMF_None || declFamily == OMF_None);
1302
1303 // No further diagnostics required on invalid declarations.
1304 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1305
1306 const ObjCMethodDecl *unmatched = impl;
1307 ObjCMethodFamily family = declFamily;
1308 unsigned errorID = diag::err_arc_lost_method_convention;
1309 unsigned noteID = diag::note_arc_lost_method_convention;
1310 if (declFamily == OMF_None) {
1311 unmatched = decl;
1312 family = implFamily;
1313 errorID = diag::err_arc_gained_method_convention;
1314 noteID = diag::note_arc_gained_method_convention;
1315 }
1316
1317 // Indexes into a %select clause in the diagnostic.
1318 enum FamilySelector {
1319 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1320 };
1321 FamilySelector familySelector = FamilySelector();
1322
1323 switch (family) {
1324 case OMF_None: llvm_unreachable("logic error, no method convention");
1325 case OMF_retain:
1326 case OMF_release:
1327 case OMF_autorelease:
1328 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001329 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001330 case OMF_retainCount:
1331 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001332 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001333 // Mismatches for these methods don't change ownership
1334 // conventions, so we don't care.
1335 return false;
1336
1337 case OMF_init: familySelector = F_init; break;
1338 case OMF_alloc: familySelector = F_alloc; break;
1339 case OMF_copy: familySelector = F_copy; break;
1340 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1341 case OMF_new: familySelector = F_new; break;
1342 }
1343
1344 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1345 ReasonSelector reasonSelector;
1346
1347 // The only reason these methods don't fall within their families is
1348 // due to unusual result types.
1349 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1350 reasonSelector = R_UnrelatedReturn;
1351 } else {
1352 reasonSelector = R_NonObjectReturn;
1353 }
1354
1355 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1356 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1357
1358 return true;
1359}
John McCall10302c02010-10-28 02:34:38 +00001360
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001361void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001362 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001363 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001364 if (getLangOptions().ObjCAutoRefCount &&
1365 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1366 return;
1367
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001368 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001369 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001370 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001371
Chris Lattner3aff9192009-04-11 19:58:42 +00001372 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001373 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001374 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001375 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001376 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001377 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001378
Fariborz Jahanian21121902011-08-08 18:03:17 +00001379 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001380 Diag(ImpMethodDecl->getLocation(),
1381 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001382 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001383 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001384}
1385
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001386void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1387 ObjCMethodDecl *Overridden,
1388 bool IsProtocolMethodDecl) {
1389
1390 CheckMethodOverrideReturn(*this, Method, Overridden,
1391 IsProtocolMethodDecl, true,
1392 true);
1393
1394 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
1395 IF = Overridden->param_begin(), EM = Method->param_end();
1396 IM != EM; ++IM, ++IF) {
1397 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1398 IsProtocolMethodDecl, true, true);
1399 }
1400
1401 if (Method->isVariadic() != Overridden->isVariadic()) {
1402 Diag(Method->getLocation(),
1403 diag::warn_conflicting_overriding_variadic);
1404 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1405 }
1406}
1407
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001408/// WarnExactTypedMethods - This routine issues a warning if method
1409/// implementation declaration matches exactly that of its declaration.
1410void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1411 ObjCMethodDecl *MethodDecl,
1412 bool IsProtocolMethodDecl) {
1413 // don't issue warning when protocol method is optional because primary
1414 // class is not required to implement it and it is safe for protocol
1415 // to implement it.
1416 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1417 return;
1418 // don't issue warning when primary class's method is
1419 // depecated/unavailable.
1420 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1421 MethodDecl->hasAttr<DeprecatedAttr>())
1422 return;
1423
1424 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1425 IsProtocolMethodDecl, false, false);
1426 if (match)
1427 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1428 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1429 IM != EM; ++IM, ++IF) {
1430 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1431 *IM, *IF,
1432 IsProtocolMethodDecl, false, false);
1433 if (!match)
1434 break;
1435 }
1436 if (match)
1437 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001438 if (match)
1439 match = !(MethodDecl->isClassMethod() &&
1440 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001441
1442 if (match) {
1443 Diag(ImpMethodDecl->getLocation(),
1444 diag::warn_category_method_impl_match);
1445 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1446 }
1447}
1448
Mike Stump390b4cc2009-05-16 07:39:55 +00001449/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1450/// improve the efficiency of selector lookups and type checking by associating
1451/// with each protocol / interface / category the flattened instance tables. If
1452/// we used an immutable set to keep the table then it wouldn't add significant
1453/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001454
Steve Naroffefe7f362008-02-08 22:06:17 +00001455/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001456/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001457void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1458 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001459 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001460 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001461 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001462 ObjCContainerDecl *CDecl) {
1463 ObjCInterfaceDecl *IDecl;
1464 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1465 IDecl = C->getClassInterface();
1466 else
1467 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1468 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1469
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001470 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001471 ObjCInterfaceDecl *NSIDecl = 0;
1472 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001473 // check to see if class implements forwardInvocation method and objects
1474 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001475 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001476 // Under such conditions, which means that every method possible is
1477 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001478 // found" warnings.
1479 // FIXME: Use a general GetUnarySelector method for this.
1480 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1481 Selector fISelector = Context.Selectors.getSelector(1, &II);
1482 if (InsMap.count(fISelector))
1483 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1484 // need be implemented in the implementation.
1485 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1486 }
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001488 // If a method lookup fails locally we still need to look and see if
1489 // the method was implemented by a base class or an inherited
1490 // protocol. This lookup is slow, but occurs rarely in correct code
1491 // and otherwise would terminate in a warning.
1492
Chris Lattner4d391482007-12-12 07:09:47 +00001493 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001494 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001495 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001496 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001497 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001498 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001499 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001500 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001501 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001502 // Ugly, but necessary. Method declared in protcol might have
1503 // have been synthesized due to a property declared in the class which
1504 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001505 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001506 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001507 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001508 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001509 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
David Blaikied6471f72011-09-25 23:23:43 +00001510 != DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001511 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001512 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001513 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1514 << PDecl->getDeclName();
1515 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001516 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001517 }
1518 }
Chris Lattner4d391482007-12-12 07:09:47 +00001519 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001520 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001521 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001522 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001523 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001524 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1525 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001526 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001527 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001528 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1529 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001530 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001531 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001532 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1533 PDecl->getDeclName();
1534 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001535 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001536 }
Chris Lattner780f3292008-07-21 21:32:27 +00001537 // Check on this protocols's referenced protocols, recursively.
1538 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1539 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001540 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001541}
1542
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001543/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001544/// or protocol against those declared in their implementations.
1545///
1546void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1547 const llvm::DenseSet<Selector> &ClsMap,
1548 llvm::DenseSet<Selector> &InsMapSeen,
1549 llvm::DenseSet<Selector> &ClsMapSeen,
1550 ObjCImplDecl* IMPDecl,
1551 ObjCContainerDecl* CDecl,
1552 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001553 bool ImmediateClass,
1554 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001555 // Check and see if instance methods in class interface have been
1556 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001557 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1558 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001559 if (InsMapSeen.count((*I)->getSelector()))
1560 continue;
1561 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001562 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001563 !InsMap.count((*I)->getSelector())) {
1564 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001565 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1566 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001567 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001568 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001569 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001570 IMPDecl->getInstanceMethod((*I)->getSelector());
1571 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1572 "Expected to find the method through lookup as well");
1573 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001574 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001575 if (ImpMethodDecl) {
1576 if (!WarnExactMatch)
1577 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1578 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001579 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001580 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1581 isa<ObjCProtocolDecl>(CDecl));
1582 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001583 }
1584 }
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001586 // Check and see if class methods in class interface have been
1587 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001588 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001589 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001590 if (ClsMapSeen.count((*I)->getSelector()))
1591 continue;
1592 ClsMapSeen.insert((*I)->getSelector());
1593 if (!ClsMap.count((*I)->getSelector())) {
1594 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001595 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1596 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001597 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001598 ObjCMethodDecl *ImpMethodDecl =
1599 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001600 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1601 "Expected to find the method through lookup as well");
1602 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001603 if (!WarnExactMatch)
1604 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1605 isa<ObjCProtocolDecl>(CDecl));
1606 else
1607 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1608 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001609 }
1610 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001611
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001612 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001613 // Also methods in class extensions need be looked at next.
1614 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1615 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1616 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1617 IMPDecl,
1618 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001619 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001620
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001621 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001622 for (ObjCInterfaceDecl::all_protocol_iterator
1623 PI = I->all_referenced_protocol_begin(),
1624 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001625 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1626 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001627 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001628
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001629 // FIXME. For now, we are not checking for extact match of methods
1630 // in category implementation and its primary class's super class.
1631 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001632 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001633 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001634 I->getSuperClass(), IncompleteImpl, false);
1635 }
1636}
1637
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001638/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1639/// category matches with those implemented in its primary class and
1640/// warns each time an exact match is found.
1641void Sema::CheckCategoryVsClassMethodMatches(
1642 ObjCCategoryImplDecl *CatIMPDecl) {
1643 llvm::DenseSet<Selector> InsMap, ClsMap;
1644
1645 for (ObjCImplementationDecl::instmeth_iterator
1646 I = CatIMPDecl->instmeth_begin(),
1647 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1648 InsMap.insert((*I)->getSelector());
1649
1650 for (ObjCImplementationDecl::classmeth_iterator
1651 I = CatIMPDecl->classmeth_begin(),
1652 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1653 ClsMap.insert((*I)->getSelector());
1654 if (InsMap.empty() && ClsMap.empty())
1655 return;
1656
1657 // Get category's primary class.
1658 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1659 if (!CatDecl)
1660 return;
1661 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1662 if (!IDecl)
1663 return;
1664 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1665 bool IncompleteImpl = false;
1666 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1667 CatIMPDecl, IDecl,
1668 IncompleteImpl, false, true /*WarnExactMatch*/);
1669}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001670
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001671void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001672 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001673 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001674 llvm::DenseSet<Selector> InsMap;
1675 // Check and see if instance methods in class interface have been
1676 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001677 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001678 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001679 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001681 // Check and see if properties declared in the interface have either 1)
1682 // an implementation or 2) there is a @synthesize/@dynamic implementation
1683 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001684 if (isa<ObjCInterfaceDecl>(CDecl) &&
1685 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001686 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001687
Chris Lattner4d391482007-12-12 07:09:47 +00001688 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001689 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001690 I = IMPDecl->classmeth_begin(),
1691 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001692 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001693
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001694 // Check for type conflict of methods declared in a class/protocol and
1695 // its implementation; if any.
1696 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001697 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1698 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001699 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001700
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001701 // check all methods implemented in category against those declared
1702 // in its primary class.
1703 if (ObjCCategoryImplDecl *CatDecl =
1704 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1705 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Chris Lattner4d391482007-12-12 07:09:47 +00001707 // Check the protocol list for unimplemented methods in the @implementation
1708 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001709 // Check and see if class methods in class interface have been
1710 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Chris Lattnercddc8882009-03-01 00:56:52 +00001712 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001713 for (ObjCInterfaceDecl::all_protocol_iterator
1714 PI = I->all_referenced_protocol_begin(),
1715 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001716 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001717 InsMap, ClsMap, I);
1718 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001719 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1720 Categories; Categories = Categories->getNextClassExtension())
1721 ImplMethodsVsClassMethods(S, IMPDecl,
1722 const_cast<ObjCCategoryDecl*>(Categories),
1723 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001724 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001725 // For extended class, unimplemented methods in its protocols will
1726 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001727 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001728 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1729 E = C->protocol_end(); PI != E; ++PI)
1730 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001731 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001732 // Report unimplemented properties in the category as well.
1733 // When reporting on missing setter/getters, do not report when
1734 // setter/getter is implemented in category's primary class
1735 // implementation.
1736 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1737 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1738 for (ObjCImplementationDecl::instmeth_iterator
1739 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1740 InsMap.insert((*I)->getSelector());
1741 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001742 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001743 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001744 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001745 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001746}
1747
Mike Stump1eb44332009-09-09 15:08:12 +00001748/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001749Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001750Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001751 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001752 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001753 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001754 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001755 for (unsigned i = 0; i != NumElts; ++i) {
1756 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001757 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001758 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001759 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001760 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001761 // Maybe we will complain about the shadowed template parameter.
1762 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1763 // Just pretend that we didn't see the previous declaration.
1764 PrevDecl = 0;
1765 }
1766
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001767 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001768 // GCC apparently allows the following idiom:
1769 //
1770 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1771 // @class XCElementToggler;
1772 //
Mike Stump1eb44332009-09-09 15:08:12 +00001773 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001774 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001775 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001776 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001777 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001778 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001779 // a forward class declaration matching a typedef name of a class refers
1780 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001781 if (const ObjCObjectType *OI =
1782 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1783 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001784 }
Chris Lattner4d391482007-12-12 07:09:47 +00001785 }
Douglas Gregor7723fec2011-12-15 20:29:51 +00001786
1787 // Create a declaration to describe this forward declaration.
Douglas Gregor0af55012011-12-16 03:12:41 +00001788 ObjCInterfaceDecl *PrevIDecl
1789 = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001790 ObjCInterfaceDecl *IDecl
1791 = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
Douglas Gregor375bb142011-12-27 22:43:10 +00001792 IdentList[i], PrevIDecl, IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001793 IDecl->setAtEndRange(IdentLocs[i]);
Douglas Gregor7723fec2011-12-15 20:29:51 +00001794
Douglas Gregor7723fec2011-12-15 20:29:51 +00001795 PushOnScopeChains(IDecl, TUScope);
Douglas Gregor375bb142011-12-27 22:43:10 +00001796 CheckObjCDeclScope(IDecl);
1797 DeclsInGroup.push_back(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001798 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001799
1800 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001801}
1802
John McCall0f4c4c42011-06-16 01:15:19 +00001803static bool tryMatchRecordTypes(ASTContext &Context,
1804 Sema::MethodMatchStrategy strategy,
1805 const Type *left, const Type *right);
1806
John McCallf85e1932011-06-15 23:02:42 +00001807static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1808 QualType leftQT, QualType rightQT) {
1809 const Type *left =
1810 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1811 const Type *right =
1812 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1813
1814 if (left == right) return true;
1815
1816 // If we're doing a strict match, the types have to match exactly.
1817 if (strategy == Sema::MMS_strict) return false;
1818
1819 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1820
1821 // Otherwise, use this absurdly complicated algorithm to try to
1822 // validate the basic, low-level compatibility of the two types.
1823
1824 // As a minimum, require the sizes and alignments to match.
1825 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1826 return false;
1827
1828 // Consider all the kinds of non-dependent canonical types:
1829 // - functions and arrays aren't possible as return and parameter types
1830
1831 // - vector types of equal size can be arbitrarily mixed
1832 if (isa<VectorType>(left)) return isa<VectorType>(right);
1833 if (isa<VectorType>(right)) return false;
1834
1835 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001836 // - structs, unions, and Objective-C objects must match more-or-less
1837 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001838 // - everything else should be a scalar
1839 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001840 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001841
John McCall1d9b3b22011-09-09 05:25:32 +00001842 // Make scalars agree in kind, except count bools as chars, and group
1843 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001844 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1845 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1846 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1847 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001848 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1849 leftSK = Type::STK_ObjCObjectPointer;
1850 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1851 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001852
1853 // Note that data member pointers and function member pointers don't
1854 // intermix because of the size differences.
1855
1856 return (leftSK == rightSK);
1857}
Chris Lattner4d391482007-12-12 07:09:47 +00001858
John McCall0f4c4c42011-06-16 01:15:19 +00001859static bool tryMatchRecordTypes(ASTContext &Context,
1860 Sema::MethodMatchStrategy strategy,
1861 const Type *lt, const Type *rt) {
1862 assert(lt && rt && lt != rt);
1863
1864 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1865 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1866 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1867
1868 // Require union-hood to match.
1869 if (left->isUnion() != right->isUnion()) return false;
1870
1871 // Require an exact match if either is non-POD.
1872 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1873 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1874 return false;
1875
1876 // Require size and alignment to match.
1877 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1878
1879 // Require fields to match.
1880 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1881 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1882 for (; li != le && ri != re; ++li, ++ri) {
1883 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1884 return false;
1885 }
1886 return (li == le && ri == re);
1887}
1888
Chris Lattner4d391482007-12-12 07:09:47 +00001889/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1890/// returns true, or false, accordingly.
1891/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001892bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1893 const ObjCMethodDecl *right,
1894 MethodMatchStrategy strategy) {
1895 if (!matchTypes(Context, strategy,
1896 left->getResultType(), right->getResultType()))
1897 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001898
John McCallf85e1932011-06-15 23:02:42 +00001899 if (getLangOptions().ObjCAutoRefCount &&
1900 (left->hasAttr<NSReturnsRetainedAttr>()
1901 != right->hasAttr<NSReturnsRetainedAttr>() ||
1902 left->hasAttr<NSConsumesSelfAttr>()
1903 != right->hasAttr<NSConsumesSelfAttr>()))
1904 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001905
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001906 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001907 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001908
John McCallf85e1932011-06-15 23:02:42 +00001909 for (; li != le; ++li, ++ri) {
1910 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001911 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001912
1913 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1914 return false;
1915
1916 if (getLangOptions().ObjCAutoRefCount &&
1917 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1918 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001919 }
1920 return true;
1921}
1922
Sebastian Redldb9d2142010-08-02 23:18:59 +00001923/// \brief Read the contents of the method pool for a given selector from
1924/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001925///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001926/// This routine should only be called once, when the method pool has no entry
1927/// for this selector.
1928Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001929 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001930 assert(MethodPool.find(Sel) == MethodPool.end() &&
1931 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001932
1933 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001934 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Sebastian Redldb9d2142010-08-02 23:18:59 +00001936 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001937}
1938
Sebastian Redldb9d2142010-08-02 23:18:59 +00001939void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1940 bool instance) {
1941 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1942 if (Pos == MethodPool.end()) {
1943 if (ExternalSource)
1944 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001945 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001946 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1947 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001948 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001949 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001950 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001951 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001952 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001953 Entry.Method = Method;
1954 Entry.Next = 0;
1955 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001956 }
Mike Stump1eb44332009-09-09 15:08:12 +00001957
Chris Lattnerb25df352009-03-04 05:16:45 +00001958 // We've seen a method with this name, see if we have already seen this type
1959 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001960 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1961 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1962
1963 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001964 ObjCMethodDecl *PrevObjCMethod = List->Method;
1965 PrevObjCMethod->setDefined(impl);
1966 // If a method is deprecated, push it in the global pool.
1967 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001968 if (Method->isDeprecated()) {
1969 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001970 List->Method = Method;
1971 }
1972 // If new method is unavailable, push it into global pool
1973 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001974 if (Method->isUnavailable()) {
1975 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001976 List->Method = Method;
1977 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001978 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001979 }
John McCallf85e1932011-06-15 23:02:42 +00001980 }
Mike Stump1eb44332009-09-09 15:08:12 +00001981
Chris Lattnerb25df352009-03-04 05:16:45 +00001982 // We have a new signature for an existing method - add it.
1983 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001984 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1985 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001986}
1987
John McCallf85e1932011-06-15 23:02:42 +00001988/// Determines if this is an "acceptable" loose mismatch in the global
1989/// method pool. This exists mostly as a hack to get around certain
1990/// global mismatches which we can't afford to make warnings / errors.
1991/// Really, what we want is a way to take a method out of the global
1992/// method pool.
1993static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1994 ObjCMethodDecl *other) {
1995 if (!chosen->isInstanceMethod())
1996 return false;
1997
1998 Selector sel = chosen->getSelector();
1999 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
2000 return false;
2001
2002 // Don't complain about mismatches for -length if the method we
2003 // chose has an integral result type.
2004 return (chosen->getResultType()->isIntegerType());
2005}
2006
Sebastian Redldb9d2142010-08-02 23:18:59 +00002007ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002008 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00002009 bool warn, bool instance) {
2010 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2011 if (Pos == MethodPool.end()) {
2012 if (ExternalSource)
2013 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002014 else
2015 return 0;
2016 }
2017
Sebastian Redldb9d2142010-08-02 23:18:59 +00002018 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00002019
Sebastian Redldb9d2142010-08-02 23:18:59 +00002020 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00002021 bool issueDiagnostic = false, issueError = false;
2022
2023 // We support a warning which complains about *any* difference in
2024 // method signature.
2025 bool strictSelectorMatch =
2026 (receiverIdOrClass && warn &&
2027 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2028 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002029 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002030 if (strictSelectorMatch)
2031 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002032 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2033 MMS_strict)) {
2034 issueDiagnostic = true;
2035 break;
2036 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002037 }
2038
John McCallf85e1932011-06-15 23:02:42 +00002039 // If we didn't see any strict differences, we won't see any loose
2040 // differences. In ARC, however, we also need to check for loose
2041 // mismatches, because most of them are errors.
2042 if (!strictSelectorMatch ||
2043 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002044 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002045 // This checks if the methods differ in type mismatch.
2046 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2047 MMS_loose) &&
2048 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2049 issueDiagnostic = true;
2050 if (getLangOptions().ObjCAutoRefCount)
2051 issueError = true;
2052 break;
2053 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002054 }
2055
John McCallf85e1932011-06-15 23:02:42 +00002056 if (issueDiagnostic) {
2057 if (issueError)
2058 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2059 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002060 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2061 else
2062 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002063
2064 Diag(MethList.Method->getLocStart(),
2065 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002066 << MethList.Method->getSourceRange();
2067 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2068 Diag(Next->Method->getLocStart(), diag::note_also_found)
2069 << Next->Method->getSourceRange();
2070 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002071 }
2072 return MethList.Method;
2073}
2074
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002075ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002076 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2077 if (Pos == MethodPool.end())
2078 return 0;
2079
2080 GlobalMethods &Methods = Pos->second;
2081
2082 if (Methods.first.Method && Methods.first.Method->isDefined())
2083 return Methods.first.Method;
2084 if (Methods.second.Method && Methods.second.Method->isDefined())
2085 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002086 return 0;
2087}
2088
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002089/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2090/// identical selector names in current and its super classes and issues
2091/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002092void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2093 ObjCMethodDecl *Method,
2094 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002095 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2096 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002097
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002098 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002099 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002100 SD->lookupMethod(Method->getSelector(), IsInstance);
2101 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002102 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002103 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002104 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002105 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2106 E = Method->param_end();
2107 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2108 for (; ParamI != E; ++ParamI, ++PrevI) {
2109 // Number of parameters are the same and is guaranteed by selector match.
2110 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2111 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2112 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002113 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002114 // respective argument type in the super class method, issue warning;
2115 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002116 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002117 << T1 << T2;
2118 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2119 return;
2120 }
2121 }
2122 ID = SD;
2123 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002124}
2125
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002126/// DiagnoseDuplicateIvars -
2127/// Check for duplicate ivars in the entire class at the start of
2128/// @implementation. This becomes necesssary because class extension can
2129/// add ivars to a class in random order which will not be known until
2130/// class's @implementation is seen.
2131void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2132 ObjCInterfaceDecl *SID) {
2133 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2134 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2135 ObjCIvarDecl* Ivar = (*IVI);
2136 if (Ivar->isInvalidDecl())
2137 continue;
2138 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2139 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2140 if (prevIvar) {
2141 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2142 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2143 Ivar->setInvalidDecl();
2144 }
2145 }
2146 }
2147}
2148
Erik Verbruggend64251f2011-12-06 09:25:23 +00002149Sema::ObjCContainerKind Sema::getObjCContainerKind() const {
2150 switch (CurContext->getDeclKind()) {
2151 case Decl::ObjCInterface:
2152 return Sema::OCK_Interface;
2153 case Decl::ObjCProtocol:
2154 return Sema::OCK_Protocol;
2155 case Decl::ObjCCategory:
2156 if (dyn_cast<ObjCCategoryDecl>(CurContext)->IsClassExtension())
2157 return Sema::OCK_ClassExtension;
2158 else
2159 return Sema::OCK_Category;
2160 case Decl::ObjCImplementation:
2161 return Sema::OCK_Implementation;
2162 case Decl::ObjCCategoryImpl:
2163 return Sema::OCK_CategoryImplementation;
2164
2165 default:
2166 return Sema::OCK_None;
2167 }
2168}
2169
Steve Naroffa56f6162007-12-18 01:30:32 +00002170// Note: For class/category implemenations, allMethods/allProperties is
2171// always null.
Erik Verbruggend64251f2011-12-06 09:25:23 +00002172Decl *Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
2173 Decl **allMethods, unsigned allNum,
2174 Decl **allProperties, unsigned pNum,
2175 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002176
Erik Verbruggend64251f2011-12-06 09:25:23 +00002177 if (getObjCContainerKind() == Sema::OCK_None)
2178 return 0;
2179
2180 assert(AtEnd.isValid() && "Invalid location for '@end'");
2181
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002182 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2183 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002184
Mike Stump1eb44332009-09-09 15:08:12 +00002185 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002186 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2187 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002188 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002189
Steve Naroff0701bbb2009-01-08 17:28:14 +00002190 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2191 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2192 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2193
Chris Lattner4d391482007-12-12 07:09:47 +00002194 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002195 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002196 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002197
2198 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002199 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002200 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002201 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002202 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002203 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002204 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002205 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002206 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002207 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002208 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002209 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002210 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002211 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002212 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002213 if (!Context.getSourceManager().isInSystemHeader(
2214 Method->getLocation()))
2215 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2216 << Method->getDeclName();
2217 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2218 }
Chris Lattner4d391482007-12-12 07:09:47 +00002219 InsMap[Method->getSelector()] = Method;
2220 /// The following allows us to typecheck messages to "id".
2221 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002222 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002223 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002224 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002225 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002226 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002227 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002228 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002229 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002230 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002231 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002232 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002233 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002234 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002235 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002236 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002237 } else {
Fariborz Jahanian72096462011-12-13 19:40:34 +00002238 if (PrevMethod) {
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002239 Method->setAsRedeclaration(PrevMethod);
Fariborz Jahanian72096462011-12-13 19:40:34 +00002240 if (!Context.getSourceManager().isInSystemHeader(
2241 Method->getLocation()))
2242 Diag(Method->getLocation(), diag::warn_duplicate_method_decl)
2243 << Method->getDeclName();
2244 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
2245 }
Chris Lattner4d391482007-12-12 07:09:47 +00002246 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002247 /// The following allows us to typecheck messages to "Class".
2248 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002249 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002250 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002251 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002252 }
2253 }
2254 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002255 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002256 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002257 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002258 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002259 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002260 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002261 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002262 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002263 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002264
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002265 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002266 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002267 if (C->IsClassExtension()) {
2268 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2269 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002270 }
Chris Lattner4d391482007-12-12 07:09:47 +00002271 }
Steve Naroff09c47192009-01-09 15:36:25 +00002272 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002273 if (CDecl->getIdentifier())
2274 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2275 // user-defined setter/getter. It also synthesizes setter/getter methods
2276 // and adds them to the DeclContext and global method pools.
2277 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2278 E = CDecl->prop_end();
2279 I != E; ++I)
2280 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002281 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002282 }
2283 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002284 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002285 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002286 // Any property declared in a class extension might have user
2287 // declared setter or getter in current class extension or one
2288 // of the other class extensions. Mark them as synthesized as
2289 // property will be synthesized when property with same name is
2290 // seen in the @implementation.
2291 for (const ObjCCategoryDecl *ClsExtDecl =
2292 IDecl->getFirstClassExtension();
2293 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2294 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2295 E = ClsExtDecl->prop_end(); I != E; ++I) {
2296 ObjCPropertyDecl *Property = (*I);
2297 // Skip over properties declared @dynamic
2298 if (const ObjCPropertyImplDecl *PIDecl
2299 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2300 if (PIDecl->getPropertyImplementation()
2301 == ObjCPropertyImplDecl::Dynamic)
2302 continue;
2303
2304 for (const ObjCCategoryDecl *CExtDecl =
2305 IDecl->getFirstClassExtension();
2306 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2307 if (ObjCMethodDecl *GetterMethod =
2308 CExtDecl->getInstanceMethod(Property->getGetterName()))
2309 GetterMethod->setSynthesized(true);
2310 if (!Property->isReadOnly())
2311 if (ObjCMethodDecl *SetterMethod =
2312 CExtDecl->getInstanceMethod(Property->getSetterName()))
2313 SetterMethod->setSynthesized(true);
2314 }
2315 }
2316 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002317 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002318 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002319 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002320
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002321 if (LangOpts.ObjCNonFragileABI2)
2322 while (IDecl->getSuperClass()) {
2323 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2324 IDecl = IDecl->getSuperClass();
2325 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002326 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002327 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002328 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002329 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002330 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002331
Chris Lattner4d391482007-12-12 07:09:47 +00002332 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002333 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002334 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002335 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002336 Categories; Categories = Categories->getNextClassCategory()) {
2337 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002338 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002339 break;
2340 }
2341 }
2342 }
2343 }
Chris Lattner682bf922009-03-29 16:50:03 +00002344 if (isInterfaceDeclKind) {
2345 // Reject invalid vardecls.
2346 for (unsigned i = 0; i != tuvNum; i++) {
2347 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2348 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2349 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002350 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002351 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002352 }
Chris Lattner682bf922009-03-29 16:50:03 +00002353 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002354 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002355 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002356
2357 for (unsigned i = 0; i != tuvNum; i++) {
2358 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
Argyrios Kyrtzidisc14a03d2011-11-23 20:27:36 +00002359 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2360 (*I)->setTopLevelDeclInObjCContainer();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002361 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2362 }
Erik Verbruggend64251f2011-12-06 09:25:23 +00002363
2364 return ClassDecl;
Chris Lattner4d391482007-12-12 07:09:47 +00002365}
2366
2367
2368/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2369/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002370static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002371CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002372 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002373}
2374
Ted Kremenek422bae72010-04-18 04:59:38 +00002375static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002376bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2377 const AttrVec &A) {
2378 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002379 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002380 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002381 return false;
2382
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002383 // method declared in interface has no attribute.
2384 // But implementation has attributes. This is invalid
2385 if (!IMD->hasAttrs())
2386 return true;
2387
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002388 const AttrVec &D = IMD->getAttrs();
2389 if (D.size() != A.size())
2390 return true;
2391
2392 // attributes on method declaration and definition must match exactly.
2393 // Note that we have at most a couple of attributes on methods, so this
2394 // n*n search is good enough.
2395 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2396 bool match = false;
2397 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2398 if ((*i)->getKind() == (*i1)->getKind()) {
2399 match = true;
2400 break;
2401 }
2402 }
2403 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002404 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002405 }
Sean Huntcf807c42010-08-18 23:23:40 +00002406 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002407}
2408
Douglas Gregore97179c2011-09-08 01:46:34 +00002409namespace {
2410 /// \brief Describes the compatibility of a result type with its method.
2411 enum ResultTypeCompatibilityKind {
2412 RTC_Compatible,
2413 RTC_Incompatible,
2414 RTC_Unknown
2415 };
2416}
2417
Douglas Gregor926df6c2011-06-11 01:09:30 +00002418/// \brief Check whether the declared result type of the given Objective-C
2419/// method declaration is compatible with the method's class.
2420///
Douglas Gregore97179c2011-09-08 01:46:34 +00002421static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002422CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2423 ObjCInterfaceDecl *CurrentClass) {
2424 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002425
2426 // If an Objective-C method inherits its related result type, then its
2427 // declared result type must be compatible with its own class type. The
2428 // declared result type is compatible if:
2429 if (const ObjCObjectPointerType *ResultObjectType
2430 = ResultType->getAs<ObjCObjectPointerType>()) {
2431 // - it is id or qualified id, or
2432 if (ResultObjectType->isObjCIdType() ||
2433 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002434 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002435
2436 if (CurrentClass) {
2437 if (ObjCInterfaceDecl *ResultClass
2438 = ResultObjectType->getInterfaceDecl()) {
2439 // - it is the same as the method's class type, or
Douglas Gregor60ef3082011-12-15 00:29:59 +00002440 if (declaresSameEntity(CurrentClass, ResultClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002441 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002442
2443 // - it is a superclass of the method's class type
2444 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002445 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002446 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002447 } else {
2448 // Any Objective-C pointer type might be acceptable for a protocol
2449 // method; we just don't know.
2450 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002451 }
2452 }
2453
Douglas Gregore97179c2011-09-08 01:46:34 +00002454 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002455}
2456
John McCall6c2c2502011-07-22 02:45:48 +00002457namespace {
2458/// A helper class for searching for methods which a particular method
2459/// overrides.
2460class OverrideSearch {
2461 Sema &S;
2462 ObjCMethodDecl *Method;
2463 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2464 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2465 bool Recursive;
2466
2467public:
2468 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2469 Selector selector = method->getSelector();
2470
2471 // Bypass this search if we've never seen an instance/class method
2472 // with this selector before.
2473 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2474 if (it == S.MethodPool.end()) {
2475 if (!S.ExternalSource) return;
2476 it = S.ReadMethodPool(selector);
2477 }
2478 ObjCMethodList &list =
2479 method->isInstanceMethod() ? it->second.first : it->second.second;
2480 if (!list.Method) return;
2481
2482 ObjCContainerDecl *container
2483 = cast<ObjCContainerDecl>(method->getDeclContext());
2484
2485 // Prevent the search from reaching this container again. This is
2486 // important with categories, which override methods from the
2487 // interface and each other.
2488 Searched.insert(container);
2489 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002490 }
John McCall6c2c2502011-07-22 02:45:48 +00002491
2492 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2493 iterator begin() const { return Overridden.begin(); }
2494 iterator end() const { return Overridden.end(); }
2495
2496private:
2497 void searchFromContainer(ObjCContainerDecl *container) {
2498 if (container->isInvalidDecl()) return;
2499
2500 switch (container->getDeclKind()) {
2501#define OBJCCONTAINER(type, base) \
2502 case Decl::type: \
2503 searchFrom(cast<type##Decl>(container)); \
2504 break;
2505#define ABSTRACT_DECL(expansion)
2506#define DECL(type, base) \
2507 case Decl::type:
2508#include "clang/AST/DeclNodes.inc"
2509 llvm_unreachable("not an ObjC container!");
2510 }
2511 }
2512
2513 void searchFrom(ObjCProtocolDecl *protocol) {
Douglas Gregor5e2a1ff2012-01-01 19:29:29 +00002514 if (!protocol->hasDefinition())
2515 return;
2516
John McCall6c2c2502011-07-22 02:45:48 +00002517 // A method in a protocol declaration overrides declarations from
2518 // referenced ("parent") protocols.
2519 search(protocol->getReferencedProtocols());
2520 }
2521
2522 void searchFrom(ObjCCategoryDecl *category) {
2523 // A method in a category declaration overrides declarations from
2524 // the main class and from protocols the category references.
2525 search(category->getClassInterface());
2526 search(category->getReferencedProtocols());
2527 }
2528
2529 void searchFrom(ObjCCategoryImplDecl *impl) {
2530 // A method in a category definition that has a category
2531 // declaration overrides declarations from the category
2532 // declaration.
2533 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2534 search(category);
2535
2536 // Otherwise it overrides declarations from the class.
2537 } else {
2538 search(impl->getClassInterface());
2539 }
2540 }
2541
2542 void searchFrom(ObjCInterfaceDecl *iface) {
2543 // A method in a class declaration overrides declarations from
Douglas Gregor2e5c15b2011-12-15 05:27:12 +00002544 if (!iface->hasDefinition())
2545 return;
2546
John McCall6c2c2502011-07-22 02:45:48 +00002547 // - categories,
2548 for (ObjCCategoryDecl *category = iface->getCategoryList();
2549 category; category = category->getNextClassCategory())
2550 search(category);
2551
2552 // - the super class, and
2553 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2554 search(super);
2555
2556 // - any referenced protocols.
2557 search(iface->getReferencedProtocols());
2558 }
2559
2560 void searchFrom(ObjCImplementationDecl *impl) {
2561 // A method in a class implementation overrides declarations from
2562 // the class interface.
2563 search(impl->getClassInterface());
2564 }
2565
2566
2567 void search(const ObjCProtocolList &protocols) {
2568 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2569 i != e; ++i)
2570 search(*i);
2571 }
2572
2573 void search(ObjCContainerDecl *container) {
2574 // Abort if we've already searched this container.
2575 if (!Searched.insert(container)) return;
2576
2577 // Check for a method in this container which matches this selector.
2578 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2579 Method->isInstanceMethod());
2580
2581 // If we find one, record it and bail out.
2582 if (meth) {
2583 Overridden.insert(meth);
2584 return;
2585 }
2586
2587 // Otherwise, search for methods that a hypothetical method here
2588 // would have overridden.
2589
2590 // Note that we're now in a recursive case.
2591 Recursive = true;
2592
2593 searchFromContainer(container);
2594 }
2595};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002596}
2597
John McCalld226f652010-08-21 09:40:31 +00002598Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002599 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002600 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002601 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002602 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002603 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002604 Selector Sel,
2605 // optional arguments. The number of types/arguments is obtained
2606 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002607 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002608 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002609 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002610 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002611 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002612 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002613 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002614 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002615 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002616 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2617 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002618 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002619
Douglas Gregore97179c2011-09-08 01:46:34 +00002620 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002621 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002622 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002623 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002624
Steve Naroffccef3712009-02-20 22:59:16 +00002625 // Methods cannot return interface types. All ObjC objects are
2626 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002627 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002628 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2629 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002630 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002631 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002632
2633 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002634 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002635 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002636 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002637 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002638 }
Mike Stump1eb44332009-09-09 15:08:12 +00002639
2640 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002641 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002642 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002643 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002644 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002645 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002646 /*isSynthesized=*/false,
2647 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002648 MethodDeclKind == tok::objc_optional
2649 ? ObjCMethodDecl::Optional
2650 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002651 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002652
Chris Lattner5f9e2722011-07-23 10:55:15 +00002653 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002654
Chris Lattner7db638d2009-04-11 19:42:43 +00002655 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002656 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002657 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002658
Chris Lattnere294d3f2009-04-11 18:57:04 +00002659 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002660 ArgType = Context.getObjCIdType();
2661 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002662 } else {
John McCall58e46772009-10-23 21:48:59 +00002663 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002664 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002665 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002666 }
Mike Stump1eb44332009-09-09 15:08:12 +00002667
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002668 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2669 LookupOrdinaryName, ForRedeclaration);
2670 LookupName(R, S);
2671 if (R.isSingleResult()) {
2672 NamedDecl *PrevDecl = R.getFoundDecl();
2673 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002674 Diag(ArgInfo[i].NameLoc,
2675 (MethodDefinition ? diag::warn_method_param_redefinition
2676 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002677 << ArgInfo[i].Name;
2678 Diag(PrevDecl->getLocation(),
2679 diag::note_previous_declaration);
2680 }
2681 }
2682
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002683 SourceLocation StartLoc = DI
2684 ? DI->getTypeLoc().getBeginLoc()
2685 : ArgInfo[i].NameLoc;
2686
John McCall81ef3e62011-04-23 02:46:06 +00002687 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2688 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2689 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002690
John McCall70798862011-05-02 00:30:12 +00002691 Param->setObjCMethodScopeInfo(i);
2692
Chris Lattner0ed844b2008-04-04 06:12:32 +00002693 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002694 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002695
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002696 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002697 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002698
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002699 S->AddDecl(Param);
2700 IdResolver.AddDecl(Param);
2701
Chris Lattner0ed844b2008-04-04 06:12:32 +00002702 Params.push_back(Param);
2703 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002704
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002705 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002706 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002707 QualType ArgType = Param->getType();
2708 if (ArgType.isNull())
2709 ArgType = Context.getObjCIdType();
2710 else
2711 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002712 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002713 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002714 Diag(Param->getLocation(),
2715 diag::err_object_cannot_be_passed_returned_by_value)
2716 << 1 << ArgType;
2717 Param->setInvalidDecl();
2718 }
2719 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002720
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002721 Params.push_back(Param);
2722 }
2723
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002724 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002725 ObjCMethod->setObjCDeclQualifier(
2726 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002727
2728 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002729 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002730
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002731 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002732 const ObjCMethodDecl *PrevMethod = 0;
2733 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002734 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002735 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2736 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002737 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002738 PrevMethod = ImpDecl->getClassMethod(Sel);
2739 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002740 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002741
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002742 ObjCMethodDecl *IMD = 0;
2743 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2744 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2745 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002746 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanianec236782011-12-06 00:02:41 +00002747 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs())) {
Fariborz Jahanian28441e62011-12-21 00:09:11 +00002748 SourceLocation MethodLoc = IMD->getLocation();
2749 if (!getSourceManager().isInSystemHeader(MethodLoc)) {
2750 Diag(EndLoc, diag::warn_attribute_method_def);
2751 Diag(MethodLoc, diag::note_method_declared_at);
2752 }
Fariborz Jahanianec236782011-12-06 00:02:41 +00002753 }
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002754 } else {
2755 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002756 }
John McCall6c2c2502011-07-22 02:45:48 +00002757
Chris Lattner4d391482007-12-12 07:09:47 +00002758 if (PrevMethod) {
2759 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002760 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002761 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002762 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002763 }
John McCall54abf7d2009-11-04 02:18:39 +00002764
Douglas Gregor926df6c2011-06-11 01:09:30 +00002765 // If this Objective-C method does not have a related result type, but we
2766 // are allowed to infer related result types, try to do so based on the
2767 // method family.
2768 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2769 if (!CurrentClass) {
2770 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2771 CurrentClass = Cat->getClassInterface();
2772 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2773 CurrentClass = Impl->getClassInterface();
2774 else if (ObjCCategoryImplDecl *CatImpl
2775 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2776 CurrentClass = CatImpl->getClassInterface();
2777 }
John McCall6c2c2502011-07-22 02:45:48 +00002778
Douglas Gregore97179c2011-09-08 01:46:34 +00002779 ResultTypeCompatibilityKind RTC
2780 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002781
2782 // Search for overridden methods and merge information down from them.
2783 OverrideSearch overrides(*this, ObjCMethod);
2784 for (OverrideSearch::iterator
2785 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2786 ObjCMethodDecl *overridden = *i;
2787
2788 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002789 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002790 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002791
2792 // Then merge the declarations.
2793 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002794
2795 // Check for overriding methods
2796 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00002797 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2798 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2799 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
Douglas Gregor926df6c2011-06-11 01:09:30 +00002800 }
2801
John McCallf85e1932011-06-15 23:02:42 +00002802 bool ARCError = false;
2803 if (getLangOptions().ObjCAutoRefCount)
2804 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2805
Douglas Gregore97179c2011-09-08 01:46:34 +00002806 // Infer the related result type when possible.
2807 if (!ARCError && RTC == RTC_Compatible &&
2808 !ObjCMethod->hasRelatedResultType() &&
2809 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002810 bool InferRelatedResultType = false;
2811 switch (ObjCMethod->getMethodFamily()) {
2812 case OMF_None:
2813 case OMF_copy:
2814 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002815 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002816 case OMF_mutableCopy:
2817 case OMF_release:
2818 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002819 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002820 break;
2821
2822 case OMF_alloc:
2823 case OMF_new:
2824 InferRelatedResultType = ObjCMethod->isClassMethod();
2825 break;
2826
2827 case OMF_init:
2828 case OMF_autorelease:
2829 case OMF_retain:
2830 case OMF_self:
2831 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2832 break;
2833 }
2834
John McCall6c2c2502011-07-22 02:45:48 +00002835 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002836 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002837 }
2838
John McCalld226f652010-08-21 09:40:31 +00002839 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002840}
2841
Chris Lattnercc98eac2008-12-17 07:13:27 +00002842bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002843 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002844 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002845 // Following is also an error. But it is caused by a missing @end
2846 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002847 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2848 return false;
2849 }
2850
Anders Carlsson15281452008-11-04 16:57:32 +00002851 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2852 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002853
Anders Carlsson15281452008-11-04 16:57:32 +00002854 return true;
2855}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002856
Chris Lattnercc98eac2008-12-17 07:13:27 +00002857/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2858/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002859void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002860 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002861 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002862 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002863 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002864 if (!Class) {
2865 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2866 return;
2867 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002868 if (LangOpts.ObjCNonFragileABI) {
2869 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2870 return;
2871 }
Mike Stump1eb44332009-09-09 15:08:12 +00002872
Chris Lattnercc98eac2008-12-17 07:13:27 +00002873 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002874 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002875 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002876 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002877 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002878 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002879 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002880 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2881 /*FIXME: StartL=*/ID->getLocation(),
2882 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002883 ID->getIdentifier(), ID->getType(),
2884 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002885 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002886 }
Mike Stump1eb44332009-09-09 15:08:12 +00002887
Chris Lattnercc98eac2008-12-17 07:13:27 +00002888 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002889 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002890 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002891 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002892 if (getLangOptions().CPlusPlus)
2893 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002894 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002895 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002896 }
2897}
2898
Douglas Gregor160b5632010-04-26 17:32:49 +00002899/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002900VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2901 SourceLocation StartLoc,
2902 SourceLocation IdLoc,
2903 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002904 bool Invalid) {
2905 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2906 // duration shall not be qualified by an address-space qualifier."
2907 // Since all parameters have automatic store duration, they can not have
2908 // an address space.
2909 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002910 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002911 Invalid = true;
2912 }
2913
2914 // An @catch parameter must be an unqualified object pointer type;
2915 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2916 if (Invalid) {
2917 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002918 } else if (T->isDependentType()) {
2919 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002920 } else if (!T->isObjCObjectPointerType()) {
2921 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002922 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002923 } else if (T->isObjCQualifiedIdType()) {
2924 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002925 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002926 }
2927
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002928 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2929 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002930 New->setExceptionVariable(true);
2931
Douglas Gregor9aab9c42011-12-10 01:22:52 +00002932 // In ARC, infer 'retaining' for variables of retainable type.
2933 if (getLangOptions().ObjCAutoRefCount && inferObjCARCLifetime(New))
2934 Invalid = true;
2935
Douglas Gregor160b5632010-04-26 17:32:49 +00002936 if (Invalid)
2937 New->setInvalidDecl();
2938 return New;
2939}
2940
John McCalld226f652010-08-21 09:40:31 +00002941Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002942 const DeclSpec &DS = D.getDeclSpec();
2943
2944 // We allow the "register" storage class on exception variables because
2945 // GCC did, but we drop it completely. Any other storage class is an error.
2946 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2947 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2948 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2949 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2950 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2951 << DS.getStorageClassSpec();
2952 }
2953 if (D.getDeclSpec().isThreadSpecified())
2954 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2955 D.getMutableDeclSpec().ClearStorageClassSpecs();
2956
2957 DiagnoseFunctionSpecifiers(D);
2958
2959 // Check that there are no default arguments inside the type of this
2960 // exception object (C++ only).
2961 if (getLangOptions().CPlusPlus)
2962 CheckExtraCXXDefaultArguments(D);
2963
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002964 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002965 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002966
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002967 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2968 D.getSourceRange().getBegin(),
2969 D.getIdentifierLoc(),
2970 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002971 D.isInvalidType());
2972
2973 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2974 if (D.getCXXScopeSpec().isSet()) {
2975 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2976 << D.getCXXScopeSpec().getRange();
2977 New->setInvalidDecl();
2978 }
2979
2980 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002981 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002982 if (D.getIdentifier())
2983 IdResolver.AddDecl(New);
2984
2985 ProcessDeclAttributes(S, New, D);
2986
2987 if (New->hasAttr<BlocksAttr>())
2988 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002989 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002990}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002991
2992/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002993/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002994void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002995 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002996 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2997 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002998 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002999 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00003000 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00003001 }
3002}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00003003
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003004void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00003005 // Load referenced selectors from the external source.
3006 if (ExternalSource) {
3007 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
3008 ExternalSource->ReadReferencedSelectors(Sels);
3009 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
3010 ReferencedSelectors[Sels[I].first] = Sels[I].second;
3011 }
3012
Fariborz Jahanian8b789132011-02-04 23:19:27 +00003013 // Warning will be issued only when selector table is
3014 // generated (which means there is at lease one implementation
3015 // in the TU). This is to match gcc's behavior.
3016 if (ReferencedSelectors.empty() ||
3017 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00003018 return;
3019 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
3020 ReferencedSelectors.begin(),
3021 E = ReferencedSelectors.end(); S != E; ++S) {
3022 Selector Sel = (*S).first;
3023 if (!LookupImplementedMethodInGlobalPool(Sel))
3024 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
3025 }
3026 return;
3027}