blob: 62b4a7c0cc7730a7cf3b4581b2d1f7f5359fa6c5 [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.
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001011 if (!Context.hasSameType(ImplIvar->getType(), ClsIvar->getType())) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001012 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_type)
Chris Lattner08631c52008-11-23 21:45:46 +00001013 << ImplIvar->getIdentifier()
1014 << ImplIvar->getType() << ClsIvar->getType();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001015 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Richard Smitha6b8b2c2011-10-10 18:28:20 +00001016 } else if (ImplIvar->isBitField() && ClsIvar->isBitField() &&
1017 ImplIvar->getBitWidthValue(Context) !=
1018 ClsIvar->getBitWidthValue(Context)) {
1019 Diag(ImplIvar->getBitWidth()->getLocStart(),
1020 diag::err_conflicting_ivar_bitwidth) << ImplIvar->getIdentifier();
1021 Diag(ClsIvar->getBitWidth()->getLocStart(),
1022 diag::note_previous_definition);
Mike Stump1eb44332009-09-09 15:08:12 +00001023 }
Steve Naroffca331292009-03-03 14:49:36 +00001024 // Make sure the names are identical.
1025 if (ImplIvar->getIdentifier() != ClsIvar->getIdentifier()) {
Chris Lattnerfa25bbb2008-11-19 05:08:23 +00001026 Diag(ImplIvar->getLocation(), diag::err_conflicting_ivar_name)
Chris Lattner08631c52008-11-23 21:45:46 +00001027 << ImplIvar->getIdentifier() << ClsIvar->getIdentifier();
Chris Lattner5f4a6822008-11-23 23:12:31 +00001028 Diag(ClsIvar->getLocation(), diag::note_previous_definition);
Chris Lattner4d391482007-12-12 07:09:47 +00001029 }
1030 --numIvars;
Chris Lattner4d391482007-12-12 07:09:47 +00001031 }
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Chris Lattner609e4c72007-12-12 18:11:49 +00001033 if (numIvars > 0)
Chris Lattner0e391052007-12-12 18:19:52 +00001034 Diag(ivars[j]->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner609e4c72007-12-12 18:11:49 +00001035 else if (IVI != IVE)
Chris Lattner0e391052007-12-12 18:19:52 +00001036 Diag((*IVI)->getLocation(), diag::err_inconsistant_ivar_count);
Chris Lattner4d391482007-12-12 07:09:47 +00001037}
1038
Steve Naroff3c2eb662008-02-10 21:38:56 +00001039void Sema::WarnUndefinedMethod(SourceLocation ImpLoc, ObjCMethodDecl *method,
Fariborz Jahanian52146832010-03-31 18:23:33 +00001040 bool &IncompleteImpl, unsigned DiagID) {
Fariborz Jahanian327126e2011-06-24 20:31:37 +00001041 // No point warning no definition of method which is 'unavailable'.
1042 if (method->hasAttr<UnavailableAttr>())
1043 return;
Steve Naroff3c2eb662008-02-10 21:38:56 +00001044 if (!IncompleteImpl) {
1045 Diag(ImpLoc, diag::warn_incomplete_impl);
1046 IncompleteImpl = true;
1047 }
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001048 if (DiagID == diag::warn_unimplemented_protocol_method)
1049 Diag(ImpLoc, DiagID) << method->getDeclName();
1050 else
1051 Diag(method->getLocation(), DiagID) << method->getDeclName();
Steve Naroff3c2eb662008-02-10 21:38:56 +00001052}
1053
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001054/// Determines if type B can be substituted for type A. Returns true if we can
1055/// guarantee that anything that the user will do to an object of type A can
1056/// also be done to an object of type B. This is trivially true if the two
1057/// types are the same, or if B is a subclass of A. It becomes more complex
1058/// in cases where protocols are involved.
1059///
1060/// Object types in Objective-C describe the minimum requirements for an
1061/// object, rather than providing a complete description of a type. For
1062/// example, if A is a subclass of B, then B* may refer to an instance of A.
1063/// The principle of substitutability means that we may use an instance of A
1064/// anywhere that we may use an instance of B - it will implement all of the
1065/// ivars of B and all of the methods of B.
1066///
1067/// This substitutability is important when type checking methods, because
1068/// the implementation may have stricter type definitions than the interface.
1069/// The interface specifies minimum requirements, but the implementation may
1070/// have more accurate ones. For example, a method may privately accept
1071/// instances of B, but only publish that it accepts instances of A. Any
1072/// object passed to it will be type checked against B, and so will implicitly
1073/// by a valid A*. Similarly, a method may return a subclass of the class that
1074/// it is declared as returning.
1075///
1076/// This is most important when considering subclassing. A method in a
1077/// subclass must accept any object as an argument that its superclass's
1078/// implementation accepts. It may, however, accept a more general type
1079/// without breaking substitutability (i.e. you can still use the subclass
1080/// anywhere that you can use the superclass, but not vice versa). The
1081/// converse requirement applies to return types: the return type for a
1082/// subclass method must be a valid object of the kind that the superclass
1083/// advertises, but it may be specified more accurately. This avoids the need
1084/// for explicit down-casting by callers.
1085///
1086/// Note: This is a stricter requirement than for assignment.
John McCall10302c02010-10-28 02:34:38 +00001087static bool isObjCTypeSubstitutable(ASTContext &Context,
1088 const ObjCObjectPointerType *A,
1089 const ObjCObjectPointerType *B,
1090 bool rejectId) {
1091 // Reject a protocol-unqualified id.
1092 if (rejectId && B->isObjCIdType()) return false;
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001093
1094 // If B is a qualified id, then A must also be a qualified id and it must
1095 // implement all of the protocols in B. It may not be a qualified class.
1096 // For example, MyClass<A> can be assigned to id<A>, but MyClass<A> is a
1097 // stricter definition so it is not substitutable for id<A>.
1098 if (B->isObjCQualifiedIdType()) {
1099 return A->isObjCQualifiedIdType() &&
John McCall10302c02010-10-28 02:34:38 +00001100 Context.ObjCQualifiedIdTypesAreCompatible(QualType(A, 0),
1101 QualType(B,0),
1102 false);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001103 }
1104
1105 /*
1106 // id is a special type that bypasses type checking completely. We want a
1107 // warning when it is used in one place but not another.
1108 if (C.isObjCIdType(A) || C.isObjCIdType(B)) return false;
1109
1110
1111 // If B is a qualified id, then A must also be a qualified id (which it isn't
1112 // if we've got this far)
1113 if (B->isObjCQualifiedIdType()) return false;
1114 */
1115
1116 // Now we know that A and B are (potentially-qualified) class types. The
1117 // normal rules for assignment apply.
John McCall10302c02010-10-28 02:34:38 +00001118 return Context.canAssignObjCInterfaces(A, B);
David Chisnalle8a2d4c2010-10-25 17:23:52 +00001119}
1120
John McCall10302c02010-10-28 02:34:38 +00001121static SourceRange getTypeRange(TypeSourceInfo *TSI) {
1122 return (TSI ? TSI->getTypeLoc().getSourceRange() : SourceRange());
1123}
1124
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001125static bool CheckMethodOverrideReturn(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001126 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001127 ObjCMethodDecl *MethodDecl,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001128 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001129 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001130 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001131 if (IsProtocolMethodDecl &&
1132 (MethodDecl->getObjCDeclQualifier() !=
1133 MethodImpl->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001134 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001135 S.Diag(MethodImpl->getLocation(),
1136 (IsOverridingMode ?
1137 diag::warn_conflicting_overriding_ret_type_modifiers
1138 : diag::warn_conflicting_ret_type_modifiers))
1139 << MethodImpl->getDeclName()
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001140 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
1141 S.Diag(MethodDecl->getLocation(), diag::note_previous_declaration)
1142 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
1143 }
1144 else
1145 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001146 }
1147
John McCall10302c02010-10-28 02:34:38 +00001148 if (S.Context.hasSameUnqualifiedType(MethodImpl->getResultType(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001149 MethodDecl->getResultType()))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001150 return true;
1151 if (!Warn)
1152 return false;
John McCall10302c02010-10-28 02:34:38 +00001153
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001154 unsigned DiagID =
1155 IsOverridingMode ? diag::warn_conflicting_overriding_ret_types
1156 : diag::warn_conflicting_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001157
1158 // Mismatches between ObjC pointers go into a different warning
1159 // category, and sometimes they're even completely whitelisted.
1160 if (const ObjCObjectPointerType *ImplPtrTy =
1161 MethodImpl->getResultType()->getAs<ObjCObjectPointerType>()) {
1162 if (const ObjCObjectPointerType *IfacePtrTy =
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001163 MethodDecl->getResultType()->getAs<ObjCObjectPointerType>()) {
John McCall10302c02010-10-28 02:34:38 +00001164 // Allow non-matching return types as long as they don't violate
1165 // the principle of substitutability. Specifically, we permit
1166 // return types that are subclasses of the declared return type,
1167 // or that are more-qualified versions of the declared type.
1168 if (isObjCTypeSubstitutable(S.Context, IfacePtrTy, ImplPtrTy, false))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001169 return false;
John McCall10302c02010-10-28 02:34:38 +00001170
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001171 DiagID =
1172 IsOverridingMode ? diag::warn_non_covariant_overriding_ret_types
1173 : diag::warn_non_covariant_ret_types;
John McCall10302c02010-10-28 02:34:38 +00001174 }
1175 }
1176
1177 S.Diag(MethodImpl->getLocation(), DiagID)
1178 << MethodImpl->getDeclName()
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001179 << MethodDecl->getResultType()
John McCall10302c02010-10-28 02:34:38 +00001180 << MethodImpl->getResultType()
1181 << getTypeRange(MethodImpl->getResultTypeSourceInfo());
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001182 S.Diag(MethodDecl->getLocation(),
1183 IsOverridingMode ? diag::note_previous_declaration
1184 : diag::note_previous_definition)
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001185 << getTypeRange(MethodDecl->getResultTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001186 return false;
John McCall10302c02010-10-28 02:34:38 +00001187}
1188
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001189static bool CheckMethodOverrideParam(Sema &S,
John McCall10302c02010-10-28 02:34:38 +00001190 ObjCMethodDecl *MethodImpl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001191 ObjCMethodDecl *MethodDecl,
John McCall10302c02010-10-28 02:34:38 +00001192 ParmVarDecl *ImplVar,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001193 ParmVarDecl *IfaceVar,
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001194 bool IsProtocolMethodDecl,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001195 bool IsOverridingMode,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001196 bool Warn) {
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001197 if (IsProtocolMethodDecl &&
1198 (ImplVar->getObjCDeclQualifier() !=
1199 IfaceVar->getObjCDeclQualifier())) {
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001200 if (Warn) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001201 if (IsOverridingMode)
1202 S.Diag(ImplVar->getLocation(),
1203 diag::warn_conflicting_overriding_param_modifiers)
1204 << getTypeRange(ImplVar->getTypeSourceInfo())
1205 << MethodImpl->getDeclName();
1206 else S.Diag(ImplVar->getLocation(),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001207 diag::warn_conflicting_param_modifiers)
1208 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001209 << MethodImpl->getDeclName();
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001210 S.Diag(IfaceVar->getLocation(), diag::note_previous_declaration)
1211 << getTypeRange(IfaceVar->getTypeSourceInfo());
1212 }
1213 else
1214 return false;
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001215 }
1216
John McCall10302c02010-10-28 02:34:38 +00001217 QualType ImplTy = ImplVar->getType();
1218 QualType IfaceTy = IfaceVar->getType();
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001219
John McCall10302c02010-10-28 02:34:38 +00001220 if (S.Context.hasSameUnqualifiedType(ImplTy, IfaceTy))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001221 return true;
1222
1223 if (!Warn)
1224 return false;
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001225 unsigned DiagID =
1226 IsOverridingMode ? diag::warn_conflicting_overriding_param_types
1227 : diag::warn_conflicting_param_types;
John McCall10302c02010-10-28 02:34:38 +00001228
1229 // Mismatches between ObjC pointers go into a different warning
1230 // category, and sometimes they're even completely whitelisted.
1231 if (const ObjCObjectPointerType *ImplPtrTy =
1232 ImplTy->getAs<ObjCObjectPointerType>()) {
1233 if (const ObjCObjectPointerType *IfacePtrTy =
1234 IfaceTy->getAs<ObjCObjectPointerType>()) {
1235 // Allow non-matching argument types as long as they don't
1236 // violate the principle of substitutability. Specifically, the
1237 // implementation must accept any objects that the superclass
1238 // accepts, however it may also accept others.
1239 if (isObjCTypeSubstitutable(S.Context, ImplPtrTy, IfacePtrTy, true))
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001240 return false;
John McCall10302c02010-10-28 02:34:38 +00001241
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001242 DiagID =
1243 IsOverridingMode ? diag::warn_non_contravariant_overriding_param_types
1244 : diag::warn_non_contravariant_param_types;
John McCall10302c02010-10-28 02:34:38 +00001245 }
1246 }
1247
1248 S.Diag(ImplVar->getLocation(), DiagID)
1249 << getTypeRange(ImplVar->getTypeSourceInfo())
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001250 << MethodImpl->getDeclName() << IfaceTy << ImplTy;
1251 S.Diag(IfaceVar->getLocation(),
1252 (IsOverridingMode ? diag::note_previous_declaration
1253 : diag::note_previous_definition))
John McCall10302c02010-10-28 02:34:38 +00001254 << getTypeRange(IfaceVar->getTypeSourceInfo());
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001255 return false;
John McCall10302c02010-10-28 02:34:38 +00001256}
John McCallf85e1932011-06-15 23:02:42 +00001257
1258/// In ARC, check whether the conventional meanings of the two methods
1259/// match. If they don't, it's a hard error.
1260static bool checkMethodFamilyMismatch(Sema &S, ObjCMethodDecl *impl,
1261 ObjCMethodDecl *decl) {
1262 ObjCMethodFamily implFamily = impl->getMethodFamily();
1263 ObjCMethodFamily declFamily = decl->getMethodFamily();
1264 if (implFamily == declFamily) return false;
1265
1266 // Since conventions are sorted by selector, the only possibility is
1267 // that the types differ enough to cause one selector or the other
1268 // to fall out of the family.
1269 assert(implFamily == OMF_None || declFamily == OMF_None);
1270
1271 // No further diagnostics required on invalid declarations.
1272 if (impl->isInvalidDecl() || decl->isInvalidDecl()) return true;
1273
1274 const ObjCMethodDecl *unmatched = impl;
1275 ObjCMethodFamily family = declFamily;
1276 unsigned errorID = diag::err_arc_lost_method_convention;
1277 unsigned noteID = diag::note_arc_lost_method_convention;
1278 if (declFamily == OMF_None) {
1279 unmatched = decl;
1280 family = implFamily;
1281 errorID = diag::err_arc_gained_method_convention;
1282 noteID = diag::note_arc_gained_method_convention;
1283 }
1284
1285 // Indexes into a %select clause in the diagnostic.
1286 enum FamilySelector {
1287 F_alloc, F_copy, F_mutableCopy = F_copy, F_init, F_new
1288 };
1289 FamilySelector familySelector = FamilySelector();
1290
1291 switch (family) {
1292 case OMF_None: llvm_unreachable("logic error, no method convention");
1293 case OMF_retain:
1294 case OMF_release:
1295 case OMF_autorelease:
1296 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00001297 case OMF_finalize:
John McCallf85e1932011-06-15 23:02:42 +00001298 case OMF_retainCount:
1299 case OMF_self:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00001300 case OMF_performSelector:
John McCallf85e1932011-06-15 23:02:42 +00001301 // Mismatches for these methods don't change ownership
1302 // conventions, so we don't care.
1303 return false;
1304
1305 case OMF_init: familySelector = F_init; break;
1306 case OMF_alloc: familySelector = F_alloc; break;
1307 case OMF_copy: familySelector = F_copy; break;
1308 case OMF_mutableCopy: familySelector = F_mutableCopy; break;
1309 case OMF_new: familySelector = F_new; break;
1310 }
1311
1312 enum ReasonSelector { R_NonObjectReturn, R_UnrelatedReturn };
1313 ReasonSelector reasonSelector;
1314
1315 // The only reason these methods don't fall within their families is
1316 // due to unusual result types.
1317 if (unmatched->getResultType()->isObjCObjectPointerType()) {
1318 reasonSelector = R_UnrelatedReturn;
1319 } else {
1320 reasonSelector = R_NonObjectReturn;
1321 }
1322
1323 S.Diag(impl->getLocation(), errorID) << familySelector << reasonSelector;
1324 S.Diag(decl->getLocation(), noteID) << familySelector << reasonSelector;
1325
1326 return true;
1327}
John McCall10302c02010-10-28 02:34:38 +00001328
Fariborz Jahanian8daab972008-12-05 18:18:52 +00001329void Sema::WarnConflictingTypedMethods(ObjCMethodDecl *ImpMethodDecl,
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001330 ObjCMethodDecl *MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001331 bool IsProtocolMethodDecl) {
John McCallf85e1932011-06-15 23:02:42 +00001332 if (getLangOptions().ObjCAutoRefCount &&
1333 checkMethodFamilyMismatch(*this, ImpMethodDecl, MethodDecl))
1334 return;
1335
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001336 CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001337 IsProtocolMethodDecl, false,
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001338 true);
Mike Stump1eb44332009-09-09 15:08:12 +00001339
Chris Lattner3aff9192009-04-11 19:58:42 +00001340 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
Fariborz Jahanian21761c82011-02-21 23:49:15 +00001341 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
Fariborz Jahanian21121902011-08-08 18:03:17 +00001342 IM != EM; ++IM, ++IF) {
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001343 CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl, *IM, *IF,
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001344 IsProtocolMethodDecl, false, true);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001345 }
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00001346
Fariborz Jahanian21121902011-08-08 18:03:17 +00001347 if (ImpMethodDecl->isVariadic() != MethodDecl->isVariadic()) {
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001348 Diag(ImpMethodDecl->getLocation(),
1349 diag::warn_conflicting_variadic);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001350 Diag(MethodDecl->getLocation(), diag::note_previous_declaration);
Fariborz Jahanian21121902011-08-08 18:03:17 +00001351 }
Fariborz Jahanian21121902011-08-08 18:03:17 +00001352}
1353
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00001354void Sema::CheckConflictingOverridingMethod(ObjCMethodDecl *Method,
1355 ObjCMethodDecl *Overridden,
1356 bool IsProtocolMethodDecl) {
1357
1358 CheckMethodOverrideReturn(*this, Method, Overridden,
1359 IsProtocolMethodDecl, true,
1360 true);
1361
1362 for (ObjCMethodDecl::param_iterator IM = Method->param_begin(),
1363 IF = Overridden->param_begin(), EM = Method->param_end();
1364 IM != EM; ++IM, ++IF) {
1365 CheckMethodOverrideParam(*this, Method, Overridden, *IM, *IF,
1366 IsProtocolMethodDecl, true, true);
1367 }
1368
1369 if (Method->isVariadic() != Overridden->isVariadic()) {
1370 Diag(Method->getLocation(),
1371 diag::warn_conflicting_overriding_variadic);
1372 Diag(Overridden->getLocation(), diag::note_previous_declaration);
1373 }
1374}
1375
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001376/// WarnExactTypedMethods - This routine issues a warning if method
1377/// implementation declaration matches exactly that of its declaration.
1378void Sema::WarnExactTypedMethods(ObjCMethodDecl *ImpMethodDecl,
1379 ObjCMethodDecl *MethodDecl,
1380 bool IsProtocolMethodDecl) {
1381 // don't issue warning when protocol method is optional because primary
1382 // class is not required to implement it and it is safe for protocol
1383 // to implement it.
1384 if (MethodDecl->getImplementationControl() == ObjCMethodDecl::Optional)
1385 return;
1386 // don't issue warning when primary class's method is
1387 // depecated/unavailable.
1388 if (MethodDecl->hasAttr<UnavailableAttr>() ||
1389 MethodDecl->hasAttr<DeprecatedAttr>())
1390 return;
1391
1392 bool match = CheckMethodOverrideReturn(*this, ImpMethodDecl, MethodDecl,
1393 IsProtocolMethodDecl, false, false);
1394 if (match)
1395 for (ObjCMethodDecl::param_iterator IM = ImpMethodDecl->param_begin(),
1396 IF = MethodDecl->param_begin(), EM = ImpMethodDecl->param_end();
1397 IM != EM; ++IM, ++IF) {
1398 match = CheckMethodOverrideParam(*this, ImpMethodDecl, MethodDecl,
1399 *IM, *IF,
1400 IsProtocolMethodDecl, false, false);
1401 if (!match)
1402 break;
1403 }
1404 if (match)
1405 match = (ImpMethodDecl->isVariadic() == MethodDecl->isVariadic());
David Chisnall7ca13ef2011-08-08 17:32:19 +00001406 if (match)
1407 match = !(MethodDecl->isClassMethod() &&
1408 MethodDecl->getSelector() == GetNullarySelector("load", Context));
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001409
1410 if (match) {
1411 Diag(ImpMethodDecl->getLocation(),
1412 diag::warn_category_method_impl_match);
1413 Diag(MethodDecl->getLocation(), diag::note_method_declared_at);
1414 }
1415}
1416
Mike Stump390b4cc2009-05-16 07:39:55 +00001417/// FIXME: Type hierarchies in Objective-C can be deep. We could most likely
1418/// improve the efficiency of selector lookups and type checking by associating
1419/// with each protocol / interface / category the flattened instance tables. If
1420/// we used an immutable set to keep the table then it wouldn't add significant
1421/// memory cost and it would be handy for lookups.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00001422
Steve Naroffefe7f362008-02-08 22:06:17 +00001423/// CheckProtocolMethodDefs - This routine checks unimplemented methods
Chris Lattner4d391482007-12-12 07:09:47 +00001424/// Declared in protocol, and those referenced by it.
Steve Naroffefe7f362008-02-08 22:06:17 +00001425void Sema::CheckProtocolMethodDefs(SourceLocation ImpLoc,
1426 ObjCProtocolDecl *PDecl,
Chris Lattner4d391482007-12-12 07:09:47 +00001427 bool& IncompleteImpl,
Steve Naroffefe7f362008-02-08 22:06:17 +00001428 const llvm::DenseSet<Selector> &InsMap,
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001429 const llvm::DenseSet<Selector> &ClsMap,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001430 ObjCContainerDecl *CDecl) {
1431 ObjCInterfaceDecl *IDecl;
1432 if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl))
1433 IDecl = C->getClassInterface();
1434 else
1435 IDecl = dyn_cast<ObjCInterfaceDecl>(CDecl);
1436 assert (IDecl && "CheckProtocolMethodDefs - IDecl is null");
1437
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001438 ObjCInterfaceDecl *Super = IDecl->getSuperClass();
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001439 ObjCInterfaceDecl *NSIDecl = 0;
1440 if (getLangOptions().NeXTRuntime) {
Mike Stump1eb44332009-09-09 15:08:12 +00001441 // check to see if class implements forwardInvocation method and objects
1442 // of this class are derived from 'NSProxy' so that to forward requests
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001443 // from one object to another.
Mike Stump1eb44332009-09-09 15:08:12 +00001444 // Under such conditions, which means that every method possible is
1445 // implemented in the class, we should not issue "Method definition not
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001446 // found" warnings.
1447 // FIXME: Use a general GetUnarySelector method for this.
1448 IdentifierInfo* II = &Context.Idents.get("forwardInvocation");
1449 Selector fISelector = Context.Selectors.getSelector(1, &II);
1450 if (InsMap.count(fISelector))
1451 // Is IDecl derived from 'NSProxy'? If so, no instance methods
1452 // need be implemented in the implementation.
1453 NSIDecl = IDecl->lookupInheritedClass(&Context.Idents.get("NSProxy"));
1454 }
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001456 // If a method lookup fails locally we still need to look and see if
1457 // the method was implemented by a base class or an inherited
1458 // protocol. This lookup is slow, but occurs rarely in correct code
1459 // and otherwise would terminate in a warning.
1460
Chris Lattner4d391482007-12-12 07:09:47 +00001461 // check unimplemented instance methods.
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001462 if (!NSIDecl)
Mike Stump1eb44332009-09-09 15:08:12 +00001463 for (ObjCProtocolDecl::instmeth_iterator I = PDecl->instmeth_begin(),
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001464 E = PDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001465 ObjCMethodDecl *method = *I;
Mike Stump1eb44332009-09-09 15:08:12 +00001466 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001467 !method->isSynthesized() && !InsMap.count(method->getSelector()) &&
Mike Stump1eb44332009-09-09 15:08:12 +00001468 (!Super ||
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001469 !Super->lookupInstanceMethod(method->getSelector()))) {
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001470 // Ugly, but necessary. Method declared in protcol might have
1471 // have been synthesized due to a property declared in the class which
1472 // uses the protocol.
Mike Stump1eb44332009-09-09 15:08:12 +00001473 ObjCMethodDecl *MethodInClass =
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001474 IDecl->lookupInstanceMethod(method->getSelector());
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001475 if (!MethodInClass || !MethodInClass->isSynthesized()) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001476 unsigned DIAG = diag::warn_unimplemented_protocol_method;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +00001477 if (Diags.getDiagnosticLevel(DIAG, ImpLoc)
David Blaikied6471f72011-09-25 23:23:43 +00001478 != DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001479 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001480 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001481 Diag(CDecl->getLocation(), diag::note_required_for_protocol_at)
1482 << PDecl->getDeclName();
1483 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001484 }
Fariborz Jahaniancd187622009-05-22 17:12:32 +00001485 }
1486 }
Chris Lattner4d391482007-12-12 07:09:47 +00001487 // check unimplemented class methods
Mike Stump1eb44332009-09-09 15:08:12 +00001488 for (ObjCProtocolDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001489 I = PDecl->classmeth_begin(), E = PDecl->classmeth_end();
Douglas Gregor6ab35242009-04-09 21:40:53 +00001490 I != E; ++I) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001491 ObjCMethodDecl *method = *I;
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001492 if (method->getImplementationControl() != ObjCMethodDecl::Optional &&
1493 !ClsMap.count(method->getSelector()) &&
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001494 (!Super || !Super->lookupClassMethod(method->getSelector()))) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001495 unsigned DIAG = diag::warn_unimplemented_protocol_method;
David Blaikied6471f72011-09-25 23:23:43 +00001496 if (Diags.getDiagnosticLevel(DIAG, ImpLoc) !=
1497 DiagnosticsEngine::Ignored) {
Fariborz Jahanian52146832010-03-31 18:23:33 +00001498 WarnUndefinedMethod(ImpLoc, method, IncompleteImpl, DIAG);
Fariborz Jahanian61c8d3e2010-10-29 23:20:05 +00001499 Diag(method->getLocation(), diag::note_method_declared_at);
Fariborz Jahanian52146832010-03-31 18:23:33 +00001500 Diag(IDecl->getLocation(), diag::note_required_for_protocol_at) <<
1501 PDecl->getDeclName();
1502 }
Fariborz Jahanian8822f7c2010-03-27 19:02:17 +00001503 }
Steve Naroff58dbdeb2007-12-14 23:37:57 +00001504 }
Chris Lattner780f3292008-07-21 21:32:27 +00001505 // Check on this protocols's referenced protocols, recursively.
1506 for (ObjCProtocolDecl::protocol_iterator PI = PDecl->protocol_begin(),
1507 E = PDecl->protocol_end(); PI != E; ++PI)
Daniel Dunbar7ad1b1f2008-09-04 20:01:15 +00001508 CheckProtocolMethodDefs(ImpLoc, *PI, IncompleteImpl, InsMap, ClsMap, IDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001509}
1510
Fariborz Jahanian1e159bc2011-07-16 00:08:33 +00001511/// MatchAllMethodDeclarations - Check methods declared in interface
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001512/// or protocol against those declared in their implementations.
1513///
1514void Sema::MatchAllMethodDeclarations(const llvm::DenseSet<Selector> &InsMap,
1515 const llvm::DenseSet<Selector> &ClsMap,
1516 llvm::DenseSet<Selector> &InsMapSeen,
1517 llvm::DenseSet<Selector> &ClsMapSeen,
1518 ObjCImplDecl* IMPDecl,
1519 ObjCContainerDecl* CDecl,
1520 bool &IncompleteImpl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001521 bool ImmediateClass,
1522 bool WarnExactMatch) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001523 // Check and see if instance methods in class interface have been
1524 // implemented in the implementation class. If so, their types match.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001525 for (ObjCInterfaceDecl::instmeth_iterator I = CDecl->instmeth_begin(),
1526 E = CDecl->instmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001527 if (InsMapSeen.count((*I)->getSelector()))
1528 continue;
1529 InsMapSeen.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001530 if (!(*I)->isSynthesized() &&
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001531 !InsMap.count((*I)->getSelector())) {
1532 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001533 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1534 diag::note_undef_method_impl);
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001535 continue;
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001536 } else {
Mike Stump1eb44332009-09-09 15:08:12 +00001537 ObjCMethodDecl *ImpMethodDecl =
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001538 IMPDecl->getInstanceMethod((*I)->getSelector());
1539 assert(CDecl->getInstanceMethod((*I)->getSelector()) &&
1540 "Expected to find the method through lookup as well");
1541 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001542 // ImpMethodDecl may be null as in a @dynamic property.
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001543 if (ImpMethodDecl) {
1544 if (!WarnExactMatch)
1545 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1546 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanian8c7e67d2011-08-25 22:58:42 +00001547 else if (!MethodDecl->isSynthesized())
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001548 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1549 isa<ObjCProtocolDecl>(CDecl));
1550 }
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001551 }
1552 }
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001554 // Check and see if class methods in class interface have been
1555 // implemented in the implementation class. If so, their types match.
Mike Stump1eb44332009-09-09 15:08:12 +00001556 for (ObjCInterfaceDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001557 I = CDecl->classmeth_begin(), E = CDecl->classmeth_end(); I != E; ++I) {
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001558 if (ClsMapSeen.count((*I)->getSelector()))
1559 continue;
1560 ClsMapSeen.insert((*I)->getSelector());
1561 if (!ClsMap.count((*I)->getSelector())) {
1562 if (ImmediateClass)
Fariborz Jahanian52146832010-03-31 18:23:33 +00001563 WarnUndefinedMethod(IMPDecl->getLocation(), *I, IncompleteImpl,
1564 diag::note_undef_method_impl);
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001565 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001566 ObjCMethodDecl *ImpMethodDecl =
1567 IMPDecl->getClassMethod((*I)->getSelector());
Argyrios Kyrtzidis2334f3a2011-08-30 19:43:21 +00001568 assert(CDecl->getClassMethod((*I)->getSelector()) &&
1569 "Expected to find the method through lookup as well");
1570 ObjCMethodDecl *MethodDecl = *I;
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001571 if (!WarnExactMatch)
1572 WarnConflictingTypedMethods(ImpMethodDecl, MethodDecl,
1573 isa<ObjCProtocolDecl>(CDecl));
1574 else
1575 WarnExactTypedMethods(ImpMethodDecl, MethodDecl,
1576 isa<ObjCProtocolDecl>(CDecl));
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001577 }
1578 }
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001579
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001580 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001581 // Also methods in class extensions need be looked at next.
1582 for (const ObjCCategoryDecl *ClsExtDecl = I->getFirstClassExtension();
1583 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension())
1584 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1585 IMPDecl,
1586 const_cast<ObjCCategoryDecl *>(ClsExtDecl),
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001587 IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianf54e3ae2010-10-08 22:59:25 +00001588
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001589 // Check for any implementation of a methods declared in protocol.
Ted Kremenek53b94412010-09-01 01:21:15 +00001590 for (ObjCInterfaceDecl::all_protocol_iterator
1591 PI = I->all_referenced_protocol_begin(),
1592 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001593 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1594 IMPDecl,
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001595 (*PI), IncompleteImpl, false, WarnExactMatch);
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001596
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001597 // FIXME. For now, we are not checking for extact match of methods
1598 // in category implementation and its primary class's super class.
1599 if (!WarnExactMatch && I->getSuperClass())
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001600 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
Mike Stump1eb44332009-09-09 15:08:12 +00001601 IMPDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001602 I->getSuperClass(), IncompleteImpl, false);
1603 }
1604}
1605
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001606/// CheckCategoryVsClassMethodMatches - Checks that methods implemented in
1607/// category matches with those implemented in its primary class and
1608/// warns each time an exact match is found.
1609void Sema::CheckCategoryVsClassMethodMatches(
1610 ObjCCategoryImplDecl *CatIMPDecl) {
1611 llvm::DenseSet<Selector> InsMap, ClsMap;
1612
1613 for (ObjCImplementationDecl::instmeth_iterator
1614 I = CatIMPDecl->instmeth_begin(),
1615 E = CatIMPDecl->instmeth_end(); I!=E; ++I)
1616 InsMap.insert((*I)->getSelector());
1617
1618 for (ObjCImplementationDecl::classmeth_iterator
1619 I = CatIMPDecl->classmeth_begin(),
1620 E = CatIMPDecl->classmeth_end(); I != E; ++I)
1621 ClsMap.insert((*I)->getSelector());
1622 if (InsMap.empty() && ClsMap.empty())
1623 return;
1624
1625 // Get category's primary class.
1626 ObjCCategoryDecl *CatDecl = CatIMPDecl->getCategoryDecl();
1627 if (!CatDecl)
1628 return;
1629 ObjCInterfaceDecl *IDecl = CatDecl->getClassInterface();
1630 if (!IDecl)
1631 return;
1632 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
1633 bool IncompleteImpl = false;
1634 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1635 CatIMPDecl, IDecl,
1636 IncompleteImpl, false, true /*WarnExactMatch*/);
1637}
Fariborz Jahanianeee3ef12011-07-24 20:53:26 +00001638
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001639void Sema::ImplMethodsVsClassMethods(Scope *S, ObjCImplDecl* IMPDecl,
Mike Stump1eb44332009-09-09 15:08:12 +00001640 ObjCContainerDecl* CDecl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001641 bool IncompleteImpl) {
Chris Lattner4d391482007-12-12 07:09:47 +00001642 llvm::DenseSet<Selector> InsMap;
1643 // Check and see if instance methods in class interface have been
1644 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001645 for (ObjCImplementationDecl::instmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001646 I = IMPDecl->instmeth_begin(), E = IMPDecl->instmeth_end(); I!=E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001647 InsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001648
Fariborz Jahanian12bac252009-04-14 23:15:21 +00001649 // Check and see if properties declared in the interface have either 1)
1650 // an implementation or 2) there is a @synthesize/@dynamic implementation
1651 // of the property in the @implementation.
Ted Kremenekc32647d2010-12-23 21:35:43 +00001652 if (isa<ObjCInterfaceDecl>(CDecl) &&
1653 !(LangOpts.ObjCDefaultSynthProperties && LangOpts.ObjCNonFragileABI2))
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001654 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ac1eda2010-01-20 01:51:55 +00001655
Chris Lattner4d391482007-12-12 07:09:47 +00001656 llvm::DenseSet<Selector> ClsMap;
Mike Stump1eb44332009-09-09 15:08:12 +00001657 for (ObjCImplementationDecl::classmeth_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001658 I = IMPDecl->classmeth_begin(),
1659 E = IMPDecl->classmeth_end(); I != E; ++I)
Chris Lattner4c525092007-12-12 17:58:05 +00001660 ClsMap.insert((*I)->getSelector());
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001662 // Check for type conflict of methods declared in a class/protocol and
1663 // its implementation; if any.
1664 llvm::DenseSet<Selector> InsMapSeen, ClsMapSeen;
Mike Stump1eb44332009-09-09 15:08:12 +00001665 MatchAllMethodDeclarations(InsMap, ClsMap, InsMapSeen, ClsMapSeen,
1666 IMPDecl, CDecl,
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001667 IncompleteImpl, true);
Fariborz Jahanian74133072011-08-03 18:21:12 +00001668
Fariborz Jahanianfefe91e2011-07-28 23:19:50 +00001669 // check all methods implemented in category against those declared
1670 // in its primary class.
1671 if (ObjCCategoryImplDecl *CatDecl =
1672 dyn_cast<ObjCCategoryImplDecl>(IMPDecl))
1673 CheckCategoryVsClassMethodMatches(CatDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00001674
Chris Lattner4d391482007-12-12 07:09:47 +00001675 // Check the protocol list for unimplemented methods in the @implementation
1676 // class.
Fariborz Jahanianb33f3ad2009-05-01 20:07:12 +00001677 // Check and see if class methods in class interface have been
1678 // implemented in the implementation class.
Mike Stump1eb44332009-09-09 15:08:12 +00001679
Chris Lattnercddc8882009-03-01 00:56:52 +00001680 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl> (CDecl)) {
Ted Kremenek53b94412010-09-01 01:21:15 +00001681 for (ObjCInterfaceDecl::all_protocol_iterator
1682 PI = I->all_referenced_protocol_begin(),
1683 E = I->all_referenced_protocol_end(); PI != E; ++PI)
Mike Stump1eb44332009-09-09 15:08:12 +00001684 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Chris Lattnercddc8882009-03-01 00:56:52 +00001685 InsMap, ClsMap, I);
1686 // Check class extensions (unnamed categories)
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +00001687 for (const ObjCCategoryDecl *Categories = I->getFirstClassExtension();
1688 Categories; Categories = Categories->getNextClassExtension())
1689 ImplMethodsVsClassMethods(S, IMPDecl,
1690 const_cast<ObjCCategoryDecl*>(Categories),
1691 IncompleteImpl);
Chris Lattnercddc8882009-03-01 00:56:52 +00001692 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(CDecl)) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001693 // For extended class, unimplemented methods in its protocols will
1694 // be reported in the primary class.
Fariborz Jahanian25760612010-02-15 21:55:26 +00001695 if (!C->IsClassExtension()) {
Fariborz Jahanianb106fc62009-10-05 21:32:49 +00001696 for (ObjCCategoryDecl::protocol_iterator PI = C->protocol_begin(),
1697 E = C->protocol_end(); PI != E; ++PI)
1698 CheckProtocolMethodDefs(IMPDecl->getLocation(), *PI, IncompleteImpl,
Fariborz Jahanianf2838592010-03-27 21:10:05 +00001699 InsMap, ClsMap, CDecl);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001700 // Report unimplemented properties in the category as well.
1701 // When reporting on missing setter/getters, do not report when
1702 // setter/getter is implemented in category's primary class
1703 // implementation.
1704 if (ObjCInterfaceDecl *ID = C->getClassInterface())
1705 if (ObjCImplDecl *IMP = ID->getImplementation()) {
1706 for (ObjCImplementationDecl::instmeth_iterator
1707 I = IMP->instmeth_begin(), E = IMP->instmeth_end(); I!=E; ++I)
1708 InsMap.insert((*I)->getSelector());
1709 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00001710 DiagnoseUnimplementedProperties(S, IMPDecl, CDecl, InsMap);
Fariborz Jahanian3ad230e2010-01-20 19:36:21 +00001711 }
Chris Lattnercddc8882009-03-01 00:56:52 +00001712 } else
David Blaikieb219cfc2011-09-23 05:06:16 +00001713 llvm_unreachable("invalid ObjCContainerDecl type.");
Chris Lattner4d391482007-12-12 07:09:47 +00001714}
1715
Mike Stump1eb44332009-09-09 15:08:12 +00001716/// ActOnForwardClassDeclaration -
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001717Sema::DeclGroupPtrTy
Chris Lattner4d391482007-12-12 07:09:47 +00001718Sema::ActOnForwardClassDeclaration(SourceLocation AtClassLoc,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001719 IdentifierInfo **IdentList,
Ted Kremenekc09cba62009-11-17 23:12:20 +00001720 SourceLocation *IdentLocs,
Chris Lattnerbdbde4d2009-02-16 19:25:52 +00001721 unsigned NumElts) {
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001722 SmallVector<Decl *, 8> DeclsInGroup;
Chris Lattner4d391482007-12-12 07:09:47 +00001723 for (unsigned i = 0; i != NumElts; ++i) {
1724 // Check for another declaration kind with the same name.
John McCallf36e02d2009-10-09 21:13:30 +00001725 NamedDecl *PrevDecl
Douglas Gregorc83c6872010-04-15 22:33:43 +00001726 = LookupSingleName(TUScope, IdentList[i], IdentLocs[i],
Douglas Gregorc0b39642010-04-15 23:40:53 +00001727 LookupOrdinaryName, ForRedeclaration);
Douglas Gregorf57172b2008-12-08 18:40:42 +00001728 if (PrevDecl && PrevDecl->isTemplateParameter()) {
Douglas Gregor72c3f312008-12-05 18:15:24 +00001729 // Maybe we will complain about the shadowed template parameter.
1730 DiagnoseTemplateParameterShadow(AtClassLoc, PrevDecl);
1731 // Just pretend that we didn't see the previous declaration.
1732 PrevDecl = 0;
1733 }
1734
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001735 if (PrevDecl && !isa<ObjCInterfaceDecl>(PrevDecl)) {
Steve Naroffc7333882008-06-05 22:57:10 +00001736 // GCC apparently allows the following idiom:
1737 //
1738 // typedef NSObject < XCElementTogglerP > XCElementToggler;
1739 // @class XCElementToggler;
1740 //
Mike Stump1eb44332009-09-09 15:08:12 +00001741 // FIXME: Make an extension?
Richard Smith162e1c12011-04-15 14:24:37 +00001742 TypedefNameDecl *TDD = dyn_cast<TypedefNameDecl>(PrevDecl);
John McCallc12c5bb2010-05-15 11:32:37 +00001743 if (!TDD || !TDD->getUnderlyingType()->isObjCObjectType()) {
Chris Lattner3c73c412008-11-19 08:23:25 +00001744 Diag(AtClassLoc, diag::err_redefinition_different_kind) << IdentList[i];
Chris Lattner5f4a6822008-11-23 23:12:31 +00001745 Diag(PrevDecl->getLocation(), diag::note_previous_definition);
John McCallc12c5bb2010-05-15 11:32:37 +00001746 } else {
Mike Stumpac5fc7c2009-08-04 21:02:39 +00001747 // a forward class declaration matching a typedef name of a class refers
1748 // to the underlying class.
John McCallc12c5bb2010-05-15 11:32:37 +00001749 if (const ObjCObjectType *OI =
1750 TDD->getUnderlyingType()->getAs<ObjCObjectType>())
1751 PrevDecl = OI->getInterface();
Fariborz Jahaniancae27c52009-05-07 21:49:26 +00001752 }
Chris Lattner4d391482007-12-12 07:09:47 +00001753 }
Douglas Gregordeacbdc2010-08-11 12:19:30 +00001754 ObjCInterfaceDecl *IDecl = dyn_cast_or_null<ObjCInterfaceDecl>(PrevDecl);
1755 if (!IDecl) { // Not already seen? Make a forward decl.
1756 IDecl = ObjCInterfaceDecl::Create(Context, CurContext, AtClassLoc,
1757 IdentList[i], IdentLocs[i], true);
1758
1759 // Push the ObjCInterfaceDecl on the scope chain but do *not* add it to
1760 // the current DeclContext. This prevents clients that walk DeclContext
1761 // from seeing the imaginary ObjCInterfaceDecl until it is actually
1762 // declared later (if at all). We also take care to explicitly make
1763 // sure this declaration is visible for name lookup.
1764 PushOnScopeChains(IDecl, TUScope, false);
1765 CurContext->makeDeclVisibleInContext(IDecl, true);
1766 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001767 ObjCClassDecl *CDecl = ObjCClassDecl::Create(Context, CurContext, AtClassLoc,
1768 IDecl, IdentLocs[i]);
1769 CurContext->addDecl(CDecl);
1770 CheckObjCDeclScope(CDecl);
1771 DeclsInGroup.push_back(CDecl);
Chris Lattner4d391482007-12-12 07:09:47 +00001772 }
Fariborz Jahanian95ed7782011-08-27 20:50:59 +00001773
1774 return BuildDeclaratorGroup(DeclsInGroup.data(), DeclsInGroup.size(), false);
Chris Lattner4d391482007-12-12 07:09:47 +00001775}
1776
John McCall0f4c4c42011-06-16 01:15:19 +00001777static bool tryMatchRecordTypes(ASTContext &Context,
1778 Sema::MethodMatchStrategy strategy,
1779 const Type *left, const Type *right);
1780
John McCallf85e1932011-06-15 23:02:42 +00001781static bool matchTypes(ASTContext &Context, Sema::MethodMatchStrategy strategy,
1782 QualType leftQT, QualType rightQT) {
1783 const Type *left =
1784 Context.getCanonicalType(leftQT).getUnqualifiedType().getTypePtr();
1785 const Type *right =
1786 Context.getCanonicalType(rightQT).getUnqualifiedType().getTypePtr();
1787
1788 if (left == right) return true;
1789
1790 // If we're doing a strict match, the types have to match exactly.
1791 if (strategy == Sema::MMS_strict) return false;
1792
1793 if (left->isIncompleteType() || right->isIncompleteType()) return false;
1794
1795 // Otherwise, use this absurdly complicated algorithm to try to
1796 // validate the basic, low-level compatibility of the two types.
1797
1798 // As a minimum, require the sizes and alignments to match.
1799 if (Context.getTypeInfo(left) != Context.getTypeInfo(right))
1800 return false;
1801
1802 // Consider all the kinds of non-dependent canonical types:
1803 // - functions and arrays aren't possible as return and parameter types
1804
1805 // - vector types of equal size can be arbitrarily mixed
1806 if (isa<VectorType>(left)) return isa<VectorType>(right);
1807 if (isa<VectorType>(right)) return false;
1808
1809 // - references should only match references of identical type
John McCall0f4c4c42011-06-16 01:15:19 +00001810 // - structs, unions, and Objective-C objects must match more-or-less
1811 // exactly
John McCallf85e1932011-06-15 23:02:42 +00001812 // - everything else should be a scalar
1813 if (!left->isScalarType() || !right->isScalarType())
John McCall0f4c4c42011-06-16 01:15:19 +00001814 return tryMatchRecordTypes(Context, strategy, left, right);
John McCallf85e1932011-06-15 23:02:42 +00001815
John McCall1d9b3b22011-09-09 05:25:32 +00001816 // Make scalars agree in kind, except count bools as chars, and group
1817 // all non-member pointers together.
John McCallf85e1932011-06-15 23:02:42 +00001818 Type::ScalarTypeKind leftSK = left->getScalarTypeKind();
1819 Type::ScalarTypeKind rightSK = right->getScalarTypeKind();
1820 if (leftSK == Type::STK_Bool) leftSK = Type::STK_Integral;
1821 if (rightSK == Type::STK_Bool) rightSK = Type::STK_Integral;
John McCall1d9b3b22011-09-09 05:25:32 +00001822 if (leftSK == Type::STK_CPointer || leftSK == Type::STK_BlockPointer)
1823 leftSK = Type::STK_ObjCObjectPointer;
1824 if (rightSK == Type::STK_CPointer || rightSK == Type::STK_BlockPointer)
1825 rightSK = Type::STK_ObjCObjectPointer;
John McCallf85e1932011-06-15 23:02:42 +00001826
1827 // Note that data member pointers and function member pointers don't
1828 // intermix because of the size differences.
1829
1830 return (leftSK == rightSK);
1831}
Chris Lattner4d391482007-12-12 07:09:47 +00001832
John McCall0f4c4c42011-06-16 01:15:19 +00001833static bool tryMatchRecordTypes(ASTContext &Context,
1834 Sema::MethodMatchStrategy strategy,
1835 const Type *lt, const Type *rt) {
1836 assert(lt && rt && lt != rt);
1837
1838 if (!isa<RecordType>(lt) || !isa<RecordType>(rt)) return false;
1839 RecordDecl *left = cast<RecordType>(lt)->getDecl();
1840 RecordDecl *right = cast<RecordType>(rt)->getDecl();
1841
1842 // Require union-hood to match.
1843 if (left->isUnion() != right->isUnion()) return false;
1844
1845 // Require an exact match if either is non-POD.
1846 if ((isa<CXXRecordDecl>(left) && !cast<CXXRecordDecl>(left)->isPOD()) ||
1847 (isa<CXXRecordDecl>(right) && !cast<CXXRecordDecl>(right)->isPOD()))
1848 return false;
1849
1850 // Require size and alignment to match.
1851 if (Context.getTypeInfo(lt) != Context.getTypeInfo(rt)) return false;
1852
1853 // Require fields to match.
1854 RecordDecl::field_iterator li = left->field_begin(), le = left->field_end();
1855 RecordDecl::field_iterator ri = right->field_begin(), re = right->field_end();
1856 for (; li != le && ri != re; ++li, ++ri) {
1857 if (!matchTypes(Context, strategy, li->getType(), ri->getType()))
1858 return false;
1859 }
1860 return (li == le && ri == re);
1861}
1862
Chris Lattner4d391482007-12-12 07:09:47 +00001863/// MatchTwoMethodDeclarations - Checks that two methods have matching type and
1864/// returns true, or false, accordingly.
1865/// TODO: Handle protocol list; such as id<p1,p2> in type comparisons
John McCallf85e1932011-06-15 23:02:42 +00001866bool Sema::MatchTwoMethodDeclarations(const ObjCMethodDecl *left,
1867 const ObjCMethodDecl *right,
1868 MethodMatchStrategy strategy) {
1869 if (!matchTypes(Context, strategy,
1870 left->getResultType(), right->getResultType()))
1871 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001872
John McCallf85e1932011-06-15 23:02:42 +00001873 if (getLangOptions().ObjCAutoRefCount &&
1874 (left->hasAttr<NSReturnsRetainedAttr>()
1875 != right->hasAttr<NSReturnsRetainedAttr>() ||
1876 left->hasAttr<NSConsumesSelfAttr>()
1877 != right->hasAttr<NSConsumesSelfAttr>()))
1878 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001879
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001880 ObjCMethodDecl::param_const_iterator
John McCallf85e1932011-06-15 23:02:42 +00001881 li = left->param_begin(), le = left->param_end(), ri = right->param_begin();
Mike Stump1eb44332009-09-09 15:08:12 +00001882
John McCallf85e1932011-06-15 23:02:42 +00001883 for (; li != le; ++li, ++ri) {
1884 assert(ri != right->param_end() && "Param mismatch");
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00001885 const ParmVarDecl *lparm = *li, *rparm = *ri;
John McCallf85e1932011-06-15 23:02:42 +00001886
1887 if (!matchTypes(Context, strategy, lparm->getType(), rparm->getType()))
1888 return false;
1889
1890 if (getLangOptions().ObjCAutoRefCount &&
1891 lparm->hasAttr<NSConsumedAttr>() != rparm->hasAttr<NSConsumedAttr>())
1892 return false;
Chris Lattner4d391482007-12-12 07:09:47 +00001893 }
1894 return true;
1895}
1896
Sebastian Redldb9d2142010-08-02 23:18:59 +00001897/// \brief Read the contents of the method pool for a given selector from
1898/// external storage.
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001899///
Sebastian Redldb9d2142010-08-02 23:18:59 +00001900/// This routine should only be called once, when the method pool has no entry
1901/// for this selector.
1902Sema::GlobalMethodPool::iterator Sema::ReadMethodPool(Selector Sel) {
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001903 assert(ExternalSource && "We need an external AST source");
Sebastian Redldb9d2142010-08-02 23:18:59 +00001904 assert(MethodPool.find(Sel) == MethodPool.end() &&
1905 "Selector data already loaded into the method pool");
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001906
1907 // Read the method list from the external source.
Sebastian Redldb9d2142010-08-02 23:18:59 +00001908 GlobalMethods Methods = ExternalSource->ReadMethodPool(Sel);
Mike Stump1eb44332009-09-09 15:08:12 +00001909
Sebastian Redldb9d2142010-08-02 23:18:59 +00001910 return MethodPool.insert(std::make_pair(Sel, Methods)).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001911}
1912
Sebastian Redldb9d2142010-08-02 23:18:59 +00001913void Sema::AddMethodToGlobalPool(ObjCMethodDecl *Method, bool impl,
1914 bool instance) {
1915 GlobalMethodPool::iterator Pos = MethodPool.find(Method->getSelector());
1916 if (Pos == MethodPool.end()) {
1917 if (ExternalSource)
1918 Pos = ReadMethodPool(Method->getSelector());
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001919 else
Sebastian Redldb9d2142010-08-02 23:18:59 +00001920 Pos = MethodPool.insert(std::make_pair(Method->getSelector(),
1921 GlobalMethods())).first;
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001922 }
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001923 Method->setDefined(impl);
Sebastian Redldb9d2142010-08-02 23:18:59 +00001924 ObjCMethodList &Entry = instance ? Pos->second.first : Pos->second.second;
Chris Lattnerb25df352009-03-04 05:16:45 +00001925 if (Entry.Method == 0) {
Chris Lattner4d391482007-12-12 07:09:47 +00001926 // Haven't seen a method with this selector name yet - add it.
Chris Lattnerb25df352009-03-04 05:16:45 +00001927 Entry.Method = Method;
1928 Entry.Next = 0;
1929 return;
Chris Lattner4d391482007-12-12 07:09:47 +00001930 }
Mike Stump1eb44332009-09-09 15:08:12 +00001931
Chris Lattnerb25df352009-03-04 05:16:45 +00001932 // We've seen a method with this name, see if we have already seen this type
1933 // signature.
John McCallf85e1932011-06-15 23:02:42 +00001934 for (ObjCMethodList *List = &Entry; List; List = List->Next) {
1935 bool match = MatchTwoMethodDeclarations(Method, List->Method);
1936
1937 if (match) {
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001938 ObjCMethodDecl *PrevObjCMethod = List->Method;
1939 PrevObjCMethod->setDefined(impl);
1940 // If a method is deprecated, push it in the global pool.
1941 // This is used for better diagnostics.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001942 if (Method->isDeprecated()) {
1943 if (!PrevObjCMethod->isDeprecated())
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001944 List->Method = Method;
1945 }
1946 // If new method is unavailable, push it into global pool
1947 // unless previous one is deprecated.
Douglas Gregor0a0d2b12011-03-23 00:50:03 +00001948 if (Method->isUnavailable()) {
1949 if (PrevObjCMethod->getAvailability() < AR_Deprecated)
Fariborz Jahanian8e5fc9b2010-12-21 00:44:01 +00001950 List->Method = Method;
1951 }
Chris Lattnerb25df352009-03-04 05:16:45 +00001952 return;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00001953 }
John McCallf85e1932011-06-15 23:02:42 +00001954 }
Mike Stump1eb44332009-09-09 15:08:12 +00001955
Chris Lattnerb25df352009-03-04 05:16:45 +00001956 // We have a new signature for an existing method - add it.
1957 // This is extremely rare. Only 1% of Cocoa selectors are "overloaded".
Ted Kremenek298ed872010-02-11 00:53:01 +00001958 ObjCMethodList *Mem = BumpAlloc.Allocate<ObjCMethodList>();
1959 Entry.Next = new (Mem) ObjCMethodList(Method, Entry.Next);
Chris Lattner4d391482007-12-12 07:09:47 +00001960}
1961
John McCallf85e1932011-06-15 23:02:42 +00001962/// Determines if this is an "acceptable" loose mismatch in the global
1963/// method pool. This exists mostly as a hack to get around certain
1964/// global mismatches which we can't afford to make warnings / errors.
1965/// Really, what we want is a way to take a method out of the global
1966/// method pool.
1967static bool isAcceptableMethodMismatch(ObjCMethodDecl *chosen,
1968 ObjCMethodDecl *other) {
1969 if (!chosen->isInstanceMethod())
1970 return false;
1971
1972 Selector sel = chosen->getSelector();
1973 if (!sel.isUnarySelector() || sel.getNameForSlot(0) != "length")
1974 return false;
1975
1976 // Don't complain about mismatches for -length if the method we
1977 // chose has an integral result type.
1978 return (chosen->getResultType()->isIntegerType());
1979}
1980
Sebastian Redldb9d2142010-08-02 23:18:59 +00001981ObjCMethodDecl *Sema::LookupMethodInGlobalPool(Selector Sel, SourceRange R,
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00001982 bool receiverIdOrClass,
Sebastian Redldb9d2142010-08-02 23:18:59 +00001983 bool warn, bool instance) {
1984 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
1985 if (Pos == MethodPool.end()) {
1986 if (ExternalSource)
1987 Pos = ReadMethodPool(Sel);
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00001988 else
1989 return 0;
1990 }
1991
Sebastian Redldb9d2142010-08-02 23:18:59 +00001992 ObjCMethodList &MethList = instance ? Pos->second.first : Pos->second.second;
Mike Stump1eb44332009-09-09 15:08:12 +00001993
Sebastian Redldb9d2142010-08-02 23:18:59 +00001994 if (warn && MethList.Method && MethList.Next) {
John McCallf85e1932011-06-15 23:02:42 +00001995 bool issueDiagnostic = false, issueError = false;
1996
1997 // We support a warning which complains about *any* difference in
1998 // method signature.
1999 bool strictSelectorMatch =
2000 (receiverIdOrClass && warn &&
2001 (Diags.getDiagnosticLevel(diag::warn_strict_multiple_method_decl,
2002 R.getBegin()) !=
David Blaikied6471f72011-09-25 23:23:43 +00002003 DiagnosticsEngine::Ignored));
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002004 if (strictSelectorMatch)
2005 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002006 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2007 MMS_strict)) {
2008 issueDiagnostic = true;
2009 break;
2010 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002011 }
2012
John McCallf85e1932011-06-15 23:02:42 +00002013 // If we didn't see any strict differences, we won't see any loose
2014 // differences. In ARC, however, we also need to check for loose
2015 // mismatches, because most of them are errors.
2016 if (!strictSelectorMatch ||
2017 (issueDiagnostic && getLangOptions().ObjCAutoRefCount))
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002018 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next) {
John McCallf85e1932011-06-15 23:02:42 +00002019 // This checks if the methods differ in type mismatch.
2020 if (!MatchTwoMethodDeclarations(MethList.Method, Next->Method,
2021 MMS_loose) &&
2022 !isAcceptableMethodMismatch(MethList.Method, Next->Method)) {
2023 issueDiagnostic = true;
2024 if (getLangOptions().ObjCAutoRefCount)
2025 issueError = true;
2026 break;
2027 }
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002028 }
2029
John McCallf85e1932011-06-15 23:02:42 +00002030 if (issueDiagnostic) {
2031 if (issueError)
2032 Diag(R.getBegin(), diag::err_arc_multiple_method_decl) << Sel << R;
2033 else if (strictSelectorMatch)
Fariborz Jahanian6b308f62010-08-09 23:27:58 +00002034 Diag(R.getBegin(), diag::warn_strict_multiple_method_decl) << Sel << R;
2035 else
2036 Diag(R.getBegin(), diag::warn_multiple_method_decl) << Sel << R;
John McCallf85e1932011-06-15 23:02:42 +00002037
2038 Diag(MethList.Method->getLocStart(),
2039 issueError ? diag::note_possibility : diag::note_using)
Sebastian Redldb9d2142010-08-02 23:18:59 +00002040 << MethList.Method->getSourceRange();
2041 for (ObjCMethodList *Next = MethList.Next; Next; Next = Next->Next)
2042 Diag(Next->Method->getLocStart(), diag::note_also_found)
2043 << Next->Method->getSourceRange();
2044 }
Douglas Gregorf0aaf7a2009-04-24 21:10:55 +00002045 }
2046 return MethList.Method;
2047}
2048
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002049ObjCMethodDecl *Sema::LookupImplementedMethodInGlobalPool(Selector Sel) {
Sebastian Redldb9d2142010-08-02 23:18:59 +00002050 GlobalMethodPool::iterator Pos = MethodPool.find(Sel);
2051 if (Pos == MethodPool.end())
2052 return 0;
2053
2054 GlobalMethods &Methods = Pos->second;
2055
2056 if (Methods.first.Method && Methods.first.Method->isDefined())
2057 return Methods.first.Method;
2058 if (Methods.second.Method && Methods.second.Method->isDefined())
2059 return Methods.second.Method;
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002060 return 0;
2061}
2062
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002063/// CompareMethodParamsInBaseAndSuper - This routine compares methods with
2064/// identical selector names in current and its super classes and issues
2065/// a warning if any of their argument types are incompatible.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002066void Sema::CompareMethodParamsInBaseAndSuper(Decl *ClassDecl,
2067 ObjCMethodDecl *Method,
2068 bool IsInstance) {
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002069 ObjCInterfaceDecl *ID = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2070 if (ID == 0) return;
Mike Stump1eb44332009-09-09 15:08:12 +00002071
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002072 while (ObjCInterfaceDecl *SD = ID->getSuperClass()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002073 ObjCMethodDecl *SuperMethodDecl =
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002074 SD->lookupMethod(Method->getSelector(), IsInstance);
2075 if (SuperMethodDecl == 0) {
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002076 ID = SD;
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002077 continue;
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002078 }
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002079 ObjCMethodDecl::param_iterator ParamI = Method->param_begin(),
2080 E = Method->param_end();
2081 ObjCMethodDecl::param_iterator PrevI = SuperMethodDecl->param_begin();
2082 for (; ParamI != E; ++ParamI, ++PrevI) {
2083 // Number of parameters are the same and is guaranteed by selector match.
2084 assert(PrevI != SuperMethodDecl->param_end() && "Param mismatch");
2085 QualType T1 = Context.getCanonicalType((*ParamI)->getType());
2086 QualType T2 = Context.getCanonicalType((*PrevI)->getType());
Chris Lattnerfc8f0e12011-04-15 05:22:18 +00002087 // If type of argument of method in this class does not match its
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002088 // respective argument type in the super class method, issue warning;
2089 if (!Context.typesAreCompatible(T1, T2)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002090 Diag((*ParamI)->getLocation(), diag::ext_typecheck_base_super)
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002091 << T1 << T2;
2092 Diag(SuperMethodDecl->getLocation(), diag::note_previous_declaration);
2093 return;
2094 }
2095 }
2096 ID = SD;
2097 }
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002098}
2099
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002100/// DiagnoseDuplicateIvars -
2101/// Check for duplicate ivars in the entire class at the start of
2102/// @implementation. This becomes necesssary because class extension can
2103/// add ivars to a class in random order which will not be known until
2104/// class's @implementation is seen.
2105void Sema::DiagnoseDuplicateIvars(ObjCInterfaceDecl *ID,
2106 ObjCInterfaceDecl *SID) {
2107 for (ObjCInterfaceDecl::ivar_iterator IVI = ID->ivar_begin(),
2108 IVE = ID->ivar_end(); IVI != IVE; ++IVI) {
2109 ObjCIvarDecl* Ivar = (*IVI);
2110 if (Ivar->isInvalidDecl())
2111 continue;
2112 if (IdentifierInfo *II = Ivar->getIdentifier()) {
2113 ObjCIvarDecl* prevIvar = SID->lookupInstanceVariable(II);
2114 if (prevIvar) {
2115 Diag(Ivar->getLocation(), diag::err_duplicate_member) << II;
2116 Diag(prevIvar->getLocation(), diag::note_previous_declaration);
2117 Ivar->setInvalidDecl();
2118 }
2119 }
2120 }
2121}
2122
Steve Naroffa56f6162007-12-18 01:30:32 +00002123// Note: For class/category implemenations, allMethods/allProperties is
2124// always null.
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002125void Sema::ActOnAtEnd(Scope *S, SourceRange AtEnd,
John McCalld226f652010-08-21 09:40:31 +00002126 Decl **allMethods, unsigned allNum,
2127 Decl **allProperties, unsigned pNum,
Chris Lattner682bf922009-03-29 16:50:03 +00002128 DeclGroupPtrTy *allTUVars, unsigned tuvNum) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002129
2130 if (!CurContext->isObjCContainer())
Chris Lattner4d391482007-12-12 07:09:47 +00002131 return;
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002132 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2133 Decl *ClassDecl = cast<Decl>(OCD);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002134
Mike Stump1eb44332009-09-09 15:08:12 +00002135 bool isInterfaceDeclKind =
Chris Lattnerf8d17a52008-03-16 21:17:37 +00002136 isa<ObjCInterfaceDecl>(ClassDecl) || isa<ObjCCategoryDecl>(ClassDecl)
2137 || isa<ObjCProtocolDecl>(ClassDecl);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002138 bool checkIdenticalMethods = isa<ObjCImplementationDecl>(ClassDecl);
Steve Naroff09c47192009-01-09 15:36:25 +00002139
Ted Kremenek782f2f52010-01-07 01:20:12 +00002140 if (!isInterfaceDeclKind && AtEnd.isInvalid()) {
2141 // FIXME: This is wrong. We shouldn't be pretending that there is
2142 // an '@end' in the declaration.
2143 SourceLocation L = ClassDecl->getLocation();
2144 AtEnd.setBegin(L);
2145 AtEnd.setEnd(L);
Fariborz Jahanian64089ce2011-04-22 22:02:28 +00002146 Diag(L, diag::err_missing_atend);
Fariborz Jahanian63e963c2009-11-16 18:57:01 +00002147 }
2148
Steve Naroff0701bbb2009-01-08 17:28:14 +00002149 // FIXME: Remove these and use the ObjCContainerDecl/DeclContext.
2150 llvm::DenseMap<Selector, const ObjCMethodDecl*> InsMap;
2151 llvm::DenseMap<Selector, const ObjCMethodDecl*> ClsMap;
2152
Chris Lattner4d391482007-12-12 07:09:47 +00002153 for (unsigned i = 0; i < allNum; i++ ) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002154 ObjCMethodDecl *Method =
John McCalld226f652010-08-21 09:40:31 +00002155 cast_or_null<ObjCMethodDecl>(allMethods[i]);
Chris Lattner4d391482007-12-12 07:09:47 +00002156
2157 if (!Method) continue; // Already issued a diagnostic.
Douglas Gregorf8d49f62009-01-09 17:18:27 +00002158 if (Method->isInstanceMethod()) {
Chris Lattner4d391482007-12-12 07:09:47 +00002159 /// Check for instance method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002160 const ObjCMethodDecl *&PrevMethod = InsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002161 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002162 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002163 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002164 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002165 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002166 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002167 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002168 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002169 } else {
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +00002170 if (PrevMethod)
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002171 Method->setAsRedeclaration(PrevMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002172 InsMap[Method->getSelector()] = Method;
2173 /// The following allows us to typecheck messages to "id".
2174 AddInstanceMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002175 // verify that the instance method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002176 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002177 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, true);
Chris Lattner4d391482007-12-12 07:09:47 +00002178 }
Mike Stumpac5fc7c2009-08-04 21:02:39 +00002179 } else {
Chris Lattner4d391482007-12-12 07:09:47 +00002180 /// Check for class method of the same name with incompatible types
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002181 const ObjCMethodDecl *&PrevMethod = ClsMap[Method->getSelector()];
Mike Stump1eb44332009-09-09 15:08:12 +00002182 bool match = PrevMethod ? MatchTwoMethodDeclarations(Method, PrevMethod)
Chris Lattner4d391482007-12-12 07:09:47 +00002183 : false;
Mike Stump1eb44332009-09-09 15:08:12 +00002184 if ((isInterfaceDeclKind && PrevMethod && !match)
Eli Friedman82b4e762008-12-16 20:15:50 +00002185 || (checkIdenticalMethods && match)) {
Chris Lattner5f4a6822008-11-23 23:12:31 +00002186 Diag(Method->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002187 << Method->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002188 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002189 Method->setInvalidDecl();
Chris Lattner4d391482007-12-12 07:09:47 +00002190 } else {
Argyrios Kyrtzidisb40034c2011-10-14 06:48:06 +00002191 if (PrevMethod)
Argyrios Kyrtzidis3a919e72011-10-14 08:02:31 +00002192 Method->setAsRedeclaration(PrevMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002193 ClsMap[Method->getSelector()] = Method;
Steve Naroffa56f6162007-12-18 01:30:32 +00002194 /// The following allows us to typecheck messages to "Class".
2195 AddFactoryMethodToGlobalPool(Method);
Mike Stump1eb44332009-09-09 15:08:12 +00002196 // verify that the class method conforms to the same definition of
Fariborz Jahaniane198f5d2009-08-04 17:01:09 +00002197 // parent methods if it shadows one.
Fariborz Jahaniandbdec8b2009-08-04 01:07:16 +00002198 CompareMethodParamsInBaseAndSuper(ClassDecl, Method, false);
Chris Lattner4d391482007-12-12 07:09:47 +00002199 }
2200 }
2201 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002202 if (ObjCInterfaceDecl *I = dyn_cast<ObjCInterfaceDecl>(ClassDecl)) {
Mike Stump1eb44332009-09-09 15:08:12 +00002203 // Compares properties declared in this class to those of its
Fariborz Jahanian02edb982008-05-01 00:03:38 +00002204 // super class.
Fariborz Jahanianaebf0cb2008-05-02 19:17:30 +00002205 ComparePropertiesInBaseAndSuper(I);
John McCalld226f652010-08-21 09:40:31 +00002206 CompareProperties(I, I);
Steve Naroff09c47192009-01-09 15:36:25 +00002207 } else if (ObjCCategoryDecl *C = dyn_cast<ObjCCategoryDecl>(ClassDecl)) {
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002208 // Categories are used to extend the class by declaring new methods.
Mike Stump1eb44332009-09-09 15:08:12 +00002209 // By the same token, they are also used to add new properties. No
Fariborz Jahanian77e14bd2008-12-06 19:59:02 +00002210 // need to compare the added property to those in the class.
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002211
Fariborz Jahanian107089f2010-01-18 18:41:16 +00002212 // Compare protocol properties with those in category
John McCalld226f652010-08-21 09:40:31 +00002213 CompareProperties(C, C);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002214 if (C->IsClassExtension()) {
2215 ObjCInterfaceDecl *CCPrimary = C->getClassInterface();
2216 DiagnoseClassExtensionDupMethods(C, CCPrimary);
Fariborz Jahanian88f5e9b2010-12-10 23:36:33 +00002217 }
Chris Lattner4d391482007-12-12 07:09:47 +00002218 }
Steve Naroff09c47192009-01-09 15:36:25 +00002219 if (ObjCContainerDecl *CDecl = dyn_cast<ObjCContainerDecl>(ClassDecl)) {
Fariborz Jahanian25760612010-02-15 21:55:26 +00002220 if (CDecl->getIdentifier())
2221 // ProcessPropertyDecl is responsible for diagnosing conflicts with any
2222 // user-defined setter/getter. It also synthesizes setter/getter methods
2223 // and adds them to the DeclContext and global method pools.
2224 for (ObjCContainerDecl::prop_iterator I = CDecl->prop_begin(),
2225 E = CDecl->prop_end();
2226 I != E; ++I)
2227 ProcessPropertyDecl(*I, CDecl);
Ted Kremenek782f2f52010-01-07 01:20:12 +00002228 CDecl->setAtEndRange(AtEnd);
Steve Naroff09c47192009-01-09 15:36:25 +00002229 }
2230 if (ObjCImplementationDecl *IC=dyn_cast<ObjCImplementationDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002231 IC->setAtEndRange(AtEnd);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002232 if (ObjCInterfaceDecl* IDecl = IC->getClassInterface()) {
Fariborz Jahanianc78f6842010-12-11 18:39:37 +00002233 // Any property declared in a class extension might have user
2234 // declared setter or getter in current class extension or one
2235 // of the other class extensions. Mark them as synthesized as
2236 // property will be synthesized when property with same name is
2237 // seen in the @implementation.
2238 for (const ObjCCategoryDecl *ClsExtDecl =
2239 IDecl->getFirstClassExtension();
2240 ClsExtDecl; ClsExtDecl = ClsExtDecl->getNextClassExtension()) {
2241 for (ObjCContainerDecl::prop_iterator I = ClsExtDecl->prop_begin(),
2242 E = ClsExtDecl->prop_end(); I != E; ++I) {
2243 ObjCPropertyDecl *Property = (*I);
2244 // Skip over properties declared @dynamic
2245 if (const ObjCPropertyImplDecl *PIDecl
2246 = IC->FindPropertyImplDecl(Property->getIdentifier()))
2247 if (PIDecl->getPropertyImplementation()
2248 == ObjCPropertyImplDecl::Dynamic)
2249 continue;
2250
2251 for (const ObjCCategoryDecl *CExtDecl =
2252 IDecl->getFirstClassExtension();
2253 CExtDecl; CExtDecl = CExtDecl->getNextClassExtension()) {
2254 if (ObjCMethodDecl *GetterMethod =
2255 CExtDecl->getInstanceMethod(Property->getGetterName()))
2256 GetterMethod->setSynthesized(true);
2257 if (!Property->isReadOnly())
2258 if (ObjCMethodDecl *SetterMethod =
2259 CExtDecl->getInstanceMethod(Property->getSetterName()))
2260 SetterMethod->setSynthesized(true);
2261 }
2262 }
2263 }
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002264 ImplMethodsVsClassMethods(S, IC, IDecl);
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002265 AtomicPropertySetterGetterRules(IC, IDecl);
John McCallf85e1932011-06-15 23:02:42 +00002266 DiagnoseOwningPropertyGetterSynthesis(IC);
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002267
Fariborz Jahanianf914b972010-02-23 23:41:11 +00002268 if (LangOpts.ObjCNonFragileABI2)
2269 while (IDecl->getSuperClass()) {
2270 DiagnoseDuplicateIvars(IDecl, IDecl->getSuperClass());
2271 IDecl = IDecl->getSuperClass();
2272 }
Fariborz Jahanian7ca8b062009-11-11 22:40:11 +00002273 }
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002274 SetIvarInitializers(IC);
Mike Stump1eb44332009-09-09 15:08:12 +00002275 } else if (ObjCCategoryImplDecl* CatImplClass =
Steve Naroff09c47192009-01-09 15:36:25 +00002276 dyn_cast<ObjCCategoryImplDecl>(ClassDecl)) {
Ted Kremenek782f2f52010-01-07 01:20:12 +00002277 CatImplClass->setAtEndRange(AtEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Chris Lattner4d391482007-12-12 07:09:47 +00002279 // Find category interface decl and then check that all methods declared
Daniel Dunbarb20ef3e2008-08-27 05:40:03 +00002280 // in this interface are implemented in the category @implementation.
Chris Lattner97a58872009-02-16 18:32:47 +00002281 if (ObjCInterfaceDecl* IDecl = CatImplClass->getClassInterface()) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002282 for (ObjCCategoryDecl *Categories = IDecl->getCategoryList();
Chris Lattner4d391482007-12-12 07:09:47 +00002283 Categories; Categories = Categories->getNextClassCategory()) {
2284 if (Categories->getIdentifier() == CatImplClass->getIdentifier()) {
Fariborz Jahanian17cb3262010-05-05 21:52:17 +00002285 ImplMethodsVsClassMethods(S, CatImplClass, Categories);
Chris Lattner4d391482007-12-12 07:09:47 +00002286 break;
2287 }
2288 }
2289 }
2290 }
Chris Lattner682bf922009-03-29 16:50:03 +00002291 if (isInterfaceDeclKind) {
2292 // Reject invalid vardecls.
2293 for (unsigned i = 0; i != tuvNum; i++) {
2294 DeclGroupRef DG = allTUVars[i].getAsVal<DeclGroupRef>();
2295 for (DeclGroupRef::iterator I = DG.begin(), E = DG.end(); I != E; ++I)
2296 if (VarDecl *VDecl = dyn_cast<VarDecl>(*I)) {
Daniel Dunbar5466c7b2009-04-14 02:25:56 +00002297 if (!VDecl->hasExternalStorage())
Steve Naroff87454162009-04-13 17:58:46 +00002298 Diag(VDecl->getLocation(), diag::err_objc_var_decl_inclass);
Fariborz Jahanianb31cb7f2009-03-21 18:06:45 +00002299 }
Chris Lattner682bf922009-03-29 16:50:03 +00002300 }
Fariborz Jahanian38e24c72009-03-18 22:33:24 +00002301 }
Fariborz Jahanian10af8792011-08-29 17:33:12 +00002302 ActOnObjCContainerFinishDefinition();
Chris Lattner4d391482007-12-12 07:09:47 +00002303}
2304
2305
2306/// CvtQTToAstBitMask - utility routine to produce an AST bitmask for
2307/// objective-c's type qualifier from the parser version of the same info.
Mike Stump1eb44332009-09-09 15:08:12 +00002308static Decl::ObjCDeclQualifier
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002309CvtQTToAstBitMask(ObjCDeclSpec::ObjCDeclQualifier PQTVal) {
John McCall09e2c522011-05-01 03:04:29 +00002310 return (Decl::ObjCDeclQualifier) (unsigned) PQTVal;
Chris Lattner4d391482007-12-12 07:09:47 +00002311}
2312
Ted Kremenek422bae72010-04-18 04:59:38 +00002313static inline
Sean Huntcf807c42010-08-18 23:23:40 +00002314bool containsInvalidMethodImplAttribute(const AttrVec &A) {
Ted Kremenek422bae72010-04-18 04:59:38 +00002315 // The 'ibaction' attribute is allowed on method definitions because of
2316 // how the IBAction macro is used on both method declarations and definitions.
2317 // If the method definitions contains any other attributes, return true.
Sean Huntcf807c42010-08-18 23:23:40 +00002318 for (AttrVec::const_iterator i = A.begin(), e = A.end(); i != e; ++i)
2319 if ((*i)->getKind() != attr::IBAction)
2320 return true;
2321 return false;
Ted Kremenek422bae72010-04-18 04:59:38 +00002322}
2323
Douglas Gregore97179c2011-09-08 01:46:34 +00002324namespace {
2325 /// \brief Describes the compatibility of a result type with its method.
2326 enum ResultTypeCompatibilityKind {
2327 RTC_Compatible,
2328 RTC_Incompatible,
2329 RTC_Unknown
2330 };
2331}
2332
Douglas Gregor926df6c2011-06-11 01:09:30 +00002333/// \brief Check whether the declared result type of the given Objective-C
2334/// method declaration is compatible with the method's class.
2335///
Douglas Gregore97179c2011-09-08 01:46:34 +00002336static ResultTypeCompatibilityKind
Douglas Gregor926df6c2011-06-11 01:09:30 +00002337CheckRelatedResultTypeCompatibility(Sema &S, ObjCMethodDecl *Method,
2338 ObjCInterfaceDecl *CurrentClass) {
2339 QualType ResultType = Method->getResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002340
2341 // If an Objective-C method inherits its related result type, then its
2342 // declared result type must be compatible with its own class type. The
2343 // declared result type is compatible if:
2344 if (const ObjCObjectPointerType *ResultObjectType
2345 = ResultType->getAs<ObjCObjectPointerType>()) {
2346 // - it is id or qualified id, or
2347 if (ResultObjectType->isObjCIdType() ||
2348 ResultObjectType->isObjCQualifiedIdType())
Douglas Gregore97179c2011-09-08 01:46:34 +00002349 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002350
2351 if (CurrentClass) {
2352 if (ObjCInterfaceDecl *ResultClass
2353 = ResultObjectType->getInterfaceDecl()) {
2354 // - it is the same as the method's class type, or
2355 if (CurrentClass == ResultClass)
Douglas Gregore97179c2011-09-08 01:46:34 +00002356 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002357
2358 // - it is a superclass of the method's class type
2359 if (ResultClass->isSuperClassOf(CurrentClass))
Douglas Gregore97179c2011-09-08 01:46:34 +00002360 return RTC_Compatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002361 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002362 } else {
2363 // Any Objective-C pointer type might be acceptable for a protocol
2364 // method; we just don't know.
2365 return RTC_Unknown;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002366 }
2367 }
2368
Douglas Gregore97179c2011-09-08 01:46:34 +00002369 return RTC_Incompatible;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002370}
2371
John McCall6c2c2502011-07-22 02:45:48 +00002372namespace {
2373/// A helper class for searching for methods which a particular method
2374/// overrides.
2375class OverrideSearch {
2376 Sema &S;
2377 ObjCMethodDecl *Method;
2378 llvm::SmallPtrSet<ObjCContainerDecl*, 8> Searched;
2379 llvm::SmallPtrSet<ObjCMethodDecl*, 8> Overridden;
2380 bool Recursive;
2381
2382public:
2383 OverrideSearch(Sema &S, ObjCMethodDecl *method) : S(S), Method(method) {
2384 Selector selector = method->getSelector();
2385
2386 // Bypass this search if we've never seen an instance/class method
2387 // with this selector before.
2388 Sema::GlobalMethodPool::iterator it = S.MethodPool.find(selector);
2389 if (it == S.MethodPool.end()) {
2390 if (!S.ExternalSource) return;
2391 it = S.ReadMethodPool(selector);
2392 }
2393 ObjCMethodList &list =
2394 method->isInstanceMethod() ? it->second.first : it->second.second;
2395 if (!list.Method) return;
2396
2397 ObjCContainerDecl *container
2398 = cast<ObjCContainerDecl>(method->getDeclContext());
2399
2400 // Prevent the search from reaching this container again. This is
2401 // important with categories, which override methods from the
2402 // interface and each other.
2403 Searched.insert(container);
2404 searchFromContainer(container);
Douglas Gregor926df6c2011-06-11 01:09:30 +00002405 }
John McCall6c2c2502011-07-22 02:45:48 +00002406
2407 typedef llvm::SmallPtrSet<ObjCMethodDecl*,8>::iterator iterator;
2408 iterator begin() const { return Overridden.begin(); }
2409 iterator end() const { return Overridden.end(); }
2410
2411private:
2412 void searchFromContainer(ObjCContainerDecl *container) {
2413 if (container->isInvalidDecl()) return;
2414
2415 switch (container->getDeclKind()) {
2416#define OBJCCONTAINER(type, base) \
2417 case Decl::type: \
2418 searchFrom(cast<type##Decl>(container)); \
2419 break;
2420#define ABSTRACT_DECL(expansion)
2421#define DECL(type, base) \
2422 case Decl::type:
2423#include "clang/AST/DeclNodes.inc"
2424 llvm_unreachable("not an ObjC container!");
2425 }
2426 }
2427
2428 void searchFrom(ObjCProtocolDecl *protocol) {
2429 // A method in a protocol declaration overrides declarations from
2430 // referenced ("parent") protocols.
2431 search(protocol->getReferencedProtocols());
2432 }
2433
2434 void searchFrom(ObjCCategoryDecl *category) {
2435 // A method in a category declaration overrides declarations from
2436 // the main class and from protocols the category references.
2437 search(category->getClassInterface());
2438 search(category->getReferencedProtocols());
2439 }
2440
2441 void searchFrom(ObjCCategoryImplDecl *impl) {
2442 // A method in a category definition that has a category
2443 // declaration overrides declarations from the category
2444 // declaration.
2445 if (ObjCCategoryDecl *category = impl->getCategoryDecl()) {
2446 search(category);
2447
2448 // Otherwise it overrides declarations from the class.
2449 } else {
2450 search(impl->getClassInterface());
2451 }
2452 }
2453
2454 void searchFrom(ObjCInterfaceDecl *iface) {
2455 // A method in a class declaration overrides declarations from
2456
2457 // - categories,
2458 for (ObjCCategoryDecl *category = iface->getCategoryList();
2459 category; category = category->getNextClassCategory())
2460 search(category);
2461
2462 // - the super class, and
2463 if (ObjCInterfaceDecl *super = iface->getSuperClass())
2464 search(super);
2465
2466 // - any referenced protocols.
2467 search(iface->getReferencedProtocols());
2468 }
2469
2470 void searchFrom(ObjCImplementationDecl *impl) {
2471 // A method in a class implementation overrides declarations from
2472 // the class interface.
2473 search(impl->getClassInterface());
2474 }
2475
2476
2477 void search(const ObjCProtocolList &protocols) {
2478 for (ObjCProtocolList::iterator i = protocols.begin(), e = protocols.end();
2479 i != e; ++i)
2480 search(*i);
2481 }
2482
2483 void search(ObjCContainerDecl *container) {
2484 // Abort if we've already searched this container.
2485 if (!Searched.insert(container)) return;
2486
2487 // Check for a method in this container which matches this selector.
2488 ObjCMethodDecl *meth = container->getMethod(Method->getSelector(),
2489 Method->isInstanceMethod());
2490
2491 // If we find one, record it and bail out.
2492 if (meth) {
2493 Overridden.insert(meth);
2494 return;
2495 }
2496
2497 // Otherwise, search for methods that a hypothetical method here
2498 // would have overridden.
2499
2500 // Note that we're now in a recursive case.
2501 Recursive = true;
2502
2503 searchFromContainer(container);
2504 }
2505};
Douglas Gregor926df6c2011-06-11 01:09:30 +00002506}
2507
John McCalld226f652010-08-21 09:40:31 +00002508Decl *Sema::ActOnMethodDeclaration(
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002509 Scope *S,
Chris Lattner4d391482007-12-12 07:09:47 +00002510 SourceLocation MethodLoc, SourceLocation EndLoc,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002511 tok::TokenKind MethodType,
John McCallb3d87482010-08-24 05:47:05 +00002512 ObjCDeclSpec &ReturnQT, ParsedType ReturnType,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002513 ArrayRef<SourceLocation> SelectorLocs,
Chris Lattner4d391482007-12-12 07:09:47 +00002514 Selector Sel,
2515 // optional arguments. The number of types/arguments is obtained
2516 // from the Sel.getNumArgs().
Chris Lattnere294d3f2009-04-11 18:57:04 +00002517 ObjCArgInfo *ArgInfo,
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002518 DeclaratorChunk::ParamInfo *CParamInfo, unsigned CNumArgs, // c-style args
Chris Lattner4d391482007-12-12 07:09:47 +00002519 AttributeList *AttrList, tok::ObjCKeywordKind MethodDeclKind,
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002520 bool isVariadic, bool MethodDefinition) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002521 // Make sure we can establish a context for the method.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002522 if (!CurContext->isObjCContainer()) {
Steve Naroffda323ad2008-02-29 21:48:07 +00002523 Diag(MethodLoc, diag::error_missing_method_context);
John McCalld226f652010-08-21 09:40:31 +00002524 return 0;
Steve Naroffda323ad2008-02-29 21:48:07 +00002525 }
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002526 ObjCContainerDecl *OCD = dyn_cast<ObjCContainerDecl>(CurContext);
2527 Decl *ClassDecl = cast<Decl>(OCD);
Chris Lattner4d391482007-12-12 07:09:47 +00002528 QualType resultDeclType;
Mike Stump1eb44332009-09-09 15:08:12 +00002529
Douglas Gregore97179c2011-09-08 01:46:34 +00002530 bool HasRelatedResultType = false;
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002531 TypeSourceInfo *ResultTInfo = 0;
Steve Naroffccef3712009-02-20 22:59:16 +00002532 if (ReturnType) {
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002533 resultDeclType = GetTypeFromParser(ReturnType, &ResultTInfo);
Mike Stump1eb44332009-09-09 15:08:12 +00002534
Steve Naroffccef3712009-02-20 22:59:16 +00002535 // Methods cannot return interface types. All ObjC objects are
2536 // passed by reference.
John McCallc12c5bb2010-05-15 11:32:37 +00002537 if (resultDeclType->isObjCObjectType()) {
Chris Lattner2dd979f2009-04-11 19:08:56 +00002538 Diag(MethodLoc, diag::err_object_cannot_be_passed_returned_by_value)
2539 << 0 << resultDeclType;
John McCalld226f652010-08-21 09:40:31 +00002540 return 0;
Douglas Gregor926df6c2011-06-11 01:09:30 +00002541 }
Douglas Gregore97179c2011-09-08 01:46:34 +00002542
2543 HasRelatedResultType = (resultDeclType == Context.getObjCInstanceType());
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002544 } else { // get the type for "id".
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002545 resultDeclType = Context.getObjCIdType();
Fariborz Jahanianfeb4fa12011-07-21 17:38:14 +00002546 Diag(MethodLoc, diag::warn_missing_method_return_type)
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002547 << FixItHint::CreateInsertion(SelectorLocs.front(), "(id)");
Fariborz Jahanianaab24a62011-07-21 17:00:47 +00002548 }
Mike Stump1eb44332009-09-09 15:08:12 +00002549
2550 ObjCMethodDecl* ObjCMethod =
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002551 ObjCMethodDecl::Create(Context, MethodLoc, EndLoc, Sel,
Argyrios Kyrtzidis11d77162011-10-03 06:36:36 +00002552 resultDeclType,
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00002553 ResultTInfo,
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002554 CurContext,
Chris Lattner6c4ae5d2008-03-16 00:49:28 +00002555 MethodType == tok::minus, isVariadic,
Argyrios Kyrtzidis75cf3e82011-08-17 19:25:08 +00002556 /*isSynthesized=*/false,
2557 /*isImplicitlyDeclared=*/false, /*isDefined=*/false,
Douglas Gregor926df6c2011-06-11 01:09:30 +00002558 MethodDeclKind == tok::objc_optional
2559 ? ObjCMethodDecl::Optional
2560 : ObjCMethodDecl::Required,
Douglas Gregore97179c2011-09-08 01:46:34 +00002561 HasRelatedResultType);
Mike Stump1eb44332009-09-09 15:08:12 +00002562
Chris Lattner5f9e2722011-07-23 10:55:15 +00002563 SmallVector<ParmVarDecl*, 16> Params;
Mike Stump1eb44332009-09-09 15:08:12 +00002564
Chris Lattner7db638d2009-04-11 19:42:43 +00002565 for (unsigned i = 0, e = Sel.getNumArgs(); i != e; ++i) {
John McCall58e46772009-10-23 21:48:59 +00002566 QualType ArgType;
John McCalla93c9342009-12-07 02:54:59 +00002567 TypeSourceInfo *DI;
Mike Stump1eb44332009-09-09 15:08:12 +00002568
Chris Lattnere294d3f2009-04-11 18:57:04 +00002569 if (ArgInfo[i].Type == 0) {
John McCall58e46772009-10-23 21:48:59 +00002570 ArgType = Context.getObjCIdType();
2571 DI = 0;
Chris Lattnere294d3f2009-04-11 18:57:04 +00002572 } else {
John McCall58e46772009-10-23 21:48:59 +00002573 ArgType = GetTypeFromParser(ArgInfo[i].Type, &DI);
Steve Naroff6082c622008-12-09 19:36:17 +00002574 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002575 ArgType = Context.getAdjustedParameterType(ArgType);
Chris Lattnere294d3f2009-04-11 18:57:04 +00002576 }
Mike Stump1eb44332009-09-09 15:08:12 +00002577
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002578 LookupResult R(*this, ArgInfo[i].Name, ArgInfo[i].NameLoc,
2579 LookupOrdinaryName, ForRedeclaration);
2580 LookupName(R, S);
2581 if (R.isSingleResult()) {
2582 NamedDecl *PrevDecl = R.getFoundDecl();
2583 if (S->isDeclScope(PrevDecl)) {
Fariborz Jahanian90ba78c2011-03-12 18:54:30 +00002584 Diag(ArgInfo[i].NameLoc,
2585 (MethodDefinition ? diag::warn_method_param_redefinition
2586 : diag::warn_method_param_declaration))
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002587 << ArgInfo[i].Name;
2588 Diag(PrevDecl->getLocation(),
2589 diag::note_previous_declaration);
2590 }
2591 }
2592
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002593 SourceLocation StartLoc = DI
2594 ? DI->getTypeLoc().getBeginLoc()
2595 : ArgInfo[i].NameLoc;
2596
John McCall81ef3e62011-04-23 02:46:06 +00002597 ParmVarDecl* Param = CheckParameter(ObjCMethod, StartLoc,
2598 ArgInfo[i].NameLoc, ArgInfo[i].Name,
2599 ArgType, DI, SC_None, SC_None);
Mike Stump1eb44332009-09-09 15:08:12 +00002600
John McCall70798862011-05-02 00:30:12 +00002601 Param->setObjCMethodScopeInfo(i);
2602
Chris Lattner0ed844b2008-04-04 06:12:32 +00002603 Param->setObjCDeclQualifier(
Chris Lattnere294d3f2009-04-11 18:57:04 +00002604 CvtQTToAstBitMask(ArgInfo[i].DeclSpec.getObjCDeclQualifier()));
Mike Stump1eb44332009-09-09 15:08:12 +00002605
Chris Lattnerf97e8fa2009-04-11 19:34:56 +00002606 // Apply the attributes to the parameter.
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002607 ProcessDeclAttributeList(TUScope, Param, ArgInfo[i].ArgAttrs);
Mike Stump1eb44332009-09-09 15:08:12 +00002608
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002609 S->AddDecl(Param);
2610 IdResolver.AddDecl(Param);
2611
Chris Lattner0ed844b2008-04-04 06:12:32 +00002612 Params.push_back(Param);
2613 }
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002614
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002615 for (unsigned i = 0, e = CNumArgs; i != e; ++i) {
John McCalld226f652010-08-21 09:40:31 +00002616 ParmVarDecl *Param = cast<ParmVarDecl>(CParamInfo[i].Param);
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002617 QualType ArgType = Param->getType();
2618 if (ArgType.isNull())
2619 ArgType = Context.getObjCIdType();
2620 else
2621 // Perform the default array/function conversions (C99 6.7.5.3p[7,8]).
Douglas Gregor79e6bd32011-07-12 04:42:08 +00002622 ArgType = Context.getAdjustedParameterType(ArgType);
John McCallc12c5bb2010-05-15 11:32:37 +00002623 if (ArgType->isObjCObjectType()) {
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002624 Diag(Param->getLocation(),
2625 diag::err_object_cannot_be_passed_returned_by_value)
2626 << 1 << ArgType;
2627 Param->setInvalidDecl();
2628 }
2629 Param->setDeclContext(ObjCMethod);
Fariborz Jahanian7f532532011-02-09 22:20:01 +00002630
Fariborz Jahanian4f4fd922010-04-08 00:30:06 +00002631 Params.push_back(Param);
2632 }
2633
Argyrios Kyrtzidis491306a2011-10-03 06:37:04 +00002634 ObjCMethod->setMethodParams(Context, Params, SelectorLocs);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002635 ObjCMethod->setObjCDeclQualifier(
2636 CvtQTToAstBitMask(ReturnQT.getObjCDeclQualifier()));
Daniel Dunbar35682492008-09-26 04:12:28 +00002637
2638 if (AttrList)
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00002639 ProcessDeclAttributeList(TUScope, ObjCMethod, AttrList);
Mike Stump1eb44332009-09-09 15:08:12 +00002640
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002641 // Add the method now.
John McCall6c2c2502011-07-22 02:45:48 +00002642 const ObjCMethodDecl *PrevMethod = 0;
2643 if (ObjCImplDecl *ImpDecl = dyn_cast<ObjCImplDecl>(ClassDecl)) {
Chris Lattner4d391482007-12-12 07:09:47 +00002644 if (MethodType == tok::minus) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002645 PrevMethod = ImpDecl->getInstanceMethod(Sel);
2646 ImpDecl->addInstanceMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002647 } else {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002648 PrevMethod = ImpDecl->getClassMethod(Sel);
2649 ImpDecl->addClassMethod(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002650 }
Douglas Gregor926df6c2011-06-11 01:09:30 +00002651
Sean Huntcf807c42010-08-18 23:23:40 +00002652 if (ObjCMethod->hasAttrs() &&
2653 containsInvalidMethodImplAttribute(ObjCMethod->getAttrs()))
Fariborz Jahanian5d36ac22009-05-12 21:36:23 +00002654 Diag(EndLoc, diag::warn_attribute_method_def);
Douglas Gregorbdb2d502010-12-21 17:34:17 +00002655 } else {
2656 cast<DeclContext>(ClassDecl)->addDecl(ObjCMethod);
Chris Lattner4d391482007-12-12 07:09:47 +00002657 }
John McCall6c2c2502011-07-22 02:45:48 +00002658
Chris Lattner4d391482007-12-12 07:09:47 +00002659 if (PrevMethod) {
2660 // You can never have two method definitions with the same name.
Chris Lattner5f4a6822008-11-23 23:12:31 +00002661 Diag(ObjCMethod->getLocation(), diag::err_duplicate_method_decl)
Chris Lattner077bf5e2008-11-24 03:33:13 +00002662 << ObjCMethod->getDeclName();
Chris Lattner5f4a6822008-11-23 23:12:31 +00002663 Diag(PrevMethod->getLocation(), diag::note_previous_declaration);
Mike Stump1eb44332009-09-09 15:08:12 +00002664 }
John McCall54abf7d2009-11-04 02:18:39 +00002665
Douglas Gregor926df6c2011-06-11 01:09:30 +00002666 // If this Objective-C method does not have a related result type, but we
2667 // are allowed to infer related result types, try to do so based on the
2668 // method family.
2669 ObjCInterfaceDecl *CurrentClass = dyn_cast<ObjCInterfaceDecl>(ClassDecl);
2670 if (!CurrentClass) {
2671 if (ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(ClassDecl))
2672 CurrentClass = Cat->getClassInterface();
2673 else if (ObjCImplDecl *Impl = dyn_cast<ObjCImplDecl>(ClassDecl))
2674 CurrentClass = Impl->getClassInterface();
2675 else if (ObjCCategoryImplDecl *CatImpl
2676 = dyn_cast<ObjCCategoryImplDecl>(ClassDecl))
2677 CurrentClass = CatImpl->getClassInterface();
2678 }
John McCall6c2c2502011-07-22 02:45:48 +00002679
Douglas Gregore97179c2011-09-08 01:46:34 +00002680 ResultTypeCompatibilityKind RTC
2681 = CheckRelatedResultTypeCompatibility(*this, ObjCMethod, CurrentClass);
John McCall6c2c2502011-07-22 02:45:48 +00002682
2683 // Search for overridden methods and merge information down from them.
2684 OverrideSearch overrides(*this, ObjCMethod);
2685 for (OverrideSearch::iterator
2686 i = overrides.begin(), e = overrides.end(); i != e; ++i) {
2687 ObjCMethodDecl *overridden = *i;
2688
2689 // Propagate down the 'related result type' bit from overridden methods.
Douglas Gregore97179c2011-09-08 01:46:34 +00002690 if (RTC != RTC_Incompatible && overridden->hasRelatedResultType())
Douglas Gregor926df6c2011-06-11 01:09:30 +00002691 ObjCMethod->SetRelatedResultType();
John McCall6c2c2502011-07-22 02:45:48 +00002692
2693 // Then merge the declarations.
2694 mergeObjCMethodDecls(ObjCMethod, overridden);
Fariborz Jahanian730cfb12011-08-10 17:16:30 +00002695
2696 // Check for overriding methods
2697 if (isa<ObjCInterfaceDecl>(ObjCMethod->getDeclContext()) ||
Fariborz Jahanian36bc2c62011-10-10 17:53:29 +00002698 isa<ObjCImplementationDecl>(ObjCMethod->getDeclContext()))
2699 CheckConflictingOverridingMethod(ObjCMethod, overridden,
2700 isa<ObjCProtocolDecl>(overridden->getDeclContext()));
Douglas Gregor926df6c2011-06-11 01:09:30 +00002701 }
2702
John McCallf85e1932011-06-15 23:02:42 +00002703 bool ARCError = false;
2704 if (getLangOptions().ObjCAutoRefCount)
2705 ARCError = CheckARCMethodDecl(*this, ObjCMethod);
2706
Douglas Gregore97179c2011-09-08 01:46:34 +00002707 // Infer the related result type when possible.
2708 if (!ARCError && RTC == RTC_Compatible &&
2709 !ObjCMethod->hasRelatedResultType() &&
2710 LangOpts.ObjCInferRelatedResultType) {
Douglas Gregor926df6c2011-06-11 01:09:30 +00002711 bool InferRelatedResultType = false;
2712 switch (ObjCMethod->getMethodFamily()) {
2713 case OMF_None:
2714 case OMF_copy:
2715 case OMF_dealloc:
Nico Weber80cb6e62011-08-28 22:35:17 +00002716 case OMF_finalize:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002717 case OMF_mutableCopy:
2718 case OMF_release:
2719 case OMF_retainCount:
Fariborz Jahanian9670e172011-07-05 22:38:59 +00002720 case OMF_performSelector:
Douglas Gregor926df6c2011-06-11 01:09:30 +00002721 break;
2722
2723 case OMF_alloc:
2724 case OMF_new:
2725 InferRelatedResultType = ObjCMethod->isClassMethod();
2726 break;
2727
2728 case OMF_init:
2729 case OMF_autorelease:
2730 case OMF_retain:
2731 case OMF_self:
2732 InferRelatedResultType = ObjCMethod->isInstanceMethod();
2733 break;
2734 }
2735
John McCall6c2c2502011-07-22 02:45:48 +00002736 if (InferRelatedResultType)
Douglas Gregor926df6c2011-06-11 01:09:30 +00002737 ObjCMethod->SetRelatedResultType();
Douglas Gregor926df6c2011-06-11 01:09:30 +00002738 }
2739
John McCalld226f652010-08-21 09:40:31 +00002740 return ObjCMethod;
Chris Lattner4d391482007-12-12 07:09:47 +00002741}
2742
Chris Lattnercc98eac2008-12-17 07:13:27 +00002743bool Sema::CheckObjCDeclScope(Decl *D) {
Sebastian Redl7a126a42010-08-31 00:36:30 +00002744 if (isa<TranslationUnitDecl>(CurContext->getRedeclContext()))
Anders Carlsson15281452008-11-04 16:57:32 +00002745 return false;
Fariborz Jahanian58a76492011-08-22 18:34:22 +00002746 // Following is also an error. But it is caused by a missing @end
2747 // and diagnostic is issued elsewhere.
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002748 if (isa<ObjCContainerDecl>(CurContext->getRedeclContext())) {
2749 return false;
2750 }
2751
Anders Carlsson15281452008-11-04 16:57:32 +00002752 Diag(D->getLocation(), diag::err_objc_decls_may_only_appear_in_global_scope);
2753 D->setInvalidDecl();
Mike Stump1eb44332009-09-09 15:08:12 +00002754
Anders Carlsson15281452008-11-04 16:57:32 +00002755 return true;
2756}
Chris Lattnercc98eac2008-12-17 07:13:27 +00002757
Chris Lattnercc98eac2008-12-17 07:13:27 +00002758/// Called whenever @defs(ClassName) is encountered in the source. Inserts the
2759/// instance variables of ClassName into Decls.
John McCalld226f652010-08-21 09:40:31 +00002760void Sema::ActOnDefs(Scope *S, Decl *TagD, SourceLocation DeclStart,
Chris Lattnercc98eac2008-12-17 07:13:27 +00002761 IdentifierInfo *ClassName,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002762 SmallVectorImpl<Decl*> &Decls) {
Chris Lattnercc98eac2008-12-17 07:13:27 +00002763 // Check that ClassName is a valid class
Douglas Gregorc83c6872010-04-15 22:33:43 +00002764 ObjCInterfaceDecl *Class = getObjCInterfaceDecl(ClassName, DeclStart);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002765 if (!Class) {
2766 Diag(DeclStart, diag::err_undef_interface) << ClassName;
2767 return;
2768 }
Fariborz Jahanian0468fb92009-04-21 20:28:41 +00002769 if (LangOpts.ObjCNonFragileABI) {
2770 Diag(DeclStart, diag::err_atdef_nonfragile_interface);
2771 return;
2772 }
Mike Stump1eb44332009-09-09 15:08:12 +00002773
Chris Lattnercc98eac2008-12-17 07:13:27 +00002774 // Collect the instance variables
Jordy Rosedb8264e2011-07-22 02:08:32 +00002775 SmallVector<const ObjCIvarDecl*, 32> Ivars;
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002776 Context.DeepCollectObjCIvars(Class, true, Ivars);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002777 // For each ivar, create a fresh ObjCAtDefsFieldDecl.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002778 for (unsigned i = 0; i < Ivars.size(); i++) {
Jordy Rosedb8264e2011-07-22 02:08:32 +00002779 const FieldDecl* ID = cast<FieldDecl>(Ivars[i]);
John McCalld226f652010-08-21 09:40:31 +00002780 RecordDecl *Record = dyn_cast<RecordDecl>(TagD);
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002781 Decl *FD = ObjCAtDefsFieldDecl::Create(Context, Record,
2782 /*FIXME: StartL=*/ID->getLocation(),
2783 ID->getLocation(),
Fariborz Jahanian41833352009-06-04 17:08:55 +00002784 ID->getIdentifier(), ID->getType(),
2785 ID->getBitWidth());
John McCalld226f652010-08-21 09:40:31 +00002786 Decls.push_back(FD);
Fariborz Jahanian41833352009-06-04 17:08:55 +00002787 }
Mike Stump1eb44332009-09-09 15:08:12 +00002788
Chris Lattnercc98eac2008-12-17 07:13:27 +00002789 // Introduce all of these fields into the appropriate scope.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002790 for (SmallVectorImpl<Decl*>::iterator D = Decls.begin();
Chris Lattnercc98eac2008-12-17 07:13:27 +00002791 D != Decls.end(); ++D) {
John McCalld226f652010-08-21 09:40:31 +00002792 FieldDecl *FD = cast<FieldDecl>(*D);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002793 if (getLangOptions().CPlusPlus)
2794 PushOnScopeChains(cast<FieldDecl>(FD), S);
John McCalld226f652010-08-21 09:40:31 +00002795 else if (RecordDecl *Record = dyn_cast<RecordDecl>(TagD))
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002796 Record->addDecl(FD);
Chris Lattnercc98eac2008-12-17 07:13:27 +00002797 }
2798}
2799
Douglas Gregor160b5632010-04-26 17:32:49 +00002800/// \brief Build a type-check a new Objective-C exception variable declaration.
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002801VarDecl *Sema::BuildObjCExceptionDecl(TypeSourceInfo *TInfo, QualType T,
2802 SourceLocation StartLoc,
2803 SourceLocation IdLoc,
2804 IdentifierInfo *Id,
Douglas Gregor160b5632010-04-26 17:32:49 +00002805 bool Invalid) {
2806 // ISO/IEC TR 18037 S6.7.3: "The type of an object with automatic storage
2807 // duration shall not be qualified by an address-space qualifier."
2808 // Since all parameters have automatic store duration, they can not have
2809 // an address space.
2810 if (T.getAddressSpace() != 0) {
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002811 Diag(IdLoc, diag::err_arg_with_address_space);
Douglas Gregor160b5632010-04-26 17:32:49 +00002812 Invalid = true;
2813 }
2814
2815 // An @catch parameter must be an unqualified object pointer type;
2816 // FIXME: Recover from "NSObject foo" by inserting the * in "NSObject *foo"?
2817 if (Invalid) {
2818 // Don't do any further checking.
Douglas Gregorbe270a02010-04-26 17:57:08 +00002819 } else if (T->isDependentType()) {
2820 // Okay: we don't know what this type will instantiate to.
Douglas Gregor160b5632010-04-26 17:32:49 +00002821 } else if (!T->isObjCObjectPointerType()) {
2822 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002823 Diag(IdLoc ,diag::err_catch_param_not_objc_type);
Douglas Gregor160b5632010-04-26 17:32:49 +00002824 } else if (T->isObjCQualifiedIdType()) {
2825 Invalid = true;
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002826 Diag(IdLoc, diag::err_illegal_qualifiers_on_catch_parm);
Douglas Gregor160b5632010-04-26 17:32:49 +00002827 }
2828
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002829 VarDecl *New = VarDecl::Create(Context, CurContext, StartLoc, IdLoc, Id,
2830 T, TInfo, SC_None, SC_None);
Douglas Gregor324b54d2010-05-03 18:51:14 +00002831 New->setExceptionVariable(true);
2832
Douglas Gregor160b5632010-04-26 17:32:49 +00002833 if (Invalid)
2834 New->setInvalidDecl();
2835 return New;
2836}
2837
John McCalld226f652010-08-21 09:40:31 +00002838Decl *Sema::ActOnObjCExceptionDecl(Scope *S, Declarator &D) {
Douglas Gregor160b5632010-04-26 17:32:49 +00002839 const DeclSpec &DS = D.getDeclSpec();
2840
2841 // We allow the "register" storage class on exception variables because
2842 // GCC did, but we drop it completely. Any other storage class is an error.
2843 if (DS.getStorageClassSpec() == DeclSpec::SCS_register) {
2844 Diag(DS.getStorageClassSpecLoc(), diag::warn_register_objc_catch_parm)
2845 << FixItHint::CreateRemoval(SourceRange(DS.getStorageClassSpecLoc()));
2846 } else if (DS.getStorageClassSpec() != DeclSpec::SCS_unspecified) {
2847 Diag(DS.getStorageClassSpecLoc(), diag::err_storage_spec_on_catch_parm)
2848 << DS.getStorageClassSpec();
2849 }
2850 if (D.getDeclSpec().isThreadSpecified())
2851 Diag(D.getDeclSpec().getThreadSpecLoc(), diag::err_invalid_thread);
2852 D.getMutableDeclSpec().ClearStorageClassSpecs();
2853
2854 DiagnoseFunctionSpecifiers(D);
2855
2856 // Check that there are no default arguments inside the type of this
2857 // exception object (C++ only).
2858 if (getLangOptions().CPlusPlus)
2859 CheckExtraCXXDefaultArguments(D);
2860
Argyrios Kyrtzidis32153982011-06-28 03:01:15 +00002861 TypeSourceInfo *TInfo = GetTypeForDeclarator(D, S);
John McCallbf1a0282010-06-04 23:28:52 +00002862 QualType ExceptionType = TInfo->getType();
Douglas Gregor160b5632010-04-26 17:32:49 +00002863
Abramo Bagnaraff676cb2011-03-08 08:55:46 +00002864 VarDecl *New = BuildObjCExceptionDecl(TInfo, ExceptionType,
2865 D.getSourceRange().getBegin(),
2866 D.getIdentifierLoc(),
2867 D.getIdentifier(),
Douglas Gregor160b5632010-04-26 17:32:49 +00002868 D.isInvalidType());
2869
2870 // Parameter declarators cannot be qualified (C++ [dcl.meaning]p1).
2871 if (D.getCXXScopeSpec().isSet()) {
2872 Diag(D.getIdentifierLoc(), diag::err_qualified_objc_catch_parm)
2873 << D.getCXXScopeSpec().getRange();
2874 New->setInvalidDecl();
2875 }
2876
2877 // Add the parameter declaration into this scope.
John McCalld226f652010-08-21 09:40:31 +00002878 S->AddDecl(New);
Douglas Gregor160b5632010-04-26 17:32:49 +00002879 if (D.getIdentifier())
2880 IdResolver.AddDecl(New);
2881
2882 ProcessDeclAttributes(S, New, D);
2883
2884 if (New->hasAttr<BlocksAttr>())
2885 Diag(New->getLocation(), diag::err_block_on_nonlocal);
John McCalld226f652010-08-21 09:40:31 +00002886 return New;
Douglas Gregor4e6c0d12010-04-23 23:01:43 +00002887}
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002888
2889/// CollectIvarsToConstructOrDestruct - Collect those ivars which require
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002890/// initialization.
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002891void Sema::CollectIvarsToConstructOrDestruct(ObjCInterfaceDecl *OI,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002892 SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002893 for (ObjCIvarDecl *Iv = OI->all_declared_ivar_begin(); Iv;
2894 Iv= Iv->getNextIvar()) {
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002895 QualType QT = Context.getBaseElementType(Iv->getType());
Douglas Gregor68dd3ee2010-05-20 02:24:22 +00002896 if (QT->isRecordType())
Fariborz Jahanian2c18bb72010-08-20 21:21:08 +00002897 Ivars.push_back(Iv);
Fariborz Jahanian786cd152010-04-27 17:18:58 +00002898 }
2899}
Fariborz Jahaniane4498c62010-04-28 16:11:27 +00002900
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002901void Sema::DiagnoseUseOfUnimplementedSelectors() {
Douglas Gregor5b9dc7c2011-07-28 14:54:22 +00002902 // Load referenced selectors from the external source.
2903 if (ExternalSource) {
2904 SmallVector<std::pair<Selector, SourceLocation>, 4> Sels;
2905 ExternalSource->ReadReferencedSelectors(Sels);
2906 for (unsigned I = 0, N = Sels.size(); I != N; ++I)
2907 ReferencedSelectors[Sels[I].first] = Sels[I].second;
2908 }
2909
Fariborz Jahanian8b789132011-02-04 23:19:27 +00002910 // Warning will be issued only when selector table is
2911 // generated (which means there is at lease one implementation
2912 // in the TU). This is to match gcc's behavior.
2913 if (ReferencedSelectors.empty() ||
2914 !Context.AnyObjCImplementation())
Fariborz Jahanian3fe10412010-07-22 18:24:20 +00002915 return;
2916 for (llvm::DenseMap<Selector, SourceLocation>::iterator S =
2917 ReferencedSelectors.begin(),
2918 E = ReferencedSelectors.end(); S != E; ++S) {
2919 Selector Sel = (*S).first;
2920 if (!LookupImplementedMethodInGlobalPool(Sel))
2921 Diag((*S).second, diag::warn_unimplemented_selector) << Sel;
2922 }
2923 return;
2924}