blob: de48de15d77c405549078c89b02a4670ca2be5b2 [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"
John McCallf85e1932011-06-15 23:02:42 +000024#include "clang/Basic/SourceManager.h"
John McCall19510852010-08-20 18:27:03 +000025#include "clang/Sema/DeclSpec.h"
John McCall50df6ae2010-08-25 07:03:20 +000026#include "llvm/ADT/DenseSet.h"
27
Chris Lattner4d391482007-12-12 07:09:47 +000028using namespace clang;
29
John McCallf85e1932011-06-15 23:02:42 +000030/// Check whether the given method, which must be in the 'init'
31/// family, is a valid member of that family.
32///
33/// \param receiverTypeIfCall - if null, check this as if declaring it;
34/// if non-null, check this as if making a call to it with the given
35/// receiver type
36///
37/// \return true to indicate that there was an error and appropriate
38/// actions were taken
39bool Sema::checkInitMethod(ObjCMethodDecl *method,
40 QualType receiverTypeIfCall) {
41 if (method->isInvalidDecl()) return true;
42
43 // This castAs is safe: methods that don't return an object
44 // pointer won't be inferred as inits and will reject an explicit
45 // objc_method_family(init).
46
47 // We ignore protocols here. Should we? What about Class?
48
49 const ObjCObjectType *result = method->getResultType()
50 ->castAs<ObjCObjectPointerType>()->getObjectType();
51
52 if (result->isObjCId()) {
53 return false;
54 } else if (result->isObjCClass()) {
55 // fall through: always an error
56 } else {
57 ObjCInterfaceDecl *resultClass = result->getInterface();
58 assert(resultClass && "unexpected object type!");
59
60 // It's okay for the result type to still be a forward declaration
61 // if we're checking an interface declaration.
62 if (resultClass->isForwardDecl()) {
63 if (receiverTypeIfCall.isNull() &&
64 !isa<ObjCImplementationDecl>(method->getDeclContext()))
65 return false;
66
67 // Otherwise, we try to compare class types.
68 } else {
69 // If this method was declared in a protocol, we can't check
70 // anything unless we have a receiver type that's an interface.
71 const ObjCInterfaceDecl *receiverClass = 0;
72 if (isa<ObjCProtocolDecl>(method->getDeclContext())) {
73 if (receiverTypeIfCall.isNull())
74 return false;
75
76 receiverClass = receiverTypeIfCall->castAs<ObjCObjectPointerType>()
77 ->getInterfaceDecl();
78
79 // This can be null for calls to e.g. id<Foo>.
80 if (!receiverClass) return false;
81 } else {
82 receiverClass = method->getClassInterface();
83 assert(receiverClass && "method not associated with a class!");
84 }
85
86 // If either class is a subclass of the other, it's fine.
87 if (receiverClass->isSuperClassOf(resultClass) ||
88 resultClass->isSuperClassOf(receiverClass))
89 return false;
90 }
91 }
92
93 SourceLocation loc = method->getLocation();
94
95 // If we're in a system header, and this is not a call, just make
96 // the method unusable.
97 if (receiverTypeIfCall.isNull() && getSourceManager().isInSystemHeader(loc)) {
98 method->addAttr(new (Context) UnavailableAttr(loc, Context,
99 "init method returns a type unrelated to its receiver type"));
100 return true;
101 }
102
103 // Otherwise, it's an error.
104 Diag(loc, diag::err_arc_init_method_unrelated_result_type);
105 method->setInvalidDecl();
106 return true;
107}
108
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000109void Sema::CheckObjCMethodOverride(ObjCMethodDecl *NewMethod,
Douglas Gregor926df6c2011-06-11 01:09:30 +0000110 const ObjCMethodDecl *Overridden,
111 bool IsImplementation) {
112 if (Overridden->hasRelatedResultType() &&
113 !NewMethod->hasRelatedResultType()) {
114 // This can only happen when the method follows a naming convention that
115 // implies a related result type, and the original (overridden) method has
116 // a suitable return type, but the new (overriding) method does not have
117 // a suitable return type.
118 QualType ResultType = NewMethod->getResultType();
119 SourceRange ResultTypeRange;
120 if (const TypeSourceInfo *ResultTypeInfo
John McCallf85e1932011-06-15 23:02:42 +0000121 = NewMethod->getResultTypeSourceInfo())
Douglas Gregor926df6c2011-06-11 01:09:30 +0000122 ResultTypeRange = ResultTypeInfo->getTypeLoc().getSourceRange();
123
124 // Figure out which class this method is part of, if any.
125 ObjCInterfaceDecl *CurrentClass
126 = dyn_cast<ObjCInterfaceDecl>(NewMethod->getDeclContext());
127 if (!CurrentClass) {
128 DeclContext *DC = NewMethod->getDeclContext();
129 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(DC))
130 CurrentClass = Cat->getClassInterface();
131 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(DC))
132 CurrentClass = Impl->getClassInterface();
133 else if (ObjCCategoryImplDecl *CatImpl
134 = dyn_cast<ObjCCategoryImplDecl>(DC))
135 CurrentClass = CatImpl->getClassInterface();
136 }
137
138 if (CurrentClass) {
139 Diag(NewMethod->getLocation(),
140 diag::warn_related_result_type_compatibility_class)
141 << Context.getObjCInterfaceType(CurrentClass)
142 << ResultType
143 << ResultTypeRange;
144 } else {
145 Diag(NewMethod->getLocation(),
146 diag::warn_related_result_type_compatibility_protocol)
147 << ResultType
148 << ResultTypeRange;
149 }
150
Douglas Gregore97179c2011-09-08 01:46:34 +0000151 if (ObjCMethodFamily Family = Overridden->getMethodFamily())
152 Diag(Overridden->getLocation(),
153 diag::note_related_result_type_overridden_family)
154 << Family;
155 else
156 Diag(Overridden->getLocation(),
157 diag::note_related_result_type_overridden);
Douglas Gregor926df6c2011-06-11 01:09:30 +0000158 }
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000159 if (getLangOptions().ObjCAutoRefCount) {
160 if ((NewMethod->hasAttr<NSReturnsRetainedAttr>() !=
161 Overridden->hasAttr<NSReturnsRetainedAttr>())) {
162 Diag(NewMethod->getLocation(),
163 diag::err_nsreturns_retained_attribute_mismatch) << 1;
164 Diag(Overridden->getLocation(), diag::note_previous_decl)
165 << "method";
166 }
167 if ((NewMethod->hasAttr<NSReturnsNotRetainedAttr>() !=
168 Overridden->hasAttr<NSReturnsNotRetainedAttr>())) {
169 Diag(NewMethod->getLocation(),
170 diag::err_nsreturns_retained_attribute_mismatch) << 0;
171 Diag(Overridden->getLocation(), diag::note_previous_decl)
172 << "method";
173 }
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000174 ObjCMethodDecl::param_const_iterator oi = Overridden->param_begin();
175 for (ObjCMethodDecl::param_iterator
176 ni = NewMethod->param_begin(), ne = NewMethod->param_end();
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000177 ni != ne; ++ni, ++oi) {
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +0000178 const ParmVarDecl *oldDecl = (*oi);
Fariborz Jahanian3240fe32011-09-27 22:35:36 +0000179 ParmVarDecl *newDecl = (*ni);
180 if (newDecl->hasAttr<NSConsumedAttr>() !=
181 oldDecl->hasAttr<NSConsumedAttr>()) {
182 Diag(newDecl->getLocation(),
183 diag::err_nsconsumed_attribute_mismatch);
184 Diag(oldDecl->getLocation(), diag::note_previous_decl)
185 << "parameter";
186 }
187 }
188 }
Douglas Gregor926df6c2011-06-11 01:09:30 +0000189}
190
John McCallf85e1932011-06-15 23:02:42 +0000191/// \brief Check a method declaration for compatibility with the Objective-C
192/// ARC conventions.
193static bool CheckARCMethodDecl(Sema &S, ObjCMethodDecl *method) {
194 ObjCMethodFamily family = method->getMethodFamily();
195 switch (family) {
196 case OMF_None:
197 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000198 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000199 case OMF_retain:
200 case OMF_release:
201 case OMF_autorelease:
202 case OMF_retainCount:
203 case OMF_self:
John McCall6c2c2502011-07-22 02:45:48 +0000204 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000205 return false;
206
207 case OMF_init:
208 // If the method doesn't obey the init rules, don't bother annotating it.
209 if (S.checkInitMethod(method, QualType()))
210 return true;
211
212 method->addAttr(new (S.Context) NSConsumesSelfAttr(SourceLocation(),
213 S.Context));
214
215 // Don't add a second copy of this attribute, but otherwise don't
216 // let it be suppressed.
217 if (method->hasAttr<NSReturnsRetainedAttr>())
218 return false;
219 break;
220
221 case OMF_alloc:
222 case OMF_copy:
223 case OMF_mutableCopy:
224 case OMF_new:
225 if (method->hasAttr<NSReturnsRetainedAttr>() ||
226 method->hasAttr<NSReturnsNotRetainedAttr>() ||
227 method->hasAttr<NSReturnsAutoreleasedAttr>())
228 return false;
229 break;
230 }
231
232 method->addAttr(new (S.Context) NSReturnsRetainedAttr(SourceLocation(),
233 S.Context));
234 return false;
235}
236
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000237static void DiagnoseObjCImplementedDeprecations(Sema &S,
238 NamedDecl *ND,
239 SourceLocation ImplLoc,
240 int select) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000241 if (ND && ND->isDeprecated()) {
Fariborz Jahanian98d810e2011-02-16 00:30:31 +0000242 S.Diag(ImplLoc, diag::warn_deprecated_def) << select;
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000243 if (select == 0)
244 S.Diag(ND->getLocation(), diag::note_method_declared_at);
245 else
246 S.Diag(ND->getLocation(), diag::note_previous_decl) << "class";
247 }
248}
249
Fariborz Jahanian140ab232011-08-31 17:37:55 +0000250/// AddAnyMethodToGlobalPool - Add any method, instance or factory to global
251/// pool.
252void Sema::AddAnyMethodToGlobalPool(Decl *D) {
253 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
254
255 // If we don't have a valid method decl, simply return.
256 if (!MDecl)
257 return;
258 if (MDecl->isInstanceMethod())
259 AddInstanceMethodToGlobalPool(MDecl, true);
260 else
261 AddFactoryMethodToGlobalPool(MDecl, true);
262}
263
Steve Naroffebf64432009-02-28 16:59:13 +0000264/// ActOnStartOfObjCMethodDef - This routine sets up parameters; invisible
Chris Lattner4d391482007-12-12 07:09:47 +0000265/// and user declared, in the method definition's AST.
John McCalld226f652010-08-21 09:40:31 +0000266void Sema::ActOnStartOfObjCMethodDef(Scope *FnBodyScope, Decl *D) {
Argyrios Kyrtzidis53d0ea52008-06-28 06:07:14 +0000267 assert(getCurMethodDecl() == 0 && "Method parsing confused");
John McCalld226f652010-08-21 09:40:31 +0000268 ObjCMethodDecl *MDecl = dyn_cast_or_null<ObjCMethodDecl>(D);
Mike Stump1eb44332009-09-09 15:08:12 +0000269
Steve Naroff394f3f42008-07-25 17:57:26 +0000270 // If we don't have a valid method decl, simply return.
271 if (!MDecl)
272 return;
Steve Naroffa56f6162007-12-18 01:30:32 +0000273
Chris Lattner4d391482007-12-12 07:09:47 +0000274 // Allow all of Sema to see that we are entering a method definition.
Douglas Gregor44b43212008-12-11 16:49:14 +0000275 PushDeclContext(FnBodyScope, MDecl);
Douglas Gregor9ea9bdb2010-03-01 23:15:13 +0000276 PushFunctionScope();
277
Chris Lattner4d391482007-12-12 07:09:47 +0000278 // Create Decl objects for each parameter, entrring them in the scope for
279 // binding to their use.
Chris Lattner4d391482007-12-12 07:09:47 +0000280
281 // Insert the invisible arguments, self and _cmd!
Fariborz Jahanianfef30b52008-12-09 20:23:04 +0000282 MDecl->createImplicitParams(Context, MDecl->getClassInterface());
Mike Stump1eb44332009-09-09 15:08:12 +0000283
Daniel Dunbar451318c2008-08-26 06:07:48 +0000284 PushOnScopeChains(MDecl->getSelfDecl(), FnBodyScope);
285 PushOnScopeChains(MDecl->getCmdDecl(), FnBodyScope);
Chris Lattner04421082008-04-08 04:40:51 +0000286
Chris Lattner8123a952008-04-10 02:22:51 +0000287 // Introduce all of the other parameters into this scope.
Chris Lattner89951a82009-02-20 18:43:26 +0000288 for (ObjCMethodDecl::param_iterator PI = MDecl->param_begin(),
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000289 E = MDecl->param_end(); PI != E; ++PI) {
290 ParmVarDecl *Param = (*PI);
291 if (!Param->isInvalidDecl() &&
292 RequireCompleteType(Param->getLocation(), Param->getType(),
293 diag::err_typecheck_decl_incomplete_type))
294 Param->setInvalidDecl();
Chris Lattner89951a82009-02-20 18:43:26 +0000295 if ((*PI)->getIdentifier())
296 PushOnScopeChains(*PI, FnBodyScope);
Fariborz Jahanian23c01042010-09-17 22:07:07 +0000297 }
John McCallf85e1932011-06-15 23:02:42 +0000298
299 // In ARC, disallow definition of retain/release/autorelease/retainCount
300 if (getLangOptions().ObjCAutoRefCount) {
301 switch (MDecl->getMethodFamily()) {
302 case OMF_retain:
303 case OMF_retainCount:
304 case OMF_release:
305 case OMF_autorelease:
306 Diag(MDecl->getLocation(), diag::err_arc_illegal_method_def)
307 << MDecl->getSelector();
308 break;
309
310 case OMF_None:
311 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +0000312 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +0000313 case OMF_alloc:
314 case OMF_init:
315 case OMF_mutableCopy:
316 case OMF_copy:
317 case OMF_new:
318 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +0000319 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +0000320 break;
321 }
322 }
323
Nico Weber9a1ecf02011-08-22 17:25:57 +0000324 // Warn on deprecated methods under -Wdeprecated-implementations,
325 // and prepare for warning on missing super calls.
326 if (ObjCInterfaceDecl *IC = MDecl->getClassInterface()) {
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000327 if (ObjCMethodDecl *IMD =
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000328 IC->lookupMethod(MDecl->getSelector(), MDecl->isInstanceMethod()))
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000329 DiagnoseObjCImplementedDeprecations(*this,
330 dyn_cast<NamedDecl>(IMD),
331 MDecl->getLocation(), 0);
Nico Weber9a1ecf02011-08-22 17:25:57 +0000332
Nico Weber80cb6e62011-08-28 22:35:17 +0000333 // If this is "dealloc" or "finalize", set some bit here.
Nico Weber9a1ecf02011-08-22 17:25:57 +0000334 // Then in ActOnSuperMessage() (SemaExprObjC), set it back to false.
335 // Finally, in ActOnFinishFunctionBody() (SemaDecl), warn if flag is set.
336 // Only do this if the current class actually has a superclass.
Nico Weber80cb6e62011-08-28 22:35:17 +0000337 if (IC->getSuperClass()) {
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000338 ObjCShouldCallSuperDealloc =
Ted Kremenek8cd8de42011-09-28 19:32:29 +0000339 !(Context.getLangOptions().ObjCAutoRefCount ||
340 Context.getLangOptions().getGC() == LangOptions::GCOnly) &&
Ted Kremenek4eb14ca2011-08-22 19:07:43 +0000341 MDecl->getMethodFamily() == OMF_dealloc;
Nico Weber27f07762011-08-29 22:59:14 +0000342 ObjCShouldCallSuperFinalize =
Ted Kremenek8cd8de42011-09-28 19:32:29 +0000343 Context.getLangOptions().getGC() != LangOptions::NonGC &&
Nico Weber27f07762011-08-29 22:59:14 +0000344 MDecl->getMethodFamily() == OMF_finalize;
Nico Weber80cb6e62011-08-28 22:35:17 +0000345 }
Nico Weber9a1ecf02011-08-22 17:25:57 +0000346 }
Chris Lattner4d391482007-12-12 07:09:47 +0000347}
348
John McCalld226f652010-08-21 09:40:31 +0000349Decl *Sema::
Chris Lattner7caeabd2008-07-21 22:17:28 +0000350ActOnStartClassInterface(SourceLocation AtInterfaceLoc,
351 IdentifierInfo *ClassName, SourceLocation ClassLoc,
352 IdentifierInfo *SuperName, SourceLocation SuperLoc,
John McCalld226f652010-08-21 09:40:31 +0000353 Decl * const *ProtoRefs, unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000354 const SourceLocation *ProtoLocs,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000355 SourceLocation EndProtoLoc, AttributeList *AttrList) {
Chris Lattner4d391482007-12-12 07:09:47 +0000356 assert(ClassName && "Missing class identifier");
Mike Stump1eb44332009-09-09 15:08:12 +0000357
Chris Lattner4d391482007-12-12 07:09:47 +0000358 // Check for another declaration kind with the same name.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000359 NamedDecl *PrevDecl = LookupSingleName(TUScope, ClassName, ClassLoc,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000360 LookupOrdinaryName, ForRedeclaration);
Douglas Gregor72c3f312008-12-05 18:15:24 +0000361
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000362 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000363 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000364 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000367 ObjCInterfaceDecl* IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
368 if (IDecl) {
Chris Lattner4d391482007-12-12 07:09:47 +0000369 // Class already seen. Is it a forward declaration?
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000370 if (!IDecl->isForwardDecl()) {
371 IDecl->setInvalidDecl();
372 Diag(AtInterfaceLoc, diag::err_duplicate_class_def)<<IDecl->getDeclName();
373 Diag(IDecl->getLocation(), diag::note_previous_definition);
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000374
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000375 // Return the previous class interface.
376 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000377 return IDecl;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000378 } else {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000379 IDecl->setLocation(ClassLoc);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000380 IDecl->setForwardDecl(false);
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000381 IDecl->setAtStartLoc(AtInterfaceLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000382 // If the forward decl was in a PCH, we need to write it again in a
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000383 // dependent AST file.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000384 IDecl->setChangedSinceDeserialization(true);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000385
386 // Since this ObjCInterfaceDecl was created by a forward declaration,
387 // we now add it to the DeclContext since it wasn't added before
388 // (see ActOnForwardClassDeclaration).
389 IDecl->setLexicalDeclContext(CurContext);
390 CurContext->addDecl(IDecl);
391
392 if (AttrList)
393 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
Chris Lattner4d391482007-12-12 07:09:47 +0000394 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000395 } else {
396 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtInterfaceLoc,
397 ClassName, ClassLoc);
398 if (AttrList)
399 ProcessDeclAttributeList(TUScope, IDecl, AttrList);
400
401 PushOnScopeChains(IDecl, TUScope);
Chris Lattner4d391482007-12-12 07:09:47 +0000402 }
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Chris Lattner4d391482007-12-12 07:09:47 +0000404 if (SuperName) {
Chris Lattner4d391482007-12-12 07:09:47 +0000405 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000406 PrevDecl = LookupSingleName(TUScope, SuperName, SuperLoc,
407 LookupOrdinaryName);
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000408
409 if (!PrevDecl) {
410 // Try to correct for a typo in the superclass name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000411 TypoCorrection Corrected = CorrectTypo(
412 DeclarationNameInfo(SuperName, SuperLoc), LookupOrdinaryName, TUScope,
413 NULL, NULL, false, CTC_NoKeywords);
414 if ((PrevDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000415 Diag(SuperLoc, diag::err_undef_superclass_suggest)
416 << SuperName << ClassName << PrevDecl->getDeclName();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000417 Diag(PrevDecl->getLocation(), diag::note_previous_decl)
418 << PrevDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000419 }
420 }
421
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000422 if (PrevDecl == IDecl) {
423 Diag(SuperLoc, diag::err_recursive_superclass)
424 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
425 IDecl->setLocEnd(ClassLoc);
Mike Stumpac5fc7c2009-08-04 21:02:39 +0000426 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000427 ObjCInterfaceDecl *SuperClassDecl =
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000428 dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner3c73c412008-11-19 08:23:25 +0000429
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000430 // Diagnose classes that inherit from deprecated classes.
431 if (SuperClassDecl)
432 (void)DiagnoseUseOfDecl(SuperClassDecl, SuperLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000433
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000434 if (PrevDecl && SuperClassDecl == 0) {
435 // The previous declaration was not a class decl. Check if we have a
436 // typedef. If we do, get the underlying class type.
Richard Smith162e1c12011-04-15 14:24:37 +0000437 if (const TypedefNameDecl *TDecl =
438 dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000439 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000440 if (T->isObjCObjectType()) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000441 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface())
442 SuperClassDecl = dyn_cast<ObjCInterfaceDecl>(IDecl);
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000443 }
444 }
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000446 // This handles the following case:
447 //
448 // typedef int SuperClass;
449 // @interface MyClass : SuperClass {} @end
450 //
451 if (!SuperClassDecl) {
452 Diag(SuperLoc, diag::err_redefinition_different_kind) << SuperName;
453 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000454 }
455 }
Mike Stump1eb44332009-09-09 15:08:12 +0000456
Richard Smith162e1c12011-04-15 14:24:37 +0000457 if (!dyn_cast_or_null<TypedefNameDecl>(PrevDecl)) {
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000458 if (!SuperClassDecl)
459 Diag(SuperLoc, diag::err_undef_superclass)
460 << SuperName << ClassName << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000461 else if (SuperClassDecl->isForwardDecl()) {
462 Diag(SuperLoc, diag::err_forward_superclass)
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000463 << SuperClassDecl->getDeclName() << ClassName
464 << SourceRange(AtInterfaceLoc, ClassLoc);
Fariborz Jahaniana8139732011-06-23 23:16:19 +0000465 Diag(SuperClassDecl->getLocation(), diag::note_forward_class);
466 SuperClassDecl = 0;
467 }
Steve Naroff818cb9e2009-02-04 17:14:05 +0000468 }
Fariborz Jahanianfdee0892009-07-09 22:08:26 +0000469 IDecl->setSuperClass(SuperClassDecl);
470 IDecl->setSuperClassLoc(SuperLoc);
471 IDecl->setLocEnd(SuperLoc);
Steve Naroff818cb9e2009-02-04 17:14:05 +0000472 }
Chris Lattner4d391482007-12-12 07:09:47 +0000473 } else { // we have a root class.
474 IDecl->setLocEnd(ClassLoc);
475 }
Mike Stump1eb44332009-09-09 15:08:12 +0000476
Sebastian Redl0b17c612010-08-13 00:28:03 +0000477 // Check then save referenced protocols.
Chris Lattner06036d32008-07-26 04:13:19 +0000478 if (NumProtoRefs) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000479 IDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000480 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000481 IDecl->setLocEnd(EndProtoLoc);
482 }
Mike Stump1eb44332009-09-09 15:08:12 +0000483
Anders Carlsson15281452008-11-04 16:57:32 +0000484 CheckObjCDeclScope(IDecl);
John McCalld226f652010-08-21 09:40:31 +0000485 return IDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000486}
487
488/// ActOnCompatiblityAlias - this action is called after complete parsing of
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000489/// @compatibility_alias declaration. It sets up the alias relationships.
John McCalld226f652010-08-21 09:40:31 +0000490Decl *Sema::ActOnCompatiblityAlias(SourceLocation AtLoc,
491 IdentifierInfo *AliasName,
492 SourceLocation AliasLocation,
493 IdentifierInfo *ClassName,
494 SourceLocation ClassLocation) {
Chris Lattner4d391482007-12-12 07:09:47 +0000495 // Look for previous declaration of alias name
Douglas Gregorc83c6872010-04-15 22:33:43 +0000496 NamedDecl *ADecl = LookupSingleName(TUScope, AliasName, AliasLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000497 LookupOrdinaryName, ForRedeclaration);
Chris Lattner4d391482007-12-12 07:09:47 +0000498 if (ADecl) {
Chris Lattner8b265bd2008-11-23 23:20:13 +0000499 if (isa<ObjCCompatibleAliasDecl>(ADecl))
Chris Lattner4d391482007-12-12 07:09:47 +0000500 Diag(AliasLocation, diag::warn_previous_alias_decl);
Chris Lattner8b265bd2008-11-23 23:20:13 +0000501 else
Chris Lattner3c73c412008-11-19 08:23:25 +0000502 Diag(AliasLocation, diag::err_conflicting_aliasing_type) << AliasName;
Chris Lattner8b265bd2008-11-23 23:20:13 +0000503 Diag(ADecl->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000504 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000505 }
506 // Check for class declaration
Douglas Gregorc83c6872010-04-15 22:33:43 +0000507 NamedDecl *CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000508 LookupOrdinaryName, ForRedeclaration);
Richard Smith162e1c12011-04-15 14:24:37 +0000509 if (const TypedefNameDecl *TDecl =
510 dyn_cast_or_null<TypedefNameDecl>(CDeclU)) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000511 QualType T = TDecl->getUnderlyingType();
John McCallc12c5bb2010-05-15 11:32:37 +0000512 if (T->isObjCObjectType()) {
513 if (NamedDecl *IDecl = T->getAs<ObjCObjectType>()->getInterface()) {
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000514 ClassName = IDecl->getIdentifier();
Douglas Gregorc83c6872010-04-15 22:33:43 +0000515 CDeclU = LookupSingleName(TUScope, ClassName, ClassLocation,
Douglas Gregorc0b39642010-04-15 23:40:53 +0000516 LookupOrdinaryName, ForRedeclaration);
Fariborz Jahanian305c6582009-01-08 01:10:55 +0000517 }
518 }
519 }
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000520 ObjCInterfaceDecl *CDecl = dyn_cast_or_null<ObjCInterfaceDecl>(CDeclU);
521 if (CDecl == 0) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000522 Diag(ClassLocation, diag::warn_undef_interface) << ClassName;
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000523 if (CDeclU)
Chris Lattner8b265bd2008-11-23 23:20:13 +0000524 Diag(CDeclU->getLocation(), diag::note_previous_declaration);
John McCalld226f652010-08-21 09:40:31 +0000525 return 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000526 }
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Chris Lattnerf8d17a52008-03-16 21:17:37 +0000528 // Everything checked out, instantiate a new alias declaration AST.
Mike Stump1eb44332009-09-09 15:08:12 +0000529 ObjCCompatibleAliasDecl *AliasDecl =
Douglas Gregord0434102009-01-09 00:49:46 +0000530 ObjCCompatibleAliasDecl::Create(Context, CurContext, AtLoc, AliasName, CDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Anders Carlsson15281452008-11-04 16:57:32 +0000532 if (!CheckObjCDeclScope(AliasDecl))
Douglas Gregor516ff432009-04-24 02:57:34 +0000533 PushOnScopeChains(AliasDecl, TUScope);
Douglas Gregord0434102009-01-09 00:49:46 +0000534
John McCalld226f652010-08-21 09:40:31 +0000535 return AliasDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000536}
537
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000538bool Sema::CheckForwardProtocolDeclarationForCircularDependency(
Steve Naroff61d68522009-03-05 15:22:01 +0000539 IdentifierInfo *PName,
540 SourceLocation &Ploc, SourceLocation PrevLoc,
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000541 const ObjCList<ObjCProtocolDecl> &PList) {
542
543 bool res = false;
Steve Naroff61d68522009-03-05 15:22:01 +0000544 for (ObjCList<ObjCProtocolDecl>::iterator I = PList.begin(),
545 E = PList.end(); I != E; ++I) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000546 if (ObjCProtocolDecl *PDecl = LookupProtocol((*I)->getIdentifier(),
547 Ploc)) {
Steve Naroff61d68522009-03-05 15:22:01 +0000548 if (PDecl->getIdentifier() == PName) {
549 Diag(Ploc, diag::err_protocol_has_circular_dependency);
550 Diag(PrevLoc, diag::note_previous_definition);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000551 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000552 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000553 if (CheckForwardProtocolDeclarationForCircularDependency(PName, Ploc,
554 PDecl->getLocation(), PDecl->getReferencedProtocols()))
555 res = true;
Steve Naroff61d68522009-03-05 15:22:01 +0000556 }
557 }
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000558 return res;
Steve Naroff61d68522009-03-05 15:22:01 +0000559}
560
John McCalld226f652010-08-21 09:40:31 +0000561Decl *
Chris Lattnere13b9592008-07-26 04:03:38 +0000562Sema::ActOnStartProtocolInterface(SourceLocation AtProtoInterfaceLoc,
563 IdentifierInfo *ProtocolName,
564 SourceLocation ProtocolLoc,
John McCalld226f652010-08-21 09:40:31 +0000565 Decl * const *ProtoRefs,
Chris Lattnere13b9592008-07-26 04:03:38 +0000566 unsigned NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000567 const SourceLocation *ProtoLocs,
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000568 SourceLocation EndProtoLoc,
569 AttributeList *AttrList) {
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000570 bool err = false;
Daniel Dunbar246e70f2008-09-26 04:48:09 +0000571 // FIXME: Deal with AttrList.
Chris Lattner4d391482007-12-12 07:09:47 +0000572 assert(ProtocolName && "Missing protocol identifier");
Douglas Gregorc83c6872010-04-15 22:33:43 +0000573 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolName, ProtocolLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000574 if (PDecl) {
575 // Protocol already seen. Better be a forward protocol declaration
Chris Lattner439e71f2008-03-16 01:25:17 +0000576 if (!PDecl->isForwardDecl()) {
Fariborz Jahaniane2573e52009-04-06 23:43:32 +0000577 Diag(ProtocolLoc, diag::warn_duplicate_protocol_def) << ProtocolName;
Chris Lattnerb8b96af2008-11-23 22:46:27 +0000578 Diag(PDecl->getLocation(), diag::note_previous_definition);
Chris Lattner439e71f2008-03-16 01:25:17 +0000579 // Just return the protocol we already had.
580 // FIXME: don't leak the objects passed in!
John McCalld226f652010-08-21 09:40:31 +0000581 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000582 }
Steve Naroff61d68522009-03-05 15:22:01 +0000583 ObjCList<ObjCProtocolDecl> PList;
Mike Stump1eb44332009-09-09 15:08:12 +0000584 PList.set((ObjCProtocolDecl *const*)ProtoRefs, NumProtoRefs, Context);
Fariborz Jahanian819e9bf2011-05-13 18:02:08 +0000585 err = CheckForwardProtocolDeclarationForCircularDependency(
586 ProtocolName, ProtocolLoc, PDecl->getLocation(), PList);
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Steve Narofff11b5082008-08-13 16:39:22 +0000588 // Make sure the cached decl gets a valid start location.
Argyrios Kyrtzidisa1e797e2011-10-05 19:37:56 +0000589 PDecl->setAtStartLoc(AtProtoInterfaceLoc);
590 PDecl->setLocation(ProtocolLoc);
Chris Lattner439e71f2008-03-16 01:25:17 +0000591 PDecl->setForwardDecl(false);
Fariborz Jahanianca4c40a2011-08-25 22:26:53 +0000592 // Since this ObjCProtocolDecl was created by a forward declaration,
593 // we now add it to the DeclContext since it wasn't added before
594 PDecl->setLexicalDeclContext(CurContext);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000595 CurContext->addDecl(PDecl);
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000596 // Repeat in dependent AST files.
Sebastian Redl0b17c612010-08-13 00:28:03 +0000597 PDecl->setChangedSinceDeserialization(true);
Chris Lattner439e71f2008-03-16 01:25:17 +0000598 } else {
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000599 PDecl = ObjCProtocolDecl::Create(Context, CurContext, ProtocolName,
600 ProtocolLoc, AtProtoInterfaceLoc);
Douglas Gregor6e378de2009-04-23 23:18:26 +0000601 PushOnScopeChains(PDecl, TUScope);
Chris Lattnerc8581052008-03-16 20:19:15 +0000602 PDecl->setForwardDecl(false);
Chris Lattnercca59d72008-03-16 01:23:04 +0000603 }
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000604 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000605 ProcessDeclAttributeList(TUScope, PDecl, AttrList);
Fariborz Jahanian96b69a72011-05-12 22:04:39 +0000606 if (!err && NumProtoRefs ) {
Chris Lattnerc8581052008-03-16 20:19:15 +0000607 /// Check then save referenced protocols.
Douglas Gregor18df52b2010-01-16 15:02:53 +0000608 PDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
609 ProtoLocs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000610 PDecl->setLocEnd(EndProtoLoc);
611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
613 CheckObjCDeclScope(PDecl);
John McCalld226f652010-08-21 09:40:31 +0000614 return PDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000615}
616
617/// FindProtocolDeclaration - This routine looks up protocols and
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +0000618/// issues an error if they are not declared. It returns list of
619/// protocol declarations in its 'Protocols' argument.
Chris Lattner4d391482007-12-12 07:09:47 +0000620void
Chris Lattnere13b9592008-07-26 04:03:38 +0000621Sema::FindProtocolDeclaration(bool WarnOnDeclarations,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000622 const IdentifierLocPair *ProtocolId,
Chris Lattner4d391482007-12-12 07:09:47 +0000623 unsigned NumProtocols,
Chris Lattner5f9e2722011-07-23 10:55:15 +0000624 SmallVectorImpl<Decl *> &Protocols) {
Chris Lattner4d391482007-12-12 07:09:47 +0000625 for (unsigned i = 0; i != NumProtocols; ++i) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000626 ObjCProtocolDecl *PDecl = LookupProtocol(ProtocolId[i].first,
627 ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000628 if (!PDecl) {
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000629 TypoCorrection Corrected = CorrectTypo(
630 DeclarationNameInfo(ProtocolId[i].first, ProtocolId[i].second),
631 LookupObjCProtocolName, TUScope, NULL, NULL, false, CTC_NoKeywords);
632 if ((PDecl = Corrected.getCorrectionDeclAs<ObjCProtocolDecl>())) {
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000633 Diag(ProtocolId[i].second, diag::err_undeclared_protocol_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000634 << ProtocolId[i].first << Corrected.getCorrection();
Douglas Gregor67dd1d42010-01-07 00:17:44 +0000635 Diag(PDecl->getLocation(), diag::note_previous_decl)
636 << PDecl->getDeclName();
Douglas Gregorf06cdae2010-01-03 18:01:57 +0000637 }
638 }
639
640 if (!PDecl) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000641 Diag(ProtocolId[i].second, diag::err_undeclared_protocol)
Chris Lattner3c73c412008-11-19 08:23:25 +0000642 << ProtocolId[i].first;
Chris Lattnereacc3922008-07-26 03:47:43 +0000643 continue;
644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Douglas Gregor48f3bb92009-02-18 21:56:37 +0000646 (void)DiagnoseUseOfDecl(PDecl, ProtocolId[i].second);
Chris Lattnereacc3922008-07-26 03:47:43 +0000647
648 // If this is a forward declaration and we are supposed to warn in this
649 // case, do it.
650 if (WarnOnDeclarations && PDecl->isForwardDecl())
Chris Lattnerfa25bbb2008-11-19 05:08:23 +0000651 Diag(ProtocolId[i].second, diag::warn_undef_protocolref)
Chris Lattner3c73c412008-11-19 08:23:25 +0000652 << ProtocolId[i].first;
John McCalld226f652010-08-21 09:40:31 +0000653 Protocols.push_back(PDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000654 }
655}
656
Fariborz Jahanian78c39c72009-03-02 19:06:08 +0000657/// DiagnoseClassExtensionDupMethods - Check for duplicate declaration of
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000658/// a class method in its extension.
659///
Mike Stump1eb44332009-09-09 15:08:12 +0000660void Sema::DiagnoseClassExtensionDupMethods(ObjCCategoryDecl *CAT,
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000661 ObjCInterfaceDecl *ID) {
662 if (!ID)
663 return; // Possibly due to previous error
664
665 llvm::DenseMap<Selector, const ObjCMethodDecl*> MethodMap;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000666 for (ObjCInterfaceDecl::method_iterator i = ID->meth_begin(),
667 e = ID->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000668 ObjCMethodDecl *MD = *i;
669 MethodMap[MD->getSelector()] = MD;
670 }
671
672 if (MethodMap.empty())
673 return;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000674 for (ObjCCategoryDecl::method_iterator i = CAT->meth_begin(),
675 e = CAT->meth_end(); i != e; ++i) {
Fariborz Jahanianb7f95f52009-03-02 19:05:07 +0000676 ObjCMethodDecl *Method = *i;
677 const ObjCMethodDecl *&PrevMethod = MethodMap[Method->getSelector()];
678 if (PrevMethod && !MatchTwoMethodDeclarations(Method, PrevMethod)) {
679 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
680 << Method->getDeclName();
681 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
682 }
683 }
684}
685
Chris Lattner58fe03b2009-04-12 08:43:13 +0000686/// ActOnForwardProtocolDeclaration - Handle @protocol foo;
John McCalld226f652010-08-21 09:40:31 +0000687Decl *
Chris Lattner4d391482007-12-12 07:09:47 +0000688Sema::ActOnForwardProtocolDeclaration(SourceLocation AtProtocolLoc,
Chris Lattner7caeabd2008-07-21 22:17:28 +0000689 const IdentifierLocPair *IdentList,
Fariborz Jahanianbc1c8772008-12-17 01:07:27 +0000690 unsigned NumElts,
691 AttributeList *attrList) {
Chris Lattner5f9e2722011-07-23 10:55:15 +0000692 SmallVector<ObjCProtocolDecl*, 32> Protocols;
693 SmallVector<SourceLocation, 8> ProtoLocs;
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Chris Lattner4d391482007-12-12 07:09:47 +0000695 for (unsigned i = 0; i != NumElts; ++i) {
Chris Lattner7caeabd2008-07-21 22:17:28 +0000696 IdentifierInfo *Ident = IdentList[i].first;
Douglas Gregorc83c6872010-04-15 22:33:43 +0000697 ObjCProtocolDecl *PDecl = LookupProtocol(Ident, IdentList[i].second);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000698 bool isNew = false;
Douglas Gregord0434102009-01-09 00:49:46 +0000699 if (PDecl == 0) { // Not already seen?
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000700 PDecl = ObjCProtocolDecl::Create(Context, CurContext, Ident,
701 IdentList[i].second, AtProtocolLoc);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000702 PushOnScopeChains(PDecl, TUScope, false);
703 isNew = true;
Douglas Gregord0434102009-01-09 00:49:46 +0000704 }
Sebastian Redl0b17c612010-08-13 00:28:03 +0000705 if (attrList) {
Douglas Gregor9cdda0c2009-06-17 21:51:59 +0000706 ProcessDeclAttributeList(TUScope, PDecl, attrList);
Sebastian Redl0b17c612010-08-13 00:28:03 +0000707 if (!isNew)
708 PDecl->setChangedSinceDeserialization(true);
709 }
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;
John McCalld226f652010-08-21 09:40:31 +0000744 return 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
773 // If the interface is deprecated, warn about it.
774 (void)DiagnoseUseOfDecl(IDecl, ClassLoc);
775
Chris Lattner4d391482007-12-12 07:09:47 +0000776 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000777 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000778 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000779 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000780 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000781 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000782 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000783 }
Mike Stump1eb44332009-09-09 15:08:12 +0000784
Anders Carlsson15281452008-11-04 16:57:32 +0000785 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000786 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000787}
788
789/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000790/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000791/// object.
John McCalld226f652010-08-21 09:40:31 +0000792Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000793 SourceLocation AtCatImplLoc,
794 IdentifierInfo *ClassName, SourceLocation ClassLoc,
795 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000796 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000797 ObjCCategoryDecl *CatIDecl = 0;
798 if (IDecl) {
799 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
800 if (!CatIDecl) {
801 // Category @implementation with no corresponding @interface.
802 // Create and install one.
803 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000804 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000805 CatName, IDecl);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000806 }
807 }
808
Mike Stump1eb44332009-09-09 15:08:12 +0000809 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000810 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
811 ClassLoc, AtCatImplLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000812 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000813 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000814 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000815 CDecl->setInvalidDecl();
816 }
Chris Lattner4d391482007-12-12 07:09:47 +0000817
Douglas Gregord0434102009-01-09 00:49:46 +0000818 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000819 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000820
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000821 /// Check that CatName, category name, is not used in another implementation.
822 if (CatIDecl) {
823 if (CatIDecl->getImplementation()) {
824 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
825 << CatName;
826 Diag(CatIDecl->getImplementation()->getLocation(),
827 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000828 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000829 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000830 // Warn on implementating category of deprecated class under
831 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000832 DiagnoseObjCImplementedDeprecations(*this,
833 dyn_cast<NamedDecl>(IDecl),
834 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000835 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000836 }
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Anders Carlsson15281452008-11-04 16:57:32 +0000838 CheckObjCDeclScope(CDecl);
John McCalld226f652010-08-21 09:40:31 +0000839 return CDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000840}
841
John McCalld226f652010-08-21 09:40:31 +0000842Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000843 SourceLocation AtClassImplLoc,
844 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000845 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000846 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000847 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000848 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000849 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000850 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
851 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000852 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000853 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000854 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000855 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
856 // If this is a forward declaration of an interface, warn.
857 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000858 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000859 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000860 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000861 } else {
862 // We did not find anything with the name ClassName; try to correct for
863 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000864 TypoCorrection Corrected = CorrectTypo(
865 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
866 NULL, NULL, false, CTC_NoKeywords);
867 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000868 // Suggest the (potentially) correct interface name. However, put the
869 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000870 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000871 // provide a code-modification hint or use the typo name for recovery,
872 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000873 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000874 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000875 << ClassName << CorrectedName;
876 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
877 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000878 IDecl = 0;
879 } else {
880 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
881 }
Chris Lattner4d391482007-12-12 07:09:47 +0000882 }
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Chris Lattner4d391482007-12-12 07:09:47 +0000884 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000885 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000886 if (SuperClassname) {
887 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000888 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
889 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000890 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000891 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
892 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000893 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000894 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000895 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000896 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000897 Diag(SuperClassLoc, diag::err_undef_superclass)
898 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000899 else if (IDecl && IDecl->getSuperClass() != SDecl) {
900 // This implementation and its interface do not have the same
901 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000902 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000903 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000904 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000905 }
906 }
907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Chris Lattner4d391482007-12-12 07:09:47 +0000909 if (!IDecl) {
910 // Legacy case of @implementation with no corresponding @interface.
911 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000912
Mike Stump390b4cc2009-05-16 07:39:55 +0000913 // FIXME: Do we support attributes on the @implementation? If so we should
914 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000915 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000916 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000917 IDecl->setSuperClass(SDecl);
918 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000919
920 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000921 } else {
922 // Mark the interface as being completed, even if it was just as
923 // @class ....;
924 // declaration; the user cannot reopen it.
925 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000926 }
Mike Stump1eb44332009-09-09 15:08:12 +0000927
928 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000929 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
930 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Anders Carlsson15281452008-11-04 16:57:32 +0000932 if (CheckObjCDeclScope(IMPDecl))
John McCalld226f652010-08-21 09:40:31 +0000933 return IMPDecl;
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Chris Lattner4d391482007-12-12 07:09:47 +0000935 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000936 if (IDecl->getImplementation()) {
937 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000938 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000939 Diag(IDecl->getImplementation()->getLocation(),
940 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000941 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000942 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000943 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000944 // Warn on implementating deprecated class under
945 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000946 DiagnoseObjCImplementedDeprecations(*this,
947 dyn_cast<NamedDecl>(IDecl),
948 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000949 }
John McCalld226f652010-08-21 09:40:31 +0000950 return IMPDecl;
Chris Lattner4d391482007-12-12 07:09:47 +0000951}
952
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000953void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
954 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000955 SourceLocation RBrace) {
956 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000957 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000958 if (!IDecl)
959 return;
960 /// Check case of non-existing @interface decl.
961 /// (legacy objective-c @implementation decl without an @interface decl).
962 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000963 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000964 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000965 // Add ivar's to class's DeclContext.
966 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000967 ivars[i]->setLexicalDeclContext(ImpDecl);
968 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000969 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000970 }
971
Chris Lattner4d391482007-12-12 07:09:47 +0000972 return;
973 }
974 // If implementation has empty ivar list, just return.
975 if (numIvars == 0)
976 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Chris Lattner4d391482007-12-12 07:09:47 +0000978 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000979 if (LangOpts.ObjCNonFragileABI2) {
980 if (ImpDecl->getSuperClass())
981 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
982 for (unsigned i = 0; i < numIvars; i++) {
983 ObjCIvarDecl* ImplIvar = ivars[i];
984 if (const ObjCIvarDecl *ClsIvar =
985 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
986 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
987 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
988 continue;
989 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000990 // Instance ivar to Implementation's DeclContext.
991 ImplIvar->setLexicalDeclContext(ImpDecl);
992 IDecl->makeDeclVisibleInContext(ImplIvar, false);
993 ImpDecl->addDecl(ImplIvar);
994 }
995 return;
996 }
Chris Lattner4d391482007-12-12 07:09:47 +0000997 // Check interface's Ivar list against those in the implementation.
998 // names and types must match.
999 //
Chris Lattner4d391482007-12-12 07:09:47 +00001000 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001001 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001002 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1003 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001004 ObjCIvarDecl* ImplIvar = ivars[j++];
1005 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001006 assert (ImplIvar && "missing implementation ivar");
1007 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001008
Steve Naroffca331292009-03-03 14:49:36 +00001009 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +00001010 if (Context.getCanonicalType(ImplIvar->getType()) !=
1011 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001012 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001013 << ImplIvar->getIdentifier()
1014 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001015 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +00001016 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
1017 Expr *ImplBitWidth = ImplIvar->getBitWidth();
1018 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001019 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
1020 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +00001021 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
1022 << ImplIvar->getIdentifier();
1023 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
1024 }
Mike Stump1eb44332009-09-09 15:08:12 +00001025 }
Steve Naroffca331292009-03-03 14:49:36 +00001026 // Make sure the names are identical.
1027 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001028 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001029 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001030 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001031 }
1032 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001033 }
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Chris Lattner609e4c72007-12-12 18:11:49 +00001035 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001036 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001037 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001038 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001039}
1040
Steve Naroff3c2eb662008-02-10 21:38:56 +00001041void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001042 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001043 // No point warning no definition of method which is 'unavailable'.
1044 if (method->hasAttr<UnavailableAttr>())
1045 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001046 if (!IncompleteImpl) {
1047 Diag(ImpLoc, diag::warn_incomplete_impl);
1048 IncompleteImpl = true;
1049 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001050 if (DiagID == diag::warn_unimplemented_protocol_method)
1051 Diag(ImpLoc, DiagID) << method->getDeclName();
1052 else
1053 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001054}
1055
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001056/// Determines if type B can be substituted for type A. Returns true if we can
1057/// guarantee that anything that the user will do to an object of type A can
1058/// also be done to an object of type B. This is trivially true if the two
1059/// types are the same, or if B is a subclass of A. It becomes more complex
1060/// in cases where protocols are involved.
1061///
1062/// Object types in Objective-C describe the minimum requirements for an
1063/// object, rather than providing a complete description of a type. For
1064/// example, if A is a subclass of B, then B* may refer to an instance of A.
1065/// The principle of substitutability means that we may use an instance of A
1066/// anywhere that we may use an instance of B - it will implement all of the
1067/// ivars of B and all of the methods of B.
1068///
1069/// This substitutability is important when type checking methods, because
1070/// the implementation may have stricter type definitions than the interface.
1071/// The interface specifies minimum requirements, but the implementation may
1072/// have more accurate ones. For example, a method may privately accept
1073/// instances of B, but only publish that it accepts instances of A. Any
1074/// object passed to it will be type checked against B, and so will implicitly
1075/// by a valid A*. Similarly, a method may return a subclass of the class that
1076/// it is declared as returning.
1077///
1078/// This is most important when considering subclassing. A method in a
1079/// subclass must accept any object as an argument that its superclass's
1080/// implementation accepts. It may, however, accept a more general type
1081/// without breaking substitutability (i.e. you can still use the subclass
1082/// anywhere that you can use the superclass, but not vice versa). The
1083/// converse requirement applies to return types: the return type for a
1084/// subclass method must be a valid object of the kind that the superclass
1085/// advertises, but it may be specified more accurately. This avoids the need
1086/// for explicit down-casting by callers.
1087///
1088/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001089static bool isObjCTypeSubstitutable(ASTContext &Context,
1090 const ObjCObjectPointerType *A,
1091 const ObjCObjectPointerType *B,
1092 bool rejectId) {
1093 // Reject a protocol-unqualified id.
1094 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001095
1096 // If B is a qualified id, then A must also be a qualified id and it must
1097 // implement all of the protocols in B. It may not be a qualified class.
1098 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1099 // stricter definition so it is not substitutable for id<A>.
1100 if (B->isObjCQualifiedIdType()) {
1101 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001102 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1103 QualType(B,0),
1104 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001105 }
1106
1107 /*
1108 // id is a special type that bypasses type checking completely. We want a
1109 // warning when it is used in one place but not another.
1110 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1111
1112
1113 // If B is a qualified id, then A must also be a qualified id (which it isn't
1114 // if we've got this far)
1115 if (B->isObjCQualifiedIdType()) return false;
1116 */
1117
1118 // Now we know that A and B are (potentially-qualified) class types. The
1119 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001120 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001121}
1122
John McCall10302c02010-10-28 02:34:38 +00001123static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1124 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1125}
1126
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001127static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001128 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001129 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001130 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001131 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001132 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001133 if (IsProtocolMethodDecl &&
1134 (MethodDecl->getObjCDeclQualifier() !=
1135 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001136 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001137 S.Diag(MethodImpl->getLocation(),
1138 (IsOverridingMode ?
1139 diag::warn_conflicting_overriding_ret_type_modifiers
1140 : diag::warn_conflicting_ret_type_modifiers))
1141 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001142 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1143 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1144 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1145 }
1146 else
1147 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001148 }
1149
John McCall10302c02010-10-28 02:34:38 +00001150 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001151 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001152 return true;
1153 if (!Warn)
1154 return false;
John McCall10302c02010-10-28 02:34:38 +00001155
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001156 unsigned DiagID =
1157 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1158 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001159
1160 // Mismatches between ObjC pointers go into a different warning
1161 // category, and sometimes they're even completely whitelisted.
1162 if (const ObjCObjectPointerType *ImplPtrTy =
1163 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1164 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001165 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001166 // Allow non-matching return types as long as they don't violate
1167 // the principle of substitutability. Specifically, we permit
1168 // return types that are subclasses of the declared return type,
1169 // or that are more-qualified versions of the declared type.
1170 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001171 return false;
John McCall10302c02010-10-28 02:34:38 +00001172
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001173 DiagID =
1174 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1175 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001176 }
1177 }
1178
1179 S.Diag(MethodImpl->getLocation(), DiagID)
1180 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001181 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001182 << MethodImpl->getResultType()
1183 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001184 S.Diag(MethodDecl->getLocation(),
1185 IsOverridingMode ? diag::note_previous_declaration
1186 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001187 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001188 return false;
John McCall10302c02010-10-28 02:34:38 +00001189}
1190
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001191static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001192 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001193 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001194 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001195 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001196 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001197 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001198 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001199 if (IsProtocolMethodDecl &&
1200 (ImplVar->getObjCDeclQualifier() !=
1201 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001202 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001203 if (IsOverridingMode)
1204 S.Diag(ImplVar->getLocation(),
1205 diag::warn_conflicting_overriding_param_modifiers)
1206 << getTypeRange(ImplVar->getTypeSourceInfo())
1207 << MethodImpl->getDeclName();
1208 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001209 diag::warn_conflicting_param_modifiers)
1210 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001211 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001212 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1213 << getTypeRange(IfaceVar->getTypeSourceInfo());
1214 }
1215 else
1216 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001217 }
1218
John McCall10302c02010-10-28 02:34:38 +00001219 QualType ImplTy = ImplVar->getType();
1220 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001221
John McCall10302c02010-10-28 02:34:38 +00001222 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001223 return true;
1224
1225 if (!Warn)
1226 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001227 unsigned DiagID =
1228 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1229 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001230
1231 // Mismatches between ObjC pointers go into a different warning
1232 // category, and sometimes they're even completely whitelisted.
1233 if (const ObjCObjectPointerType *ImplPtrTy =
1234 ImplTy->getAs<ObjCObjectPointerType>()) {
1235 if (const ObjCObjectPointerType *IfacePtrTy =
1236 IfaceTy->getAs<ObjCObjectPointerType>()) {
1237 // Allow non-matching argument types as long as they don't
1238 // violate the principle of substitutability. Specifically, the
1239 // implementation must accept any objects that the superclass
1240 // accepts, however it may also accept others.
1241 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001242 return false;
John McCall10302c02010-10-28 02:34:38 +00001243
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001244 DiagID =
1245 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1246 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001247 }
1248 }
1249
1250 S.Diag(ImplVar->getLocation(), DiagID)
1251 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001252 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1253 S.Diag(IfaceVar->getLocation(),
1254 (IsOverridingMode ? diag::note_previous_declaration
1255 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001256 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001257 return false;
John McCall10302c02010-10-28 02:34:38 +00001258}
John McCallf85e1932011-06-15 23:02:42 +00001259
1260/// In ARC, check whether the conventional meanings of the two methods
1261/// match. If they don't, it's a hard error.
1262static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1263 ObjCMethodDecl *decl) {
1264 ObjCMethodFamily implFamily = impl->getMethodFamily();
1265 ObjCMethodFamily declFamily = decl->getMethodFamily();
1266 if (implFamily == declFamily) return false;
1267
1268 // Since conventions are sorted by selector, the only possibility is
1269 // that the types differ enough to cause one selector or the other
1270 // to fall out of the family.
1271 assert(implFamily == OMF_None || declFamily == OMF_None);
1272
1273 // No further diagnostics required on invalid declarations.
1274 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1275
1276 const ObjCMethodDecl *unmatched = impl;
1277 ObjCMethodFamily family = declFamily;
1278 unsigned errorID = diag::err_arc_lost_method_convention;
1279 unsigned noteID = diag::note_arc_lost_method_convention;
1280 if (declFamily == OMF_None) {
1281 unmatched = decl;
1282 family = implFamily;
1283 errorID = diag::err_arc_gained_method_convention;
1284 noteID = diag::note_arc_gained_method_convention;
1285 }
1286
1287 // Indexes into a %select clause in the diagnostic.
1288 enum FamilySelector {
1289 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1290 };
1291 FamilySelector familySelector = FamilySelector();
1292
1293 switch (family) {
1294 case OMF_None: llvm_unreachable("logic error, no method convention");
1295 case OMF_retain:
1296 case OMF_release:
1297 case OMF_autorelease:
1298 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001299 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001300 case OMF_retainCount:
1301 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001302 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001303 // Mismatches for these methods don't change ownership
1304 // conventions, so we don't care.
1305 return false;
1306
1307 case OMF_init: familySelector = F_init; break;
1308 case OMF_alloc: familySelector = F_alloc; break;
1309 case OMF_copy: familySelector = F_copy; break;
1310 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1311 case OMF_new: familySelector = F_new; break;
1312 }
1313
1314 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1315 ReasonSelector reasonSelector;
1316
1317 // The only reason these methods don't fall within their families is
1318 // due to unusual result types.
1319 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1320 reasonSelector = R_UnrelatedReturn;
1321 } else {
1322 reasonSelector = R_NonObjectReturn;
1323 }
1324
1325 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1326 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1327
1328 return true;
1329}
John McCall10302c02010-10-28 02:34:38 +00001330
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001331void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001332 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001333 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001334 bool IsOverridingMode) {
John McCallf85e1932011-06-15 23:02:42 +00001335 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001336 !IsOverridingMode &&
John McCallf85e1932011-06-15 23:02:42 +00001337 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1338 return;
1339
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001340 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001341 IsProtocolMethodDecl, IsOverridingMode,
1342 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Chris Lattner3aff9192009-04-11 19:58:42 +00001344 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001345 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001346 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001347 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1348 IsProtocolMethodDecl, IsOverridingMode, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001349 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001350
Fariborz Jahanian21121902011-08-08 18:03:17 +00001351 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001352 if (IsOverridingMode)
1353 Diag(ImpMethodDecl->getLocation(),
1354 diag::warn_conflicting_overriding_variadic);
1355 else
1356 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001357 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001358 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001359}
1360
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001361/// WarnExactTypedMethods - This routine issues a warning if method
1362/// implementation declaration matches exactly that of its declaration.
1363void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1364 ObjCMethodDecl *MethodDecl,
1365 bool IsProtocolMethodDecl) {
1366 // don't issue warning when protocol method is optional because primary
1367 // class is not required to implement it and it is safe for protocol
1368 // to implement it.
1369 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1370 return;
1371 // don't issue warning when primary class's method is
1372 // depecated/unavailable.
1373 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1374 MethodDecl->hasAttr<DeprecatedAttr>())
1375 return;
1376
1377 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1378 IsProtocolMethodDecl, false, false);
1379 if (match)
1380 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1381 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1382 IM != EM; ++IM, ++IF) {
1383 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1384 *IM, *IF,
1385 IsProtocolMethodDecl, false, false);
1386 if (!match)
1387 break;
1388 }
1389 if (match)
1390 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001391 if (match)
1392 match = !(MethodDecl->isClassMethod() &&
1393 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001394
1395 if (match) {
1396 Diag(ImpMethodDecl->getLocation(),
1397 diag::warn_category_method_impl_match);
1398 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1399 }
1400}
1401
Mike Stump390b4cc2009-05-16 07:39:55 +00001402/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1403/// improve the efficiency of selector lookups and type checking by associating
1404/// with each protocol / interface / category the flattened instance tables. If
1405/// we used an immutable set to keep the table then it wouldn't add significant
1406/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001407
Steve Naroffefe7f362008-02-08 22:06:17 +00001408/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001409/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001410void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1411 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001412 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001413 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001414 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001415 ObjCContainerDecl *CDecl) {
1416 ObjCInterfaceDecl *IDecl;
1417 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1418 IDecl = C->getClassInterface();
1419 else
1420 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1421 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1422
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001423 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001424 ObjCInterfaceDecl *NSIDecl = 0;
1425 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001426 // check to see if class implements forwardInvocation method and objects
1427 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001428 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001429 // Under such conditions, which means that every method possible is
1430 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001431 // found" warnings.
1432 // FIXME: Use a general GetUnarySelector method for this.
1433 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1434 Selector fISelector = Context.Selectors.getSelector(1, &II);
1435 if (InsMap.count(fISelector))
1436 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1437 // need be implemented in the implementation.
1438 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1439 }
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001441 // If a method lookup fails locally we still need to look and see if
1442 // the method was implemented by a base class or an inherited
1443 // protocol. This lookup is slow, but occurs rarely in correct code
1444 // and otherwise would terminate in a warning.
1445
Chris Lattner4d391482007-12-12 07:09:47 +00001446 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001447 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001448 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001449 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001450 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001451 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001452 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001453 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001454 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001455 // Ugly, but necessary. Method declared in protcol might have
1456 // have been synthesized due to a property declared in the class which
1457 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001458 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001459 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001460 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001461 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001462 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
David Blaikied6471f72011-09-25 23:23:43 +00001463 != DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001464 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001465 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001466 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1467 << PDecl->getDeclName();
1468 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001469 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001470 }
1471 }
Chris Lattner4d391482007-12-12 07:09:47 +00001472 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001473 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001474 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001475 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001476 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001477 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1478 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001479 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001480 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001481 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1482 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001483 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001484 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001485 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1486 PDecl->getDeclName();
1487 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001488 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001489 }
Chris Lattner780f3292008-07-21 21:32:27 +00001490 // Check on this protocols's referenced protocols, recursively.
1491 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1492 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001493 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001494}
1495
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001496/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001497/// or protocol against those declared in their implementations.
1498///
1499void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1500 const llvm::DenseSet<Selector> &ClsMap,
1501 llvm::DenseSet<Selector> &InsMapSeen,
1502 llvm::DenseSet<Selector> &ClsMapSeen,
1503 ObjCImplDecl* IMPDecl,
1504 ObjCContainerDecl* CDecl,
1505 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001506 bool ImmediateClass,
1507 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001508 // Check and see if instance methods in class interface have been
1509 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001510 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1511 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001512 if (InsMapSeen.count((*I)->getSelector()))
1513 continue;
1514 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001515 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001516 !InsMap.count((*I)->getSelector())) {
1517 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001518 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1519 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001520 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001521 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001522 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001523 IMPDecl->getInstanceMethod((*I)->getSelector());
1524 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1525 "Expected to find the method through lookup as well");
1526 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001527 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001528 if (ImpMethodDecl) {
1529 if (!WarnExactMatch)
1530 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1531 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001532 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001533 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1534 isa<ObjCProtocolDecl>(CDecl));
1535 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001536 }
1537 }
Mike Stump1eb44332009-09-09 15:08:12 +00001538
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001539 // Check and see if class methods in class interface have been
1540 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001541 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001542 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001543 if (ClsMapSeen.count((*I)->getSelector()))
1544 continue;
1545 ClsMapSeen.insert((*I)->getSelector());
1546 if (!ClsMap.count((*I)->getSelector())) {
1547 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001548 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1549 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001550 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001551 ObjCMethodDecl *ImpMethodDecl =
1552 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001553 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1554 "Expected to find the method through lookup as well");
1555 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001556 if (!WarnExactMatch)
1557 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1558 isa<ObjCProtocolDecl>(CDecl));
1559 else
1560 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1561 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001562 }
1563 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001564
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001565 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001566 // Also methods in class extensions need be looked at next.
1567 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1568 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1569 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1570 IMPDecl,
1571 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001572 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001573
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001574 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001575 for (ObjCInterfaceDecl::all_protocol_iterator
1576 PI = I->all_referenced_protocol_begin(),
1577 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001578 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1579 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001580 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001581
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001582 // FIXME. For now, we are not checking for extact match of methods
1583 // in category implementation and its primary class's super class.
1584 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001585 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001586 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001587 I->getSuperClass(), IncompleteImpl, false);
1588 }
1589}
1590
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001591/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1592/// category matches with those implemented in its primary class and
1593/// warns each time an exact match is found.
1594void Sema::CheckCategoryVsClassMethodMatches(
1595 ObjCCategoryImplDecl *CatIMPDecl) {
1596 llvm::DenseSet<Selector> InsMap, ClsMap;
1597
1598 for (ObjCImplementationDecl::instmeth_iterator
1599 I = CatIMPDecl->instmeth_begin(),
1600 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1601 InsMap.insert((*I)->getSelector());
1602
1603 for (ObjCImplementationDecl::classmeth_iterator
1604 I = CatIMPDecl->classmeth_begin(),
1605 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1606 ClsMap.insert((*I)->getSelector());
1607 if (InsMap.empty() && ClsMap.empty())
1608 return;
1609
1610 // Get category's primary class.
1611 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1612 if (!CatDecl)
1613 return;
1614 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1615 if (!IDecl)
1616 return;
1617 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1618 bool IncompleteImpl = false;
1619 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1620 CatIMPDecl, IDecl,
1621 IncompleteImpl, false, true /*WarnExactMatch*/);
1622}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001623
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001624void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001625 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001626 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001627 llvm::DenseSet<Selector> InsMap;
1628 // Check and see if instance methods in class interface have been
1629 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001630 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001631 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001632 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001633
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001634 // Check and see if properties declared in the interface have either 1)
1635 // an implementation or 2) there is a @synthesize/@dynamic implementation
1636 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001637 if (isa<ObjCInterfaceDecl>(CDecl) &&
1638 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001639 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001640
Chris Lattner4d391482007-12-12 07:09:47 +00001641 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001642 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001643 I = IMPDecl->classmeth_begin(),
1644 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001645 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001646
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001647 // Check for type conflict of methods declared in a class/protocol and
1648 // its implementation; if any.
1649 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001650 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1651 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001652 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001653
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001654 // check all methods implemented in category against those declared
1655 // in its primary class.
1656 if (ObjCCategoryImplDecl *CatDecl =
1657 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1658 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Chris Lattner4d391482007-12-12 07:09:47 +00001660 // Check the protocol list for unimplemented methods in the @implementation
1661 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001662 // Check and see if class methods in class interface have been
1663 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Chris Lattnercddc8882009-03-01 00:56:52 +00001665 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001666 for (ObjCInterfaceDecl::all_protocol_iterator
1667 PI = I->all_referenced_protocol_begin(),
1668 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001669 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001670 InsMap, ClsMap, I);
1671 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001672 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1673 Categories; Categories = Categories->getNextClassExtension())
1674 ImplMethodsVsClassMethods(S, IMPDecl,
1675 const_cast<ObjCCategoryDecl*>(Categories),
1676 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001677 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001678 // For extended class, unimplemented methods in its protocols will
1679 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001680 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001681 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1682 E = C->protocol_end(); PI != E; ++PI)
1683 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001684 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001685 // Report unimplemented properties in the category as well.
1686 // When reporting on missing setter/getters, do not report when
1687 // setter/getter is implemented in category's primary class
1688 // implementation.
1689 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1690 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1691 for (ObjCImplementationDecl::instmeth_iterator
1692 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1693 InsMap.insert((*I)->getSelector());
1694 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001695 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001696 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001697 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001698 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001699}
1700
Mike Stump1eb44332009-09-09 15:08:12 +00001701/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001702Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001703Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001704 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001705 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001706 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001707 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001708 for (unsigned i = 0; i != NumElts; ++i) {
1709 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001710 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001711 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001712 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001713 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001714 // Maybe we will complain about the shadowed template parameter.
1715 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1716 // Just pretend that we didn't see the previous declaration.
1717 PrevDecl = 0;
1718 }
1719
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001720 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001721 // GCC apparently allows the following idiom:
1722 //
1723 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1724 // @class XCElementToggler;
1725 //
Mike Stump1eb44332009-09-09 15:08:12 +00001726 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001727 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001728 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001729 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001730 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001731 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001732 // a forward class declaration matching a typedef name of a class refers
1733 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001734 if (const ObjCObjectType *OI =
1735 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1736 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001737 }
Chris Lattner4d391482007-12-12 07:09:47 +00001738 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001739 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1740 if (!IDecl) { // Not already seen? Make a forward decl.
1741 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1742 IdentList[i], IdentLocs[i], true);
1743
1744 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1745 // the current DeclContext. This prevents clients that walk DeclContext
1746 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1747 // declared later (if at all). We also take care to explicitly make
1748 // sure this declaration is visible for name lookup.
1749 PushOnScopeChains(IDecl, TUScope, false);
1750 CurContext->makeDeclVisibleInContext(IDecl, true);
1751 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001752 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1753 IDecl, IdentLocs[i]);
1754 CurContext->addDecl(CDecl);
1755 CheckObjCDeclScope(CDecl);
1756 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001757 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001758
1759 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001760}
1761
John McCall0f4c4c42011-06-16 01:15:19 +00001762static bool tryMatchRecordTypes(ASTContext &Context,
1763 Sema::MethodMatchStrategy strategy,
1764 const Type *left, const Type *right);
1765
John McCallf85e1932011-06-15 23:02:42 +00001766static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1767 QualType leftQT, QualType rightQT) {
1768 const Type *left =
1769 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1770 const Type *right =
1771 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1772
1773 if (left == right) return true;
1774
1775 // If we're doing a strict match, the types have to match exactly.
1776 if (strategy == Sema::MMS_strict) return false;
1777
1778 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1779
1780 // Otherwise, use this absurdly complicated algorithm to try to
1781 // validate the basic, low-level compatibility of the two types.
1782
1783 // As a minimum, require the sizes and alignments to match.
1784 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1785 return false;
1786
1787 // Consider all the kinds of non-dependent canonical types:
1788 // - functions and arrays aren't possible as return and parameter types
1789
1790 // - vector types of equal size can be arbitrarily mixed
1791 if (isa<VectorType>(left)) return isa<VectorType>(right);
1792 if (isa<VectorType>(right)) return false;
1793
1794 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001795 // - structs, unions, and Objective-C objects must match more-or-less
1796 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001797 // - everything else should be a scalar
1798 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001799 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001800
John McCall1d9b3b22011-09-09 05:25:32 +00001801 // Make scalars agree in kind, except count bools as chars, and group
1802 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001803 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1804 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1805 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1806 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001807 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1808 leftSK = Type::STK_ObjCObjectPointer;
1809 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1810 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001811
1812 // Note that data member pointers and function member pointers don't
1813 // intermix because of the size differences.
1814
1815 return (leftSK == rightSK);
1816}
Chris Lattner4d391482007-12-12 07:09:47 +00001817
John McCall0f4c4c42011-06-16 01:15:19 +00001818static bool tryMatchRecordTypes(ASTContext &Context,
1819 Sema::MethodMatchStrategy strategy,
1820 const Type *lt, const Type *rt) {
1821 assert(lt && rt && lt != rt);
1822
1823 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1824 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1825 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1826
1827 // Require union-hood to match.
1828 if (left->isUnion() != right->isUnion()) return false;
1829
1830 // Require an exact match if either is non-POD.
1831 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1832 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1833 return false;
1834
1835 // Require size and alignment to match.
1836 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1837
1838 // Require fields to match.
1839 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1840 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1841 for (; li != le && ri != re; ++li, ++ri) {
1842 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1843 return false;
1844 }
1845 return (li == le && ri == re);
1846}
1847
Chris Lattner4d391482007-12-12 07:09:47 +00001848/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1849/// returns true, or false, accordingly.
1850/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001851bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1852 const ObjCMethodDecl *right,
1853 MethodMatchStrategy strategy) {
1854 if (!matchTypes(Context, strategy,
1855 left->getResultType(), right->getResultType()))
1856 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001857
John McCallf85e1932011-06-15 23:02:42 +00001858 if (getLangOptions().ObjCAutoRefCount &&
1859 (left->hasAttr<NSReturnsRetainedAttr>()
1860 != right->hasAttr<NSReturnsRetainedAttr>() ||
1861 left->hasAttr<NSConsumesSelfAttr>()
1862 != right->hasAttr<NSConsumesSelfAttr>()))
1863 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001864
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001865 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001866 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001867
John McCallf85e1932011-06-15 23:02:42 +00001868 for (; li != le; ++li, ++ri) {
1869 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001870 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001871
1872 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1873 return false;
1874
1875 if (getLangOptions().ObjCAutoRefCount &&
1876 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1877 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001878 }
1879 return true;
1880}
1881
Sebastian Redldb9d2142010-08-02 23:18:59 +00001882/// \brief Read the contents of the method pool for a given selector from
1883/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001884///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001885/// This routine should only be called once, when the method pool has no entry
1886/// for this selector.
1887Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001888 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001889 assert(MethodPool.find(Sel) == MethodPool.end() &&
1890 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001891
1892 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001893 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Sebastian Redldb9d2142010-08-02 23:18:59 +00001895 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001896}
1897
Sebastian Redldb9d2142010-08-02 23:18:59 +00001898void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1899 bool instance) {
1900 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1901 if (Pos == MethodPool.end()) {
1902 if (ExternalSource)
1903 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001904 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001905 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1906 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001907 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001908 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001909 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001910 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001911 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001912 Entry.Method = Method;
1913 Entry.Next = 0;
1914 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001915 }
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Chris Lattnerb25df352009-03-04 05:16:45 +00001917 // We've seen a method with this name, see if we have already seen this type
1918 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001919 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1920 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1921
1922 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001923 ObjCMethodDecl *PrevObjCMethod = List->Method;
1924 PrevObjCMethod->setDefined(impl);
1925 // If a method is deprecated, push it in the global pool.
1926 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001927 if (Method->isDeprecated()) {
1928 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001929 List->Method = Method;
1930 }
1931 // If new method is unavailable, push it into global pool
1932 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001933 if (Method->isUnavailable()) {
1934 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001935 List->Method = Method;
1936 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001937 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001938 }
John McCallf85e1932011-06-15 23:02:42 +00001939 }
Mike Stump1eb44332009-09-09 15:08:12 +00001940
Chris Lattnerb25df352009-03-04 05:16:45 +00001941 // We have a new signature for an existing method - add it.
1942 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001943 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1944 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001945}
1946
John McCallf85e1932011-06-15 23:02:42 +00001947/// Determines if this is an "acceptable" loose mismatch in the global
1948/// method pool. This exists mostly as a hack to get around certain
1949/// global mismatches which we can't afford to make warnings / errors.
1950/// Really, what we want is a way to take a method out of the global
1951/// method pool.
1952static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1953 ObjCMethodDecl *other) {
1954 if (!chosen->isInstanceMethod())
1955 return false;
1956
1957 Selector sel = chosen->getSelector();
1958 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1959 return false;
1960
1961 // Don't complain about mismatches for -length if the method we
1962 // chose has an integral result type.
1963 return (chosen->getResultType()->isIntegerType());
1964}
1965
Sebastian Redldb9d2142010-08-02 23:18:59 +00001966ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001967 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001968 bool warn, bool instance) {
1969 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1970 if (Pos == MethodPool.end()) {
1971 if (ExternalSource)
1972 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001973 else
1974 return 0;
1975 }
1976
Sebastian Redldb9d2142010-08-02 23:18:59 +00001977 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001978
Sebastian Redldb9d2142010-08-02 23:18:59 +00001979 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001980 bool issueDiagnostic = false, issueError = false;
1981
1982 // We support a warning which complains about *any* difference in
1983 // method signature.
1984 bool strictSelectorMatch =
1985 (receiverIdOrClass && warn &&
1986 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1987 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00001988 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001989 if (strictSelectorMatch)
1990 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001991 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1992 MMS_strict)) {
1993 issueDiagnostic = true;
1994 break;
1995 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001996 }
1997
John McCallf85e1932011-06-15 23:02:42 +00001998 // If we didn't see any strict differences, we won't see any loose
1999 // differences. In ARC, however, we also need to check for loose
2000 // mismatches, because most of them are errors.
2001 if (!strictSelectorMatch ||
2002 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002003 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002004 // This checks if the methods differ in type mismatch.
2005 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2006 MMS_loose) &&
2007 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2008 issueDiagnostic = true;
2009 if (getLangOptions().ObjCAutoRefCount)
2010 issueError = true;
2011 break;
2012 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002013 }
2014
John McCallf85e1932011-06-15 23:02:42 +00002015 if (issueDiagnostic) {
2016 if (issueError)
2017 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2018 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002019 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2020 else
2021 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002022
2023 Diag(MethList.Method->getLocStart(),
2024 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002025 << MethList.Method->getSourceRange();
2026 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2027 Diag(Next->Method->getLocStart(), diag::note_also_found)
2028 << Next->Method->getSourceRange();
2029 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002030 }
2031 return MethList.Method;
2032}
2033
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002034ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002035 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2036 if (Pos == MethodPool.end())
2037 return 0;
2038
2039 GlobalMethods &Methods = Pos->second;
2040
2041 if (Methods.first.Method && Methods.first.Method->isDefined())
2042 return Methods.first.Method;
2043 if (Methods.second.Method && Methods.second.Method->isDefined())
2044 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002045 return 0;
2046}
2047
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002048/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2049/// identical selector names in current and its super classes and issues
2050/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002051void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2052 ObjCMethodDecl *Method,
2053 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002054 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2055 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002056
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002057 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002058 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002059 SD->lookupMethod(Method->getSelector(), IsInstance);
2060 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002061 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002062 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002063 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002064 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2065 E = Method->param_end();
2066 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2067 for (; ParamI != E; ++ParamI, ++PrevI) {
2068 // Number of parameters are the same and is guaranteed by selector match.
2069 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2070 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2071 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002072 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002073 // respective argument type in the super class method, issue warning;
2074 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002075 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002076 << T1 << T2;
2077 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2078 return;
2079 }
2080 }
2081 ID = SD;
2082 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002083}
2084
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002085/// DiagnoseDuplicateIvars -
2086/// Check for duplicate ivars in the entire class at the start of
2087/// @implementation. This becomes necesssary because class extension can
2088/// add ivars to a class in random order which will not be known until
2089/// class's @implementation is seen.
2090void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2091 ObjCInterfaceDecl *SID) {
2092 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2093 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2094 ObjCIvarDecl* Ivar = (*IVI);
2095 if (Ivar->isInvalidDecl())
2096 continue;
2097 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2098 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2099 if (prevIvar) {
2100 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2101 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2102 Ivar->setInvalidDecl();
2103 }
2104 }
2105 }
2106}
2107
Steve Naroffa56f6162007-12-18 01:30:32 +00002108// Note: For class/category implemenations, allMethods/allProperties is
2109// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002110void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002111 Decl **allMethods, unsigned allNum,
2112 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002113 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002114
2115 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002116 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002117 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2118 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002119
Mike Stump1eb44332009-09-09 15:08:12 +00002120 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002121 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2122 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002123 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002124
Ted Kremenek782f2f52010-01-07 01:20:12 +00002125 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2126 // FIXME: This is wrong. We shouldn't be pretending that there is
2127 // an '@end' in the declaration.
2128 SourceLocation L = ClassDecl->getLocation();
2129 AtEnd.setBegin(L);
2130 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002131 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002132 }
2133
Steve Naroff0701bbb2009-01-08 17:28:14 +00002134 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2135 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2136 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2137
Chris Lattner4d391482007-12-12 07:09:47 +00002138 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002139 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002140 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002141
2142 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002143 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002144 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002145 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002146 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002147 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002148 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002149 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002150 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002151 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002152 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002153 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002154 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002155 InsMap[Method->getSelector()] = Method;
2156 /// The following allows us to typecheck messages to "id".
2157 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002158 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002159 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002160 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002161 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002162 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002163 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002164 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002165 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002166 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002167 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002168 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002169 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002170 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002171 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002172 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002173 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002174 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002175 /// The following allows us to typecheck messages to "Class".
2176 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002177 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002178 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002179 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002180 }
2181 }
2182 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002183 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002184 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002185 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002186 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002187 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002188 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002189 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002190 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002191 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002192
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002193 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002194 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002195 if (C->IsClassExtension()) {
2196 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2197 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002198 }
Chris Lattner4d391482007-12-12 07:09:47 +00002199 }
Steve Naroff09c47192009-01-09 15:36:25 +00002200 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002201 if (CDecl->getIdentifier())
2202 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2203 // user-defined setter/getter. It also synthesizes setter/getter methods
2204 // and adds them to the DeclContext and global method pools.
2205 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2206 E = CDecl->prop_end();
2207 I != E; ++I)
2208 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002209 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002210 }
2211 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002212 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002213 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002214 // Any property declared in a class extension might have user
2215 // declared setter or getter in current class extension or one
2216 // of the other class extensions. Mark them as synthesized as
2217 // property will be synthesized when property with same name is
2218 // seen in the @implementation.
2219 for (const ObjCCategoryDecl *ClsExtDecl =
2220 IDecl->getFirstClassExtension();
2221 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2222 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2223 E = ClsExtDecl->prop_end(); I != E; ++I) {
2224 ObjCPropertyDecl *Property = (*I);
2225 // Skip over properties declared @dynamic
2226 if (const ObjCPropertyImplDecl *PIDecl
2227 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2228 if (PIDecl->getPropertyImplementation()
2229 == ObjCPropertyImplDecl::Dynamic)
2230 continue;
2231
2232 for (const ObjCCategoryDecl *CExtDecl =
2233 IDecl->getFirstClassExtension();
2234 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2235 if (ObjCMethodDecl *GetterMethod =
2236 CExtDecl->getInstanceMethod(Property->getGetterName()))
2237 GetterMethod->setSynthesized(true);
2238 if (!Property->isReadOnly())
2239 if (ObjCMethodDecl *SetterMethod =
2240 CExtDecl->getInstanceMethod(Property->getSetterName()))
2241 SetterMethod->setSynthesized(true);
2242 }
2243 }
2244 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002245 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002246 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002247 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002248
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002249 if (LangOpts.ObjCNonFragileABI2)
2250 while (IDecl->getSuperClass()) {
2251 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2252 IDecl = IDecl->getSuperClass();
2253 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002254 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002255 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002256 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002257 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002258 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002259
Chris Lattner4d391482007-12-12 07:09:47 +00002260 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002261 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002262 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002263 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002264 Categories; Categories = Categories->getNextClassCategory()) {
2265 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002266 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002267 break;
2268 }
2269 }
2270 }
2271 }
Chris Lattner682bf922009-03-29 16:50:03 +00002272 if (isInterfaceDeclKind) {
2273 // Reject invalid vardecls.
2274 for (unsigned i = 0; i != tuvNum; i++) {
2275 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2276 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2277 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002278 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002279 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002280 }
Chris Lattner682bf922009-03-29 16:50:03 +00002281 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002282 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002283 ActOnObjCContainerFinishDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00002284}
2285
2286
2287/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2288/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002289static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002290CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002291 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002292}
2293
Ted Kremenek422bae72010-04-18 04:59:38 +00002294static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002295bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002296 // The 'ibaction' attribute is allowed on method definitions because of
2297 // how the IBAction macro is used on both method declarations and definitions.
2298 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002299 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2300 if ((*i)->getKind() != attr::IBAction)
2301 return true;
2302 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002303}
2304
Douglas Gregore97179c2011-09-08 01:46:34 +00002305namespace {
2306 /// \brief Describes the compatibility of a result type with its method.
2307 enum ResultTypeCompatibilityKind {
2308 RTC_Compatible,
2309 RTC_Incompatible,
2310 RTC_Unknown
2311 };
2312}
2313
Douglas Gregor926df6c2011-06-11 01:09:30 +00002314/// \brief Check whether the declared result type of the given Objective-C
2315/// method declaration is compatible with the method's class.
2316///
Douglas Gregore97179c2011-09-08 01:46:34 +00002317static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002318CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2319 ObjCInterfaceDecl *CurrentClass) {
2320 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002321
2322 // If an Objective-C method inherits its related result type, then its
2323 // declared result type must be compatible with its own class type. The
2324 // declared result type is compatible if:
2325 if (const ObjCObjectPointerType *ResultObjectType
2326 = ResultType->getAs<ObjCObjectPointerType>()) {
2327 // - it is id or qualified id, or
2328 if (ResultObjectType->isObjCIdType() ||
2329 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002330 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002331
2332 if (CurrentClass) {
2333 if (ObjCInterfaceDecl *ResultClass
2334 = ResultObjectType->getInterfaceDecl()) {
2335 // - it is the same as the method's class type, or
2336 if (CurrentClass == ResultClass)
Douglas Gregore97179c2011-09-08 01:46:34 +00002337 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002338
2339 // - it is a superclass of the method's class type
2340 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002341 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002342 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002343 } else {
2344 // Any Objective-C pointer type might be acceptable for a protocol
2345 // method; we just don't know.
2346 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002347 }
2348 }
2349
Douglas Gregore97179c2011-09-08 01:46:34 +00002350 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002351}
2352
John McCall6c2c2502011-07-22 02:45:48 +00002353namespace {
2354/// A helper class for searching for methods which a particular method
2355/// overrides.
2356class OverrideSearch {
2357 Sema &S;
2358 ObjCMethodDecl *Method;
2359 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2360 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2361 bool Recursive;
2362
2363public:
2364 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2365 Selector selector = method->getSelector();
2366
2367 // Bypass this search if we've never seen an instance/class method
2368 // with this selector before.
2369 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2370 if (it == S.MethodPool.end()) {
2371 if (!S.ExternalSource) return;
2372 it = S.ReadMethodPool(selector);
2373 }
2374 ObjCMethodList &list =
2375 method->isInstanceMethod() ? it->second.first : it->second.second;
2376 if (!list.Method) return;
2377
2378 ObjCContainerDecl *container
2379 = cast<ObjCContainerDecl>(method->getDeclContext());
2380
2381 // Prevent the search from reaching this container again. This is
2382 // important with categories, which override methods from the
2383 // interface and each other.
2384 Searched.insert(container);
2385 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002386 }
John McCall6c2c2502011-07-22 02:45:48 +00002387
2388 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2389 iterator begin() const { return Overridden.begin(); }
2390 iterator end() const { return Overridden.end(); }
2391
2392private:
2393 void searchFromContainer(ObjCContainerDecl *container) {
2394 if (container->isInvalidDecl()) return;
2395
2396 switch (container->getDeclKind()) {
2397#define OBJCCONTAINER(type, base) \
2398 case Decl::type: \
2399 searchFrom(cast<type##Decl>(container)); \
2400 break;
2401#define ABSTRACT_DECL(expansion)
2402#define DECL(type, base) \
2403 case Decl::type:
2404#include "clang/AST/DeclNodes.inc"
2405 llvm_unreachable("not an ObjC container!");
2406 }
2407 }
2408
2409 void searchFrom(ObjCProtocolDecl *protocol) {
2410 // A method in a protocol declaration overrides declarations from
2411 // referenced ("parent") protocols.
2412 search(protocol->getReferencedProtocols());
2413 }
2414
2415 void searchFrom(ObjCCategoryDecl *category) {
2416 // A method in a category declaration overrides declarations from
2417 // the main class and from protocols the category references.
2418 search(category->getClassInterface());
2419 search(category->getReferencedProtocols());
2420 }
2421
2422 void searchFrom(ObjCCategoryImplDecl *impl) {
2423 // A method in a category definition that has a category
2424 // declaration overrides declarations from the category
2425 // declaration.
2426 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2427 search(category);
2428
2429 // Otherwise it overrides declarations from the class.
2430 } else {
2431 search(impl->getClassInterface());
2432 }
2433 }
2434
2435 void searchFrom(ObjCInterfaceDecl *iface) {
2436 // A method in a class declaration overrides declarations from
2437
2438 // - categories,
2439 for (ObjCCategoryDecl *category = iface->getCategoryList();
2440 category; category = category->getNextClassCategory())
2441 search(category);
2442
2443 // - the super class, and
2444 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2445 search(super);
2446
2447 // - any referenced protocols.
2448 search(iface->getReferencedProtocols());
2449 }
2450
2451 void searchFrom(ObjCImplementationDecl *impl) {
2452 // A method in a class implementation overrides declarations from
2453 // the class interface.
2454 search(impl->getClassInterface());
2455 }
2456
2457
2458 void search(const ObjCProtocolList &protocols) {
2459 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2460 i != e; ++i)
2461 search(*i);
2462 }
2463
2464 void search(ObjCContainerDecl *container) {
2465 // Abort if we've already searched this container.
2466 if (!Searched.insert(container)) return;
2467
2468 // Check for a method in this container which matches this selector.
2469 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2470 Method->isInstanceMethod());
2471
2472 // If we find one, record it and bail out.
2473 if (meth) {
2474 Overridden.insert(meth);
2475 return;
2476 }
2477
2478 // Otherwise, search for methods that a hypothetical method here
2479 // would have overridden.
2480
2481 // Note that we're now in a recursive case.
2482 Recursive = true;
2483
2484 searchFromContainer(container);
2485 }
2486};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002487}
2488
John McCalld226f652010-08-21 09:40:31 +00002489Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002490 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002491 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002492 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002493 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002494 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002495 Selector Sel,
2496 // optional arguments. The number of types/arguments is obtained
2497 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002498 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002499 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002500 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002501 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002502 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002503 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002504 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002505 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002506 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002507 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2508 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002509 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002510
Douglas Gregore97179c2011-09-08 01:46:34 +00002511 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002512 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002513 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002514 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002515
Steve Naroffccef3712009-02-20 22:59:16 +00002516 // Methods cannot return interface types. All ObjC objects are
2517 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002518 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002519 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2520 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002521 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002522 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002523
2524 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002525 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002526 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002527 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002528 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002529 }
Mike Stump1eb44332009-09-09 15:08:12 +00002530
2531 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002532 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002533 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002534 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002535 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002536 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002537 /*isSynthesized=*/false,
2538 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002539 MethodDeclKind == tok::objc_optional
2540 ? ObjCMethodDecl::Optional
2541 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002542 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002543
Chris Lattner5f9e2722011-07-23 10:55:15 +00002544 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002545
Chris Lattner7db638d2009-04-11 19:42:43 +00002546 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002547 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002548 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002549
Chris Lattnere294d3f2009-04-11 18:57:04 +00002550 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002551 ArgType = Context.getObjCIdType();
2552 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002553 } else {
John McCall58e46772009-10-23 21:48:59 +00002554 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002555 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002556 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002557 }
Mike Stump1eb44332009-09-09 15:08:12 +00002558
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002559 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2560 LookupOrdinaryName, ForRedeclaration);
2561 LookupName(R, S);
2562 if (R.isSingleResult()) {
2563 NamedDecl *PrevDecl = R.getFoundDecl();
2564 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002565 Diag(ArgInfo[i].NameLoc,
2566 (MethodDefinition ? diag::warn_method_param_redefinition
2567 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002568 << ArgInfo[i].Name;
2569 Diag(PrevDecl->getLocation(),
2570 diag::note_previous_declaration);
2571 }
2572 }
2573
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002574 SourceLocation StartLoc = DI
2575 ? DI->getTypeLoc().getBeginLoc()
2576 : ArgInfo[i].NameLoc;
2577
John McCall81ef3e62011-04-23 02:46:06 +00002578 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2579 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2580 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002581
John McCall70798862011-05-02 00:30:12 +00002582 Param->setObjCMethodScopeInfo(i);
2583
Chris Lattner0ed844b2008-04-04 06:12:32 +00002584 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002585 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002586
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002587 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002588 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002589
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002590 S->AddDecl(Param);
2591 IdResolver.AddDecl(Param);
2592
Chris Lattner0ed844b2008-04-04 06:12:32 +00002593 Params.push_back(Param);
2594 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002595
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002596 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002597 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002598 QualType ArgType = Param->getType();
2599 if (ArgType.isNull())
2600 ArgType = Context.getObjCIdType();
2601 else
2602 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002603 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002604 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002605 Diag(Param->getLocation(),
2606 diag::err_object_cannot_be_passed_returned_by_value)
2607 << 1 << ArgType;
2608 Param->setInvalidDecl();
2609 }
2610 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002611
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002612 Params.push_back(Param);
2613 }
2614
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002615 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002616 ObjCMethod->setObjCDeclQualifier(
2617 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002618
2619 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002620 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002621
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002622 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002623 const ObjCMethodDecl *PrevMethod = 0;
2624 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002625 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002626 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2627 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002628 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002629 PrevMethod = ImpDecl->getClassMethod(Sel);
2630 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002631 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002632
Sean Huntcf807c42010-08-18 23:23:40 +00002633 if (ObjCMethod->hasAttrs() &&
2634 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002635 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002636 } else {
2637 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002638 }
John McCall6c2c2502011-07-22 02:45:48 +00002639
Chris Lattner4d391482007-12-12 07:09:47 +00002640 if (PrevMethod) {
2641 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002642 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002643 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002644 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002645 }
John McCall54abf7d2009-11-04 02:18:39 +00002646
Douglas Gregor926df6c2011-06-11 01:09:30 +00002647 // If this Objective-C method does not have a related result type, but we
2648 // are allowed to infer related result types, try to do so based on the
2649 // method family.
2650 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2651 if (!CurrentClass) {
2652 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2653 CurrentClass = Cat->getClassInterface();
2654 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2655 CurrentClass = Impl->getClassInterface();
2656 else if (ObjCCategoryImplDecl *CatImpl
2657 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2658 CurrentClass = CatImpl->getClassInterface();
2659 }
John McCall6c2c2502011-07-22 02:45:48 +00002660
Douglas Gregore97179c2011-09-08 01:46:34 +00002661 ResultTypeCompatibilityKind RTC
2662 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002663
2664 // Search for overridden methods and merge information down from them.
2665 OverrideSearch overrides(*this, ObjCMethod);
2666 for (OverrideSearch::iterator
2667 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2668 ObjCMethodDecl *overridden = *i;
2669
2670 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002671 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002672 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002673
2674 // Then merge the declarations.
2675 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002676
2677 // Check for overriding methods
2678 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2679 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) {
2680 WarnConflictingTypedMethods(ObjCMethod, overridden,
2681 isa<ObjCProtocolDecl>(overridden->getDeclContext()), true);
2682 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002683 }
2684
John McCallf85e1932011-06-15 23:02:42 +00002685 bool ARCError = false;
2686 if (getLangOptions().ObjCAutoRefCount)
2687 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2688
Douglas Gregore97179c2011-09-08 01:46:34 +00002689 // Infer the related result type when possible.
2690 if (!ARCError && RTC == RTC_Compatible &&
2691 !ObjCMethod->hasRelatedResultType() &&
2692 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002693 bool InferRelatedResultType = false;
2694 switch (ObjCMethod->getMethodFamily()) {
2695 case OMF_None:
2696 case OMF_copy:
2697 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002698 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002699 case OMF_mutableCopy:
2700 case OMF_release:
2701 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002702 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002703 break;
2704
2705 case OMF_alloc:
2706 case OMF_new:
2707 InferRelatedResultType = ObjCMethod->isClassMethod();
2708 break;
2709
2710 case OMF_init:
2711 case OMF_autorelease:
2712 case OMF_retain:
2713 case OMF_self:
2714 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2715 break;
2716 }
2717
John McCall6c2c2502011-07-22 02:45:48 +00002718 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002719 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002720 }
2721
John McCalld226f652010-08-21 09:40:31 +00002722 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002723}
2724
Chris Lattnercc98eac2008-12-17 07:13:27 +00002725bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002726 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002727 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002728 // Following is also an error. But it is caused by a missing @end
2729 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002730 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2731 return false;
2732 }
2733
Anders Carlsson15281452008-11-04 16:57:32 +00002734 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2735 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002736
Anders Carlsson15281452008-11-04 16:57:32 +00002737 return true;
2738}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002739
Chris Lattnercc98eac2008-12-17 07:13:27 +00002740/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2741/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002742void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002743 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002744 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002745 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002746 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002747 if (!Class) {
2748 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2749 return;
2750 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002751 if (LangOpts.ObjCNonFragileABI) {
2752 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2753 return;
2754 }
Mike Stump1eb44332009-09-09 15:08:12 +00002755
Chris Lattnercc98eac2008-12-17 07:13:27 +00002756 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002757 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002758 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002759 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002760 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002761 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002762 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002763 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2764 /*FIXME: StartL=*/ID->getLocation(),
2765 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002766 ID->getIdentifier(), ID->getType(),
2767 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002768 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002769 }
Mike Stump1eb44332009-09-09 15:08:12 +00002770
Chris Lattnercc98eac2008-12-17 07:13:27 +00002771 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002772 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002773 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002774 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002775 if (getLangOptions().CPlusPlus)
2776 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002777 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002778 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002779 }
2780}
2781
Douglas Gregor160b5632010-04-26 17:32:49 +00002782/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002783VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2784 SourceLocation StartLoc,
2785 SourceLocation IdLoc,
2786 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002787 bool Invalid) {
2788 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2789 // duration shall not be qualified by an address-space qualifier."
2790 // Since all parameters have automatic store duration, they can not have
2791 // an address space.
2792 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002793 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002794 Invalid = true;
2795 }
2796
2797 // An @catch parameter must be an unqualified object pointer type;
2798 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2799 if (Invalid) {
2800 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002801 } else if (T->isDependentType()) {
2802 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002803 } else if (!T->isObjCObjectPointerType()) {
2804 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002805 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002806 } else if (T->isObjCQualifiedIdType()) {
2807 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002808 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002809 }
2810
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002811 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2812 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002813 New->setExceptionVariable(true);
2814
Douglas Gregor160b5632010-04-26 17:32:49 +00002815 if (Invalid)
2816 New->setInvalidDecl();
2817 return New;
2818}
2819
John McCalld226f652010-08-21 09:40:31 +00002820Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002821 const DeclSpec &DS = D.getDeclSpec();
2822
2823 // We allow the "register" storage class on exception variables because
2824 // GCC did, but we drop it completely. Any other storage class is an error.
2825 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2826 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2827 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2828 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2829 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2830 << DS.getStorageClassSpec();
2831 }
2832 if (D.getDeclSpec().isThreadSpecified())
2833 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2834 D.getMutableDeclSpec().ClearStorageClassSpecs();
2835
2836 DiagnoseFunctionSpecifiers(D);
2837
2838 // Check that there are no default arguments inside the type of this
2839 // exception object (C++ only).
2840 if (getLangOptions().CPlusPlus)
2841 CheckExtraCXXDefaultArguments(D);
2842
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002843 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002844 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002845
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002846 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2847 D.getSourceRange().getBegin(),
2848 D.getIdentifierLoc(),
2849 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002850 D.isInvalidType());
2851
2852 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2853 if (D.getCXXScopeSpec().isSet()) {
2854 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2855 << D.getCXXScopeSpec().getRange();
2856 New->setInvalidDecl();
2857 }
2858
2859 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002860 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002861 if (D.getIdentifier())
2862 IdResolver.AddDecl(New);
2863
2864 ProcessDeclAttributes(S, New, D);
2865
2866 if (New->hasAttr<BlocksAttr>())
2867 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002868 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002869}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002870
2871/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002872/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002873void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002874 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002875 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2876 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002877 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002878 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002879 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002880 }
2881}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002882
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002883void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002884 // Load referenced selectors from the external source.
2885 if (ExternalSource) {
2886 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2887 ExternalSource->ReadReferencedSelectors(Sels);
2888 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2889 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2890 }
2891
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002892 // Warning will be issued only when selector table is
2893 // generated (which means there is at lease one implementation
2894 // in the TU). This is to match gcc's behavior.
2895 if (ReferencedSelectors.empty() ||
2896 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002897 return;
2898 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2899 ReferencedSelectors.begin(),
2900 E = ReferencedSelectors.end(); S != E; ++S) {
2901 Selector Sel = (*S).first;
2902 if (!LookupImplementedMethodInGlobalPool(Sel))
2903 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2904 }
2905 return;
2906}