blob: af2eb1287b3ca10b19dca5279dc27adc0ad6104b [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!
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000377 return ActOnObjCContainerStartDefinition(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);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000485 return ActOnObjCContainerStartDefinition(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!
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000581 return ActOnObjCContainerStartDefinition(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);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000614 return ActOnObjCContainerStartDefinition(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;
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000744 return ActOnObjCContainerStartDefinition(CDecl);
Ted Kremenek09b68972010-02-23 19:39:46 +0000745 }
746
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000747 if (!CategoryName && IDecl->getImplementation()) {
748 Diag(ClassLoc, diag::err_class_extension_after_impl) << ClassName;
749 Diag(IDecl->getImplementation()->getLocation(),
750 diag::note_implementation_declared);
Ted Kremenek09b68972010-02-23 19:39:46 +0000751 }
752
Fariborz Jahanian25760612010-02-15 21:55:26 +0000753 if (CategoryName) {
754 /// Check for duplicate interface declaration for this category
755 ObjCCategoryDecl *CDeclChain;
756 for (CDeclChain = IDecl->getCategoryList(); CDeclChain;
757 CDeclChain = CDeclChain->getNextClassCategory()) {
758 if (CDeclChain->getIdentifier() == CategoryName) {
759 // Class extensions can be declared multiple times.
760 Diag(CategoryLoc, diag::warn_dup_category_def)
761 << ClassName << CategoryName;
762 Diag(CDeclChain->getLocation(), diag::note_previous_definition);
763 break;
764 }
Chris Lattner70f19542009-02-16 21:26:43 +0000765 }
766 }
Chris Lattner70f19542009-02-16 21:26:43 +0000767
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000768 CDecl = ObjCCategoryDecl::Create(Context, CurContext, AtInterfaceLoc,
769 ClassLoc, CategoryLoc, CategoryName, IDecl);
770 // FIXME: PushOnScopeChains?
771 CurContext->addDecl(CDecl);
772
Chris Lattner4d391482007-12-12 07:09:47 +0000773 if (NumProtoRefs) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +0000774 CDecl->setProtocolList((ObjCProtocolDecl**)ProtoRefs, NumProtoRefs,
Douglas Gregor18df52b2010-01-16 15:02:53 +0000775 ProtoLocs, Context);
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000776 // Protocols in the class extension belong to the class.
Fariborz Jahanian25760612010-02-15 21:55:26 +0000777 if (CDecl->IsClassExtension())
Fariborz Jahanian339798e2009-10-05 20:41:32 +0000778 IDecl->mergeClassExtensionProtocolList((ObjCProtocolDecl**)ProtoRefs,
Ted Kremenek53b94412010-09-01 01:21:15 +0000779 NumProtoRefs, Context);
Chris Lattner4d391482007-12-12 07:09:47 +0000780 }
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Anders Carlsson15281452008-11-04 16:57:32 +0000782 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000783 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000784}
785
786/// ActOnStartCategoryImplementation - Perform semantic checks on the
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000787/// category implementation declaration and build an ObjCCategoryImplDecl
Chris Lattner4d391482007-12-12 07:09:47 +0000788/// object.
John McCalld226f652010-08-21 09:40:31 +0000789Decl *Sema::ActOnStartCategoryImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000790 SourceLocation AtCatImplLoc,
791 IdentifierInfo *ClassName, SourceLocation ClassLoc,
792 IdentifierInfo *CatName, SourceLocation CatLoc) {
Douglas Gregorc83c6872010-04-15 22:33:43 +0000793 ObjCInterfaceDecl *IDecl = getObjCInterfaceDecl(ClassName, ClassLoc, true);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000794 ObjCCategoryDecl *CatIDecl = 0;
795 if (IDecl) {
796 CatIDecl = IDecl->FindCategoryDeclaration(CatName);
797 if (!CatIDecl) {
798 // Category @implementation with no corresponding @interface.
799 // Create and install one.
800 CatIDecl = ObjCCategoryDecl::Create(Context, CurContext, SourceLocation(),
Douglas Gregor3db211b2010-01-16 16:38:58 +0000801 SourceLocation(), SourceLocation(),
Argyrios Kyrtzidis955fadb2011-08-30 19:43:26 +0000802 CatName, IDecl);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000803 }
804 }
805
Mike Stump1eb44332009-09-09 15:08:12 +0000806 ObjCCategoryImplDecl *CDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000807 ObjCCategoryImplDecl::Create(Context, CurContext, CatName, IDecl,
808 ClassLoc, AtCatImplLoc);
Chris Lattner4d391482007-12-12 07:09:47 +0000809 /// Check that class of this category is already completely declared.
John McCall6c2c2502011-07-22 02:45:48 +0000810 if (!IDecl || IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000811 Diag(ClassLoc, diag::err_undef_interface) << ClassName;
John McCall6c2c2502011-07-22 02:45:48 +0000812 CDecl->setInvalidDecl();
813 }
Chris Lattner4d391482007-12-12 07:09:47 +0000814
Douglas Gregord0434102009-01-09 00:49:46 +0000815 // FIXME: PushOnScopeChains?
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000816 CurContext->addDecl(CDecl);
Douglas Gregord0434102009-01-09 00:49:46 +0000817
Argyrios Kyrtzidisc076e372011-10-06 23:23:27 +0000818 // If the interface is deprecated/unavailable, warn/error about it.
819 if (IDecl)
820 DiagnoseUseOfDecl(IDecl, ClassLoc);
821
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000822 /// Check that CatName, category name, is not used in another implementation.
823 if (CatIDecl) {
824 if (CatIDecl->getImplementation()) {
825 Diag(ClassLoc, diag::err_dup_implementation_category) << ClassName
826 << CatName;
827 Diag(CatIDecl->getImplementation()->getLocation(),
828 diag::note_previous_definition);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000829 } else {
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000830 CatIDecl->setImplementation(CDecl);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000831 // Warn on implementating category of deprecated class under
832 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000833 DiagnoseObjCImplementedDeprecations(*this,
834 dyn_cast<NamedDecl>(IDecl),
835 CDecl->getLocation(), 2);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000836 }
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000837 }
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Anders Carlsson15281452008-11-04 16:57:32 +0000839 CheckObjCDeclScope(CDecl);
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000840 return ActOnObjCContainerStartDefinition(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000841}
842
John McCalld226f652010-08-21 09:40:31 +0000843Decl *Sema::ActOnStartClassImplementation(
Chris Lattner4d391482007-12-12 07:09:47 +0000844 SourceLocation AtClassImplLoc,
845 IdentifierInfo *ClassName, SourceLocation ClassLoc,
Mike Stump1eb44332009-09-09 15:08:12 +0000846 IdentifierInfo *SuperClassname,
Chris Lattner4d391482007-12-12 07:09:47 +0000847 SourceLocation SuperClassLoc) {
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000848 ObjCInterfaceDecl* IDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000849 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +0000850 NamedDecl *PrevDecl
Douglas Gregorc0b39642010-04-15 23:40:53 +0000851 = LookupSingleName(TUScope, ClassName, ClassLoc, LookupOrdinaryName,
852 ForRedeclaration);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000853 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000854 Diag(ClassLoc, diag::err_redefinition_different_kind) << ClassName;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000855 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000856 } else if ((IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl))) {
857 // If this is a forward declaration of an interface, warn.
858 if (IDecl->isForwardDecl()) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000859 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000860 IDecl = 0;
Fariborz Jahanian77a6be42009-04-23 21:49:04 +0000861 }
Douglas Gregor95ff7422010-01-04 17:27:12 +0000862 } else {
863 // We did not find anything with the name ClassName; try to correct for
864 // typos in the class name.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000865 TypoCorrection Corrected = CorrectTypo(
866 DeclarationNameInfo(ClassName, ClassLoc), LookupOrdinaryName, TUScope,
867 NULL, NULL, false, CTC_NoKeywords);
868 if ((IDecl = Corrected.getCorrectionDeclAs<ObjCInterfaceDecl>())) {
Douglas Gregora6f26382010-01-06 23:44:25 +0000869 // Suggest the (potentially) correct interface name. However, put the
870 // fix-it hint itself in a separate note, since changing the name in
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000871 // the warning would make the fix-it change semantics.However, don't
Douglas Gregor95ff7422010-01-04 17:27:12 +0000872 // provide a code-modification hint or use the typo name for recovery,
873 // because this is just a warning. The program may actually be correct.
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000874 DeclarationName CorrectedName = Corrected.getCorrection();
Douglas Gregor95ff7422010-01-04 17:27:12 +0000875 Diag(ClassLoc, diag::warn_undef_interface_suggest)
Douglas Gregord8bba9c2011-06-28 16:20:02 +0000876 << ClassName << CorrectedName;
877 Diag(IDecl->getLocation(), diag::note_previous_decl) << CorrectedName
878 << FixItHint::CreateReplacement(ClassLoc, CorrectedName.getAsString());
Douglas Gregor95ff7422010-01-04 17:27:12 +0000879 IDecl = 0;
880 } else {
881 Diag(ClassLoc, diag::warn_undef_interface) << ClassName;
882 }
Chris Lattner4d391482007-12-12 07:09:47 +0000883 }
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Chris Lattner4d391482007-12-12 07:09:47 +0000885 // Check that super class name is valid class name
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000886 ObjCInterfaceDecl* SDecl = 0;
Chris Lattner4d391482007-12-12 07:09:47 +0000887 if (SuperClassname) {
888 // Check if a different kind of symbol declared in this scope.
Douglas Gregorc83c6872010-04-15 22:33:43 +0000889 PrevDecl = LookupSingleName(TUScope, SuperClassname, SuperClassLoc,
890 LookupOrdinaryName);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000891 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Chris Lattner3c73c412008-11-19 08:23:25 +0000892 Diag(SuperClassLoc, diag::err_redefinition_different_kind)
893 << SuperClassname;
Chris Lattner5f4a6822008-11-23 23:12:31 +0000894 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
Chris Lattner3c73c412008-11-19 08:23:25 +0000895 } else {
Mike Stump1eb44332009-09-09 15:08:12 +0000896 SDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000897 if (!SDecl)
Chris Lattner3c73c412008-11-19 08:23:25 +0000898 Diag(SuperClassLoc, diag::err_undef_superclass)
899 << SuperClassname << ClassName;
Chris Lattner4d391482007-12-12 07:09:47 +0000900 else if (IDecl && IDecl->getSuperClass() != SDecl) {
901 // This implementation and its interface do not have the same
902 // super class.
Chris Lattner3c73c412008-11-19 08:23:25 +0000903 Diag(SuperClassLoc, diag::err_conflicting_super_class)
Chris Lattner08631c52008-11-23 21:45:46 +0000904 << SDecl->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +0000905 Diag(SDecl->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +0000906 }
907 }
908 }
Mike Stump1eb44332009-09-09 15:08:12 +0000909
Chris Lattner4d391482007-12-12 07:09:47 +0000910 if (!IDecl) {
911 // Legacy case of @implementation with no corresponding @interface.
912 // Build, chain & install the interface decl into the identifier.
Daniel Dunbarf6414922008-08-20 18:02:42 +0000913
Mike Stump390b4cc2009-05-16 07:39:55 +0000914 // FIXME: Do we support attributes on the @implementation? If so we should
915 // copy them over.
Mike Stump1eb44332009-09-09 15:08:12 +0000916 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassImplLoc,
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000917 ClassName, ClassLoc, false, true);
Chris Lattner4d391482007-12-12 07:09:47 +0000918 IDecl->setSuperClass(SDecl);
919 IDecl->setLocEnd(ClassLoc);
Douglas Gregor8b9fb302009-04-24 00:16:12 +0000920
921 PushOnScopeChains(IDecl, TUScope);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000922 } else {
923 // Mark the interface as being completed, even if it was just as
924 // @class ....;
925 // declaration; the user cannot reopen it.
926 IDecl->setForwardDecl(false);
Chris Lattner4d391482007-12-12 07:09:47 +0000927 }
Mike Stump1eb44332009-09-09 15:08:12 +0000928
929 ObjCImplementationDecl* IMPDecl =
Argyrios Kyrtzidis1711fc92011-10-04 04:48:02 +0000930 ObjCImplementationDecl::Create(Context, CurContext, IDecl, SDecl,
931 ClassLoc, AtClassImplLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Anders Carlsson15281452008-11-04 16:57:32 +0000933 if (CheckObjCDeclScope(IMPDecl))
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000934 return ActOnObjCContainerStartDefinition(IMPDecl);
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Chris Lattner4d391482007-12-12 07:09:47 +0000936 // Check that there is no duplicate implementation of this class.
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000937 if (IDecl->getImplementation()) {
938 // FIXME: Don't leak everything!
Chris Lattner3c73c412008-11-19 08:23:25 +0000939 Diag(ClassLoc, diag::err_dup_implementation_class) << ClassName;
Argyrios Kyrtzidis87018772009-07-21 00:06:04 +0000940 Diag(IDecl->getImplementation()->getLocation(),
941 diag::note_previous_definition);
Douglas Gregordeacbdc2010-08-11 12:19:30 +0000942 } else { // add it to the list.
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000943 IDecl->setImplementation(IMPDecl);
Douglas Gregor8fc463a2009-04-24 00:11:27 +0000944 PushOnScopeChains(IMPDecl, TUScope);
Fariborz Jahanianb1224f62011-02-15 00:59:30 +0000945 // Warn on implementating deprecated class under
946 // -Wdeprecated-implementations flag.
Fariborz Jahanian5ac96d52011-02-15 17:49:58 +0000947 DiagnoseObjCImplementedDeprecations(*this,
948 dyn_cast<NamedDecl>(IDecl),
949 IMPDecl->getLocation(), 1);
Argyrios Kyrtzidis8a1d7222009-07-21 00:05:53 +0000950 }
Argyrios Kyrtzidis3a387442011-10-06 23:23:20 +0000951 return ActOnObjCContainerStartDefinition(IMPDecl);
Chris Lattner4d391482007-12-12 07:09:47 +0000952}
953
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000954void Sema::CheckImplementationIvars(ObjCImplementationDecl *ImpDecl,
955 ObjCIvarDecl **ivars, unsigned numIvars,
Chris Lattner4d391482007-12-12 07:09:47 +0000956 SourceLocation RBrace) {
957 assert(ImpDecl && "missing implementation decl");
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000958 ObjCInterfaceDecl* IDecl = ImpDecl->getClassInterface();
Chris Lattner4d391482007-12-12 07:09:47 +0000959 if (!IDecl)
960 return;
961 /// Check case of non-existing @interface decl.
962 /// (legacy objective-c @implementation decl without an @interface decl).
963 /// Add implementations's ivar to the synthesize class's ivar list.
Steve Naroff33feeb02009-04-20 20:09:33 +0000964 if (IDecl->isImplicitInterfaceDecl()) {
Chris Lattner38af2de2009-02-20 21:35:13 +0000965 IDecl->setLocEnd(RBrace);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000966 // Add ivar's to class's DeclContext.
967 for (unsigned i = 0, e = numIvars; i != e; ++i) {
Fariborz Jahanian2f14c4d2010-02-17 18:10:54 +0000968 ivars[i]->setLexicalDeclContext(ImpDecl);
969 IDecl->makeDeclVisibleInContext(ivars[i], false);
Fariborz Jahanian11062e12010-02-19 00:31:17 +0000970 ImpDecl->addDecl(ivars[i]);
Fariborz Jahanian3a21cd92010-02-17 17:00:07 +0000971 }
972
Chris Lattner4d391482007-12-12 07:09:47 +0000973 return;
974 }
975 // If implementation has empty ivar list, just return.
976 if (numIvars == 0)
977 return;
Mike Stump1eb44332009-09-09 15:08:12 +0000978
Chris Lattner4d391482007-12-12 07:09:47 +0000979 assert(ivars && "missing @implementation ivars");
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000980 if (LangOpts.ObjCNonFragileABI2) {
981 if (ImpDecl->getSuperClass())
982 Diag(ImpDecl->getLocation(), diag::warn_on_superclass_use);
983 for (unsigned i = 0; i < numIvars; i++) {
984 ObjCIvarDecl* ImplIvar = ivars[i];
985 if (const ObjCIvarDecl *ClsIvar =
986 IDecl->getIvarDecl(ImplIvar->getIdentifier())) {
987 Diag(ImplIvar->getLocation(), diag::err_duplicate_ivar_declaration);
988 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
989 continue;
990 }
Fariborz Jahanianbd94d442010-02-19 20:58:54 +0000991 // Instance ivar to Implementation's DeclContext.
992 ImplIvar->setLexicalDeclContext(ImpDecl);
993 IDecl->makeDeclVisibleInContext(ImplIvar, false);
994 ImpDecl->addDecl(ImplIvar);
995 }
996 return;
997 }
Chris Lattner4d391482007-12-12 07:09:47 +0000998 // Check interface's Ivar list against those in the implementation.
999 // names and types must match.
1000 //
Chris Lattner4d391482007-12-12 07:09:47 +00001001 unsigned j = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001002 ObjCInterfaceDecl::ivar_iterator
Chris Lattner4c525092007-12-12 17:58:05 +00001003 IVI = IDecl->ivar_begin(), IVE = IDecl->ivar_end();
1004 for (; numIvars > 0 && IVI != IVE; ++IVI) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001005 ObjCIvarDecl* ImplIvar = ivars[j++];
1006 ObjCIvarDecl* ClsIvar = *IVI;
Chris Lattner4d391482007-12-12 07:09:47 +00001007 assert (ImplIvar && "missing implementation ivar");
1008 assert (ClsIvar && "missing class ivar");
Mike Stump1eb44332009-09-09 15:08:12 +00001009
Steve Naroffca331292009-03-03 14:49:36 +00001010 // First, make sure the types match.
Chris Lattner1b63eef2008-07-27 00:05:05 +00001011 if (Context.getCanonicalType(ImplIvar->getType()) !=
1012 Context.getCanonicalType(ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001013 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001014 << ImplIvar->getIdentifier()
1015 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001016 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Steve Naroffca331292009-03-03 14:49:36 +00001017 } else if (ImplIvar->isBitField() && ClsIvar->isBitField()) {
1018 Expr *ImplBitWidth = ImplIvar->getBitWidth();
1019 Expr *ClsBitWidth = ClsIvar->getBitWidth();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001020 if (ImplBitWidth->EvaluateAsInt(Context).getZExtValue() !=
1021 ClsBitWidth->EvaluateAsInt(Context).getZExtValue()) {
Steve Naroffca331292009-03-03 14:49:36 +00001022 Diag(ImplBitWidth->getLocStart(), diag::err_conflicting_ivar_bitwidth)
1023 << ImplIvar->getIdentifier();
1024 Diag(ClsBitWidth->getLocStart(), diag::note_previous_definition);
1025 }
Mike Stump1eb44332009-09-09 15:08:12 +00001026 }
Steve Naroffca331292009-03-03 14:49:36 +00001027 // Make sure the names are identical.
1028 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001029 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001030 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001031 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001032 }
1033 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001034 }
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Chris Lattner609e4c72007-12-12 18:11:49 +00001036 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001037 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001038 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001039 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001040}
1041
Steve Naroff3c2eb662008-02-10 21:38:56 +00001042void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001043 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001044 // No point warning no definition of method which is 'unavailable'.
1045 if (method->hasAttr<UnavailableAttr>())
1046 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001047 if (!IncompleteImpl) {
1048 Diag(ImpLoc, diag::warn_incomplete_impl);
1049 IncompleteImpl = true;
1050 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001051 if (DiagID == diag::warn_unimplemented_protocol_method)
1052 Diag(ImpLoc, DiagID) << method->getDeclName();
1053 else
1054 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001055}
1056
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001057/// Determines if type B can be substituted for type A. Returns true if we can
1058/// guarantee that anything that the user will do to an object of type A can
1059/// also be done to an object of type B. This is trivially true if the two
1060/// types are the same, or if B is a subclass of A. It becomes more complex
1061/// in cases where protocols are involved.
1062///
1063/// Object types in Objective-C describe the minimum requirements for an
1064/// object, rather than providing a complete description of a type. For
1065/// example, if A is a subclass of B, then B* may refer to an instance of A.
1066/// The principle of substitutability means that we may use an instance of A
1067/// anywhere that we may use an instance of B - it will implement all of the
1068/// ivars of B and all of the methods of B.
1069///
1070/// This substitutability is important when type checking methods, because
1071/// the implementation may have stricter type definitions than the interface.
1072/// The interface specifies minimum requirements, but the implementation may
1073/// have more accurate ones. For example, a method may privately accept
1074/// instances of B, but only publish that it accepts instances of A. Any
1075/// object passed to it will be type checked against B, and so will implicitly
1076/// by a valid A*. Similarly, a method may return a subclass of the class that
1077/// it is declared as returning.
1078///
1079/// This is most important when considering subclassing. A method in a
1080/// subclass must accept any object as an argument that its superclass's
1081/// implementation accepts. It may, however, accept a more general type
1082/// without breaking substitutability (i.e. you can still use the subclass
1083/// anywhere that you can use the superclass, but not vice versa). The
1084/// converse requirement applies to return types: the return type for a
1085/// subclass method must be a valid object of the kind that the superclass
1086/// advertises, but it may be specified more accurately. This avoids the need
1087/// for explicit down-casting by callers.
1088///
1089/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001090static bool isObjCTypeSubstitutable(ASTContext &Context,
1091 const ObjCObjectPointerType *A,
1092 const ObjCObjectPointerType *B,
1093 bool rejectId) {
1094 // Reject a protocol-unqualified id.
1095 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001096
1097 // If B is a qualified id, then A must also be a qualified id and it must
1098 // implement all of the protocols in B. It may not be a qualified class.
1099 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1100 // stricter definition so it is not substitutable for id<A>.
1101 if (B->isObjCQualifiedIdType()) {
1102 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001103 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1104 QualType(B,0),
1105 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001106 }
1107
1108 /*
1109 // id is a special type that bypasses type checking completely. We want a
1110 // warning when it is used in one place but not another.
1111 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1112
1113
1114 // If B is a qualified id, then A must also be a qualified id (which it isn't
1115 // if we've got this far)
1116 if (B->isObjCQualifiedIdType()) return false;
1117 */
1118
1119 // Now we know that A and B are (potentially-qualified) class types. The
1120 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001121 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001122}
1123
John McCall10302c02010-10-28 02:34:38 +00001124static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1125 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1126}
1127
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001128static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001129 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001130 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001131 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001132 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001133 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001134 if (IsProtocolMethodDecl &&
1135 (MethodDecl->getObjCDeclQualifier() !=
1136 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001137 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001138 S.Diag(MethodImpl->getLocation(),
1139 (IsOverridingMode ?
1140 diag::warn_conflicting_overriding_ret_type_modifiers
1141 : diag::warn_conflicting_ret_type_modifiers))
1142 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001143 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1144 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1145 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1146 }
1147 else
1148 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001149 }
1150
John McCall10302c02010-10-28 02:34:38 +00001151 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001152 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001153 return true;
1154 if (!Warn)
1155 return false;
John McCall10302c02010-10-28 02:34:38 +00001156
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001157 unsigned DiagID =
1158 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1159 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001160
1161 // Mismatches between ObjC pointers go into a different warning
1162 // category, and sometimes they're even completely whitelisted.
1163 if (const ObjCObjectPointerType *ImplPtrTy =
1164 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1165 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001166 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001167 // Allow non-matching return types as long as they don't violate
1168 // the principle of substitutability. Specifically, we permit
1169 // return types that are subclasses of the declared return type,
1170 // or that are more-qualified versions of the declared type.
1171 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001172 return false;
John McCall10302c02010-10-28 02:34:38 +00001173
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001174 DiagID =
1175 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1176 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001177 }
1178 }
1179
1180 S.Diag(MethodImpl->getLocation(), DiagID)
1181 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001182 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001183 << MethodImpl->getResultType()
1184 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001185 S.Diag(MethodDecl->getLocation(),
1186 IsOverridingMode ? diag::note_previous_declaration
1187 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001188 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001189 return false;
John McCall10302c02010-10-28 02:34:38 +00001190}
1191
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001192static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001193 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001194 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001195 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001196 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001197 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001198 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001199 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001200 if (IsProtocolMethodDecl &&
1201 (ImplVar->getObjCDeclQualifier() !=
1202 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001203 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001204 if (IsOverridingMode)
1205 S.Diag(ImplVar->getLocation(),
1206 diag::warn_conflicting_overriding_param_modifiers)
1207 << getTypeRange(ImplVar->getTypeSourceInfo())
1208 << MethodImpl->getDeclName();
1209 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001210 diag::warn_conflicting_param_modifiers)
1211 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001212 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001213 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1214 << getTypeRange(IfaceVar->getTypeSourceInfo());
1215 }
1216 else
1217 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001218 }
1219
John McCall10302c02010-10-28 02:34:38 +00001220 QualType ImplTy = ImplVar->getType();
1221 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001222
John McCall10302c02010-10-28 02:34:38 +00001223 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001224 return true;
1225
1226 if (!Warn)
1227 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001228 unsigned DiagID =
1229 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1230 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001231
1232 // Mismatches between ObjC pointers go into a different warning
1233 // category, and sometimes they're even completely whitelisted.
1234 if (const ObjCObjectPointerType *ImplPtrTy =
1235 ImplTy->getAs<ObjCObjectPointerType>()) {
1236 if (const ObjCObjectPointerType *IfacePtrTy =
1237 IfaceTy->getAs<ObjCObjectPointerType>()) {
1238 // Allow non-matching argument types as long as they don't
1239 // violate the principle of substitutability. Specifically, the
1240 // implementation must accept any objects that the superclass
1241 // accepts, however it may also accept others.
1242 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001243 return false;
John McCall10302c02010-10-28 02:34:38 +00001244
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001245 DiagID =
1246 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1247 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001248 }
1249 }
1250
1251 S.Diag(ImplVar->getLocation(), DiagID)
1252 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001253 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1254 S.Diag(IfaceVar->getLocation(),
1255 (IsOverridingMode ? diag::note_previous_declaration
1256 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001257 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001258 return false;
John McCall10302c02010-10-28 02:34:38 +00001259}
John McCallf85e1932011-06-15 23:02:42 +00001260
1261/// In ARC, check whether the conventional meanings of the two methods
1262/// match. If they don't, it's a hard error.
1263static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1264 ObjCMethodDecl *decl) {
1265 ObjCMethodFamily implFamily = impl->getMethodFamily();
1266 ObjCMethodFamily declFamily = decl->getMethodFamily();
1267 if (implFamily == declFamily) return false;
1268
1269 // Since conventions are sorted by selector, the only possibility is
1270 // that the types differ enough to cause one selector or the other
1271 // to fall out of the family.
1272 assert(implFamily == OMF_None || declFamily == OMF_None);
1273
1274 // No further diagnostics required on invalid declarations.
1275 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1276
1277 const ObjCMethodDecl *unmatched = impl;
1278 ObjCMethodFamily family = declFamily;
1279 unsigned errorID = diag::err_arc_lost_method_convention;
1280 unsigned noteID = diag::note_arc_lost_method_convention;
1281 if (declFamily == OMF_None) {
1282 unmatched = decl;
1283 family = implFamily;
1284 errorID = diag::err_arc_gained_method_convention;
1285 noteID = diag::note_arc_gained_method_convention;
1286 }
1287
1288 // Indexes into a %select clause in the diagnostic.
1289 enum FamilySelector {
1290 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1291 };
1292 FamilySelector familySelector = FamilySelector();
1293
1294 switch (family) {
1295 case OMF_None: llvm_unreachable("logic error, no method convention");
1296 case OMF_retain:
1297 case OMF_release:
1298 case OMF_autorelease:
1299 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001300 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001301 case OMF_retainCount:
1302 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001303 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001304 // Mismatches for these methods don't change ownership
1305 // conventions, so we don't care.
1306 return false;
1307
1308 case OMF_init: familySelector = F_init; break;
1309 case OMF_alloc: familySelector = F_alloc; break;
1310 case OMF_copy: familySelector = F_copy; break;
1311 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1312 case OMF_new: familySelector = F_new; break;
1313 }
1314
1315 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1316 ReasonSelector reasonSelector;
1317
1318 // The only reason these methods don't fall within their families is
1319 // due to unusual result types.
1320 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1321 reasonSelector = R_UnrelatedReturn;
1322 } else {
1323 reasonSelector = R_NonObjectReturn;
1324 }
1325
1326 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1327 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1328
1329 return true;
1330}
John McCall10302c02010-10-28 02:34:38 +00001331
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001332void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001333 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001334 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001335 bool IsOverridingMode) {
John McCallf85e1932011-06-15 23:02:42 +00001336 if (getLangOptions().ObjCAutoRefCount &&
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001337 !IsOverridingMode &&
John McCallf85e1932011-06-15 23:02:42 +00001338 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1339 return;
1340
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001341 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001342 IsProtocolMethodDecl, IsOverridingMode,
1343 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001344
Chris Lattner3aff9192009-04-11 19:58:42 +00001345 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001346 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001347 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001348 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
1349 IsProtocolMethodDecl, IsOverridingMode, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001350 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001351
Fariborz Jahanian21121902011-08-08 18:03:17 +00001352 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001353 if (IsOverridingMode)
1354 Diag(ImpMethodDecl->getLocation(),
1355 diag::warn_conflicting_overriding_variadic);
1356 else
1357 Diag(ImpMethodDecl->getLocation(), diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001358 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001359 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001360}
1361
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001362/// WarnExactTypedMethods - This routine issues a warning if method
1363/// implementation declaration matches exactly that of its declaration.
1364void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1365 ObjCMethodDecl *MethodDecl,
1366 bool IsProtocolMethodDecl) {
1367 // don't issue warning when protocol method is optional because primary
1368 // class is not required to implement it and it is safe for protocol
1369 // to implement it.
1370 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1371 return;
1372 // don't issue warning when primary class's method is
1373 // depecated/unavailable.
1374 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1375 MethodDecl->hasAttr<DeprecatedAttr>())
1376 return;
1377
1378 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1379 IsProtocolMethodDecl, false, false);
1380 if (match)
1381 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1382 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1383 IM != EM; ++IM, ++IF) {
1384 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1385 *IM, *IF,
1386 IsProtocolMethodDecl, false, false);
1387 if (!match)
1388 break;
1389 }
1390 if (match)
1391 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001392 if (match)
1393 match = !(MethodDecl->isClassMethod() &&
1394 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001395
1396 if (match) {
1397 Diag(ImpMethodDecl->getLocation(),
1398 diag::warn_category_method_impl_match);
1399 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1400 }
1401}
1402
Mike Stump390b4cc2009-05-16 07:39:55 +00001403/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1404/// improve the efficiency of selector lookups and type checking by associating
1405/// with each protocol / interface / category the flattened instance tables. If
1406/// we used an immutable set to keep the table then it wouldn't add significant
1407/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001408
Steve Naroffefe7f362008-02-08 22:06:17 +00001409/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001410/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001411void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1412 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001413 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001414 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001415 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001416 ObjCContainerDecl *CDecl) {
1417 ObjCInterfaceDecl *IDecl;
1418 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1419 IDecl = C->getClassInterface();
1420 else
1421 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1422 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1423
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001424 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001425 ObjCInterfaceDecl *NSIDecl = 0;
1426 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001427 // check to see if class implements forwardInvocation method and objects
1428 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001429 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001430 // Under such conditions, which means that every method possible is
1431 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001432 // found" warnings.
1433 // FIXME: Use a general GetUnarySelector method for this.
1434 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1435 Selector fISelector = Context.Selectors.getSelector(1, &II);
1436 if (InsMap.count(fISelector))
1437 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1438 // need be implemented in the implementation.
1439 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1440 }
Mike Stump1eb44332009-09-09 15:08:12 +00001441
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001442 // If a method lookup fails locally we still need to look and see if
1443 // the method was implemented by a base class or an inherited
1444 // protocol. This lookup is slow, but occurs rarely in correct code
1445 // and otherwise would terminate in a warning.
1446
Chris Lattner4d391482007-12-12 07:09:47 +00001447 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001448 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001449 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001450 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001451 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001452 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001453 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001454 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001455 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001456 // Ugly, but necessary. Method declared in protcol might have
1457 // have been synthesized due to a property declared in the class which
1458 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001459 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001460 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001461 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001462 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001463 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
David Blaikied6471f72011-09-25 23:23:43 +00001464 != DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001465 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001466 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001467 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1468 << PDecl->getDeclName();
1469 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001470 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001471 }
1472 }
Chris Lattner4d391482007-12-12 07:09:47 +00001473 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001474 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001475 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001476 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001477 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001478 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1479 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001480 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001481 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001482 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1483 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001484 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001485 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001486 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1487 PDecl->getDeclName();
1488 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001489 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001490 }
Chris Lattner780f3292008-07-21 21:32:27 +00001491 // Check on this protocols's referenced protocols, recursively.
1492 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1493 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001494 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001495}
1496
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001497/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001498/// or protocol against those declared in their implementations.
1499///
1500void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1501 const llvm::DenseSet<Selector> &ClsMap,
1502 llvm::DenseSet<Selector> &InsMapSeen,
1503 llvm::DenseSet<Selector> &ClsMapSeen,
1504 ObjCImplDecl* IMPDecl,
1505 ObjCContainerDecl* CDecl,
1506 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001507 bool ImmediateClass,
1508 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001509 // Check and see if instance methods in class interface have been
1510 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001511 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1512 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001513 if (InsMapSeen.count((*I)->getSelector()))
1514 continue;
1515 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001516 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001517 !InsMap.count((*I)->getSelector())) {
1518 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001519 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1520 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001521 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001522 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001523 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001524 IMPDecl->getInstanceMethod((*I)->getSelector());
1525 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1526 "Expected to find the method through lookup as well");
1527 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001528 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001529 if (ImpMethodDecl) {
1530 if (!WarnExactMatch)
1531 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1532 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001533 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001534 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1535 isa<ObjCProtocolDecl>(CDecl));
1536 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001537 }
1538 }
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001540 // Check and see if class methods in class interface have been
1541 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001542 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001543 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001544 if (ClsMapSeen.count((*I)->getSelector()))
1545 continue;
1546 ClsMapSeen.insert((*I)->getSelector());
1547 if (!ClsMap.count((*I)->getSelector())) {
1548 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001549 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1550 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001551 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001552 ObjCMethodDecl *ImpMethodDecl =
1553 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001554 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1555 "Expected to find the method through lookup as well");
1556 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001557 if (!WarnExactMatch)
1558 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1559 isa<ObjCProtocolDecl>(CDecl));
1560 else
1561 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1562 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001563 }
1564 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001565
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001566 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001567 // Also methods in class extensions need be looked at next.
1568 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1569 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1570 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1571 IMPDecl,
1572 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001573 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001574
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001575 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001576 for (ObjCInterfaceDecl::all_protocol_iterator
1577 PI = I->all_referenced_protocol_begin(),
1578 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001579 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1580 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001581 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001582
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001583 // FIXME. For now, we are not checking for extact match of methods
1584 // in category implementation and its primary class's super class.
1585 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001586 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001587 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001588 I->getSuperClass(), IncompleteImpl, false);
1589 }
1590}
1591
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001592/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1593/// category matches with those implemented in its primary class and
1594/// warns each time an exact match is found.
1595void Sema::CheckCategoryVsClassMethodMatches(
1596 ObjCCategoryImplDecl *CatIMPDecl) {
1597 llvm::DenseSet<Selector> InsMap, ClsMap;
1598
1599 for (ObjCImplementationDecl::instmeth_iterator
1600 I = CatIMPDecl->instmeth_begin(),
1601 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1602 InsMap.insert((*I)->getSelector());
1603
1604 for (ObjCImplementationDecl::classmeth_iterator
1605 I = CatIMPDecl->classmeth_begin(),
1606 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1607 ClsMap.insert((*I)->getSelector());
1608 if (InsMap.empty() && ClsMap.empty())
1609 return;
1610
1611 // Get category's primary class.
1612 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1613 if (!CatDecl)
1614 return;
1615 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1616 if (!IDecl)
1617 return;
1618 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1619 bool IncompleteImpl = false;
1620 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1621 CatIMPDecl, IDecl,
1622 IncompleteImpl, false, true /*WarnExactMatch*/);
1623}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001624
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001625void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001626 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001627 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001628 llvm::DenseSet<Selector> InsMap;
1629 // Check and see if instance methods in class interface have been
1630 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001631 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001632 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001633 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001635 // Check and see if properties declared in the interface have either 1)
1636 // an implementation or 2) there is a @synthesize/@dynamic implementation
1637 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001638 if (isa<ObjCInterfaceDecl>(CDecl) &&
1639 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001640 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001641
Chris Lattner4d391482007-12-12 07:09:47 +00001642 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001643 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001644 I = IMPDecl->classmeth_begin(),
1645 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001646 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001647
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001648 // Check for type conflict of methods declared in a class/protocol and
1649 // its implementation; if any.
1650 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001651 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1652 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001653 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001654
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001655 // check all methods implemented in category against those declared
1656 // in its primary class.
1657 if (ObjCCategoryImplDecl *CatDecl =
1658 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1659 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001660
Chris Lattner4d391482007-12-12 07:09:47 +00001661 // Check the protocol list for unimplemented methods in the @implementation
1662 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001663 // Check and see if class methods in class interface have been
1664 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Chris Lattnercddc8882009-03-01 00:56:52 +00001666 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001667 for (ObjCInterfaceDecl::all_protocol_iterator
1668 PI = I->all_referenced_protocol_begin(),
1669 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001670 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001671 InsMap, ClsMap, I);
1672 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001673 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1674 Categories; Categories = Categories->getNextClassExtension())
1675 ImplMethodsVsClassMethods(S, IMPDecl,
1676 const_cast<ObjCCategoryDecl*>(Categories),
1677 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001678 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001679 // For extended class, unimplemented methods in its protocols will
1680 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001681 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001682 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1683 E = C->protocol_end(); PI != E; ++PI)
1684 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001685 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001686 // Report unimplemented properties in the category as well.
1687 // When reporting on missing setter/getters, do not report when
1688 // setter/getter is implemented in category's primary class
1689 // implementation.
1690 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1691 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1692 for (ObjCImplementationDecl::instmeth_iterator
1693 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1694 InsMap.insert((*I)->getSelector());
1695 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001696 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001697 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001698 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001699 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001700}
1701
Mike Stump1eb44332009-09-09 15:08:12 +00001702/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001703Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001704Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001705 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001706 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001707 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001708 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001709 for (unsigned i = 0; i != NumElts; ++i) {
1710 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001711 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001712 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001713 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001714 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001715 // Maybe we will complain about the shadowed template parameter.
1716 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1717 // Just pretend that we didn't see the previous declaration.
1718 PrevDecl = 0;
1719 }
1720
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001721 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001722 // GCC apparently allows the following idiom:
1723 //
1724 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1725 // @class XCElementToggler;
1726 //
Mike Stump1eb44332009-09-09 15:08:12 +00001727 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001728 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001729 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001730 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001731 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001732 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001733 // a forward class declaration matching a typedef name of a class refers
1734 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001735 if (const ObjCObjectType *OI =
1736 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1737 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001738 }
Chris Lattner4d391482007-12-12 07:09:47 +00001739 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001740 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1741 if (!IDecl) { // Not already seen? Make a forward decl.
1742 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1743 IdentList[i], IdentLocs[i], true);
1744
1745 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1746 // the current DeclContext. This prevents clients that walk DeclContext
1747 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1748 // declared later (if at all). We also take care to explicitly make
1749 // sure this declaration is visible for name lookup.
1750 PushOnScopeChains(IDecl, TUScope, false);
1751 CurContext->makeDeclVisibleInContext(IDecl, true);
1752 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001753 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1754 IDecl, IdentLocs[i]);
1755 CurContext->addDecl(CDecl);
1756 CheckObjCDeclScope(CDecl);
1757 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001758 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001759
1760 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001761}
1762
John McCall0f4c4c42011-06-16 01:15:19 +00001763static bool tryMatchRecordTypes(ASTContext &Context,
1764 Sema::MethodMatchStrategy strategy,
1765 const Type *left, const Type *right);
1766
John McCallf85e1932011-06-15 23:02:42 +00001767static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1768 QualType leftQT, QualType rightQT) {
1769 const Type *left =
1770 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1771 const Type *right =
1772 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1773
1774 if (left == right) return true;
1775
1776 // If we're doing a strict match, the types have to match exactly.
1777 if (strategy == Sema::MMS_strict) return false;
1778
1779 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1780
1781 // Otherwise, use this absurdly complicated algorithm to try to
1782 // validate the basic, low-level compatibility of the two types.
1783
1784 // As a minimum, require the sizes and alignments to match.
1785 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1786 return false;
1787
1788 // Consider all the kinds of non-dependent canonical types:
1789 // - functions and arrays aren't possible as return and parameter types
1790
1791 // - vector types of equal size can be arbitrarily mixed
1792 if (isa<VectorType>(left)) return isa<VectorType>(right);
1793 if (isa<VectorType>(right)) return false;
1794
1795 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001796 // - structs, unions, and Objective-C objects must match more-or-less
1797 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001798 // - everything else should be a scalar
1799 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001800 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001801
John McCall1d9b3b22011-09-09 05:25:32 +00001802 // Make scalars agree in kind, except count bools as chars, and group
1803 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001804 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1805 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1806 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1807 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001808 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1809 leftSK = Type::STK_ObjCObjectPointer;
1810 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1811 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001812
1813 // Note that data member pointers and function member pointers don't
1814 // intermix because of the size differences.
1815
1816 return (leftSK == rightSK);
1817}
Chris Lattner4d391482007-12-12 07:09:47 +00001818
John McCall0f4c4c42011-06-16 01:15:19 +00001819static bool tryMatchRecordTypes(ASTContext &Context,
1820 Sema::MethodMatchStrategy strategy,
1821 const Type *lt, const Type *rt) {
1822 assert(lt && rt && lt != rt);
1823
1824 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1825 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1826 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1827
1828 // Require union-hood to match.
1829 if (left->isUnion() != right->isUnion()) return false;
1830
1831 // Require an exact match if either is non-POD.
1832 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1833 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1834 return false;
1835
1836 // Require size and alignment to match.
1837 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1838
1839 // Require fields to match.
1840 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1841 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1842 for (; li != le && ri != re; ++li, ++ri) {
1843 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1844 return false;
1845 }
1846 return (li == le && ri == re);
1847}
1848
Chris Lattner4d391482007-12-12 07:09:47 +00001849/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1850/// returns true, or false, accordingly.
1851/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001852bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1853 const ObjCMethodDecl *right,
1854 MethodMatchStrategy strategy) {
1855 if (!matchTypes(Context, strategy,
1856 left->getResultType(), right->getResultType()))
1857 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001858
John McCallf85e1932011-06-15 23:02:42 +00001859 if (getLangOptions().ObjCAutoRefCount &&
1860 (left->hasAttr<NSReturnsRetainedAttr>()
1861 != right->hasAttr<NSReturnsRetainedAttr>() ||
1862 left->hasAttr<NSConsumesSelfAttr>()
1863 != right->hasAttr<NSConsumesSelfAttr>()))
1864 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001865
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001866 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001867 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001868
John McCallf85e1932011-06-15 23:02:42 +00001869 for (; li != le; ++li, ++ri) {
1870 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001871 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001872
1873 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1874 return false;
1875
1876 if (getLangOptions().ObjCAutoRefCount &&
1877 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1878 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001879 }
1880 return true;
1881}
1882
Sebastian Redldb9d2142010-08-02 23:18:59 +00001883/// \brief Read the contents of the method pool for a given selector from
1884/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001885///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001886/// This routine should only be called once, when the method pool has no entry
1887/// for this selector.
1888Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001889 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001890 assert(MethodPool.find(Sel) == MethodPool.end() &&
1891 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001892
1893 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001894 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Sebastian Redldb9d2142010-08-02 23:18:59 +00001896 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001897}
1898
Sebastian Redldb9d2142010-08-02 23:18:59 +00001899void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1900 bool instance) {
1901 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1902 if (Pos == MethodPool.end()) {
1903 if (ExternalSource)
1904 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001905 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001906 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1907 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001908 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001909 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001910 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001911 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001912 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001913 Entry.Method = Method;
1914 Entry.Next = 0;
1915 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001916 }
Mike Stump1eb44332009-09-09 15:08:12 +00001917
Chris Lattnerb25df352009-03-04 05:16:45 +00001918 // We've seen a method with this name, see if we have already seen this type
1919 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001920 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1921 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1922
1923 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001924 ObjCMethodDecl *PrevObjCMethod = List->Method;
1925 PrevObjCMethod->setDefined(impl);
1926 // If a method is deprecated, push it in the global pool.
1927 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001928 if (Method->isDeprecated()) {
1929 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001930 List->Method = Method;
1931 }
1932 // If new method is unavailable, push it into global pool
1933 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001934 if (Method->isUnavailable()) {
1935 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001936 List->Method = Method;
1937 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001938 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001939 }
John McCallf85e1932011-06-15 23:02:42 +00001940 }
Mike Stump1eb44332009-09-09 15:08:12 +00001941
Chris Lattnerb25df352009-03-04 05:16:45 +00001942 // We have a new signature for an existing method - add it.
1943 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001944 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1945 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001946}
1947
John McCallf85e1932011-06-15 23:02:42 +00001948/// Determines if this is an "acceptable" loose mismatch in the global
1949/// method pool. This exists mostly as a hack to get around certain
1950/// global mismatches which we can't afford to make warnings / errors.
1951/// Really, what we want is a way to take a method out of the global
1952/// method pool.
1953static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1954 ObjCMethodDecl *other) {
1955 if (!chosen->isInstanceMethod())
1956 return false;
1957
1958 Selector sel = chosen->getSelector();
1959 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1960 return false;
1961
1962 // Don't complain about mismatches for -length if the method we
1963 // chose has an integral result type.
1964 return (chosen->getResultType()->isIntegerType());
1965}
1966
Sebastian Redldb9d2142010-08-02 23:18:59 +00001967ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001968 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001969 bool warn, bool instance) {
1970 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1971 if (Pos == MethodPool.end()) {
1972 if (ExternalSource)
1973 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001974 else
1975 return 0;
1976 }
1977
Sebastian Redldb9d2142010-08-02 23:18:59 +00001978 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001979
Sebastian Redldb9d2142010-08-02 23:18:59 +00001980 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001981 bool issueDiagnostic = false, issueError = false;
1982
1983 // We support a warning which complains about *any* difference in
1984 // method signature.
1985 bool strictSelectorMatch =
1986 (receiverIdOrClass && warn &&
1987 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
1988 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00001989 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001990 if (strictSelectorMatch)
1991 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00001992 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
1993 MMS_strict)) {
1994 issueDiagnostic = true;
1995 break;
1996 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001997 }
1998
John McCallf85e1932011-06-15 23:02:42 +00001999 // If we didn't see any strict differences, we won't see any loose
2000 // differences. In ARC, however, we also need to check for loose
2001 // mismatches, because most of them are errors.
2002 if (!strictSelectorMatch ||
2003 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002004 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002005 // This checks if the methods differ in type mismatch.
2006 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2007 MMS_loose) &&
2008 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2009 issueDiagnostic = true;
2010 if (getLangOptions().ObjCAutoRefCount)
2011 issueError = true;
2012 break;
2013 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002014 }
2015
John McCallf85e1932011-06-15 23:02:42 +00002016 if (issueDiagnostic) {
2017 if (issueError)
2018 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2019 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002020 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2021 else
2022 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002023
2024 Diag(MethList.Method->getLocStart(),
2025 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002026 << MethList.Method->getSourceRange();
2027 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2028 Diag(Next->Method->getLocStart(), diag::note_also_found)
2029 << Next->Method->getSourceRange();
2030 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002031 }
2032 return MethList.Method;
2033}
2034
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002035ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002036 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2037 if (Pos == MethodPool.end())
2038 return 0;
2039
2040 GlobalMethods &Methods = Pos->second;
2041
2042 if (Methods.first.Method && Methods.first.Method->isDefined())
2043 return Methods.first.Method;
2044 if (Methods.second.Method && Methods.second.Method->isDefined())
2045 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002046 return 0;
2047}
2048
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002049/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2050/// identical selector names in current and its super classes and issues
2051/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002052void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2053 ObjCMethodDecl *Method,
2054 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002055 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2056 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002057
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002058 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002059 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002060 SD->lookupMethod(Method->getSelector(), IsInstance);
2061 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002062 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002063 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002064 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002065 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2066 E = Method->param_end();
2067 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2068 for (; ParamI != E; ++ParamI, ++PrevI) {
2069 // Number of parameters are the same and is guaranteed by selector match.
2070 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2071 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2072 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002073 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002074 // respective argument type in the super class method, issue warning;
2075 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002076 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002077 << T1 << T2;
2078 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2079 return;
2080 }
2081 }
2082 ID = SD;
2083 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002084}
2085
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002086/// DiagnoseDuplicateIvars -
2087/// Check for duplicate ivars in the entire class at the start of
2088/// @implementation. This becomes necesssary because class extension can
2089/// add ivars to a class in random order which will not be known until
2090/// class's @implementation is seen.
2091void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2092 ObjCInterfaceDecl *SID) {
2093 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2094 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2095 ObjCIvarDecl* Ivar = (*IVI);
2096 if (Ivar->isInvalidDecl())
2097 continue;
2098 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2099 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2100 if (prevIvar) {
2101 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2102 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2103 Ivar->setInvalidDecl();
2104 }
2105 }
2106 }
2107}
2108
Steve Naroffa56f6162007-12-18 01:30:32 +00002109// Note: For class/category implemenations, allMethods/allProperties is
2110// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002111void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002112 Decl **allMethods, unsigned allNum,
2113 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002114 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002115
2116 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002117 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002118 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2119 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002120
Mike Stump1eb44332009-09-09 15:08:12 +00002121 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002122 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2123 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002124 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002125
Ted Kremenek782f2f52010-01-07 01:20:12 +00002126 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2127 // FIXME: This is wrong. We shouldn't be pretending that there is
2128 // an '@end' in the declaration.
2129 SourceLocation L = ClassDecl->getLocation();
2130 AtEnd.setBegin(L);
2131 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002132 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002133 }
2134
Steve Naroff0701bbb2009-01-08 17:28:14 +00002135 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2136 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2137 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2138
Chris Lattner4d391482007-12-12 07:09:47 +00002139 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002140 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002141 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002142
2143 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002144 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002145 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002146 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002147 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002148 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002149 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002150 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002151 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002152 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002153 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002154 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002155 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002156 InsMap[Method->getSelector()] = Method;
2157 /// The following allows us to typecheck messages to "id".
2158 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002159 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002160 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002161 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002162 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002163 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002164 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002165 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002166 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002167 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002168 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002169 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002170 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002171 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002172 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002173 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002174 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002175 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002176 /// The following allows us to typecheck messages to "Class".
2177 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002178 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002179 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002180 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002181 }
2182 }
2183 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002184 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002185 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002186 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002187 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002188 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002189 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002190 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002191 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002192 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002193
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002194 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002195 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002196 if (C->IsClassExtension()) {
2197 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2198 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002199 }
Chris Lattner4d391482007-12-12 07:09:47 +00002200 }
Steve Naroff09c47192009-01-09 15:36:25 +00002201 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002202 if (CDecl->getIdentifier())
2203 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2204 // user-defined setter/getter. It also synthesizes setter/getter methods
2205 // and adds them to the DeclContext and global method pools.
2206 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2207 E = CDecl->prop_end();
2208 I != E; ++I)
2209 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002210 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002211 }
2212 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002213 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002214 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002215 // Any property declared in a class extension might have user
2216 // declared setter or getter in current class extension or one
2217 // of the other class extensions. Mark them as synthesized as
2218 // property will be synthesized when property with same name is
2219 // seen in the @implementation.
2220 for (const ObjCCategoryDecl *ClsExtDecl =
2221 IDecl->getFirstClassExtension();
2222 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2223 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2224 E = ClsExtDecl->prop_end(); I != E; ++I) {
2225 ObjCPropertyDecl *Property = (*I);
2226 // Skip over properties declared @dynamic
2227 if (const ObjCPropertyImplDecl *PIDecl
2228 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2229 if (PIDecl->getPropertyImplementation()
2230 == ObjCPropertyImplDecl::Dynamic)
2231 continue;
2232
2233 for (const ObjCCategoryDecl *CExtDecl =
2234 IDecl->getFirstClassExtension();
2235 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2236 if (ObjCMethodDecl *GetterMethod =
2237 CExtDecl->getInstanceMethod(Property->getGetterName()))
2238 GetterMethod->setSynthesized(true);
2239 if (!Property->isReadOnly())
2240 if (ObjCMethodDecl *SetterMethod =
2241 CExtDecl->getInstanceMethod(Property->getSetterName()))
2242 SetterMethod->setSynthesized(true);
2243 }
2244 }
2245 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002246 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002247 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002248 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002249
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002250 if (LangOpts.ObjCNonFragileABI2)
2251 while (IDecl->getSuperClass()) {
2252 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2253 IDecl = IDecl->getSuperClass();
2254 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002255 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002256 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002257 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002258 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002259 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002260
Chris Lattner4d391482007-12-12 07:09:47 +00002261 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002262 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002263 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002264 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002265 Categories; Categories = Categories->getNextClassCategory()) {
2266 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002267 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002268 break;
2269 }
2270 }
2271 }
2272 }
Chris Lattner682bf922009-03-29 16:50:03 +00002273 if (isInterfaceDeclKind) {
2274 // Reject invalid vardecls.
2275 for (unsigned i = 0; i != tuvNum; i++) {
2276 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2277 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2278 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002279 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002280 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002281 }
Chris Lattner682bf922009-03-29 16:50:03 +00002282 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002283 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002284 ActOnObjCContainerFinishDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00002285}
2286
2287
2288/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2289/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002290static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002291CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002292 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002293}
2294
Ted Kremenek422bae72010-04-18 04:59:38 +00002295static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002296bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002297 // The 'ibaction' attribute is allowed on method definitions because of
2298 // how the IBAction macro is used on both method declarations and definitions.
2299 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002300 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2301 if ((*i)->getKind() != attr::IBAction)
2302 return true;
2303 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002304}
2305
Douglas Gregore97179c2011-09-08 01:46:34 +00002306namespace {
2307 /// \brief Describes the compatibility of a result type with its method.
2308 enum ResultTypeCompatibilityKind {
2309 RTC_Compatible,
2310 RTC_Incompatible,
2311 RTC_Unknown
2312 };
2313}
2314
Douglas Gregor926df6c2011-06-11 01:09:30 +00002315/// \brief Check whether the declared result type of the given Objective-C
2316/// method declaration is compatible with the method's class.
2317///
Douglas Gregore97179c2011-09-08 01:46:34 +00002318static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002319CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2320 ObjCInterfaceDecl *CurrentClass) {
2321 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002322
2323 // If an Objective-C method inherits its related result type, then its
2324 // declared result type must be compatible with its own class type. The
2325 // declared result type is compatible if:
2326 if (const ObjCObjectPointerType *ResultObjectType
2327 = ResultType->getAs<ObjCObjectPointerType>()) {
2328 // - it is id or qualified id, or
2329 if (ResultObjectType->isObjCIdType() ||
2330 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002331 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002332
2333 if (CurrentClass) {
2334 if (ObjCInterfaceDecl *ResultClass
2335 = ResultObjectType->getInterfaceDecl()) {
2336 // - it is the same as the method's class type, or
2337 if (CurrentClass == ResultClass)
Douglas Gregore97179c2011-09-08 01:46:34 +00002338 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002339
2340 // - it is a superclass of the method's class type
2341 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002342 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002343 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002344 } else {
2345 // Any Objective-C pointer type might be acceptable for a protocol
2346 // method; we just don't know.
2347 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002348 }
2349 }
2350
Douglas Gregore97179c2011-09-08 01:46:34 +00002351 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002352}
2353
John McCall6c2c2502011-07-22 02:45:48 +00002354namespace {
2355/// A helper class for searching for methods which a particular method
2356/// overrides.
2357class OverrideSearch {
2358 Sema &S;
2359 ObjCMethodDecl *Method;
2360 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2361 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2362 bool Recursive;
2363
2364public:
2365 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2366 Selector selector = method->getSelector();
2367
2368 // Bypass this search if we've never seen an instance/class method
2369 // with this selector before.
2370 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2371 if (it == S.MethodPool.end()) {
2372 if (!S.ExternalSource) return;
2373 it = S.ReadMethodPool(selector);
2374 }
2375 ObjCMethodList &list =
2376 method->isInstanceMethod() ? it->second.first : it->second.second;
2377 if (!list.Method) return;
2378
2379 ObjCContainerDecl *container
2380 = cast<ObjCContainerDecl>(method->getDeclContext());
2381
2382 // Prevent the search from reaching this container again. This is
2383 // important with categories, which override methods from the
2384 // interface and each other.
2385 Searched.insert(container);
2386 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002387 }
John McCall6c2c2502011-07-22 02:45:48 +00002388
2389 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2390 iterator begin() const { return Overridden.begin(); }
2391 iterator end() const { return Overridden.end(); }
2392
2393private:
2394 void searchFromContainer(ObjCContainerDecl *container) {
2395 if (container->isInvalidDecl()) return;
2396
2397 switch (container->getDeclKind()) {
2398#define OBJCCONTAINER(type, base) \
2399 case Decl::type: \
2400 searchFrom(cast<type##Decl>(container)); \
2401 break;
2402#define ABSTRACT_DECL(expansion)
2403#define DECL(type, base) \
2404 case Decl::type:
2405#include "clang/AST/DeclNodes.inc"
2406 llvm_unreachable("not an ObjC container!");
2407 }
2408 }
2409
2410 void searchFrom(ObjCProtocolDecl *protocol) {
2411 // A method in a protocol declaration overrides declarations from
2412 // referenced ("parent") protocols.
2413 search(protocol->getReferencedProtocols());
2414 }
2415
2416 void searchFrom(ObjCCategoryDecl *category) {
2417 // A method in a category declaration overrides declarations from
2418 // the main class and from protocols the category references.
2419 search(category->getClassInterface());
2420 search(category->getReferencedProtocols());
2421 }
2422
2423 void searchFrom(ObjCCategoryImplDecl *impl) {
2424 // A method in a category definition that has a category
2425 // declaration overrides declarations from the category
2426 // declaration.
2427 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2428 search(category);
2429
2430 // Otherwise it overrides declarations from the class.
2431 } else {
2432 search(impl->getClassInterface());
2433 }
2434 }
2435
2436 void searchFrom(ObjCInterfaceDecl *iface) {
2437 // A method in a class declaration overrides declarations from
2438
2439 // - categories,
2440 for (ObjCCategoryDecl *category = iface->getCategoryList();
2441 category; category = category->getNextClassCategory())
2442 search(category);
2443
2444 // - the super class, and
2445 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2446 search(super);
2447
2448 // - any referenced protocols.
2449 search(iface->getReferencedProtocols());
2450 }
2451
2452 void searchFrom(ObjCImplementationDecl *impl) {
2453 // A method in a class implementation overrides declarations from
2454 // the class interface.
2455 search(impl->getClassInterface());
2456 }
2457
2458
2459 void search(const ObjCProtocolList &protocols) {
2460 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2461 i != e; ++i)
2462 search(*i);
2463 }
2464
2465 void search(ObjCContainerDecl *container) {
2466 // Abort if we've already searched this container.
2467 if (!Searched.insert(container)) return;
2468
2469 // Check for a method in this container which matches this selector.
2470 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2471 Method->isInstanceMethod());
2472
2473 // If we find one, record it and bail out.
2474 if (meth) {
2475 Overridden.insert(meth);
2476 return;
2477 }
2478
2479 // Otherwise, search for methods that a hypothetical method here
2480 // would have overridden.
2481
2482 // Note that we're now in a recursive case.
2483 Recursive = true;
2484
2485 searchFromContainer(container);
2486 }
2487};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002488}
2489
John McCalld226f652010-08-21 09:40:31 +00002490Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002491 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002492 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002493 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002494 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002495 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002496 Selector Sel,
2497 // optional arguments. The number of types/arguments is obtained
2498 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002499 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002500 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002501 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002502 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002503 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002504 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002505 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002506 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002507 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002508 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2509 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002510 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002511
Douglas Gregore97179c2011-09-08 01:46:34 +00002512 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002513 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002514 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002515 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002516
Steve Naroffccef3712009-02-20 22:59:16 +00002517 // Methods cannot return interface types. All ObjC objects are
2518 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002519 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002520 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2521 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002522 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002523 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002524
2525 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002526 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002527 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002528 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002529 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002530 }
Mike Stump1eb44332009-09-09 15:08:12 +00002531
2532 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002533 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002534 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002535 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002536 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002537 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002538 /*isSynthesized=*/false,
2539 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002540 MethodDeclKind == tok::objc_optional
2541 ? ObjCMethodDecl::Optional
2542 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002543 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002544
Chris Lattner5f9e2722011-07-23 10:55:15 +00002545 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002546
Chris Lattner7db638d2009-04-11 19:42:43 +00002547 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002548 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002549 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002550
Chris Lattnere294d3f2009-04-11 18:57:04 +00002551 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002552 ArgType = Context.getObjCIdType();
2553 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002554 } else {
John McCall58e46772009-10-23 21:48:59 +00002555 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002556 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002557 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002558 }
Mike Stump1eb44332009-09-09 15:08:12 +00002559
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002560 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2561 LookupOrdinaryName, ForRedeclaration);
2562 LookupName(R, S);
2563 if (R.isSingleResult()) {
2564 NamedDecl *PrevDecl = R.getFoundDecl();
2565 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002566 Diag(ArgInfo[i].NameLoc,
2567 (MethodDefinition ? diag::warn_method_param_redefinition
2568 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002569 << ArgInfo[i].Name;
2570 Diag(PrevDecl->getLocation(),
2571 diag::note_previous_declaration);
2572 }
2573 }
2574
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002575 SourceLocation StartLoc = DI
2576 ? DI->getTypeLoc().getBeginLoc()
2577 : ArgInfo[i].NameLoc;
2578
John McCall81ef3e62011-04-23 02:46:06 +00002579 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2580 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2581 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002582
John McCall70798862011-05-02 00:30:12 +00002583 Param->setObjCMethodScopeInfo(i);
2584
Chris Lattner0ed844b2008-04-04 06:12:32 +00002585 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002586 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002587
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002588 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002589 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002590
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002591 S->AddDecl(Param);
2592 IdResolver.AddDecl(Param);
2593
Chris Lattner0ed844b2008-04-04 06:12:32 +00002594 Params.push_back(Param);
2595 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002596
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002597 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002598 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002599 QualType ArgType = Param->getType();
2600 if (ArgType.isNull())
2601 ArgType = Context.getObjCIdType();
2602 else
2603 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002604 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002605 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002606 Diag(Param->getLocation(),
2607 diag::err_object_cannot_be_passed_returned_by_value)
2608 << 1 << ArgType;
2609 Param->setInvalidDecl();
2610 }
2611 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002612
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002613 Params.push_back(Param);
2614 }
2615
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002616 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002617 ObjCMethod->setObjCDeclQualifier(
2618 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002619
2620 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002621 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002622
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002623 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002624 const ObjCMethodDecl *PrevMethod = 0;
2625 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002626 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002627 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2628 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002629 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002630 PrevMethod = ImpDecl->getClassMethod(Sel);
2631 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002632 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002633
Sean Huntcf807c42010-08-18 23:23:40 +00002634 if (ObjCMethod->hasAttrs() &&
2635 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002636 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002637 } else {
2638 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002639 }
John McCall6c2c2502011-07-22 02:45:48 +00002640
Chris Lattner4d391482007-12-12 07:09:47 +00002641 if (PrevMethod) {
2642 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002643 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002644 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002645 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002646 }
John McCall54abf7d2009-11-04 02:18:39 +00002647
Douglas Gregor926df6c2011-06-11 01:09:30 +00002648 // If this Objective-C method does not have a related result type, but we
2649 // are allowed to infer related result types, try to do so based on the
2650 // method family.
2651 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2652 if (!CurrentClass) {
2653 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2654 CurrentClass = Cat->getClassInterface();
2655 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2656 CurrentClass = Impl->getClassInterface();
2657 else if (ObjCCategoryImplDecl *CatImpl
2658 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2659 CurrentClass = CatImpl->getClassInterface();
2660 }
John McCall6c2c2502011-07-22 02:45:48 +00002661
Douglas Gregore97179c2011-09-08 01:46:34 +00002662 ResultTypeCompatibilityKind RTC
2663 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002664
2665 // Search for overridden methods and merge information down from them.
2666 OverrideSearch overrides(*this, ObjCMethod);
2667 for (OverrideSearch::iterator
2668 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2669 ObjCMethodDecl *overridden = *i;
2670
2671 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002672 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002673 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002674
2675 // Then merge the declarations.
2676 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002677
2678 // Check for overriding methods
2679 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
2680 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext())) {
2681 WarnConflictingTypedMethods(ObjCMethod, overridden,
2682 isa<ObjCProtocolDecl>(overridden->getDeclContext()), true);
2683 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002684 }
2685
John McCallf85e1932011-06-15 23:02:42 +00002686 bool ARCError = false;
2687 if (getLangOptions().ObjCAutoRefCount)
2688 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2689
Douglas Gregore97179c2011-09-08 01:46:34 +00002690 // Infer the related result type when possible.
2691 if (!ARCError && RTC == RTC_Compatible &&
2692 !ObjCMethod->hasRelatedResultType() &&
2693 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002694 bool InferRelatedResultType = false;
2695 switch (ObjCMethod->getMethodFamily()) {
2696 case OMF_None:
2697 case OMF_copy:
2698 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002699 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002700 case OMF_mutableCopy:
2701 case OMF_release:
2702 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002703 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002704 break;
2705
2706 case OMF_alloc:
2707 case OMF_new:
2708 InferRelatedResultType = ObjCMethod->isClassMethod();
2709 break;
2710
2711 case OMF_init:
2712 case OMF_autorelease:
2713 case OMF_retain:
2714 case OMF_self:
2715 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2716 break;
2717 }
2718
John McCall6c2c2502011-07-22 02:45:48 +00002719 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002720 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002721 }
2722
John McCalld226f652010-08-21 09:40:31 +00002723 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002724}
2725
Chris Lattnercc98eac2008-12-17 07:13:27 +00002726bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002727 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002728 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002729 // Following is also an error. But it is caused by a missing @end
2730 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002731 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2732 return false;
2733 }
2734
Anders Carlsson15281452008-11-04 16:57:32 +00002735 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2736 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002737
Anders Carlsson15281452008-11-04 16:57:32 +00002738 return true;
2739}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002740
Chris Lattnercc98eac2008-12-17 07:13:27 +00002741/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2742/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002743void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002744 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002745 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002746 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002747 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002748 if (!Class) {
2749 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2750 return;
2751 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002752 if (LangOpts.ObjCNonFragileABI) {
2753 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2754 return;
2755 }
Mike Stump1eb44332009-09-09 15:08:12 +00002756
Chris Lattnercc98eac2008-12-17 07:13:27 +00002757 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002758 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002759 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002760 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002761 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002762 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002763 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002764 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2765 /*FIXME: StartL=*/ID->getLocation(),
2766 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002767 ID->getIdentifier(), ID->getType(),
2768 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002769 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002770 }
Mike Stump1eb44332009-09-09 15:08:12 +00002771
Chris Lattnercc98eac2008-12-17 07:13:27 +00002772 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002773 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002774 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002775 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002776 if (getLangOptions().CPlusPlus)
2777 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002778 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002779 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002780 }
2781}
2782
Douglas Gregor160b5632010-04-26 17:32:49 +00002783/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002784VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2785 SourceLocation StartLoc,
2786 SourceLocation IdLoc,
2787 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002788 bool Invalid) {
2789 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2790 // duration shall not be qualified by an address-space qualifier."
2791 // Since all parameters have automatic store duration, they can not have
2792 // an address space.
2793 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002794 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002795 Invalid = true;
2796 }
2797
2798 // An @catch parameter must be an unqualified object pointer type;
2799 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2800 if (Invalid) {
2801 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002802 } else if (T->isDependentType()) {
2803 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002804 } else if (!T->isObjCObjectPointerType()) {
2805 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002806 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002807 } else if (T->isObjCQualifiedIdType()) {
2808 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002809 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002810 }
2811
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002812 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2813 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002814 New->setExceptionVariable(true);
2815
Douglas Gregor160b5632010-04-26 17:32:49 +00002816 if (Invalid)
2817 New->setInvalidDecl();
2818 return New;
2819}
2820
John McCalld226f652010-08-21 09:40:31 +00002821Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002822 const DeclSpec &DS = D.getDeclSpec();
2823
2824 // We allow the "register" storage class on exception variables because
2825 // GCC did, but we drop it completely. Any other storage class is an error.
2826 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2827 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2828 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2829 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2830 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2831 << DS.getStorageClassSpec();
2832 }
2833 if (D.getDeclSpec().isThreadSpecified())
2834 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2835 D.getMutableDeclSpec().ClearStorageClassSpecs();
2836
2837 DiagnoseFunctionSpecifiers(D);
2838
2839 // Check that there are no default arguments inside the type of this
2840 // exception object (C++ only).
2841 if (getLangOptions().CPlusPlus)
2842 CheckExtraCXXDefaultArguments(D);
2843
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002844 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002845 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002846
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002847 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2848 D.getSourceRange().getBegin(),
2849 D.getIdentifierLoc(),
2850 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002851 D.isInvalidType());
2852
2853 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2854 if (D.getCXXScopeSpec().isSet()) {
2855 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2856 << D.getCXXScopeSpec().getRange();
2857 New->setInvalidDecl();
2858 }
2859
2860 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002861 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002862 if (D.getIdentifier())
2863 IdResolver.AddDecl(New);
2864
2865 ProcessDeclAttributes(S, New, D);
2866
2867 if (New->hasAttr<BlocksAttr>())
2868 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002869 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002870}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002871
2872/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002873/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002874void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002875 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002876 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2877 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002878 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002879 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002880 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002881 }
2882}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002883
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002884void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002885 // Load referenced selectors from the external source.
2886 if (ExternalSource) {
2887 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2888 ExternalSource->ReadReferencedSelectors(Sels);
2889 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2890 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2891 }
2892
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002893 // Warning will be issued only when selector table is
2894 // generated (which means there is at lease one implementation
2895 // in the TU). This is to match gcc's behavior.
2896 if (ReferencedSelectors.empty() ||
2897 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002898 return;
2899 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2900 ReferencedSelectors.begin(),
2901 E = ReferencedSelectors.end(); S != E; ++S) {
2902 Selector Sel = (*S).first;
2903 if (!LookupImplementedMethodInGlobalPool(Sel))
2904 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2905 }
2906 return;
2907}