blob: 592d7c231e4de0c20222e119f6b70a8d40e32e68 [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.
63 if (resultClass->isForwardDecl()) {
64 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 Gregordeacbdc2010-08-11 12:19:30 +0000368 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
369 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000370 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000371 if (!IDecl->isForwardDecl()) {
372 IDecl->setInvalidDecl();
373 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
374 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000375
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000376 // Return the previous class interface.
377 // FIXME: don't leak the objects passed in!
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000378 return ActOnObjCContainerStartDefinition(IDecl);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000379 } else {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000380 IDecl->setLocation(ClassLoc);
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000381 IDecl->setAtStartLoc(AtInterfaceLoc);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000382
383 // Since this ObjCInterfaceDecl was created by a forward declaration,
384 // we now add it to the DeclContext since it wasn't added before
385 // (see ActOnForwardClassDeclaration).
386 IDecl->setLexicalDeclContext(CurContext);
387 CurContext->addDecl(IDecl);
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000388
389 IDecl->completedForwardDecl();
390
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000391 if (AttrList)
392 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000393 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000394 } else {
395 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
396 ClassName, ClassLoc);
397 if (AttrList)
398 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
399
400 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000401 }
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Chris Lattner4d391482007-12-12 07:09:47 +0000403 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000404 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000405 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
406 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000407
408 if (!PrevDecl) {
409 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000410 TypoCorrection Corrected = CorrectTypo(
411 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
412 NULL, NULL, false, CTC_NoKeywords);
413 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000414 Diag(SuperLoc, diag::err_undef_superclass_suggest)
415 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000416 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
417 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000418 }
419 }
420
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000421 if (PrevDecl == IDecl) {
422 Diag(SuperLoc, diag::err_recursive_superclass)
423 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
424 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000425 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000426 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000427 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000428
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000429 // Diagnose classes that inherit from deprecated classes.
430 if (SuperClassDecl)
431 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000432
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000433 if (PrevDecl && SuperClassDecl == 0) {
434 // The previous declaration was not a class decl. Check if we have a
435 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000436 if (const TypedefNameDecl *TDecl =
437 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000438 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000439 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000440 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
441 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000442 }
443 }
Mike Stump1eb44332009-09-09 15:08:12 +0000444
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000445 // This handles the following case:
446 //
447 // typedef int SuperClass;
448 // @interface MyClass : SuperClass {} @end
449 //
450 if (!SuperClassDecl) {
451 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
452 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000453 }
454 }
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Richard Smith162e1c12011-04-15 14:24:37 +0000456 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000457 if (!SuperClassDecl)
458 Diag(SuperLoc, diag::err_undef_superclass)
459 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000460 else if (SuperClassDecl->isForwardDecl()) {
461 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000462 << SuperClassDecl->getDeclName() << ClassName
463 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000464 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
465 SuperClassDecl = 0;
466 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000467 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000468 IDecl->setSuperClass(SuperClassDecl);
469 IDecl->setSuperClassLoc(SuperLoc);
470 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000471 }
Chris Lattner4d391482007-12-12 07:09:47 +0000472 } else { // we have a root class.
473 IDecl->setLocEnd(ClassLoc);
474 }
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Sebastian Redl0b17c612010-08-13 00:28:03 +0000476 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000477 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000478 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000479 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000480 IDecl->setLocEnd(EndProtoLoc);
481 }
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Anders Carlsson15281452008-11-04 16:57:32 +0000483 CheckObjCDeclScope(IDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000484 return ActOnObjCContainerStartDefinition(IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000485}
486
487/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000488/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000489Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
490 IdentifierInfo *AliasName,
491 SourceLocation AliasLocation,
492 IdentifierInfo *ClassName,
493 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000494 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000495 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000496 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000497 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000498 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000499 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000500 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000501 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000502 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000503 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000504 }
505 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000506 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000507 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000508 if (const TypedefNameDecl *TDecl =
509 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000510 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000511 if (T->isObjCObjectType()) {
512 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000513 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000514 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000515 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000516 }
517 }
518 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000519 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
520 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000521 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000522 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000523 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000524 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000525 }
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000527 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000528 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000529 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Anders Carlsson15281452008-11-04 16:57:32 +0000531 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000532 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000533
John McCalld226f652010-08-21 09:40:31 +0000534 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000535}
536
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000537bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000538 IdentifierInfo *PName,
539 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000540 const ObjCList<ObjCProtocolDecl> &PList) {
541
542 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000543 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
544 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000545 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
546 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000547 if (PDecl->getIdentifier() == PName) {
548 Diag(Ploc, diag::err_protocol_has_circular_dependency);
549 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000550 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000551 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000552 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
553 PDecl->getLocation(), PDecl->getReferencedProtocols()))
554 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000555 }
556 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000557 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000558}
559
John McCalld226f652010-08-21 09:40:31 +0000560Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000561Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
562 IdentifierInfo *ProtocolName,
563 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000564 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000565 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000566 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000567 SourceLocation EndProtoLoc,
568 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000569 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000570 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000571 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000572 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000573 if (PDecl) {
574 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000575 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000576 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000577 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000578 // Just return the protocol we already had.
579 // FIXME: don't leak the objects passed in!
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000580 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000581 }
Steve Naroff61d68522009-03-05 15:22:01 +0000582 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000583 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000584 err = CheckForwardProtocolDeclarationForCircularDependency(
585 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Steve Narofff11b5082008-08-13 16:39:22 +0000587 // Make sure the cached decl gets a valid start location.
Argyrios Kyrtzidisa1e797e2011-10-05 19:37:56 +0000588 PDecl->setAtStartLoc(AtProtoInterfaceLoc);
589 PDecl->setLocation(ProtocolLoc);
Fariborz Jahanianca4c40a2011-08-25 22:26:53 +0000590 // Since this ObjCProtocolDecl was created by a forward declaration,
591 // we now add it to the DeclContext since it wasn't added before
592 PDecl->setLexicalDeclContext(CurContext);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000593 CurContext->addDecl(PDecl);
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000594 PDecl->completedForwardDecl();
Chris Lattner439e71f2008-03-16 01:25:17 +0000595 } else {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000596 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000597 ProtocolLoc, AtProtoInterfaceLoc,
598 /*isForwardDecl=*/false);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000599 PushOnScopeChains(PDecl, TUScope);
Chris Lattnercca59d72008-03-16 01:23:04 +0000600 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000601 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000602 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000603 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000604 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000605 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
606 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000607 PDecl->setLocEnd(EndProtoLoc);
608 }
Mike Stump1eb44332009-09-09 15:08:12 +0000609
610 CheckObjCDeclScope(PDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000611 return ActOnObjCContainerStartDefinition(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000612}
613
614/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000615/// issues an error if they are not declared. It returns list of
616/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000617void
Chris Lattnere13b9592008-07-26 04:03:38 +0000618Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000619 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000620 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000621 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000622 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000623 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
624 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000625 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000626 TypoCorrection Corrected = CorrectTypo(
627 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
628 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
629 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000630 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000631 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000632 Diag(PDecl->getLocation(), diag::note_previous_decl)
633 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000634 }
635 }
636
637 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000638 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000639 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000640 continue;
641 }
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000643 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000644
645 // If this is a forward declaration and we are supposed to warn in this
646 // case, do it.
647 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000648 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000649 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000650 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000651 }
652}
653
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000654/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000655/// a class method in its extension.
656///
Mike Stump1eb44332009-09-09 15:08:12 +0000657void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000658 ObjCInterfaceDecl *ID) {
659 if (!ID)
660 return; // Possibly due to previous error
661
662 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000663 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
664 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000665 ObjCMethodDecl *MD = *i;
666 MethodMap[MD->getSelector()] = MD;
667 }
668
669 if (MethodMap.empty())
670 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000671 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
672 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000673 ObjCMethodDecl *Method = *i;
674 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
675 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
676 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
677 << Method->getDeclName();
678 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
679 }
680 }
681}
682
Chris Lattner58fe03b2009-04-12 08:43:13 +0000683/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000684Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000685Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000686 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000687 unsigned NumElts,
688 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000689 SmallVector<ObjCProtocolDecl*, 32> Protocols;
690 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattner4d391482007-12-12 07:09:47 +0000692 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000693 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000694 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000695 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000696 if (PDecl == 0) { // Not already seen?
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000697 PDecl = ObjCProtocolDecl::Create(Context, CurContext, Ident,
Argyrios Kyrtzidisb05d7b22011-10-17 19:48:06 +0000698 IdentList[i].second, AtProtocolLoc,
699 /*isForwardDecl=*/true);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000700 PushOnScopeChains(PDecl, TUScope, false);
701 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000702 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000703 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000704 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Argyrios Kyrtzidis1a434152011-11-12 21:07:52 +0000705 if (!isNew) {
706 if (ASTMutationListener *L = Context.getASTMutationListener())
707 L->UpdatedAttributeList(PDecl);
708 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000709 }
Chris Lattner4d391482007-12-12 07:09:47 +0000710 Protocols.push_back(PDecl);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000711 ProtoLocs.push_back(IdentList[i].second);
Chris Lattner4d391482007-12-12 07:09:47 +0000712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
714 ObjCForwardProtocolDecl *PDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000715 ObjCForwardProtocolDecl::Create(Context, CurContext, AtProtocolLoc,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000716 Protocols.data(), Protocols.size(),
717 ProtoLocs.data());
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000718 CurContext->addDecl(PDecl);
Anders Carlsson15281452008-11-04 16:57:32 +0000719 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000720 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000721}
722
John McCalld226f652010-08-21 09:40:31 +0000723Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000724ActOnStartCategoryInterface(SourceLocation AtInterfaceLoc,
725 IdentifierInfo *ClassName, SourceLocation ClassLoc,
726 IdentifierInfo *CategoryName,
727 SourceLocation CategoryLoc,
John McCalld226f652010-08-21 09:40:31 +0000728 Decl * const *ProtoRefs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000729 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000730 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000731 SourceLocation EndProtoLoc) {
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000732 ObjCCategoryDecl *CDecl;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000733 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Ted Kremenek09b68972010-02-23 19:39:46 +0000734
735 /// Check that class of this category is already completely declared.
736 if (!IDecl || IDecl->isForwardDecl()) {
737 // Create an invalid ObjCCategoryDecl to serve as context for
738 // the enclosing method declarations. We mark the decl invalid
739 // to make it clear that this isn't a valid AST.
740 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000741 ClassLoc, CategoryLoc, CategoryName,IDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000742 CDecl->setInvalidDecl();
743 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000744 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000745 }
746
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000747 if (!CategoryName && IDecl->getImplementation()) {
748 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
749 Diag(IDecl->getImplementation()->getLocation(),
750 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000751 }
752
Fariborz Jahanian25760612010-02-15 21:55:26 +0000753 if (CategoryName) {
754 /// Check for duplicate interface declaration for this category
755 ObjCCategoryDecl *CDeclChain;
756 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
757 CDeclChain = CDeclChain->getNextClassCategory()) {
758 if (CDeclChain->getIdentifier() == CategoryName) {
759 // Class extensions can be declared multiple times.
760 Diag(CategoryLoc, diag::warn_dup_category_def)
761 << ClassName << CategoryName;
762 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
763 break;
764 }
Chris Lattner70f19542009-02-16 21:26:43 +0000765 }
766 }
Chris Lattner70f19542009-02-16 21:26:43 +0000767
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000768 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
769 ClassLoc, CategoryLoc, CategoryName, IDecl);
770 // FIXME: PushOnScopeChains?
771 CurContext->addDecl(CDecl);
772
Chris Lattner4d391482007-12-12 07:09:47 +0000773 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000774 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000775 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000776 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000777 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000778 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000779 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000780 }
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Anders Carlsson15281452008-11-04 16:57:32 +0000782 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000783 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000784}
785
786/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000787/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000788/// object.
John McCalld226f652010-08-21 09:40:31 +0000789Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000790 SourceLocation AtCatImplLoc,
791 IdentifierInfo *ClassName, SourceLocation ClassLoc,
792 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000793 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000794 ObjCCategoryDecl *CatIDecl = 0;
795 if (IDecl) {
796 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
797 if (!CatIDecl) {
798 // Category @implementation with no corresponding @interface.
799 // Create and install one.
800 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000801 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000802 CatName, IDecl);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000803 }
804 }
805
Mike Stump1eb44332009-09-09 15:08:12 +0000806 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000807 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
808 ClassLoc, AtCatImplLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000809 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000810 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000811 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000812 CDecl->setInvalidDecl();
813 }
Chris Lattner4d391482007-12-12 07:09:47 +0000814
Douglas Gregord0434102009-01-09 00:49:46 +0000815 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000816 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000817
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000818 // If the interface is deprecated/unavailable, warn/error about it.
819 if (IDecl)
820 DiagnoseUseOfDecl(IDecl, ClassLoc);
821
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000822 /// Check that CatName, category name, is not used in another implementation.
823 if (CatIDecl) {
824 if (CatIDecl->getImplementation()) {
825 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
826 << CatName;
827 Diag(CatIDecl->getImplementation()->getLocation(),
828 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000829 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000830 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000831 // Warn on implementating category of deprecated class under
832 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000833 DiagnoseObjCImplementedDeprecations(*this,
834 dyn_cast<NamedDecl>(IDecl),
835 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000836 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Anders Carlsson15281452008-11-04 16:57:32 +0000839 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000840 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000841}
842
John McCalld226f652010-08-21 09:40:31 +0000843Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000844 SourceLocation AtClassImplLoc,
845 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000846 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000847 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000848 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000849 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000850 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000851 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
852 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000853 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000854 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000855 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000856 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
857 // If this is a forward declaration of an interface, warn.
858 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000859 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000860 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000861 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000862 } else {
863 // We did not find anything with the name ClassName; try to correct for
864 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000865 TypoCorrection Corrected = CorrectTypo(
866 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
867 NULL, NULL, false, CTC_NoKeywords);
868 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000869 // Suggest the (potentially) correct interface name. However, put the
870 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000871 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000872 // provide a code-modification hint or use the typo name for recovery,
873 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000874 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000875 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000876 << ClassName << CorrectedName;
877 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
878 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000879 IDecl = 0;
880 } else {
881 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
882 }
Chris Lattner4d391482007-12-12 07:09:47 +0000883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Chris Lattner4d391482007-12-12 07:09:47 +0000885 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000886 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000887 if (SuperClassname) {
888 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000889 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
890 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000891 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000892 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
893 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000894 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000895 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000896 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000897 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000898 Diag(SuperClassLoc, diag::err_undef_superclass)
899 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000900 else if (IDecl && IDecl->getSuperClass() != SDecl) {
901 // This implementation and its interface do not have the same
902 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000903 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000904 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000905 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000906 }
907 }
908 }
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Chris Lattner4d391482007-12-12 07:09:47 +0000910 if (!IDecl) {
911 // Legacy case of @implementation with no corresponding @interface.
912 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000913
Mike Stump390b4cc2009-05-16 07:39:55 +0000914 // FIXME: Do we support attributes on the @implementation? If so we should
915 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000916 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000917 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000918 IDecl->setSuperClass(SDecl);
919 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000920
921 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000922 } else {
923 // Mark the interface as being completed, even if it was just as
924 // @class ....;
925 // declaration; the user cannot reopen it.
Argyrios Kyrtzidisad834d52011-11-12 21:07:46 +0000926 if (IDecl->isForwardDecl())
927 IDecl->completedForwardDecl();
Chris Lattner4d391482007-12-12 07:09:47 +0000928 }
Mike Stump1eb44332009-09-09 15:08:12 +0000929
930 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000931 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
932 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Anders Carlsson15281452008-11-04 16:57:32 +0000934 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000935 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000936
Chris Lattner4d391482007-12-12 07:09:47 +0000937 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000938 if (IDecl->getImplementation()) {
939 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000940 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000941 Diag(IDecl->getImplementation()->getLocation(),
942 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000943 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000944 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000945 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000946 // Warn on implementating deprecated class under
947 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000948 DiagnoseObjCImplementedDeprecations(*this,
949 dyn_cast<NamedDecl>(IDecl),
950 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000951 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000952 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000953}
954
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000955void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
956 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000957 SourceLocation RBrace) {
958 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000959 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000960 if (!IDecl)
961 return;
962 /// Check case of non-existing @interface decl.
963 /// (legacy objective-c @implementation decl without an @interface decl).
964 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000965 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000966 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000967 // Add ivar's to class's DeclContext.
968 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000969 ivars[i]->setLexicalDeclContext(ImpDecl);
970 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000971 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000972 }
973
Chris Lattner4d391482007-12-12 07:09:47 +0000974 return;
975 }
976 // If implementation has empty ivar list, just return.
977 if (numIvars == 0)
978 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000979
Chris Lattner4d391482007-12-12 07:09:47 +0000980 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000981 if (LangOpts.ObjCNonFragileABI2) {
982 if (ImpDecl->getSuperClass())
983 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
984 for (unsigned i = 0; i < numIvars; i++) {
985 ObjCIvarDecl* ImplIvar = ivars[i];
986 if (const ObjCIvarDecl *ClsIvar =
987 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
988 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
989 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
990 continue;
991 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000992 // Instance ivar to Implementation's DeclContext.
993 ImplIvar->setLexicalDeclContext(ImpDecl);
994 IDecl->makeDeclVisibleInContext(ImplIvar, false);
995 ImpDecl->addDecl(ImplIvar);
996 }
997 return;
998 }
Chris Lattner4d391482007-12-12 07:09:47 +0000999 // Check interface's Ivar list against those in the implementation.
1000 // names and types must match.
1001 //
Chris Lattner4d391482007-12-12 07:09:47 +00001002 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001003 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001004 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1005 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001006 ObjCIvarDecl* ImplIvar = ivars[j++];
1007 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001008 assert (ImplIvar && "missing implementation ivar");
1009 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001010
Steve Naroffca331292009-03-03 14:49:36 +00001011 // First, make sure the types match.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001012 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001013 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001014 << ImplIvar->getIdentifier()
1015 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001016 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001017 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1018 ImplIvar->getBitWidthValue(Context) !=
1019 ClsIvar->getBitWidthValue(Context)) {
1020 Diag(ImplIvar->getBitWidth()->getLocStart(),
1021 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1022 Diag(ClsIvar->getBitWidth()->getLocStart(),
1023 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001024 }
Steve Naroffca331292009-03-03 14:49:36 +00001025 // Make sure the names are identical.
1026 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001027 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001028 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001029 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001030 }
1031 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001032 }
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Chris Lattner609e4c72007-12-12 18:11:49 +00001034 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001035 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001036 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001037 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001038}
1039
Steve Naroff3c2eb662008-02-10 21:38:56 +00001040void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001041 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001042 // No point warning no definition of method which is 'unavailable'.
1043 if (method->hasAttr<UnavailableAttr>())
1044 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001045 if (!IncompleteImpl) {
1046 Diag(ImpLoc, diag::warn_incomplete_impl);
1047 IncompleteImpl = true;
1048 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001049 if (DiagID == diag::warn_unimplemented_protocol_method)
1050 Diag(ImpLoc, DiagID) << method->getDeclName();
1051 else
1052 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001053}
1054
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001055/// Determines if type B can be substituted for type A. Returns true if we can
1056/// guarantee that anything that the user will do to an object of type A can
1057/// also be done to an object of type B. This is trivially true if the two
1058/// types are the same, or if B is a subclass of A. It becomes more complex
1059/// in cases where protocols are involved.
1060///
1061/// Object types in Objective-C describe the minimum requirements for an
1062/// object, rather than providing a complete description of a type. For
1063/// example, if A is a subclass of B, then B* may refer to an instance of A.
1064/// The principle of substitutability means that we may use an instance of A
1065/// anywhere that we may use an instance of B - it will implement all of the
1066/// ivars of B and all of the methods of B.
1067///
1068/// This substitutability is important when type checking methods, because
1069/// the implementation may have stricter type definitions than the interface.
1070/// The interface specifies minimum requirements, but the implementation may
1071/// have more accurate ones. For example, a method may privately accept
1072/// instances of B, but only publish that it accepts instances of A. Any
1073/// object passed to it will be type checked against B, and so will implicitly
1074/// by a valid A*. Similarly, a method may return a subclass of the class that
1075/// it is declared as returning.
1076///
1077/// This is most important when considering subclassing. A method in a
1078/// subclass must accept any object as an argument that its superclass's
1079/// implementation accepts. It may, however, accept a more general type
1080/// without breaking substitutability (i.e. you can still use the subclass
1081/// anywhere that you can use the superclass, but not vice versa). The
1082/// converse requirement applies to return types: the return type for a
1083/// subclass method must be a valid object of the kind that the superclass
1084/// advertises, but it may be specified more accurately. This avoids the need
1085/// for explicit down-casting by callers.
1086///
1087/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001088static bool isObjCTypeSubstitutable(ASTContext &Context,
1089 const ObjCObjectPointerType *A,
1090 const ObjCObjectPointerType *B,
1091 bool rejectId) {
1092 // Reject a protocol-unqualified id.
1093 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001094
1095 // If B is a qualified id, then A must also be a qualified id and it must
1096 // implement all of the protocols in B. It may not be a qualified class.
1097 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1098 // stricter definition so it is not substitutable for id<A>.
1099 if (B->isObjCQualifiedIdType()) {
1100 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001101 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1102 QualType(B,0),
1103 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001104 }
1105
1106 /*
1107 // id is a special type that bypasses type checking completely. We want a
1108 // warning when it is used in one place but not another.
1109 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1110
1111
1112 // If B is a qualified id, then A must also be a qualified id (which it isn't
1113 // if we've got this far)
1114 if (B->isObjCQualifiedIdType()) return false;
1115 */
1116
1117 // Now we know that A and B are (potentially-qualified) class types. The
1118 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001119 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001120}
1121
John McCall10302c02010-10-28 02:34:38 +00001122static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1123 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1124}
1125
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001126static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001127 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001128 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001129 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001130 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001131 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001132 if (IsProtocolMethodDecl &&
1133 (MethodDecl->getObjCDeclQualifier() !=
1134 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001135 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001136 S.Diag(MethodImpl->getLocation(),
1137 (IsOverridingMode ?
1138 diag::warn_conflicting_overriding_ret_type_modifiers
1139 : diag::warn_conflicting_ret_type_modifiers))
1140 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001141 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1142 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1143 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1144 }
1145 else
1146 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001147 }
1148
John McCall10302c02010-10-28 02:34:38 +00001149 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001150 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001151 return true;
1152 if (!Warn)
1153 return false;
John McCall10302c02010-10-28 02:34:38 +00001154
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001155 unsigned DiagID =
1156 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1157 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001158
1159 // Mismatches between ObjC pointers go into a different warning
1160 // category, and sometimes they're even completely whitelisted.
1161 if (const ObjCObjectPointerType *ImplPtrTy =
1162 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1163 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001164 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001165 // Allow non-matching return types as long as they don't violate
1166 // the principle of substitutability. Specifically, we permit
1167 // return types that are subclasses of the declared return type,
1168 // or that are more-qualified versions of the declared type.
1169 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001170 return false;
John McCall10302c02010-10-28 02:34:38 +00001171
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001172 DiagID =
1173 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1174 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001175 }
1176 }
1177
1178 S.Diag(MethodImpl->getLocation(), DiagID)
1179 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001180 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001181 << MethodImpl->getResultType()
1182 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001183 S.Diag(MethodDecl->getLocation(),
1184 IsOverridingMode ? diag::note_previous_declaration
1185 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001186 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001187 return false;
John McCall10302c02010-10-28 02:34:38 +00001188}
1189
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001190static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001191 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001192 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001193 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001194 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001195 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001196 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001197 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001198 if (IsProtocolMethodDecl &&
1199 (ImplVar->getObjCDeclQualifier() !=
1200 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001201 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001202 if (IsOverridingMode)
1203 S.Diag(ImplVar->getLocation(),
1204 diag::warn_conflicting_overriding_param_modifiers)
1205 << getTypeRange(ImplVar->getTypeSourceInfo())
1206 << MethodImpl->getDeclName();
1207 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001208 diag::warn_conflicting_param_modifiers)
1209 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001210 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001211 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1212 << getTypeRange(IfaceVar->getTypeSourceInfo());
1213 }
1214 else
1215 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001216 }
1217
John McCall10302c02010-10-28 02:34:38 +00001218 QualType ImplTy = ImplVar->getType();
1219 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001220
John McCall10302c02010-10-28 02:34:38 +00001221 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001222 return true;
1223
1224 if (!Warn)
1225 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001226 unsigned DiagID =
1227 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1228 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001229
1230 // Mismatches between ObjC pointers go into a different warning
1231 // category, and sometimes they're even completely whitelisted.
1232 if (const ObjCObjectPointerType *ImplPtrTy =
1233 ImplTy->getAs<ObjCObjectPointerType>()) {
1234 if (const ObjCObjectPointerType *IfacePtrTy =
1235 IfaceTy->getAs<ObjCObjectPointerType>()) {
1236 // Allow non-matching argument types as long as they don't
1237 // violate the principle of substitutability. Specifically, the
1238 // implementation must accept any objects that the superclass
1239 // accepts, however it may also accept others.
1240 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001241 return false;
John McCall10302c02010-10-28 02:34:38 +00001242
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001243 DiagID =
1244 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1245 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001246 }
1247 }
1248
1249 S.Diag(ImplVar->getLocation(), DiagID)
1250 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001251 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1252 S.Diag(IfaceVar->getLocation(),
1253 (IsOverridingMode ? diag::note_previous_declaration
1254 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001255 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001256 return false;
John McCall10302c02010-10-28 02:34:38 +00001257}
John McCallf85e1932011-06-15 23:02:42 +00001258
1259/// In ARC, check whether the conventional meanings of the two methods
1260/// match. If they don't, it's a hard error.
1261static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1262 ObjCMethodDecl *decl) {
1263 ObjCMethodFamily implFamily = impl->getMethodFamily();
1264 ObjCMethodFamily declFamily = decl->getMethodFamily();
1265 if (implFamily == declFamily) return false;
1266
1267 // Since conventions are sorted by selector, the only possibility is
1268 // that the types differ enough to cause one selector or the other
1269 // to fall out of the family.
1270 assert(implFamily == OMF_None || declFamily == OMF_None);
1271
1272 // No further diagnostics required on invalid declarations.
1273 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1274
1275 const ObjCMethodDecl *unmatched = impl;
1276 ObjCMethodFamily family = declFamily;
1277 unsigned errorID = diag::err_arc_lost_method_convention;
1278 unsigned noteID = diag::note_arc_lost_method_convention;
1279 if (declFamily == OMF_None) {
1280 unmatched = decl;
1281 family = implFamily;
1282 errorID = diag::err_arc_gained_method_convention;
1283 noteID = diag::note_arc_gained_method_convention;
1284 }
1285
1286 // Indexes into a %select clause in the diagnostic.
1287 enum FamilySelector {
1288 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1289 };
1290 FamilySelector familySelector = FamilySelector();
1291
1292 switch (family) {
1293 case OMF_None: llvm_unreachable("logic error, no method convention");
1294 case OMF_retain:
1295 case OMF_release:
1296 case OMF_autorelease:
1297 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001298 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001299 case OMF_retainCount:
1300 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001301 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001302 // Mismatches for these methods don't change ownership
1303 // conventions, so we don't care.
1304 return false;
1305
1306 case OMF_init: familySelector = F_init; break;
1307 case OMF_alloc: familySelector = F_alloc; break;
1308 case OMF_copy: familySelector = F_copy; break;
1309 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1310 case OMF_new: familySelector = F_new; break;
1311 }
1312
1313 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1314 ReasonSelector reasonSelector;
1315
1316 // The only reason these methods don't fall within their families is
1317 // due to unusual result types.
1318 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1319 reasonSelector = R_UnrelatedReturn;
1320 } else {
1321 reasonSelector = R_NonObjectReturn;
1322 }
1323
1324 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1325 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1326
1327 return true;
1328}
John McCall10302c02010-10-28 02:34:38 +00001329
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001330void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001331 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001332 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001333 if (getLangOptions().ObjCAutoRefCount &&
1334 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1335 return;
1336
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001337 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001338 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001339 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Chris Lattner3aff9192009-04-11 19:58:42 +00001341 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001342 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001343 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001344 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001345 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001346 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001347
Fariborz Jahanian21121902011-08-08 18:03:17 +00001348 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001349 Diag(ImpMethodDecl->getLocation(),
1350 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001351 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001352 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001353}
1354
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001355void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1356 ObjCMethodDecl *Overridden,
1357 bool IsProtocolMethodDecl) {
1358
1359 CheckMethodOverrideReturn(*this, Method, Overridden,
1360 IsProtocolMethodDecl, true,
1361 true);
1362
1363 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
1364 IF = Overridden->param_begin(), EM = Method->param_end();
1365 IM != EM; ++IM, ++IF) {
1366 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1367 IsProtocolMethodDecl, true, true);
1368 }
1369
1370 if (Method->isVariadic() != Overridden->isVariadic()) {
1371 Diag(Method->getLocation(),
1372 diag::warn_conflicting_overriding_variadic);
1373 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1374 }
1375}
1376
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001377/// WarnExactTypedMethods - This routine issues a warning if method
1378/// implementation declaration matches exactly that of its declaration.
1379void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1380 ObjCMethodDecl *MethodDecl,
1381 bool IsProtocolMethodDecl) {
1382 // don't issue warning when protocol method is optional because primary
1383 // class is not required to implement it and it is safe for protocol
1384 // to implement it.
1385 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1386 return;
1387 // don't issue warning when primary class's method is
1388 // depecated/unavailable.
1389 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1390 MethodDecl->hasAttr<DeprecatedAttr>())
1391 return;
1392
1393 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1394 IsProtocolMethodDecl, false, false);
1395 if (match)
1396 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1397 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1398 IM != EM; ++IM, ++IF) {
1399 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1400 *IM, *IF,
1401 IsProtocolMethodDecl, false, false);
1402 if (!match)
1403 break;
1404 }
1405 if (match)
1406 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001407 if (match)
1408 match = !(MethodDecl->isClassMethod() &&
1409 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001410
1411 if (match) {
1412 Diag(ImpMethodDecl->getLocation(),
1413 diag::warn_category_method_impl_match);
1414 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1415 }
1416}
1417
Mike Stump390b4cc2009-05-16 07:39:55 +00001418/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1419/// improve the efficiency of selector lookups and type checking by associating
1420/// with each protocol / interface / category the flattened instance tables. If
1421/// we used an immutable set to keep the table then it wouldn't add significant
1422/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001423
Steve Naroffefe7f362008-02-08 22:06:17 +00001424/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001425/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001426void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1427 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001428 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001429 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001430 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001431 ObjCContainerDecl *CDecl) {
1432 ObjCInterfaceDecl *IDecl;
1433 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1434 IDecl = C->getClassInterface();
1435 else
1436 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1437 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1438
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001439 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001440 ObjCInterfaceDecl *NSIDecl = 0;
1441 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001442 // check to see if class implements forwardInvocation method and objects
1443 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001444 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001445 // Under such conditions, which means that every method possible is
1446 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001447 // found" warnings.
1448 // FIXME: Use a general GetUnarySelector method for this.
1449 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1450 Selector fISelector = Context.Selectors.getSelector(1, &II);
1451 if (InsMap.count(fISelector))
1452 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1453 // need be implemented in the implementation.
1454 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1455 }
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001457 // If a method lookup fails locally we still need to look and see if
1458 // the method was implemented by a base class or an inherited
1459 // protocol. This lookup is slow, but occurs rarely in correct code
1460 // and otherwise would terminate in a warning.
1461
Chris Lattner4d391482007-12-12 07:09:47 +00001462 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001463 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001464 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001465 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001466 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001467 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001468 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001469 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001470 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001471 // Ugly, but necessary. Method declared in protcol might have
1472 // have been synthesized due to a property declared in the class which
1473 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001474 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001475 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001476 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001477 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001478 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
David Blaikied6471f72011-09-25 23:23:43 +00001479 != DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001480 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001481 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001482 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1483 << PDecl->getDeclName();
1484 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001485 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001486 }
1487 }
Chris Lattner4d391482007-12-12 07:09:47 +00001488 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001489 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001490 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001491 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001492 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001493 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1494 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001495 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001496 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001497 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1498 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001499 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001500 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001501 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1502 PDecl->getDeclName();
1503 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001504 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001505 }
Chris Lattner780f3292008-07-21 21:32:27 +00001506 // Check on this protocols's referenced protocols, recursively.
1507 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1508 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001509 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001510}
1511
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001512/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001513/// or protocol against those declared in their implementations.
1514///
1515void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1516 const llvm::DenseSet<Selector> &ClsMap,
1517 llvm::DenseSet<Selector> &InsMapSeen,
1518 llvm::DenseSet<Selector> &ClsMapSeen,
1519 ObjCImplDecl* IMPDecl,
1520 ObjCContainerDecl* CDecl,
1521 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001522 bool ImmediateClass,
1523 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001524 // Check and see if instance methods in class interface have been
1525 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001526 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1527 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001528 if (InsMapSeen.count((*I)->getSelector()))
1529 continue;
1530 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001531 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001532 !InsMap.count((*I)->getSelector())) {
1533 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001534 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1535 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001536 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001537 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001538 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001539 IMPDecl->getInstanceMethod((*I)->getSelector());
1540 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1541 "Expected to find the method through lookup as well");
1542 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001543 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001544 if (ImpMethodDecl) {
1545 if (!WarnExactMatch)
1546 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1547 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001548 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001549 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1550 isa<ObjCProtocolDecl>(CDecl));
1551 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001552 }
1553 }
Mike Stump1eb44332009-09-09 15:08:12 +00001554
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001555 // Check and see if class methods in class interface have been
1556 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001557 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001558 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001559 if (ClsMapSeen.count((*I)->getSelector()))
1560 continue;
1561 ClsMapSeen.insert((*I)->getSelector());
1562 if (!ClsMap.count((*I)->getSelector())) {
1563 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001564 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1565 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001566 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001567 ObjCMethodDecl *ImpMethodDecl =
1568 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001569 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1570 "Expected to find the method through lookup as well");
1571 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001572 if (!WarnExactMatch)
1573 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1574 isa<ObjCProtocolDecl>(CDecl));
1575 else
1576 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1577 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001578 }
1579 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001580
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001581 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001582 // Also methods in class extensions need be looked at next.
1583 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1584 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1585 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1586 IMPDecl,
1587 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001588 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001589
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001590 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001591 for (ObjCInterfaceDecl::all_protocol_iterator
1592 PI = I->all_referenced_protocol_begin(),
1593 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001594 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1595 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001596 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001597
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001598 // FIXME. For now, we are not checking for extact match of methods
1599 // in category implementation and its primary class's super class.
1600 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001601 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001602 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001603 I->getSuperClass(), IncompleteImpl, false);
1604 }
1605}
1606
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001607/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1608/// category matches with those implemented in its primary class and
1609/// warns each time an exact match is found.
1610void Sema::CheckCategoryVsClassMethodMatches(
1611 ObjCCategoryImplDecl *CatIMPDecl) {
1612 llvm::DenseSet<Selector> InsMap, ClsMap;
1613
1614 for (ObjCImplementationDecl::instmeth_iterator
1615 I = CatIMPDecl->instmeth_begin(),
1616 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1617 InsMap.insert((*I)->getSelector());
1618
1619 for (ObjCImplementationDecl::classmeth_iterator
1620 I = CatIMPDecl->classmeth_begin(),
1621 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1622 ClsMap.insert((*I)->getSelector());
1623 if (InsMap.empty() && ClsMap.empty())
1624 return;
1625
1626 // Get category's primary class.
1627 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1628 if (!CatDecl)
1629 return;
1630 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1631 if (!IDecl)
1632 return;
1633 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1634 bool IncompleteImpl = false;
1635 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1636 CatIMPDecl, IDecl,
1637 IncompleteImpl, false, true /*WarnExactMatch*/);
1638}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001639
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001640void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001641 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001642 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001643 llvm::DenseSet<Selector> InsMap;
1644 // Check and see if instance methods in class interface have been
1645 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001646 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001647 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001648 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001649
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001650 // Check and see if properties declared in the interface have either 1)
1651 // an implementation or 2) there is a @synthesize/@dynamic implementation
1652 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001653 if (isa<ObjCInterfaceDecl>(CDecl) &&
1654 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001655 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001656
Chris Lattner4d391482007-12-12 07:09:47 +00001657 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001658 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001659 I = IMPDecl->classmeth_begin(),
1660 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001661 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001662
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001663 // Check for type conflict of methods declared in a class/protocol and
1664 // its implementation; if any.
1665 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001666 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1667 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001668 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001669
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001670 // check all methods implemented in category against those declared
1671 // in its primary class.
1672 if (ObjCCategoryImplDecl *CatDecl =
1673 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1674 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001675
Chris Lattner4d391482007-12-12 07:09:47 +00001676 // Check the protocol list for unimplemented methods in the @implementation
1677 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001678 // Check and see if class methods in class interface have been
1679 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001680
Chris Lattnercddc8882009-03-01 00:56:52 +00001681 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001682 for (ObjCInterfaceDecl::all_protocol_iterator
1683 PI = I->all_referenced_protocol_begin(),
1684 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001685 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001686 InsMap, ClsMap, I);
1687 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001688 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1689 Categories; Categories = Categories->getNextClassExtension())
1690 ImplMethodsVsClassMethods(S, IMPDecl,
1691 const_cast<ObjCCategoryDecl*>(Categories),
1692 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001693 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001694 // For extended class, unimplemented methods in its protocols will
1695 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001696 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001697 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1698 E = C->protocol_end(); PI != E; ++PI)
1699 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001700 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001701 // Report unimplemented properties in the category as well.
1702 // When reporting on missing setter/getters, do not report when
1703 // setter/getter is implemented in category's primary class
1704 // implementation.
1705 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1706 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1707 for (ObjCImplementationDecl::instmeth_iterator
1708 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1709 InsMap.insert((*I)->getSelector());
1710 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001711 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001712 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001713 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001714 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001715}
1716
Mike Stump1eb44332009-09-09 15:08:12 +00001717/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001718Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001719Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001720 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001721 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001722 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001723 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001724 for (unsigned i = 0; i != NumElts; ++i) {
1725 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001726 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001727 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001728 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001729 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001730 // Maybe we will complain about the shadowed template parameter.
1731 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1732 // Just pretend that we didn't see the previous declaration.
1733 PrevDecl = 0;
1734 }
1735
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001736 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001737 // GCC apparently allows the following idiom:
1738 //
1739 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1740 // @class XCElementToggler;
1741 //
Mike Stump1eb44332009-09-09 15:08:12 +00001742 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001743 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001744 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001745 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001746 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001747 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001748 // a forward class declaration matching a typedef name of a class refers
1749 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001750 if (const ObjCObjectType *OI =
1751 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1752 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001753 }
Chris Lattner4d391482007-12-12 07:09:47 +00001754 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001755 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1756 if (!IDecl) { // Not already seen? Make a forward decl.
1757 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1758 IdentList[i], IdentLocs[i], true);
1759
1760 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1761 // the current DeclContext. This prevents clients that walk DeclContext
1762 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1763 // declared later (if at all). We also take care to explicitly make
1764 // sure this declaration is visible for name lookup.
1765 PushOnScopeChains(IDecl, TUScope, false);
1766 CurContext->makeDeclVisibleInContext(IDecl, true);
1767 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001768 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1769 IDecl, IdentLocs[i]);
1770 CurContext->addDecl(CDecl);
1771 CheckObjCDeclScope(CDecl);
1772 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001773 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001774
1775 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001776}
1777
John McCall0f4c4c42011-06-16 01:15:19 +00001778static bool tryMatchRecordTypes(ASTContext &Context,
1779 Sema::MethodMatchStrategy strategy,
1780 const Type *left, const Type *right);
1781
John McCallf85e1932011-06-15 23:02:42 +00001782static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1783 QualType leftQT, QualType rightQT) {
1784 const Type *left =
1785 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1786 const Type *right =
1787 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1788
1789 if (left == right) return true;
1790
1791 // If we're doing a strict match, the types have to match exactly.
1792 if (strategy == Sema::MMS_strict) return false;
1793
1794 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1795
1796 // Otherwise, use this absurdly complicated algorithm to try to
1797 // validate the basic, low-level compatibility of the two types.
1798
1799 // As a minimum, require the sizes and alignments to match.
1800 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1801 return false;
1802
1803 // Consider all the kinds of non-dependent canonical types:
1804 // - functions and arrays aren't possible as return and parameter types
1805
1806 // - vector types of equal size can be arbitrarily mixed
1807 if (isa<VectorType>(left)) return isa<VectorType>(right);
1808 if (isa<VectorType>(right)) return false;
1809
1810 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001811 // - structs, unions, and Objective-C objects must match more-or-less
1812 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001813 // - everything else should be a scalar
1814 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001815 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001816
John McCall1d9b3b22011-09-09 05:25:32 +00001817 // Make scalars agree in kind, except count bools as chars, and group
1818 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001819 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1820 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1821 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1822 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001823 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1824 leftSK = Type::STK_ObjCObjectPointer;
1825 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1826 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001827
1828 // Note that data member pointers and function member pointers don't
1829 // intermix because of the size differences.
1830
1831 return (leftSK == rightSK);
1832}
Chris Lattner4d391482007-12-12 07:09:47 +00001833
John McCall0f4c4c42011-06-16 01:15:19 +00001834static bool tryMatchRecordTypes(ASTContext &Context,
1835 Sema::MethodMatchStrategy strategy,
1836 const Type *lt, const Type *rt) {
1837 assert(lt && rt && lt != rt);
1838
1839 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1840 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1841 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1842
1843 // Require union-hood to match.
1844 if (left->isUnion() != right->isUnion()) return false;
1845
1846 // Require an exact match if either is non-POD.
1847 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1848 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1849 return false;
1850
1851 // Require size and alignment to match.
1852 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1853
1854 // Require fields to match.
1855 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1856 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1857 for (; li != le && ri != re; ++li, ++ri) {
1858 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1859 return false;
1860 }
1861 return (li == le && ri == re);
1862}
1863
Chris Lattner4d391482007-12-12 07:09:47 +00001864/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1865/// returns true, or false, accordingly.
1866/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001867bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1868 const ObjCMethodDecl *right,
1869 MethodMatchStrategy strategy) {
1870 if (!matchTypes(Context, strategy,
1871 left->getResultType(), right->getResultType()))
1872 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001873
John McCallf85e1932011-06-15 23:02:42 +00001874 if (getLangOptions().ObjCAutoRefCount &&
1875 (left->hasAttr<NSReturnsRetainedAttr>()
1876 != right->hasAttr<NSReturnsRetainedAttr>() ||
1877 left->hasAttr<NSConsumesSelfAttr>()
1878 != right->hasAttr<NSConsumesSelfAttr>()))
1879 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001880
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001881 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001882 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001883
John McCallf85e1932011-06-15 23:02:42 +00001884 for (; li != le; ++li, ++ri) {
1885 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001886 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001887
1888 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1889 return false;
1890
1891 if (getLangOptions().ObjCAutoRefCount &&
1892 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1893 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001894 }
1895 return true;
1896}
1897
Sebastian Redldb9d2142010-08-02 23:18:59 +00001898/// \brief Read the contents of the method pool for a given selector from
1899/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001900///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001901/// This routine should only be called once, when the method pool has no entry
1902/// for this selector.
1903Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001904 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001905 assert(MethodPool.find(Sel) == MethodPool.end() &&
1906 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001907
1908 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001909 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Sebastian Redldb9d2142010-08-02 23:18:59 +00001911 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001912}
1913
Sebastian Redldb9d2142010-08-02 23:18:59 +00001914void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1915 bool instance) {
1916 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1917 if (Pos == MethodPool.end()) {
1918 if (ExternalSource)
1919 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001920 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001921 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1922 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001923 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001924 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001925 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001926 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001927 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001928 Entry.Method = Method;
1929 Entry.Next = 0;
1930 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001931 }
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Chris Lattnerb25df352009-03-04 05:16:45 +00001933 // We've seen a method with this name, see if we have already seen this type
1934 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001935 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1936 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1937
1938 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001939 ObjCMethodDecl *PrevObjCMethod = List->Method;
1940 PrevObjCMethod->setDefined(impl);
1941 // If a method is deprecated, push it in the global pool.
1942 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001943 if (Method->isDeprecated()) {
1944 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001945 List->Method = Method;
1946 }
1947 // If new method is unavailable, push it into global pool
1948 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001949 if (Method->isUnavailable()) {
1950 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001951 List->Method = Method;
1952 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001953 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001954 }
John McCallf85e1932011-06-15 23:02:42 +00001955 }
Mike Stump1eb44332009-09-09 15:08:12 +00001956
Chris Lattnerb25df352009-03-04 05:16:45 +00001957 // We have a new signature for an existing method - add it.
1958 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001959 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1960 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001961}
1962
John McCallf85e1932011-06-15 23:02:42 +00001963/// Determines if this is an "acceptable" loose mismatch in the global
1964/// method pool. This exists mostly as a hack to get around certain
1965/// global mismatches which we can't afford to make warnings / errors.
1966/// Really, what we want is a way to take a method out of the global
1967/// method pool.
1968static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1969 ObjCMethodDecl *other) {
1970 if (!chosen->isInstanceMethod())
1971 return false;
1972
1973 Selector sel = chosen->getSelector();
1974 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1975 return false;
1976
1977 // Don't complain about mismatches for -length if the method we
1978 // chose has an integral result type.
1979 return (chosen->getResultType()->isIntegerType());
1980}
1981
Sebastian Redldb9d2142010-08-02 23:18:59 +00001982ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001983 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001984 bool warn, bool instance) {
1985 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1986 if (Pos == MethodPool.end()) {
1987 if (ExternalSource)
1988 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001989 else
1990 return 0;
1991 }
1992
Sebastian Redldb9d2142010-08-02 23:18:59 +00001993 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001994
Sebastian Redldb9d2142010-08-02 23:18:59 +00001995 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001996 bool issueDiagnostic = false, issueError = false;
1997
1998 // We support a warning which complains about *any* difference in
1999 // method signature.
2000 bool strictSelectorMatch =
2001 (receiverIdOrClass && warn &&
2002 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2003 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002004 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002005 if (strictSelectorMatch)
2006 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002007 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2008 MMS_strict)) {
2009 issueDiagnostic = true;
2010 break;
2011 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002012 }
2013
John McCallf85e1932011-06-15 23:02:42 +00002014 // If we didn't see any strict differences, we won't see any loose
2015 // differences. In ARC, however, we also need to check for loose
2016 // mismatches, because most of them are errors.
2017 if (!strictSelectorMatch ||
2018 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002019 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002020 // This checks if the methods differ in type mismatch.
2021 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2022 MMS_loose) &&
2023 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2024 issueDiagnostic = true;
2025 if (getLangOptions().ObjCAutoRefCount)
2026 issueError = true;
2027 break;
2028 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002029 }
2030
John McCallf85e1932011-06-15 23:02:42 +00002031 if (issueDiagnostic) {
2032 if (issueError)
2033 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2034 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002035 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2036 else
2037 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002038
2039 Diag(MethList.Method->getLocStart(),
2040 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002041 << MethList.Method->getSourceRange();
2042 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2043 Diag(Next->Method->getLocStart(), diag::note_also_found)
2044 << Next->Method->getSourceRange();
2045 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002046 }
2047 return MethList.Method;
2048}
2049
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002050ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002051 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2052 if (Pos == MethodPool.end())
2053 return 0;
2054
2055 GlobalMethods &Methods = Pos->second;
2056
2057 if (Methods.first.Method && Methods.first.Method->isDefined())
2058 return Methods.first.Method;
2059 if (Methods.second.Method && Methods.second.Method->isDefined())
2060 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002061 return 0;
2062}
2063
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002064/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2065/// identical selector names in current and its super classes and issues
2066/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002067void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2068 ObjCMethodDecl *Method,
2069 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002070 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2071 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002072
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002073 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002074 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002075 SD->lookupMethod(Method->getSelector(), IsInstance);
2076 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002077 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002078 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002079 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002080 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2081 E = Method->param_end();
2082 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2083 for (; ParamI != E; ++ParamI, ++PrevI) {
2084 // Number of parameters are the same and is guaranteed by selector match.
2085 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2086 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2087 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002088 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002089 // respective argument type in the super class method, issue warning;
2090 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002091 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002092 << T1 << T2;
2093 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2094 return;
2095 }
2096 }
2097 ID = SD;
2098 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002099}
2100
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002101/// DiagnoseDuplicateIvars -
2102/// Check for duplicate ivars in the entire class at the start of
2103/// @implementation. This becomes necesssary because class extension can
2104/// add ivars to a class in random order which will not be known until
2105/// class's @implementation is seen.
2106void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2107 ObjCInterfaceDecl *SID) {
2108 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2109 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2110 ObjCIvarDecl* Ivar = (*IVI);
2111 if (Ivar->isInvalidDecl())
2112 continue;
2113 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2114 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2115 if (prevIvar) {
2116 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2117 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2118 Ivar->setInvalidDecl();
2119 }
2120 }
2121 }
2122}
2123
Steve Naroffa56f6162007-12-18 01:30:32 +00002124// Note: For class/category implemenations, allMethods/allProperties is
2125// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002126void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002127 Decl **allMethods, unsigned allNum,
2128 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002129 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002130
2131 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002132 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002133 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2134 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002135
Mike Stump1eb44332009-09-09 15:08:12 +00002136 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002137 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2138 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002139 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002140
Ted Kremenek782f2f52010-01-07 01:20:12 +00002141 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2142 // FIXME: This is wrong. We shouldn't be pretending that there is
2143 // an '@end' in the declaration.
Argyrios Kyrtzidis1104d9b2011-10-27 00:09:29 +00002144 SourceLocation L = OCD->getAtStartLoc();
Ted Kremenek782f2f52010-01-07 01:20:12 +00002145 AtEnd.setBegin(L);
2146 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002147 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002148 }
2149
Steve Naroff0701bbb2009-01-08 17:28:14 +00002150 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2151 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2152 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2153
Chris Lattner4d391482007-12-12 07:09:47 +00002154 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002155 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002156 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002157
2158 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002159 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002160 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002161 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002162 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002163 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002164 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002165 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002166 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002167 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002168 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002169 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002170 } else {
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +00002171 if (PrevMethod)
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002172 Method->setAsRedeclaration(PrevMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002173 InsMap[Method->getSelector()] = Method;
2174 /// The following allows us to typecheck messages to "id".
2175 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002176 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002177 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002178 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002179 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002180 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002181 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002182 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002183 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002184 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002185 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002186 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002187 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002188 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002189 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002190 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002191 } else {
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +00002192 if (PrevMethod)
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002193 Method->setAsRedeclaration(PrevMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002194 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002195 /// The following allows us to typecheck messages to "Class".
2196 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002197 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002198 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002199 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002200 }
2201 }
2202 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002203 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002204 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002205 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002206 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002207 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002208 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002209 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002210 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002211 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002212
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002213 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002214 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002215 if (C->IsClassExtension()) {
2216 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2217 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002218 }
Chris Lattner4d391482007-12-12 07:09:47 +00002219 }
Steve Naroff09c47192009-01-09 15:36:25 +00002220 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002221 if (CDecl->getIdentifier())
2222 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2223 // user-defined setter/getter. It also synthesizes setter/getter methods
2224 // and adds them to the DeclContext and global method pools.
2225 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2226 E = CDecl->prop_end();
2227 I != E; ++I)
2228 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002229 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002230 }
2231 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002232 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002233 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002234 // Any property declared in a class extension might have user
2235 // declared setter or getter in current class extension or one
2236 // of the other class extensions. Mark them as synthesized as
2237 // property will be synthesized when property with same name is
2238 // seen in the @implementation.
2239 for (const ObjCCategoryDecl *ClsExtDecl =
2240 IDecl->getFirstClassExtension();
2241 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2242 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2243 E = ClsExtDecl->prop_end(); I != E; ++I) {
2244 ObjCPropertyDecl *Property = (*I);
2245 // Skip over properties declared @dynamic
2246 if (const ObjCPropertyImplDecl *PIDecl
2247 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2248 if (PIDecl->getPropertyImplementation()
2249 == ObjCPropertyImplDecl::Dynamic)
2250 continue;
2251
2252 for (const ObjCCategoryDecl *CExtDecl =
2253 IDecl->getFirstClassExtension();
2254 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2255 if (ObjCMethodDecl *GetterMethod =
2256 CExtDecl->getInstanceMethod(Property->getGetterName()))
2257 GetterMethod->setSynthesized(true);
2258 if (!Property->isReadOnly())
2259 if (ObjCMethodDecl *SetterMethod =
2260 CExtDecl->getInstanceMethod(Property->getSetterName()))
2261 SetterMethod->setSynthesized(true);
2262 }
2263 }
2264 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002265 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002266 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002267 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002268
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002269 if (LangOpts.ObjCNonFragileABI2)
2270 while (IDecl->getSuperClass()) {
2271 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2272 IDecl = IDecl->getSuperClass();
2273 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002274 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002275 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002276 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002277 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002278 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002279
Chris Lattner4d391482007-12-12 07:09:47 +00002280 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002281 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002282 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002283 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002284 Categories; Categories = Categories->getNextClassCategory()) {
2285 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002286 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002287 break;
2288 }
2289 }
2290 }
2291 }
Chris Lattner682bf922009-03-29 16:50:03 +00002292 if (isInterfaceDeclKind) {
2293 // Reject invalid vardecls.
2294 for (unsigned i = 0; i != tuvNum; i++) {
2295 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2296 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2297 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002298 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002299 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002300 }
Chris Lattner682bf922009-03-29 16:50:03 +00002301 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002302 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002303 ActOnObjCContainerFinishDefinition();
Argyrios Kyrtzidisb4a686d2011-10-17 19:48:13 +00002304
2305 for (unsigned i = 0; i != tuvNum; i++) {
2306 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2307 Consumer.HandleTopLevelDeclInObjCContainer(DG);
2308 }
Chris Lattner4d391482007-12-12 07:09:47 +00002309}
2310
2311
2312/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2313/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002314static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002315CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002316 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002317}
2318
Ted Kremenek422bae72010-04-18 04:59:38 +00002319static inline
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002320bool containsInvalidMethodImplAttribute(ObjCMethodDecl *IMD,
2321 const AttrVec &A) {
2322 // If method is only declared in implementation (private method),
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002323 // No need to issue any diagnostics on method definition with attributes.
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002324 if (!IMD)
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002325 return false;
2326
Fariborz Jahanianee28a4b2011-10-22 01:56:45 +00002327 // method declared in interface has no attribute.
2328 // But implementation has attributes. This is invalid
2329 if (!IMD->hasAttrs())
2330 return true;
2331
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002332 const AttrVec &D = IMD->getAttrs();
2333 if (D.size() != A.size())
2334 return true;
2335
2336 // attributes on method declaration and definition must match exactly.
2337 // Note that we have at most a couple of attributes on methods, so this
2338 // n*n search is good enough.
2339 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i) {
2340 bool match = false;
2341 for (AttrVec::const_iterator i1 = D.begin(), e1 = D.end(); i1 != e1; ++i1) {
2342 if ((*i)->getKind() == (*i1)->getKind()) {
2343 match = true;
2344 break;
2345 }
2346 }
2347 if (!match)
Sean Huntcf807c42010-08-18 23:23:40 +00002348 return true;
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002349 }
Sean Huntcf807c42010-08-18 23:23:40 +00002350 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002351}
2352
Douglas Gregore97179c2011-09-08 01:46:34 +00002353namespace {
2354 /// \brief Describes the compatibility of a result type with its method.
2355 enum ResultTypeCompatibilityKind {
2356 RTC_Compatible,
2357 RTC_Incompatible,
2358 RTC_Unknown
2359 };
2360}
2361
Douglas Gregor926df6c2011-06-11 01:09:30 +00002362/// \brief Check whether the declared result type of the given Objective-C
2363/// method declaration is compatible with the method's class.
2364///
Douglas Gregore97179c2011-09-08 01:46:34 +00002365static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002366CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2367 ObjCInterfaceDecl *CurrentClass) {
2368 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002369
2370 // If an Objective-C method inherits its related result type, then its
2371 // declared result type must be compatible with its own class type. The
2372 // declared result type is compatible if:
2373 if (const ObjCObjectPointerType *ResultObjectType
2374 = ResultType->getAs<ObjCObjectPointerType>()) {
2375 // - it is id or qualified id, or
2376 if (ResultObjectType->isObjCIdType() ||
2377 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002378 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002379
2380 if (CurrentClass) {
2381 if (ObjCInterfaceDecl *ResultClass
2382 = ResultObjectType->getInterfaceDecl()) {
2383 // - it is the same as the method's class type, or
2384 if (CurrentClass == ResultClass)
Douglas Gregore97179c2011-09-08 01:46:34 +00002385 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002386
2387 // - it is a superclass of the method's class type
2388 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002389 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002390 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002391 } else {
2392 // Any Objective-C pointer type might be acceptable for a protocol
2393 // method; we just don't know.
2394 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002395 }
2396 }
2397
Douglas Gregore97179c2011-09-08 01:46:34 +00002398 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002399}
2400
John McCall6c2c2502011-07-22 02:45:48 +00002401namespace {
2402/// A helper class for searching for methods which a particular method
2403/// overrides.
2404class OverrideSearch {
2405 Sema &S;
2406 ObjCMethodDecl *Method;
2407 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2408 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2409 bool Recursive;
2410
2411public:
2412 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2413 Selector selector = method->getSelector();
2414
2415 // Bypass this search if we've never seen an instance/class method
2416 // with this selector before.
2417 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2418 if (it == S.MethodPool.end()) {
2419 if (!S.ExternalSource) return;
2420 it = S.ReadMethodPool(selector);
2421 }
2422 ObjCMethodList &list =
2423 method->isInstanceMethod() ? it->second.first : it->second.second;
2424 if (!list.Method) return;
2425
2426 ObjCContainerDecl *container
2427 = cast<ObjCContainerDecl>(method->getDeclContext());
2428
2429 // Prevent the search from reaching this container again. This is
2430 // important with categories, which override methods from the
2431 // interface and each other.
2432 Searched.insert(container);
2433 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002434 }
John McCall6c2c2502011-07-22 02:45:48 +00002435
2436 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2437 iterator begin() const { return Overridden.begin(); }
2438 iterator end() const { return Overridden.end(); }
2439
2440private:
2441 void searchFromContainer(ObjCContainerDecl *container) {
2442 if (container->isInvalidDecl()) return;
2443
2444 switch (container->getDeclKind()) {
2445#define OBJCCONTAINER(type, base) \
2446 case Decl::type: \
2447 searchFrom(cast<type##Decl>(container)); \
2448 break;
2449#define ABSTRACT_DECL(expansion)
2450#define DECL(type, base) \
2451 case Decl::type:
2452#include "clang/AST/DeclNodes.inc"
2453 llvm_unreachable("not an ObjC container!");
2454 }
2455 }
2456
2457 void searchFrom(ObjCProtocolDecl *protocol) {
2458 // A method in a protocol declaration overrides declarations from
2459 // referenced ("parent") protocols.
2460 search(protocol->getReferencedProtocols());
2461 }
2462
2463 void searchFrom(ObjCCategoryDecl *category) {
2464 // A method in a category declaration overrides declarations from
2465 // the main class and from protocols the category references.
2466 search(category->getClassInterface());
2467 search(category->getReferencedProtocols());
2468 }
2469
2470 void searchFrom(ObjCCategoryImplDecl *impl) {
2471 // A method in a category definition that has a category
2472 // declaration overrides declarations from the category
2473 // declaration.
2474 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2475 search(category);
2476
2477 // Otherwise it overrides declarations from the class.
2478 } else {
2479 search(impl->getClassInterface());
2480 }
2481 }
2482
2483 void searchFrom(ObjCInterfaceDecl *iface) {
2484 // A method in a class declaration overrides declarations from
2485
2486 // - categories,
2487 for (ObjCCategoryDecl *category = iface->getCategoryList();
2488 category; category = category->getNextClassCategory())
2489 search(category);
2490
2491 // - the super class, and
2492 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2493 search(super);
2494
2495 // - any referenced protocols.
2496 search(iface->getReferencedProtocols());
2497 }
2498
2499 void searchFrom(ObjCImplementationDecl *impl) {
2500 // A method in a class implementation overrides declarations from
2501 // the class interface.
2502 search(impl->getClassInterface());
2503 }
2504
2505
2506 void search(const ObjCProtocolList &protocols) {
2507 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2508 i != e; ++i)
2509 search(*i);
2510 }
2511
2512 void search(ObjCContainerDecl *container) {
2513 // Abort if we've already searched this container.
2514 if (!Searched.insert(container)) return;
2515
2516 // Check for a method in this container which matches this selector.
2517 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2518 Method->isInstanceMethod());
2519
2520 // If we find one, record it and bail out.
2521 if (meth) {
2522 Overridden.insert(meth);
2523 return;
2524 }
2525
2526 // Otherwise, search for methods that a hypothetical method here
2527 // would have overridden.
2528
2529 // Note that we're now in a recursive case.
2530 Recursive = true;
2531
2532 searchFromContainer(container);
2533 }
2534};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002535}
2536
John McCalld226f652010-08-21 09:40:31 +00002537Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002538 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002539 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002540 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002541 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002542 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002543 Selector Sel,
2544 // optional arguments. The number of types/arguments is obtained
2545 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002546 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002547 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002548 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002549 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002550 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002551 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002552 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002553 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002554 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002555 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2556 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002557 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002558
Douglas Gregore97179c2011-09-08 01:46:34 +00002559 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002560 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002561 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002562 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002563
Steve Naroffccef3712009-02-20 22:59:16 +00002564 // Methods cannot return interface types. All ObjC objects are
2565 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002566 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002567 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2568 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002569 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002570 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002571
2572 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002573 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002574 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002575 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002576 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002577 }
Mike Stump1eb44332009-09-09 15:08:12 +00002578
2579 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002580 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002581 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002582 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002583 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002584 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002585 /*isSynthesized=*/false,
2586 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002587 MethodDeclKind == tok::objc_optional
2588 ? ObjCMethodDecl::Optional
2589 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002590 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002591
Chris Lattner5f9e2722011-07-23 10:55:15 +00002592 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002593
Chris Lattner7db638d2009-04-11 19:42:43 +00002594 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002595 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002596 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002597
Chris Lattnere294d3f2009-04-11 18:57:04 +00002598 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002599 ArgType = Context.getObjCIdType();
2600 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002601 } else {
John McCall58e46772009-10-23 21:48:59 +00002602 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002603 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002604 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002605 }
Mike Stump1eb44332009-09-09 15:08:12 +00002606
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002607 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2608 LookupOrdinaryName, ForRedeclaration);
2609 LookupName(R, S);
2610 if (R.isSingleResult()) {
2611 NamedDecl *PrevDecl = R.getFoundDecl();
2612 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002613 Diag(ArgInfo[i].NameLoc,
2614 (MethodDefinition ? diag::warn_method_param_redefinition
2615 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002616 << ArgInfo[i].Name;
2617 Diag(PrevDecl->getLocation(),
2618 diag::note_previous_declaration);
2619 }
2620 }
2621
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002622 SourceLocation StartLoc = DI
2623 ? DI->getTypeLoc().getBeginLoc()
2624 : ArgInfo[i].NameLoc;
2625
John McCall81ef3e62011-04-23 02:46:06 +00002626 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2627 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2628 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002629
John McCall70798862011-05-02 00:30:12 +00002630 Param->setObjCMethodScopeInfo(i);
2631
Chris Lattner0ed844b2008-04-04 06:12:32 +00002632 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002633 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002634
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002635 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002636 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002637
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002638 S->AddDecl(Param);
2639 IdResolver.AddDecl(Param);
2640
Chris Lattner0ed844b2008-04-04 06:12:32 +00002641 Params.push_back(Param);
2642 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002643
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002644 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002645 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002646 QualType ArgType = Param->getType();
2647 if (ArgType.isNull())
2648 ArgType = Context.getObjCIdType();
2649 else
2650 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002651 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002652 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002653 Diag(Param->getLocation(),
2654 diag::err_object_cannot_be_passed_returned_by_value)
2655 << 1 << ArgType;
2656 Param->setInvalidDecl();
2657 }
2658 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002659
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002660 Params.push_back(Param);
2661 }
2662
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002663 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002664 ObjCMethod->setObjCDeclQualifier(
2665 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002666
2667 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002668 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002669
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002670 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002671 const ObjCMethodDecl *PrevMethod = 0;
2672 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002673 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002674 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2675 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002676 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002677 PrevMethod = ImpDecl->getClassMethod(Sel);
2678 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002679 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002680
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002681 ObjCMethodDecl *IMD = 0;
2682 if (ObjCInterfaceDecl *IDecl = ImpDecl->getClassInterface())
2683 IMD = IDecl->lookupMethod(ObjCMethod->getSelector(),
2684 ObjCMethod->isInstanceMethod());
Sean Huntcf807c42010-08-18 23:23:40 +00002685 if (ObjCMethod->hasAttrs() &&
Fariborz Jahanian7fda4002011-10-22 01:21:15 +00002686 containsInvalidMethodImplAttribute(IMD, ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002687 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002688 } else {
2689 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002690 }
John McCall6c2c2502011-07-22 02:45:48 +00002691
Chris Lattner4d391482007-12-12 07:09:47 +00002692 if (PrevMethod) {
2693 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002694 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002695 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002696 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002697 }
John McCall54abf7d2009-11-04 02:18:39 +00002698
Douglas Gregor926df6c2011-06-11 01:09:30 +00002699 // If this Objective-C method does not have a related result type, but we
2700 // are allowed to infer related result types, try to do so based on the
2701 // method family.
2702 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2703 if (!CurrentClass) {
2704 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2705 CurrentClass = Cat->getClassInterface();
2706 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2707 CurrentClass = Impl->getClassInterface();
2708 else if (ObjCCategoryImplDecl *CatImpl
2709 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2710 CurrentClass = CatImpl->getClassInterface();
2711 }
John McCall6c2c2502011-07-22 02:45:48 +00002712
Douglas Gregore97179c2011-09-08 01:46:34 +00002713 ResultTypeCompatibilityKind RTC
2714 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002715
2716 // Search for overridden methods and merge information down from them.
2717 OverrideSearch overrides(*this, ObjCMethod);
2718 for (OverrideSearch::iterator
2719 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2720 ObjCMethodDecl *overridden = *i;
2721
2722 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002723 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002724 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002725
2726 // Then merge the declarations.
2727 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002728
2729 // Check for overriding methods
2730 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00002731 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2732 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2733 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
Douglas Gregor926df6c2011-06-11 01:09:30 +00002734 }
2735
John McCallf85e1932011-06-15 23:02:42 +00002736 bool ARCError = false;
2737 if (getLangOptions().ObjCAutoRefCount)
2738 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2739
Douglas Gregore97179c2011-09-08 01:46:34 +00002740 // Infer the related result type when possible.
2741 if (!ARCError && RTC == RTC_Compatible &&
2742 !ObjCMethod->hasRelatedResultType() &&
2743 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002744 bool InferRelatedResultType = false;
2745 switch (ObjCMethod->getMethodFamily()) {
2746 case OMF_None:
2747 case OMF_copy:
2748 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002749 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002750 case OMF_mutableCopy:
2751 case OMF_release:
2752 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002753 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002754 break;
2755
2756 case OMF_alloc:
2757 case OMF_new:
2758 InferRelatedResultType = ObjCMethod->isClassMethod();
2759 break;
2760
2761 case OMF_init:
2762 case OMF_autorelease:
2763 case OMF_retain:
2764 case OMF_self:
2765 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2766 break;
2767 }
2768
John McCall6c2c2502011-07-22 02:45:48 +00002769 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002770 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002771 }
2772
John McCalld226f652010-08-21 09:40:31 +00002773 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002774}
2775
Chris Lattnercc98eac2008-12-17 07:13:27 +00002776bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002777 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002778 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002779 // Following is also an error. But it is caused by a missing @end
2780 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002781 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2782 return false;
2783 }
2784
Anders Carlsson15281452008-11-04 16:57:32 +00002785 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2786 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002787
Anders Carlsson15281452008-11-04 16:57:32 +00002788 return true;
2789}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002790
Chris Lattnercc98eac2008-12-17 07:13:27 +00002791/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2792/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002793void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002794 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002795 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002796 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002797 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002798 if (!Class) {
2799 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2800 return;
2801 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002802 if (LangOpts.ObjCNonFragileABI) {
2803 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2804 return;
2805 }
Mike Stump1eb44332009-09-09 15:08:12 +00002806
Chris Lattnercc98eac2008-12-17 07:13:27 +00002807 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002808 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002809 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002810 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002811 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002812 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002813 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002814 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2815 /*FIXME: StartL=*/ID->getLocation(),
2816 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002817 ID->getIdentifier(), ID->getType(),
2818 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002819 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002820 }
Mike Stump1eb44332009-09-09 15:08:12 +00002821
Chris Lattnercc98eac2008-12-17 07:13:27 +00002822 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002823 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002824 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002825 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002826 if (getLangOptions().CPlusPlus)
2827 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002828 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002829 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002830 }
2831}
2832
Douglas Gregor160b5632010-04-26 17:32:49 +00002833/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002834VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2835 SourceLocation StartLoc,
2836 SourceLocation IdLoc,
2837 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002838 bool Invalid) {
2839 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2840 // duration shall not be qualified by an address-space qualifier."
2841 // Since all parameters have automatic store duration, they can not have
2842 // an address space.
2843 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002844 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002845 Invalid = true;
2846 }
2847
2848 // An @catch parameter must be an unqualified object pointer type;
2849 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2850 if (Invalid) {
2851 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002852 } else if (T->isDependentType()) {
2853 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002854 } else if (!T->isObjCObjectPointerType()) {
2855 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002856 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002857 } else if (T->isObjCQualifiedIdType()) {
2858 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002859 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002860 }
2861
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002862 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2863 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002864 New->setExceptionVariable(true);
2865
Douglas Gregor160b5632010-04-26 17:32:49 +00002866 if (Invalid)
2867 New->setInvalidDecl();
2868 return New;
2869}
2870
John McCalld226f652010-08-21 09:40:31 +00002871Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002872 const DeclSpec &DS = D.getDeclSpec();
2873
2874 // We allow the "register" storage class on exception variables because
2875 // GCC did, but we drop it completely. Any other storage class is an error.
2876 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2877 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2878 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2879 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2880 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2881 << DS.getStorageClassSpec();
2882 }
2883 if (D.getDeclSpec().isThreadSpecified())
2884 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2885 D.getMutableDeclSpec().ClearStorageClassSpecs();
2886
2887 DiagnoseFunctionSpecifiers(D);
2888
2889 // Check that there are no default arguments inside the type of this
2890 // exception object (C++ only).
2891 if (getLangOptions().CPlusPlus)
2892 CheckExtraCXXDefaultArguments(D);
2893
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002894 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002895 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002896
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002897 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2898 D.getSourceRange().getBegin(),
2899 D.getIdentifierLoc(),
2900 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002901 D.isInvalidType());
2902
2903 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2904 if (D.getCXXScopeSpec().isSet()) {
2905 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2906 << D.getCXXScopeSpec().getRange();
2907 New->setInvalidDecl();
2908 }
2909
2910 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002911 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002912 if (D.getIdentifier())
2913 IdResolver.AddDecl(New);
2914
2915 ProcessDeclAttributes(S, New, D);
2916
2917 if (New->hasAttr<BlocksAttr>())
2918 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002919 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002920}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002921
2922/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002923/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002924void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002925 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002926 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2927 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002928 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002929 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002930 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002931 }
2932}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002933
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002934void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002935 // Load referenced selectors from the external source.
2936 if (ExternalSource) {
2937 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2938 ExternalSource->ReadReferencedSelectors(Sels);
2939 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2940 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2941 }
2942
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002943 // Warning will be issued only when selector table is
2944 // generated (which means there is at lease one implementation
2945 // in the TU). This is to match gcc's behavior.
2946 if (ReferencedSelectors.empty() ||
2947 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002948 return;
2949 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2950 ReferencedSelectors.begin(),
2951 E = ReferencedSelectors.end(); S != E; ++S) {
2952 Selector Sel = (*S).first;
2953 if (!LookupImplementedMethodInGlobalPool(Sel))
2954 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2955 }
2956 return;
2957}